organizing and refactoring
This commit is contained in:
parent
13b5474cd8
commit
6f159d4728
20 changed files with 832 additions and 488 deletions
36
agent/tactical/tactical.go
Normal file
36
agent/tactical/tactical.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package tactical
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/amidaware/rmmagent/agent/utils"
|
||||
"github.com/amidaware/rmmagent/shared"
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
func SyncMeshNodeID() bool {
|
||||
id, err := GetMeshNodeID()
|
||||
if err != nil {
|
||||
//a.Logger.Errorln("SyncMeshNodeID() getMeshNodeID()", err)
|
||||
return false
|
||||
}
|
||||
|
||||
agentConfig := NewAgentConfig()
|
||||
|
||||
payload := shared.MeshNodeID{
|
||||
Func: "syncmesh",
|
||||
Agentid: agentConfig.AgentID,
|
||||
NodeID: utils.StripAll(id),
|
||||
}
|
||||
|
||||
client := resty.New()
|
||||
client.SetBaseURL(agentConfig.BaseURL)
|
||||
client.SetTimeout(15 * time.Second)
|
||||
client.SetCloseConnection(true)
|
||||
if shared.DEBUG {
|
||||
client.SetDebug(true)
|
||||
}
|
||||
|
||||
_, err = client.R().SetBody(payload).Post("/api/v3/syncmesh/")
|
||||
return err == nil
|
||||
}
|
||||
170
agent/tactical/tactical_linux.go
Normal file
170
agent/tactical/tactical_linux.go
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
package tactical
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/amidaware/rmmagent/agent/system"
|
||||
"github.com/amidaware/rmmagent/agent/utils"
|
||||
"github.com/amidaware/rmmagent/shared"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/wh1te909/trmm-shared"
|
||||
)
|
||||
|
||||
func GetMeshBinary() string {
|
||||
return "/opt/tacticalmesh/meshagent"
|
||||
}
|
||||
|
||||
func NewAgentConfig() *shared.AgentConfig {
|
||||
viper.SetConfigName("tacticalagent")
|
||||
viper.SetConfigType("json")
|
||||
viper.AddConfigPath("/etc/")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
err := viper.ReadInConfig()
|
||||
|
||||
if err != nil {
|
||||
return &shared.AgentConfig{}
|
||||
}
|
||||
|
||||
agentpk := viper.GetString("agentpk")
|
||||
pk, _ := strconv.Atoi(agentpk)
|
||||
|
||||
ret := &shared.AgentConfig{
|
||||
BaseURL: viper.GetString("baseurl"),
|
||||
AgentID: viper.GetString("agentid"),
|
||||
APIURL: viper.GetString("apiurl"),
|
||||
Token: viper.GetString("token"),
|
||||
AgentPK: agentpk,
|
||||
PK: pk,
|
||||
Cert: viper.GetString("cert"),
|
||||
Proxy: viper.GetString("proxy"),
|
||||
CustomMeshDir: viper.GetString("meshdir"),
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func AgentUpdate(url, inno, version string) bool {
|
||||
self, err := os.Executable()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
f, err := utils.CreateTmpFile()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
//logger.Infof("Agent updating from %s to %s", a.Version, version)
|
||||
//logger.Infoln("Downloading agent update from", url)
|
||||
|
||||
rClient := resty.New()
|
||||
rClient.SetCloseConnection(true)
|
||||
rClient.SetTimeout(15 * time.Minute)
|
||||
if shared.DEBUG {
|
||||
rClient.SetDebug(true)
|
||||
}
|
||||
|
||||
config := NewAgentConfig()
|
||||
if len(config.Proxy) > 0 {
|
||||
rClient.SetProxy(config.Proxy)
|
||||
}
|
||||
|
||||
r, err := rClient.R().SetOutput(f.Name()).Get(url)
|
||||
if err != nil {
|
||||
//a.Logger.Errorln("AgentUpdate() download:", err)
|
||||
f.Close()
|
||||
return false
|
||||
}
|
||||
if r.IsError() {
|
||||
//a.Logger.Errorln("AgentUpdate() status code:", r.StatusCode())
|
||||
f.Close()
|
||||
return false
|
||||
}
|
||||
|
||||
f.Close()
|
||||
os.Chmod(f.Name(), 0755)
|
||||
err = os.Rename(f.Name(), self)
|
||||
if err != nil {
|
||||
//a.Logger.Errorln("AgentUpdate() os.Rename():", err)
|
||||
return false
|
||||
}
|
||||
|
||||
opts := system.NewCMDOpts()
|
||||
opts.Detached = true
|
||||
opts.Command = "systemctl restart tacticalagent.service"
|
||||
system.CmdV2(opts)
|
||||
return true
|
||||
}
|
||||
|
||||
func AgentUninstall(code string) bool {
|
||||
f, err := utils.CreateTmpFile()
|
||||
if err != nil {
|
||||
//a.Logger.Errorln("AgentUninstall createTmpFile():", err)
|
||||
return false
|
||||
}
|
||||
|
||||
f.Write([]byte(code))
|
||||
f.Close()
|
||||
os.Chmod(f.Name(), 0770)
|
||||
|
||||
opts := system.NewCMDOpts()
|
||||
opts.IsScript = true
|
||||
opts.Shell = f.Name()
|
||||
opts.Args = []string{"uninstall"}
|
||||
opts.Detached = true
|
||||
system.CmdV2(opts)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func NixMeshNodeID() string {
|
||||
var meshNodeID string
|
||||
meshSuccess := false
|
||||
//a.Logger.Debugln("Getting mesh node id")
|
||||
|
||||
if !trmm.FileExists(GetMeshBinary()) {
|
||||
//a.Logger.Debugln(a.MeshSystemBin, "does not exist. Skipping.")
|
||||
return ""
|
||||
}
|
||||
|
||||
opts := system.NewCMDOpts()
|
||||
opts.IsExecutable = true
|
||||
opts.Shell = GetMeshBinary()
|
||||
opts.Command = "-nodeid"
|
||||
|
||||
for !meshSuccess {
|
||||
out := system.CmdV2(opts)
|
||||
meshNodeID = out.Stdout
|
||||
//a.Logger.Debugln("Stdout:", out.Stdout)
|
||||
//a.Logger.Debugln("Stderr:", out.Stderr)
|
||||
if meshNodeID == "" {
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
} else if strings.Contains(strings.ToLower(meshNodeID), "graphical version") || strings.Contains(strings.ToLower(meshNodeID), "zenity") {
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
meshSuccess = true
|
||||
}
|
||||
|
||||
return meshNodeID
|
||||
}
|
||||
|
||||
func GetMeshNodeID() (string, error) {
|
||||
return NixMeshNodeID(), nil
|
||||
}
|
||||
|
||||
func RecoverMesh(agentID string) {
|
||||
//a.Logger.Infoln("Attempting mesh recovery")
|
||||
opts := system.NewCMDOpts()
|
||||
opts.Command = "systemctl restart meshagent.service"
|
||||
system.CmdV2(opts)
|
||||
SyncMeshNodeID()
|
||||
}
|
||||
43
agent/tactical/tactical_linux_test.go
Normal file
43
agent/tactical/tactical_linux_test.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package tactical
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAgentConfig(t *testing.T) {
|
||||
config := NewAgentConfig()
|
||||
if config.BaseURL == "" {
|
||||
t.Fatal("Could not get config")
|
||||
}
|
||||
|
||||
t.Logf("Config BaseURL: %s", config.BaseURL)
|
||||
}
|
||||
|
||||
func TestAgentUpdate(t *testing.T) {
|
||||
url := "https://github.com/redanthrax/rmmagent/releases/download/v2.0.4/linuxagent"
|
||||
result := AgentUpdate(url, "", "v2.0.4")
|
||||
if(!result) {
|
||||
t.Fatal("Agent update resulted in false")
|
||||
}
|
||||
|
||||
t.Log("Agent update resulted in true")
|
||||
}
|
||||
|
||||
func TestAgentUninstall(t *testing.T) {
|
||||
result := AgentUninstall("foo")
|
||||
if !result {
|
||||
t.Fatal("Agent uninstall resulted in error")
|
||||
}
|
||||
|
||||
t.Log("Agent uninstall was true")
|
||||
}
|
||||
|
||||
func TestNixMeshNodeID(t *testing.T) {
|
||||
nodeid := NixMeshNodeID()
|
||||
if nodeid == "" {
|
||||
t.Fatal("Unable to get mesh node id")
|
||||
}
|
||||
|
||||
t.Logf("MeshNodeID: %s", nodeid)
|
||||
}
|
||||
|
||||
17
agent/tactical/tactical_test.go
Normal file
17
agent/tactical/tactical_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package tactical
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSyncMeshNodeID(t *testing.T) {
|
||||
agentConfig := NewAgentConfig()
|
||||
if agentConfig.AgentID == "" {
|
||||
t.Fatal("Could not get AgentID")
|
||||
}
|
||||
|
||||
result := SyncMeshNodeID()
|
||||
if !result {
|
||||
t.Fatal("SyncMeshNodeID resulted in error")
|
||||
}
|
||||
|
||||
t.Log("Synced mesh node id")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue