agent.go 3.28 KB
Newer Older
Lei Li's avatar
Lei Li committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
18
func start(root string) {
Lei Li's avatar
Lei Li committed
19
20
21
22
23
24
25
26
27
28
29
	dir, err := os.Getwd()
	if err != nil {
		LogError("MainActivity", dir)
	}
	LogInfo("MainActivity", dir)
	exePath, err := os.Executable()
	if err != nil {
		LogError("MainActivity", exePath)
	}
	LogInfo("MainActivity", exePath)

Lei Li's avatar
Lei Li committed
30
	RootDir = GetCurrentDirectory()
31
32
33
34
	if RootDir == "/" {
		RootDir = root
	}
	LogInfo("MainActivity", RootDir)
Lei Li's avatar
Lei Li committed
35
	msg := ""
Lei Li's avatar
Lei Li committed
36
37
38
39
40
41
42
43
	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)
Lei Li's avatar
Lei Li committed
44
45
				msg = fmt.Sprintf("DownloadFile err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
46
47
48
49
50
51
				time.Sleep(5 * time.Second)
				continue
			}
			content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName))
			if err != nil {
				l.Errorf("ReadFile err: %v", err)
Lei Li's avatar
Lei Li committed
52
53
				msg = fmt.Sprintf("ReadFile err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
54
55
56
57
58
59
60
				time.Sleep(5 * time.Second)
				continue
			}
			data := make(map[string]string)
			err = json.Unmarshal(content, &data)
			if err != nil {
				l.Error("unmarshal VersionReport content failed...")
Lei Li's avatar
Lei Li committed
61
62
63
				msg = fmt.Sprintf("unmarshal VersionReport content failed... err:%s", err.Error())
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
64
65
66
67
68
69
70
71
72
				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 {
				l.Error("download Agent failed...")
Lei Li's avatar
Lei Li committed
73
74
75
				msg = fmt.Sprintf("download Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
76
77
78
79
80
81
				continue
			}

			err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName))
			if err != nil {
				l.Errorf("copy Agent failed...err:%v", err)
Lei Li's avatar
Lei Li committed
82
83
				msg = fmt.Sprintf("copy Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
84
85
86
87
88
89
				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)
Lei Li's avatar
Lei Li committed
90
91
				msg = fmt.Sprintf("chmod Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
92
93
94
95
96
97
				time.Sleep(5 * time.Second)
				continue
			}
			md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName))
			if err != nil {
				l.Warnf("GetFileMD5 err: %v", err)
Lei Li's avatar
Lei Li committed
98
99
				msg = fmt.Sprintf("GetFileMD5 err:%s", err.Error())
				LogWarn("MainActivity", msg)
Lei Li's avatar
Lei Li committed
100
101
102
			}
			if md5 != data["md5"] {
				l.Error("update new agent failed, check md5sum err!")
Lei Li's avatar
Lei Li committed
103
104
105
				msg = "update new agent failed, check md5sum err!"
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
106
107
108
109
110
111
112
113
114
115
116
				continue
			}

			os.Remove(filepath.Join(RootDir, newExecFile))
			os.Remove(filepath.Join(RootDir, agentVersionReportFileName))

			go startAgent()
			break
		}
	}
	l.Info("start")
Lei Li's avatar
Lei Li committed
117
	LogInfo("MainActivity", "start")
Lei Li's avatar
Lei Li committed
118
119
120
121
122
123
124
125
126
}

//export stop
func stop() {
	err := syscall.Kill(childPID, syscall.SIGTERM)
	if err != nil {
		l.Error(err)
	}
	l.Info("stop")
Lei Li's avatar
Lei Li committed
127
	LogInfo("MainActivity", "stop")
Lei Li's avatar
Lei Li committed
128
129
130
}

func main() {}