2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-11-22 11:40:14 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
// 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
|
2017-12-12 11:36:57 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
import (
|
2017-12-20 18:22:36 +08:00
|
|
|
"bufio"
|
2017-12-12 11:36:57 +08:00
|
|
|
"bytes"
|
2017-12-25 19:49:32 +08:00
|
|
|
"fmt"
|
2017-11-22 11:40:14 +08:00
|
|
|
"io/ioutil"
|
2017-12-12 11:36:57 +08:00
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
2017-11-22 11:40:14 +08:00
|
|
|
"strings"
|
2017-12-20 18:22:36 +08:00
|
|
|
"time"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
2018-04-24 16:44:59 +08:00
|
|
|
//"github.com/goodrain/rainbond/grctl/clients"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"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"
|
2017-11-22 11:40:14 +08:00
|
|
|
)
|
|
|
|
|
2017-12-20 18:39:08 +08:00
|
|
|
//NewCmdInit grctl init
|
2017-11-22 11:40:14 +08:00
|
|
|
func NewCmdInit() cli.Command {
|
2017-12-12 11:36:57 +08:00
|
|
|
c := cli.Command{
|
|
|
|
Name: "init",
|
2017-11-22 11:40:14 +08:00
|
|
|
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",
|
|
|
|
},
|
2017-12-20 18:39:08 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "test",
|
|
|
|
Usage: "use test shell",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2017-11-22 11:40:14 +08:00
|
|
|
},
|
|
|
|
Usage: "初始化集群。grctl init cluster",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return initCluster(c)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2017-11-29 18:44:39 +08:00
|
|
|
func NewCmdInstallStatus() cli.Command {
|
2017-12-12 11:36:57 +08:00
|
|
|
c := cli.Command{
|
|
|
|
Name: "install_status",
|
2017-11-29 18:44:39 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "taskID",
|
|
|
|
Usage: "install_k8s,空则自动寻找",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Usage: "获取task执行状态。grctl install_status",
|
|
|
|
Action: func(c *cli.Context) error {
|
2017-12-12 11:36:57 +08:00
|
|
|
taskID := c.String("taskID")
|
|
|
|
if taskID == "" {
|
2018-07-17 16:18:19 +08:00
|
|
|
tasks, err := clients.RegionClient.Tasks().List()
|
2017-11-29 18:44:39 +08:00
|
|
|
if err != nil {
|
2017-12-12 11:36:57 +08:00
|
|
|
logrus.Errorf("error get task list,details %s", err.Error())
|
2017-11-29 23:19:06 +08:00
|
|
|
return nil
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
for _, v := range tasks {
|
|
|
|
for _, vs := range v.Status {
|
|
|
|
if vs.Status == "start" || vs.Status == "create" {
|
2017-12-08 17:32:40 +08:00
|
|
|
//Status(v.ID)
|
2017-11-29 18:49:24 +08:00
|
|
|
return nil
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-07 23:48:07 +08:00
|
|
|
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
} else {
|
2017-12-08 17:32:40 +08:00
|
|
|
//Status(taskID)
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-11-29 18:49:24 +08:00
|
|
|
return nil
|
2017-11-29 18:44:39 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2017-11-27 18:48:51 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
func initCluster(c *cli.Context) error {
|
2018-03-13 11:25:48 +08:00
|
|
|
url := "http://repo.goodrain.com/release/3.5/gaops/jobs/install/prepare/init.sh"
|
2017-12-20 18:39:08 +08:00
|
|
|
if c.Bool("test") {
|
|
|
|
url = "http://dev.repo.goodrain.com/gaops/jobs/install/prepare/init.sh"
|
|
|
|
}
|
|
|
|
resp, err := http.Get(url)
|
2017-11-22 11:40:14 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2017-12-12 11:36:57 +08:00
|
|
|
logrus.Errorf("error get init script,details %s", err.Error())
|
2017-11-23 15:05:46 +08:00
|
|
|
return err
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
2017-12-12 11:36:57 +08:00
|
|
|
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, "")
|
2017-11-22 11:40:14 +08:00
|
|
|
if len(argCheck) > 0 {
|
2017-12-12 11:36:57 +08:00
|
|
|
arg += ";"
|
|
|
|
} else {
|
|
|
|
arg = ""
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 18:22:36 +08:00
|
|
|
fmt.Println("begin init cluster first node,please don't exit,wait install")
|
2017-12-12 11:36:57 +08:00
|
|
|
cmd := exec.Command("bash", "-c", arg+string(b))
|
2017-12-20 19:21:25 +08:00
|
|
|
var buffe bytes.Buffer
|
|
|
|
cmd.Stderr = &buffe
|
2017-12-20 18:22:36 +08:00
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
go func() {
|
|
|
|
read := bufio.NewReader(stdout)
|
|
|
|
for {
|
|
|
|
line, _, err := read.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-12-20 18:53:24 +08:00
|
|
|
fmt.Println(string(line))
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
logrus.Errorf("current node init error,%s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//检测并设置init的结果
|
2017-12-20 19:21:25 +08:00
|
|
|
result := buffe.String()
|
|
|
|
index := strings.Index(result, "{")
|
|
|
|
jsonOutPut := result
|
2017-12-20 18:22:36 +08:00
|
|
|
if index > -1 {
|
2017-12-20 19:21:25 +08:00
|
|
|
jsonOutPut = result[index:]
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
2017-12-20 19:21:25 +08:00
|
|
|
fmt.Println("Result:" + jsonOutPut)
|
2017-12-20 18:22:36 +08:00
|
|
|
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 {
|
2017-12-24 00:46:24 +08:00
|
|
|
if strings.Index(v, ",") > -1 {
|
|
|
|
values := strings.Split(v, ",")
|
2017-12-24 18:38:07 +08:00
|
|
|
coreutil.Deweight(&values)
|
2017-12-20 18:22:36 +08:00
|
|
|
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
|
2017-12-22 19:24:10 +08:00
|
|
|
var error *util.APIHandleError
|
2017-12-20 18:22:36 +08:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
time.Sleep(time.Second * 2)
|
2018-07-17 16:18:19 +08:00
|
|
|
gc, error = clients.RegionClient.Configs().Get()
|
2017-12-20 18:22:36 +08:00
|
|
|
if err == nil && gc != nil {
|
|
|
|
for _, nc := range newConfigs {
|
|
|
|
gc.Add(nc)
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
error = clients.RegionClient.Configs().Put(gc)
|
2017-12-20 18:22:36 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 19:24:10 +08:00
|
|
|
if error != nil {
|
2017-12-20 18:22:36 +08:00
|
|
|
logrus.Errorf("Update Datacenter configs error,please check node status")
|
|
|
|
return err
|
2017-11-28 18:04:21 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
//获取当前节点ID
|
2017-12-22 19:24:10 +08:00
|
|
|
hostID, err := coreutil.ReadHostID("")
|
2017-12-20 18:22:36 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("read nodeid error,please check node status")
|
|
|
|
return err
|
2017-11-28 18:04:21 +08:00
|
|
|
}
|
2017-11-29 11:16:51 +08:00
|
|
|
|
2018-03-13 14:26:39 +08:00
|
|
|
//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
|
|
|
|
//}
|
2018-07-17 16:18:19 +08:00
|
|
|
error = clients.RegionClient.Tasks().Exec("check_manage_services", []string{hostID})
|
2017-12-22 19:24:10 +08:00
|
|
|
if error != nil {
|
|
|
|
logrus.Errorf("error exec task:%s,details %s", "check_manage_services", error.String())
|
|
|
|
return error.Err
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
2018-03-13 14:26:39 +08:00
|
|
|
//Status("check_manage_base_services", []string{hostID})
|
2017-12-20 18:22:36 +08:00
|
|
|
Status("check_manage_services", []string{hostID})
|
2017-12-11 14:29:36 +08:00
|
|
|
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")
|
2017-11-22 11:40:14 +08:00
|
|
|
return nil
|
|
|
|
}
|