package main import "C" import ( "encoding/json" "fmt" "os" "path" "path/filepath" "syscall" "time" "linkfog.com/public/lib/file" "linkfog.com/public/lib/l" ) //export start func start() { RootDir = GetCurrentDirectory() for { if file.Exists(filepath.Join(RootDir, agentFileName)) { go startAgent() break } else { err := DownloadFile(agentVersionReportFileName, agentVersionURL) if err != nil { l.Errorf("DownloadFile err: %v", err) time.Sleep(5 * time.Second) continue } content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName)) if err != nil { l.Errorf("ReadFile err: %v", err) time.Sleep(5 * time.Second) continue } data := make(map[string]string) err = json.Unmarshal(content, &data) if err != nil { time.Sleep(5 * time.Second) l.Error("unmarshal VersionReport content failed...") return } l.Info("look up new version proc,updating...") newFileURL := data["url"] newExecFile := fmt.Sprintf("%s-new", agentFileName) err = DownloadFile(newExecFile, newFileURL) if err != nil { time.Sleep(5 * time.Second) l.Error("download Agent failed...") continue } err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName)) if err != nil { l.Errorf("copy Agent failed...err:%v", err) time.Sleep(5 * time.Second) continue } err = os.Chmod(filepath.Join(RootDir, agentFileName), 0755) if err != nil { l.Errorf("chmod Agent failed...err:%v", err) time.Sleep(5 * time.Second) continue } md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName)) if err != nil { l.Warnf("GetFileMD5 err: %v", err) } if md5 != data["md5"] { time.Sleep(5 * time.Second) l.Error("update new agent failed, check md5sum err!") continue } os.Remove(filepath.Join(RootDir, newExecFile)) os.Remove(filepath.Join(RootDir, agentVersionReportFileName)) go startAgent() break } } l.Info("start") } //export stop func stop() { err := syscall.Kill(childPID, syscall.SIGTERM) if err != nil { l.Error(err) } l.Info("stop") } func main() {}