forked from goodrain/rainbond
/
installStatus.go
282 lines (269 loc) · 6.94 KB
/
installStatus.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/urfave/cli"
)
func GetCommand(status bool) []cli.Command {
c := []cli.Command{
{
Name: "compute",
Usage: "安装计算节点 compute -h",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "nodes",
Usage: "hostID1 hostID2 ...,空表示全部",
},
},
Action: func(c *cli.Context) error {
return Task(c, "check_compute_services", status)
},
Subcommands: []cli.Command{
{
Name: "storage_client",
Usage: "step 1 storage_client",
Action: func(c *cli.Context) error {
return Task(c, "install_storage_client", status)
},
},
{
Name: "kubelet",
Usage: "need storage_client",
Action: func(c *cli.Context) error {
return Task(c, "install_kubelet", status)
},
},
{
Name: "network_compute",
Usage: "need storage_client,kubelet",
Action: func(c *cli.Context) error {
return Task(c, "install_network_compute", status)
},
},
},
},
{
Name: "manage_base",
Usage: "安装管理节点基础服务。 manage_base -h",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "nodes",
Usage: "hostID1 hostID2 ...,空表示全部",
},
},
Action: func(c *cli.Context) error {
return Task(c, "check_manage_base_services", status)
},
Subcommands: []cli.Command{
{
Name: "docker",
Usage: "step 1 安装docker",
Action: func(c *cli.Context) error {
return Task(c, "install_docker", status)
},
},
{
Name: "db",
Usage: "step 2 安装db",
Action: func(c *cli.Context) error {
return Task(c, "install_db", status)
},
},
{
Name: "base_plugins",
Usage: "step 3 基础插件",
Action: func(c *cli.Context) error {
return Task(c, "install_base_plugins", status)
},
},
{
Name: "acp_plugins",
Usage: "step 4 acp插件",
Action: func(c *cli.Context) error {
return Task(c, "install_acp_plugins", status)
},
},
},
},
{
Name: "manage",
Usage: "安装管理节点。 manage -h",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "nodes",
Usage: "hostID1 hostID2 ...,空表示全部",
},
},
Subcommands: []cli.Command{
{
Name: "storage",
Usage: "step 1 安装存储",
Action: func(c *cli.Context) error {
return Task(c, "install_storage", status)
},
},
{
Name: "k8s",
Usage: "need storage",
Action: func(c *cli.Context) error {
return Task(c, "install_k8s", status)
},
},
{
Name: "network",
Usage: "need storage,k8s",
Action: func(c *cli.Context) error {
return Task(c, "install_network", status)
},
},
{
Name: "plugins",
Usage: "need storage,k8s,network",
Action: func(c *cli.Context) error {
return Task(c, "install_plugins", status)
},
},
},
Action: func(c *cli.Context) error {
return Task(c, "check_manage_services", status)
},
},
}
return c
}
func NewCmdInstall() cli.Command {
c := cli.Command{
Name: "install",
Usage: "安装命令相关子命令。grctl install -h",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "nodes",
Usage: "hostID1 hostID2 ...,空表示全部",
},
},
Subcommands: GetCommand(false),
}
return c
}
//func NewCmdStatus() cli.Command {
// c:=cli.Command{
// Name: "status",
// Usage: "状态命令相关子命令。grctl status -h",
// Flags: []cli.Flag{
// cli.StringSliceFlag{
// Name: "nodes",
// Usage: "hostID1 hostID2 ...,空表示全部",
// },
// },
// Subcommands:GetCommand(true),
// }
// return c
//}
func Status(task string, nodes []string) {
checkFail := 0
lastState := ""
set := make(map[string]bool)
for _, v := range nodes {
set[v] = true
}
fmt.Printf("%s task is start\n", task)
lastState = "Start"
for checkFail < 3 {
time.Sleep(3 * time.Second)
taskE, err := clients.RegionClient.Tasks().Get(task)
if err != nil {
logrus.Warnf("error get task %s ,details %s,retry", task, err.String())
checkFail += 1
continue
}
//status,error:=clients.NodeClient.Tasks().Status(task)
status, err := clients.RegionClient.Tasks().GetTaskStatus(task)
if err != nil || status == nil {
logrus.Warnf("error get task %s status,details %s,retry", task, err.String())
checkFail += 1
continue
}
for k, v := range status {
//不是当前任务需要检测的status
if !set[k] {
fmt.Print("..")
continue
}
if strings.Contains(v.Status, "error") || strings.Contains(v.CompleStatus, "Failure") || strings.Contains(v.CompleStatus, "Unknow") {
checkFail += 1
fmt.Errorf("error executing task %s", task)
for _, v := range taskE.OutPut {
if set[v.NodeID] {
fmt.Printf("on %s :\n %s", v.NodeID, v.Body)
}
}
os.Exit(1)
}
if lastState != v.Status {
fmt.Printf("task %s is %s\n", task, v.Status)
} else {
fmt.Print("..")
}
lastState = v.Status
if v.Status == "complete" || v.CompleStatus == "Success" {
fmt.Printf("task %s is %s %s\n", task, v.Status, v.CompleStatus)
lastState = v.Status
taskFinished := taskE
var nextTasks []string
for _, v := range taskFinished.OutPut {
if !set[v.NodeID] {
continue
}
for _, sv := range v.Status {
if sv.NextTask == nil || len(sv.NextTask) == 0 {
continue
} else {
for _, v := range sv.NextTask {
nextTasks = append(nextTasks, v)
}
}
}
}
if len(nextTasks) > 0 {
fmt.Printf("next will install %v \n", nextTasks)
for _, v := range nextTasks {
Status(v, nodes)
}
}
return
}
}
checkFail = 0
}
}
func Task(c *cli.Context, task string, status bool) error {
nodes := c.StringSlice("nodes")
if len(nodes) == 0 {
return fmt.Errorf("nodes can not be empty")
}
err := clients.RegionClient.Tasks().Exec(task, nodes)
if err != nil {
logrus.Errorf("error exec task:%s,details %s", task, err.Error())
return err
}
Status(task, nodes)
return nil
}