-
Notifications
You must be signed in to change notification settings - Fork 76
/
deploy.go
161 lines (153 loc) · 3.8 KB
/
deploy.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package funcs
import (
"fmt"
"io/ioutil"
"log"
"net"
"os/exec"
"strconv"
"strings"
)
var deployPathMap map[string]string
var deployGuidStatus map[string]string
var LocalIp string
var osBashCommand string
var osPsPidIndex string
func InitDeploy() {
log.Println("init deploy")
deployPathMap = make(map[string]string)
deployGuidStatus = make(map[string]string)
initOsCommand()
if !Config().Deploy.Enable || len(Config().Deploy.PackagePath) == 0 {
return
}
for _, v := range Config().Deploy.PackagePath {
tmpName := strings.Split(v, "/")[strings.Count(v, "/")]
deployPathMap[tmpName] = v
}
}
func AddDeploy(name, configFile, guid string, param map[string]string, configHash string) (port int, err error) {
port = 0
if _, b := deployGuidStatus[guid]; b {
log.Println("enter exist action")
port = GlobalProcessMap[guid].Port
DeleteDeploy(guid)
}
var p ProcessObj
p.ConfigHash = configHash
tmpName := fmt.Sprintf("%s_%d", name, getNextDirIndex(name))
deployPath := fmt.Sprintf("%s/%s", Config().Deploy.DeployDir, tmpName)
err = exec.Command(osBashCommand, "-c", fmt.Sprintf("mkdir -p %s && cp -r %s/* %s/", deployPath, deployPathMap[name], deployPath)).Run()
if err != nil {
return port, err
}
if configFile != "" {
configFile = deployPath + "/" + configFile
}
startFile := deployPath + "/start.sh"
p.init(tmpName, deployPath, "./start.sh")
ProcessMapLock.Lock()
GlobalProcessMap[guid] = &p
ProcessMapLock.Unlock()
if _, b := param["port"]; !b {
if port == 0 {
port = GetPort()
}
param["port"] = fmt.Sprintf("%d", port)
} else {
port, _ = strconv.Atoi(param["port"])
}
err = p.start(configFile, startFile, guid, port, param)
if err != nil && p.Status == "broken" {
p.destroy()
delete(GlobalProcessMap, guid)
return 0, err
}
deployGuidStatus[guid] = p.Status
for k, v := range deployGuidStatus {
log.Printf("deploy guid status ---> k:%s v:%s \n", k, v)
}
return port, err
}
func DeleteDeploy(guid string) error {
log.Printf("try to delete %s \n", guid)
if v, b := GlobalProcessMap[guid]; b {
v.stop()
clearUselessDir(v.Path)
delete(GlobalProcessMap, guid)
delete(deployGuidStatus, guid)
return nil
} else {
return fmt.Errorf("guid:%s not exist", guid)
}
}
func InitLocalIp() bool {
addrs, err := net.InterfaceAddrs()
re := []string{}
if err != nil {
log.Println(err)
return false
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
re = append(re, ipNet.IP.String())
}
}
}
if len(re) == 0 {
return false
} else {
LocalIp = re[0]
log.Printf("local ip : %s \n", LocalIp)
return true
}
}
func initOsCommand() {
for _, v := range Config().OsBash {
_, err := exec.Command(v, "-c", "date").Output()
if err == nil {
osBashCommand = v
break
}
}
if osBashCommand == "" {
osBashCommand = "bash"
}
b, err := exec.Command(osBashCommand, "-c", "ps aux|grep PID|grep -v grep").Output()
if err == nil {
index := 0
for _, v := range strings.Split(string(b), " ") {
if v != "" {
index += 1
if v == "PID" {
break
}
}
}
osPsPidIndex = fmt.Sprintf("$%d", index)
} else {
osPsPidIndex = "$2"
}
log.Printf("init os command done, bash: %s index:%s \n", osBashCommand, osPsPidIndex)
}
func getNextDirIndex(name string) int {
log.Printf("start get next dir index for %s \n", name)
index := 0
files, err := ioutil.ReadDir(Config().Deploy.DeployDir)
if err != nil {
log.Printf("read dir %s error %v \n", Config().Deploy.DeployDir, err)
} else {
for _, v := range files {
if strings.Contains(v.Name(), name) {
tmpList := strings.Split(v.Name(), "_")
tmpIndex, _ := strconv.Atoi(tmpList[len(tmpList)-1])
if tmpIndex > index {
index = tmpIndex
}
}
}
}
return index + 1
}