use cwd for agent update fixes #24

This commit is contained in:
wh1te909 2023-03-29 05:21:26 +00:00
parent bee870bd58
commit 407f2a8072

View File

@ -226,9 +226,15 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return return
} }
f, err := createNixTmpFile() // more reliable method to get current working directory than os.Getwd()
cwd := filepath.Dir(self)
// create a tmpfile in same location as current binary
// avoids issues with /tmp dir and other fs mount issues
tmpfile := filepath.Join(cwd, GenerateAgentID())
f, err := os.Create(tmpfile)
if err != nil { if err != nil {
a.Logger.Errorln("AgentUpdate createNixTmpFile()", err) a.Logger.Errorln("AgentUpdate() os.Create(tmpfile)", err)
return return
} }
defer os.Remove(f.Name()) defer os.Remove(f.Name())
@ -260,30 +266,9 @@ 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.Debugln("Detected /tmp on different filesystem") a.Logger.Errorln("AgentUpdate() os.Rename():", err)
// 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 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
}
}
if runtime.GOOS == "linux" && a.seEnforcing() { if runtime.GOOS == "linux" && a.seEnforcing() {
se := a.NewCMDOpts() se := a.NewCMDOpts()