Rainbond/grctl/cmd/node.go

497 lines
13 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2017-11-10 15:00:52 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2017-11-10 15:00:52 +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-10 15:00:52 +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-10 15:00:52 +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
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
2017-11-10 15:00:52 +08:00
"github.com/Sirupsen/logrus"
"github.com/apcera/termtables"
"github.com/goodrain/rainbond/api/util"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/urfave/cli"
2017-11-10 15:00:52 +08:00
)
2017-12-23 19:40:00 +08:00
func handleErr(err *util.APIHandleError) {
if err != nil && err.Err != nil {
2018-05-11 17:45:42 +08:00
fmt.Printf("%v\n", err.String())
os.Exit(1)
}
}
func NewCmdShow() cli.Command {
c := cli.Command{
Name: "show",
Usage: "显示region安装完成后访问地址",
Action: func(c *cli.Context) error {
Common(c)
manageHosts, err := clients.RegionClient.Nodes().GetNodeByRule("manage")
handleErr(err)
ips := getExternalIP("/etc/goodrain/envs/.exip", manageHosts)
2017-12-11 11:10:36 +08:00
fmt.Println("Manage your apps with webui")
for _, v := range ips {
url := v + ":7070"
fmt.Print(url + " ")
2017-12-11 11:10:36 +08:00
}
2017-12-11 19:41:25 +08:00
fmt.Println()
2017-12-11 11:10:36 +08:00
fmt.Println("The webui use websocket to provide more feture")
for _, v := range ips {
url := v + ":6060"
fmt.Print(url + " ")
2017-12-11 11:10:36 +08:00
}
2017-12-11 19:41:25 +08:00
fmt.Println()
2017-12-11 11:10:36 +08:00
fmt.Println("Your web apps use nginx for reverse proxy:")
for _, v := range ips {
url := v + ":80"
fmt.Print(url + " ")
}
2017-12-11 19:41:25 +08:00
fmt.Println()
return nil
},
}
return c
}
2017-11-10 15:00:52 +08:00
func getExternalIP(path string, node []*client.HostNode) []string {
2017-12-11 19:41:25 +08:00
var result []string
if fileExist(path) {
externalIP, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
strings.TrimSpace(string(externalIP))
result = append(result, strings.TrimSpace(string(externalIP)))
} else {
for _, v := range node {
result = append(result, v.InternalIP)
2017-12-11 19:41:25 +08:00
}
}
return result
}
func fileExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func handleStatus(serviceTable *termtables.Table, ready bool, v *client.HostNode) {
if v.Role.HasRule("compute") && !v.Role.HasRule("manage") {
2018-05-11 17:45:42 +08:00
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Status, v.Alived, !v.Unschedulable, ready)
} else if v.Role.HasRule("manage") && !v.Role.HasRule("compute") {
2017-12-11 17:58:17 +08:00
//scheduable="n/a"
2018-05-11 18:57:31 +08:00
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Status, v.Alived, "N/A", "N/A")
} else if v.Role.HasRule("compute") && v.Role.HasRule("manage") {
2018-05-11 17:45:42 +08:00
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Status, v.Alived, !v.Unschedulable, ready)
2017-12-11 17:58:17 +08:00
}
}
2018-05-11 17:45:42 +08:00
2018-07-30 17:41:37 +08:00
func handleResult(serviceTable *termtables.Table, v *client.HostNode) {
2018-07-27 16:54:01 +08:00
2018-07-27 15:24:59 +08:00
for _, v := range v.NodeStatus.Conditions {
2018-07-30 15:56:43 +08:00
if v.Type == client.NodeReady{
continue
}
2018-07-30 17:41:37 +08:00
serviceTable.AddRow(string(v.Type), string(v.Status), handleMessage(string(v.Status), v.Message))
2018-07-27 15:24:59 +08:00
}
}
2018-07-30 17:41:37 +08:00
func extractReady(serviceTable *termtables.Table, v *client.HostNode, name string) {
2018-07-30 15:56:43 +08:00
for _, v := range v.NodeStatus.Conditions {
if string(v.Type) == name{
2018-07-30 17:41:37 +08:00
serviceTable.AddRow(string(v.Type), string(v.Status), handleMessage(string(v.Status), v.Message))
2018-07-30 15:56:43 +08:00
}
}
}
2018-07-29 21:20:17 +08:00
func handleMessage(status string, message string) string {
if status == "True" {
return "N/A"
}
return message
}
2018-07-27 16:23:05 +08:00
2018-05-11 17:45:42 +08:00
//NewCmdNode NewCmdNode
2017-11-10 15:00:52 +08:00
func NewCmdNode() cli.Command {
c := cli.Command{
2017-11-10 15:00:52 +08:00
Name: "node",
Usage: "节点。grctl node",
Subcommands: []cli.Command{
2017-11-22 18:46:17 +08:00
{
Name: "get",
2017-12-05 15:02:43 +08:00
Usage: "get hostID/internal ip",
2017-11-22 18:46:17 +08:00
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
2017-12-05 15:02:43 +08:00
logrus.Errorf("need args")
return nil
}
nodes, err := clients.RegionClient.Nodes().List()
handleErr(err)
for _, v := range nodes {
if v.InternalIP == id {
id = v.ID
2017-12-05 15:02:43 +08:00
break
}
}
v, err := clients.RegionClient.Nodes().Get(id)
handleErr(err)
nodeByte, _ := json.Marshal(v)
var out bytes.Buffer
error := json.Indent(&out, nodeByte, "", "\t")
if error != nil {
2017-12-23 19:40:00 +08:00
handleErr(util.CreateAPIHandleError(500, err))
}
fmt.Println(out.String())
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "list",
Usage: "list",
2017-11-22 18:46:17 +08:00
Action: func(c *cli.Context) error {
Common(c)
list, err := clients.RegionClient.Nodes().List()
handleErr(err)
2017-11-23 16:13:45 +08:00
serviceTable := termtables.CreateTable()
2018-05-11 17:45:42 +08:00
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Status", "Alived", "Schedulable", "Ready")
var rest []*client.HostNode
for _, v := range list {
2017-12-11 17:58:17 +08:00
if v.Role.HasRule("manage") {
2018-05-11 17:45:42 +08:00
handleStatus(serviceTable, isNodeReady(v), v)
} else {
rest = append(rest, v)
}
}
if len(rest) > 0 {
serviceTable.AddSeparator()
}
for _, v := range rest {
2018-05-11 17:45:42 +08:00
handleStatus(serviceTable, isNodeReady(v), v)
2017-11-23 16:13:45 +08:00
}
fmt.Println(serviceTable.Render())
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "health",
2018-07-27 16:54:01 +08:00
Usage: "health hostID/internal ip",
Action: func(c *cli.Context) error {
Common(c)
2018-07-27 16:54:01 +08:00
id := c.Args().First()
if id == "" {
logrus.Errorf("need args")
return nil
}
nodes, err := clients.RegionClient.Nodes().List()
handleErr(err)
2018-07-27 16:54:01 +08:00
for _, v := range nodes {
if v.InternalIP == id {
id = v.ID
break
}
}
2018-07-27 16:23:05 +08:00
2018-07-27 16:54:01 +08:00
v, err := clients.RegionClient.Nodes().Get(id)
handleErr(err)
2018-07-30 17:41:37 +08:00
serviceTable := termtables.CreateTable()
serviceTable.AddHeaders("Title", "Result", "Message")
serviceTable.AddRow("Uid:", v.ID, "")
serviceTable.AddRow("IP:", v.InternalIP, "")
serviceTable.AddRow("HostName:", v.HostName, "")
extractReady(serviceTable, v, "Ready")
handleResult(serviceTable, v)
2018-07-27 15:24:59 +08:00
2018-07-30 17:41:37 +08:00
fmt.Println(serviceTable.Render())
return nil
},
},
2017-11-22 18:46:17 +08:00
{
Name: "up",
Usage: "up hostID",
2017-11-22 18:46:17 +08:00
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
logrus.Errorf("need hostID")
return nil
}
err := clients.RegionClient.Nodes().Up(id)
handleErr(err)
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "down",
Usage: "down hostID",
2017-11-22 18:46:17 +08:00
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
logrus.Errorf("need hostID")
return nil
}
err := clients.RegionClient.Nodes().Down(id)
handleErr(err)
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "unscheduable",
Usage: "unscheduable hostID",
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
logrus.Errorf("need hostID")
return nil
}
node, err := clients.RegionClient.Nodes().Get(id)
handleErr(err)
if !node.Role.HasRule("compute") {
logrus.Errorf("管理节点不支持此功能")
return nil
}
err = clients.RegionClient.Nodes().UnSchedulable(id)
handleErr(err)
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "rescheduable",
Usage: "rescheduable hostID",
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
logrus.Errorf("need hostID")
return nil
}
node, err := clients.RegionClient.Nodes().Get(id)
handleErr(err)
if !node.Role.HasRule("compute") {
logrus.Errorf("管理节点不支持此功能")
return nil
}
err = clients.RegionClient.Nodes().ReSchedulable(id)
handleErr(err)
2017-11-22 18:46:17 +08:00
return nil
},
},
{
Name: "delete",
Usage: "delete hostID",
Action: func(c *cli.Context) error {
Common(c)
id := c.Args().First()
if id == "" {
logrus.Errorf("need hostID")
return nil
}
err := clients.RegionClient.Nodes().Delete(id)
handleErr(err)
return nil
},
},
{
Name: "rule",
Usage: "rule ruleName",
Action: func(c *cli.Context) error {
Common(c)
rule := c.Args().First()
if rule == "" {
logrus.Errorf("need rule name")
return nil
}
hostnodes, err := clients.RegionClient.Nodes().GetNodeByRule(rule)
2017-12-23 19:40:00 +08:00
handleErr(err)
serviceTable := termtables.CreateTable()
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Status", "Alived", "Schedulable", "Ready")
2017-12-23 19:40:00 +08:00
for _, v := range hostnodes {
handleStatus(serviceTable, isNodeReady(v), v)
2017-12-23 19:40:00 +08:00
}
return nil
},
},
{
Name: "label",
Usage: "label hostID",
Flags: []cli.Flag{
cli.StringFlag{
Name: "key",
Value: "",
Usage: "key",
},
cli.StringFlag{
Name: "val",
Value: "",
Usage: "val",
},
},
Action: func(c *cli.Context) error {
Common(c)
hostID := c.Args().First()
if hostID == "" {
logrus.Errorf("need hostID")
return nil
}
k := c.String("key")
v := c.String("val")
label := make(map[string]string)
label[k] = v
err := clients.RegionClient.Nodes().Label(hostID, label)
handleErr(err)
return nil
},
},
{
Name: "add",
Usage: "add 添加节点",
Flags: []cli.Flag{
cli.StringFlag{
Name: "Hostname,hn",
Value: "",
Usage: "Hostname",
},
cli.StringFlag{
Name: "InternalIP,i",
Value: "",
2017-11-27 12:06:28 +08:00
Usage: "InternalIP|required",
},
cli.StringFlag{
Name: "ExternalIP,e",
Value: "",
Usage: "ExternalIP",
},
cli.StringFlag{
Name: "RootPass,p",
Value: "",
Usage: "RootPass",
},
cli.StringFlag{
Name: "Role,ro",
Usage: "Role|required",
},
},
Action: func(c *cli.Context) error {
Common(c)
var node client.APIHostNode
if c.IsSet("Role") {
node.Role = append(node.Role, c.String("Role"))
node.InternalIP = c.String("InternalIP")
node.HostName = c.String("HostName")
node.ExternalIP = c.String("ExternalIP")
node.RootPass = c.String("RootPass")
err := clients.RegionClient.Nodes().Add(&node)
2017-12-23 19:40:00 +08:00
handleErr(err)
fmt.Println("success add node")
2017-12-23 19:10:12 +08:00
// var hostNode *client.HostNode
// timer := time.NewTimer(15 * time.Second)
// gotNode := false
// for !gotNode {
// time.Sleep(3 * time.Second)
// list, err := clients.RegionClient.Nodes().List()
// handleErr(err)
// for _, v := range list {
// if node.InternalIP == v.InternalIP {
// hostNode = v
// timer.Stop()
// gotNode = true
// //todo 初始化其它节点失败判定
// }
// }
// }
// fmt.Println("添加节点成功,正在初始化")
// tableC := termtables.CreateTable()
// var header []string
// var content []string
// for {
// time.Sleep(3 * time.Second)
// list, err := clients.RegionClient.Nodes().List()
// handleErr(err)
// select {
// case <-timer.C:
// fmt.Println("添加节点超时请检查etcd")
// return nil
// default:
// for _, v := range list {
// if node.InternalIP == v.InternalIP {
// hostNode = v
// break
// }
// }
// for _, val := range hostNode.NodeStatus.Conditions {
// fmt.Println("正在判断节点状态,请稍等")
// if hostNode.Alived || (val.Type == client.NodeInit && val.Status == client.ConditionTrue) {
// fmt.Printf("节点 %s 初始化成功", hostNode.ID)
// fmt.Println()
// header = append(header, string(val.Type))
// content = append(content, string(val.Status))
// tableC.AddHeaders(header)
// tableC.AddRow(content)
// fmt.Println(tableC.Render())
// return nil
// } else if val.Type == client.NodeInit && val.Status == client.ConditionFalse {
// fmt.Printf("节点 %s 初始化失败:%s", hostNode.ID, val.Reason)
// return nil
// } else {
// fmt.Printf("..")
// }
// }
// }
// }
// fmt.Println("节点初始化结束")
return nil
}
return errors.New("role must not null")
},
},
2017-11-22 18:46:17 +08:00
},
}
return c
}
2017-11-10 15:00:52 +08:00
func isNodeReady(node *client.HostNode) bool {
2017-12-18 18:50:10 +08:00
if node.NodeStatus == nil {
return false
}
2017-12-18 18:50:10 +08:00
for _, v := range node.NodeStatus.Conditions {
if strings.ToLower(string(v.Type)) == "ready" {
if strings.ToLower(string(v.Status)) == "true" {
return true
}
}
}
2017-11-10 15:00:52 +08:00
return false
}