Merge remote-tracking branch 'upstream/develop' into BigRefactor

This commit is contained in:
redanthrax 2022-07-05 09:53:16 -07:00
commit be0ea6b544
4 changed files with 68 additions and 36 deletions

View file

@ -118,7 +118,13 @@ func New(logger *logrus.Logger, version string) *Agent {
natsProxyPort = "443" natsProxyPort = "443"
} }
natsServer := fmt.Sprintf("wss://%s:%s", ac.APIURL, natsProxyPort) // check if using nats standard tcp, otherwise use nats websockets by default
var natsServer string
if ac.NatsStandardPort != "" {
natsServer = fmt.Sprintf("tls://%s:%s", ac.APIURL, ac.NatsStandardPort)
} else {
natsServer = fmt.Sprintf("wss://%s:%s", ac.APIURL, natsProxyPort)
}
return &Agent{ return &Agent{
Hostname: info.Hostname, Hostname: info.Hostname,

View file

@ -18,6 +18,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -145,17 +146,18 @@ func NewAgentConfig() *rmm.AgentConfig {
pk, _ := strconv.Atoi(agentpk) pk, _ := strconv.Atoi(agentpk)
ret := &rmm.AgentConfig{ ret := &rmm.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"),
Token: viper.GetString("token"), Token: viper.GetString("token"),
AgentPK: agentpk, AgentPK: agentpk,
PK: pk, PK: pk,
Cert: viper.GetString("cert"), Cert: viper.GetString("cert"),
Proxy: viper.GetString("proxy"), Proxy: viper.GetString("proxy"),
CustomMeshDir: viper.GetString("meshdir"), CustomMeshDir: viper.GetString("meshdir"),
NatsProxyPath: viper.GetString("natsproxypath"), NatsProxyPath: viper.GetString("natsproxypath"),
NatsProxyPort: viper.GetString("natsproxyport"), NatsProxyPort: viper.GetString("natsproxyport"),
NatsStandardPort: viper.GetString("natsstandardport"),
} }
return ret return ret
} }
@ -249,8 +251,29 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
os.Chmod(f.Name(), 0755) os.Chmod(f.Name(), 0755)
err = os.Rename(f.Name(), self) err = os.Rename(f.Name(), self)
if err != nil { if err != nil {
a.Logger.Errorln("AgentUpdate() os.Rename():", err) a.Logger.Debugln("Detected /tmp on different filesystem")
return // rename does not work when src and dest are on different filesystems
// so we need to manually copy it to the same fs then rename it
cwd, err := os.Getwd()
if err != nil {
a.Logger.Errorln("AgentUpdate() os.Getwd():", err)
return
}
// create a tmpfile in same fs as agent
tmpfile := filepath.Join(cwd, GenerateAgentID())
defer os.Remove(tmpfile)
a.Logger.Debugln("Copying tmpfile from", f.Name(), "to", tmpfile)
cperr := copyFile(f.Name(), tmpfile)
if cperr != nil {
a.Logger.Errorln("AgentUpdate() copyFile:", cperr)
return
}
os.Chmod(tmpfile, 0755)
rerr := os.Rename(tmpfile, self)
if rerr != nil {
a.Logger.Errorln("AgentUpdate() os.Rename():", rerr)
return
}
} }
opts := a.NewCMDOpts() opts := a.NewCMDOpts()

View file

@ -63,19 +63,21 @@ func NewAgentConfig() *rmm.AgentConfig {
customMeshDir, _, _ := k.GetStringValue("MeshDir") customMeshDir, _, _ := k.GetStringValue("MeshDir")
natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath") natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath")
natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort") natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort")
natsStandardPort, _, _ := k.GetStringValue("NatsStandardPort")
return &rmm.AgentConfig{ return &rmm.AgentConfig{
BaseURL: baseurl, BaseURL: baseurl,
AgentID: agentid, AgentID: agentid,
APIURL: apiurl, APIURL: apiurl,
Token: token, Token: token,
AgentPK: agentpk, AgentPK: agentpk,
PK: pk, PK: pk,
Cert: cert, Cert: cert,
Proxy: proxy, Proxy: proxy,
CustomMeshDir: customMeshDir, CustomMeshDir: customMeshDir,
NatsProxyPath: natsProxyPath, NatsProxyPath: natsProxyPath,
NatsProxyPort: natsProxyPort, NatsProxyPort: natsProxyPort,
NatsStandardPort: natsStandardPort,
} }
} }

View file

@ -33,17 +33,18 @@ type ProcessMsg struct {
} }
type AgentConfig struct { type AgentConfig struct {
BaseURL string BaseURL string
AgentID string AgentID string
APIURL string APIURL string
Token string Token string
AgentPK string AgentPK string
PK int PK int
Cert string Cert string
Proxy string Proxy string
CustomMeshDir string CustomMeshDir string
NatsProxyPath string NatsProxyPath string
NatsProxyPort string NatsProxyPort string
NatsStandardPort string
} }
type RunScriptResp struct { type RunScriptResp struct {