/
init.go
232 lines (219 loc) · 6.12 KB
/
init.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
// 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 (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/urfave/cli"
//"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/api/util"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/node/api/model"
coreutil "github.com/goodrain/rainbond/util"
)
//NewCmdInit grctl init
func NewCmdInit() cli.Command {
c := cli.Command{
Name: "init",
Flags: []cli.Flag{
cli.StringFlag{
Name: "etcd",
Usage: "etcd ip,127.0.0.1",
},
cli.StringFlag{
Name: "type",
Usage: "node type:manage/compute, manage",
},
cli.StringFlag{
Name: "mip",
Usage: "当前节点内网IP, 10.0.0.1",
},
cli.StringFlag{
Name: "repo_ver",
Usage: "repo version,3.4",
},
cli.StringFlag{
Name: "install_type",
Usage: "online/offline ,online",
},
cli.BoolFlag{
Name: "test",
Usage: "use test shell",
Hidden: true,
},
},
Usage: "初始化集群。grctl init cluster",
Action: func(c *cli.Context) error {
return initCluster(c)
},
}
return c
}
func NewCmdInstallStatus() cli.Command {
c := cli.Command{
Name: "install_status",
Flags: []cli.Flag{
cli.StringFlag{
Name: "taskID",
Usage: "install_k8s,空则自动寻找",
},
},
Usage: "获取task执行状态。grctl install_status",
Action: func(c *cli.Context) error {
taskID := c.String("taskID")
if taskID == "" {
tasks, err := clients.NodeClient.Tasks().List()
if err != nil {
logrus.Errorf("error get task list,details %s", err.Error())
return nil
}
for _, v := range tasks {
for _, vs := range v.Status {
if vs.Status == "start" || vs.Status == "create" {
//Status(v.ID)
return nil
}
}
}
} else {
//Status(taskID)
}
return nil
},
}
return c
}
func initCluster(c *cli.Context) error {
url := "http://repo.goodrain.com/release/3.5/gaops/jobs/install/prepare/init.sh"
if c.Bool("test") {
url = "http://dev.repo.goodrain.com/gaops/jobs/install/prepare/init.sh"
}
resp, err := http.Get(url)
if err != nil {
logrus.Errorf("error get init script,details %s", err.Error())
return err
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
args := []string{c.String("etcd"), c.String("type"), c.String("mip"), c.String("repo_ver"), c.String("install_type")}
arg := strings.Join(args, " ")
argCheck := strings.Join(args, "")
if len(argCheck) > 0 {
arg += ";"
} else {
arg = ""
}
fmt.Println("begin init cluster first node,please don't exit,wait install")
cmd := exec.Command("bash", "-c", arg+string(b))
var buffe bytes.Buffer
cmd.Stderr = &buffe
stdout, _ := cmd.StdoutPipe()
go func() {
read := bufio.NewReader(stdout)
for {
line, _, err := read.ReadLine()
if err != nil {
return
}
fmt.Println(string(line))
}
}()
if err := cmd.Run(); err != nil {
logrus.Errorf("current node init error,%s", err.Error())
return err
}
//检测并设置init的结果
result := buffe.String()
index := strings.Index(result, "{")
jsonOutPut := result
if index > -1 {
jsonOutPut = result[index:]
}
fmt.Println("Result:" + jsonOutPut)
output, err := model.ParseTaskOutPut(jsonOutPut)
if err != nil {
logrus.Errorf("get init current node result error:%s", err.Error())
return err
}
var newConfigs []model.ConfigUnit
if output.Global != nil {
for k, v := range output.Global {
if strings.Index(v, ",") > -1 {
values := strings.Split(v, ",")
coreutil.Deweight(&values)
newConfigs = append(newConfigs, model.ConfigUnit{
Name: strings.ToUpper(k),
Value: values,
ValueType: "array",
IsConfigurable: false,
})
} else {
newConfigs = append(newConfigs, model.ConfigUnit{
Name: strings.ToUpper(k),
Value: v,
ValueType: "string",
IsConfigurable: false,
})
}
}
}
var gc *model.GlobalConfig
var error *util.APIHandleError
for i := 0; i < 10; i++ {
time.Sleep(time.Second * 2)
gc, error = clients.NodeClient.Configs().Get()
if err == nil && gc != nil {
for _, nc := range newConfigs {
gc.Add(nc)
}
error = clients.NodeClient.Configs().Put(gc)
break
}
}
if error != nil {
logrus.Errorf("Update Datacenter configs error,please check node status")
return err
}
//获取当前节点ID
hostID, err := coreutil.ReadHostID("")
if err != nil {
logrus.Errorf("read nodeid error,please check node status")
return err
}
//error = clients.NodeClient.Tasks().Exec("check_manage_base_services", []string{hostID})
//if error != nil {
// logrus.Errorf("error exec task:%s,details %s", "check_manage_base_services", error.String())
// return error.Err
//}
error = clients.NodeClient.Tasks().Exec("check_manage_services", []string{hostID})
if error != nil {
logrus.Errorf("error exec task:%s,details %s", "check_manage_services", error.String())
return error.Err
}
//Status("check_manage_base_services", []string{hostID})
Status("check_manage_services", []string{hostID})
fmt.Println("install manage node success,next you can :")
fmt.Println(" add compute node--grctl node add -h")
fmt.Println(" install compute node--grctl install compute -h")
fmt.Println(" up compute node--grctl node up -h")
return nil
}