fix linux agent update when /tmp on different fs fixes #10

This commit is contained in:
wh1te909 2022-07-01 19:10:10 +00:00
parent ac2637d14b
commit 38907f6bc2

View File

@ -18,6 +18,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -249,9 +250,30 @@ 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")
// 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
}
}
opts := a.NewCMDOpts() opts := a.NewCMDOpts()
opts.Detached = true opts.Detached = true