forked from goodrain/rainbond
/
init.go
298 lines (282 loc) · 7.75 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/goodrain/rainbond/util"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/urfave/cli" //"github.com/goodrain/rainbond/grctl/clients"
)
//NewCmdInit grctl init
func NewCmdInit() cli.Command {
c := cli.Command{
Name: "init",
Flags: []cli.Flag{
cli.StringFlag{
Name: "role",
Usage: "Node identity property",
Value: "master,worker",
},
cli.StringFlag{
Name: "work_dir",
Usage: "Installation configuration directory",
Value: "/opt/rainbond/rainbond-ansible",
},
cli.StringFlag{
Name: "iip",
Usage: "Internal IP",
Value: "",
},
cli.StringFlag{
Name: "eip",
Usage: "External IP",
Value: "",
},
cli.StringFlag{
Name: "vip",
Usage: "Virtual IP",
Value: "",
},
cli.StringFlag{
Name: "rainbond-version",
Usage: "Rainbond Install Version. default 5.0",
Value: "5.0",
},
cli.StringFlag{
Name: "rainbond-repo",
Usage: "Rainbond install repo",
Value: "https://github.com/goodrain/rainbond-ansible.git",
},
cli.StringFlag{
Name: "install-type",
Usage: "Install Type: online/offline",
Value: "online",
},
cli.StringFlag{
Name: "deploy-type",
Usage: "Deploy Type: onenode/multinode/thirdparty,默认onenode",
Value: "onenode",
},
cli.StringFlag{
Name: "domain",
Usage: "Application domain",
Value: "",
},
cli.StringFlag{
Name: "pod-cidr",
Usage: "Configuration pod-cidr",
Value: "",
},
cli.StringFlag{
Name: "enable-feature",
Usage: "New feature,disabled by default. default: windows",
Value: "",
},
cli.StringFlag{
Name: "enable-online-images",
Usage: "Get image online. default: offline",
Value: "",
},
cli.StringFlag{
Name: "storage",
Usage: "Storage type, default:NFS",
Value: "nfs",
},
cli.StringFlag{
Name: "network",
Usage: "Network type, support calico/flannel/midonet,default: calico",
Value: "calico",
},
cli.StringFlag{
Name: "enable-check",
Usage: "enable check cpu/mem. default: enable/disable",
Value: "enable",
},
cli.StringFlag{
Name: "storage-args",
Usage: "Stores mount parameters",
Value: "/grdata nfs rw 0 0",
},
cli.StringFlag{
Name: "config-file,f",
Usage: "Global Config Path, default",
Value: "/opt/rainbond/rainbond-ansible/scripts/installer/global.sh",
},
cli.BoolFlag{
Name: "test",
Usage: "use test shell",
Hidden: true,
},
},
Usage: "grctl init cluster",
Action: func(c *cli.Context) error {
initCluster(c)
return nil
},
}
return c
}
//NewCmdInstallStatus install status
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.RegionClient.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 updateConfigFile(path string, config map[string]string) error {
initConfig := make(map[string]string)
var file *os.File
var err error
if ok, _ := util.FileExists(path); ok {
file, err = os.OpenFile(path, os.O_RDWR, 0755)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != nil {
break
}
if strings.Contains(string(line), "=") {
keyvalue := strings.SplitN(string(line), "=", 1)
if len(keyvalue) < 2 {
break
}
initConfig[keyvalue[0]] = keyvalue[1]
}
}
} else {
file, err = util.OpenOrCreateFile(path)
if err != nil {
return err
}
defer file.Close()
}
for k, v := range config {
initConfig[k] = v
}
for k, v := range initConfig {
if k == "" {
continue
}
if v == "" {
file.WriteString(fmt.Sprintf("%s=\"\"\n", k))
} else {
file.WriteString(fmt.Sprintf("%s=\"%s\"\n", k, v))
}
}
return nil
}
func getConfig(c *cli.Context) map[string]string {
configs := make(map[string]string)
configs["ROLE"] = c.String("role")
//configs["work_dir"] = c.String("work_dir")
configs["IIP"] = c.String("iip")
configs["EIP"] = c.String("eip")
configs["VIP"] = c.String("vip")
configs["VERSION"] = c.String("rainbond-version")
// configs["rainbond-repo"] = c.String("rainbond-repo")
configs["INSTALL_TYPE"] = c.String("install-type")
configs["DEPLOY_TYPE"] = c.String("deploy-type")
configs["DOMAIN"] = c.String("domain")
configs["STORAGE"] = c.String("storage")
configs["NETWORK_TYPE"] = c.String("network")
configs["POD_NETWORK_CIDR"] = c.String("pod-cidr")
configs["STORAGE_ARGS"] = c.String("storage-args")
configs["ENABLE_CHECK"] = c.String("enable-check")
configs["PULL_ONLINE_IMAGES"] = c.String("enable-online-images")
return configs
}
func initCluster(c *cli.Context) {
// check if the rainbond is already installed
//fmt.Println("Checking install enviremant.")
_, err := os.Stat("/opt/rainbond/.rainbond.success")
if err == nil {
println("Rainbond is already installed, if you whant reinstall, then please delete the file: /opt/rainbond/.rainbond.success")
return
}
// download source code from github if in online model
if c.String("install-type") == "online" {
fmt.Println("Download the installation configuration file remotely...")
csi := sources.CodeSourceInfo{
RepositoryURL: c.String("rainbond-repo"),
Branch: c.String("rainbond-version"),
}
os.RemoveAll(c.String("work_dir"))
os.MkdirAll(c.String("work_dir"), 0755)
_, err := sources.GitClone(csi, c.String("work_dir"), event.GetTestLogger(), 5)
if err != nil {
println(err.Error())
return
}
}
if err := updateConfigFile(c.String("config-file"), getConfig(c)); err != nil {
showError("update config file failure " + err.Error())
}
//storage file
//fmt.Println("Check storage type")
//ioutil.WriteFile("/tmp/.storage.value", []byte(c.String("storage-args")), 0644)
// start setup script to install rainbond
fmt.Println("Initializes the installation of the first node...")
cmd := exec.Command("bash", "-c", fmt.Sprintf("cd %s ; ./setup.sh", c.String("work_dir")))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
println(err.Error())
return
}
ioutil.WriteFile("/opt/rainbond/.rainbond.success", []byte(c.String("rainbond-version")), 0644)
return
}