-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
71 lines (64 loc) · 1.46 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package p2pcontrollers
import (
"controllers"
"encoding/json"
"library/components"
"library/p2p/init_sever"
"models"
"strings"
"github.com/astaxie/beego"
)
type AgentController struct {
controllers.BaseController
}
func (c *AgentController) Get() {
if c.Project == nil || c.Project.Id == 0 {
c.SetJson(1, nil, "Parameter error")
return
}
s := components.BaseComponents{}
s.SetProject(c.Project)
s.SetTask(&models.Task{Id: -3})
ips := s.GetHostIps()
ss := init_sever.P2pSvc.CheckAllClient(ips)
reIps := []string{}
for ip, status := range ss {
if status == "dead" {
reIps = append(reIps, strings.Split(ip, ":")[0])
}
}
if len(reIps) > 0 && c.Project.P2p == 1 {
AgentDestDir := beego.AppConfig.String("AgentDestDir")
err := s.StartP2pAgent(reIps, AgentDestDir)
if err != nil {
c.SetJson(1, nil, "重启失败"+err.Error())
return
} else {
c.SetJson(0, nil, "重启成功")
return
}
} else {
c.SetJson(0, nil, "已全部启动")
return
}
return
}
func (c *AgentController) Post() {
if c.Project == nil || c.Project.Id == 0 {
c.SetJson(1, nil, "Parameter error")
return
}
ips := []string{}
json.Unmarshal(c.Ctx.Input.RequestBody, &ips)
s := components.BaseComponents{}
s.SetProject(c.Project)
AgentDestDir := beego.AppConfig.String("AgentDestDir")
err := s.StartP2pAgent(ips, AgentDestDir)
if err != nil {
c.SetJson(1, nil, "重启失败"+err.Error())
return
} else {
c.SetJson(0, nil, "重启成功")
return
}
}