updated tests and methods

This commit is contained in:
redanthrax 2022-06-23 16:04:48 -07:00
parent de8e795254
commit da1e250ce9
8 changed files with 94 additions and 40 deletions

View file

@ -3,8 +3,6 @@ https://github.com/amidaware/tacticalrmm
#### building the agent - linux #### building the agent - linux
``` ```
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo
go generate
env CGO_ENABLED=0 GOOS=<GOOS> GOARCH=<GOARCH> go build -ldflags "-s -w -X 'main.version=v2.0.4'" env CGO_ENABLED=0 GOOS=<GOOS> GOARCH=<GOARCH> go build -ldflags "-s -w -X 'main.version=v2.0.4'"
example: env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X 'main.version=v2.0.4' -o build/output/rmmagent" example: env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X 'main.version=v2.0.4' -o build/output/rmmagent"
``` ```

View file

@ -7,7 +7,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
"syscall" "syscall"
@ -288,20 +287,3 @@ func CMDShell(shell string, cmdArgs []string, command string, timeout int, detac
func CMD(exe string, args []string, timeout int, detached bool) (output [2]string, e error) { func CMD(exe string, args []string, timeout int, detached bool) (output [2]string, e error) {
return [2]string{"", ""}, nil return [2]string{"", ""}, nil
} }
func GetPythonBin() string {
opts := NewCMDOpts()
opts.Command = "which python"
out := CmdV2(opts)
return out.Stdout
}
func GetProgramDirectory() string {
pd := filepath.Join(os.Getenv("ProgramFiles"), ProgFilesName)
return pd
}
func GetProgramBin() string {
exe := filepath.Join(GetProgramDirectory(), winExeName)
return exe
}

View file

@ -0,0 +1,16 @@
package shared_test
import (
"testing"
"github.com/amidaware/rmmagent/agent/tactical/shared"
)
func TestGetPythonBin(t *testing.T) {
pybin := shared.GetPythonBin()
if pybin == "" {
t.Errorf("expected path, got %s", pybin)
}
t.Logf("result: %s", pybin)
}

View file

@ -3,6 +3,34 @@
package shared package shared
import (
"os/exec"
"path/filepath"
"strings"
)
const (
binName = "tacticalagent"
)
func GetPython(force bool) {} func GetPython(force bool) {}
func RunMigrations() {} func RunMigrations() {}
func GetPythonBin() string {
pybin, err := exec.Command("python", "-c", "import sys; print(sys.executable)").Output()
if err != nil {
return ""
}
return strings.TrimSuffix(string(pybin), "\n")
}
func GetProgramDirectory() string {
return "/usr/local/bin"
}
func GetProgramBin() string {
bin := filepath.Join(GetProgramDirectory(), binName)
return bin
}

View file

@ -1 +1,13 @@
package tactical package tactical
type AgentConfig struct {
BaseURL string
AgentID string
APIURL string
Token string
AgentPK string
PK int
Cert string
Proxy string
CustomMeshDir string
}

View file

@ -8,5 +8,9 @@ import (
func TestGetVersion(t *testing.T) { func TestGetVersion(t *testing.T) {
version := tactical.GetVersion() version := tactical.GetVersion()
if version == "" {
t.Errorf("expected version, got empty version")
}
t.Logf("got version %s", version) t.Logf("got version %s", version)
} }

View file

@ -1,15 +1,20 @@
//go:build !windows
// +build !windows
package tactical package tactical
import ( import (
"os" "os"
"os/exec"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/amidaware/rmmagent/agent/system" "github.com/amidaware/rmmagent/agent/system"
"github.com/amidaware/rmmagent/agent/tactical/mesh" "github.com/amidaware/rmmagent/agent/tactical/mesh"
"github.com/amidaware/rmmagent/agent/tactical/shared"
"github.com/amidaware/rmmagent/agent/utils" "github.com/amidaware/rmmagent/agent/utils"
"github.com/amidaware/rmmagent/shared"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/wh1te909/trmm-shared" "github.com/wh1te909/trmm-shared"
@ -19,7 +24,7 @@ func GetMeshBinary() string {
return "/opt/tacticalmesh/meshagent" return "/opt/tacticalmesh/meshagent"
} }
func NewAgentConfig() *shared.AgentConfig { func NewAgentConfig() *AgentConfig {
viper.SetConfigName("tacticalagent") viper.SetConfigName("tacticalagent")
viper.SetConfigType("json") viper.SetConfigType("json")
viper.AddConfigPath("/etc/") viper.AddConfigPath("/etc/")
@ -28,13 +33,13 @@ func NewAgentConfig() *shared.AgentConfig {
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
return &shared.AgentConfig{} return &AgentConfig{}
} }
agentpk := viper.GetString("agentpk") agentpk := viper.GetString("agentpk")
pk, _ := strconv.Atoi(agentpk) pk, _ := strconv.Atoi(agentpk)
ret := &shared.AgentConfig{ ret := &AgentConfig{
BaseURL: viper.GetString("baseurl"), BaseURL: viper.GetString("baseurl"),
AgentID: viper.GetString("agentid"), AgentID: viper.GetString("agentid"),
APIURL: viper.GetString("apiurl"), APIURL: viper.GetString("apiurl"),
@ -49,7 +54,7 @@ func NewAgentConfig() *shared.AgentConfig {
return ret return ret
} }
func AgentUpdate(url, inno, version string) bool { func AgentUpdate(url string, inno string) bool {
self, err := os.Executable() self, err := os.Executable()
if err != nil { if err != nil {
return false return false
@ -61,15 +66,12 @@ func AgentUpdate(url, inno, version string) bool {
} }
defer os.Remove(f.Name()) 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 := resty.New()
rClient.SetCloseConnection(true) rClient.SetCloseConnection(true)
rClient.SetTimeout(15 * time.Minute) rClient.SetTimeout(15 * time.Minute)
if shared.DEBUG { //if shared.DEBUG {
rClient.SetDebug(true) //rClient.SetDebug(true)
} //}
config := NewAgentConfig() config := NewAgentConfig()
if len(config.Proxy) > 0 { if len(config.Proxy) > 0 {
@ -185,3 +187,14 @@ func installMesh(meshbin, exe, proxy string) (string, error) {
} }
func SendSoftware() {} func SendSoftware() {}
func GetVersion() string {
version, err := exec.Command(shared.GetProgramBin(), "-version").Output()
if err != nil {
return ""
}
re := regexp.MustCompile(`Tactical RMM Agent: v([0-9]\.[0-9]\.[0-9])`)
match := re.FindStringSubmatch(string(version))
return match[1]
}

View file

@ -1,11 +1,13 @@
package tactical package tactical_test
import ( import (
"testing" "testing"
"github.com/amidaware/rmmagent/agent/tactical"
) )
func TestNewAgentConfig(t *testing.T) { func TestNewAgentConfig(t *testing.T) {
config := NewAgentConfig() config := tactical.NewAgentConfig()
if config.BaseURL == "" { if config.BaseURL == "" {
t.Fatal("Could not get config") t.Fatal("Could not get config")
} }
@ -15,8 +17,8 @@ func TestNewAgentConfig(t *testing.T) {
func TestAgentUpdate(t *testing.T) { func TestAgentUpdate(t *testing.T) {
url := "https://github.com/redanthrax/rmmagent/releases/download/v2.0.4/linuxagent" url := "https://github.com/redanthrax/rmmagent/releases/download/v2.0.4/linuxagent"
result := AgentUpdate(url, "", "v2.0.4") result := tactical.AgentUpdate(url, "")
if(!result) { if !result {
t.Fatal("Agent update resulted in false") t.Fatal("Agent update resulted in false")
} }
@ -24,7 +26,7 @@ func TestAgentUpdate(t *testing.T) {
} }
func TestAgentUninstall(t *testing.T) { func TestAgentUninstall(t *testing.T) {
result := AgentUninstall("foo") result := tactical.AgentUninstall("foo")
if !result { if !result {
t.Fatal("Agent uninstall resulted in error") t.Fatal("Agent uninstall resulted in error")
} }
@ -33,11 +35,10 @@ func TestAgentUninstall(t *testing.T) {
} }
func TestNixMeshNodeID(t *testing.T) { func TestNixMeshNodeID(t *testing.T) {
nodeid := NixMeshNodeID() nodeid := tactical.NixMeshNodeID()
if nodeid == "" { if nodeid == "" {
t.Fatal("Unable to get mesh node id") t.Fatal("Unable to get mesh node id")
} }
t.Logf("MeshNodeID: %s", nodeid) t.Logf("MeshNodeID: %s", nodeid)
} }