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 (
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"fmt"
|
2017-11-10 15:00:52 +08:00
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"github.com/apcera/termtables"
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/api/util"
|
|
|
|
|
"github.com/goodrain/rainbond/grctl/clients"
|
2018-07-16 15:30:53 +08:00
|
|
|
|
"github.com/goodrain/rainbond/node/nodem/client"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"github.com/urfave/cli"
|
2018-08-02 13:58:55 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"os/exec"
|
2018-08-05 01:06:34 +08:00
|
|
|
|
"github.com/gosuri/uitable"
|
2018-08-23 10:18:29 +08:00
|
|
|
|
"strconv"
|
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())
|
2017-12-21 17:09:17 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-05 14:47:36 +08:00
|
|
|
|
func NewCmdShow() cli.Command {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
c := cli.Command{
|
|
|
|
|
Name: "show",
|
|
|
|
|
Usage: "显示region安装完成后访问地址",
|
2017-12-05 14:47:36 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2018-07-17 16:18:19 +08:00
|
|
|
|
manageHosts, err := clients.RegionClient.Nodes().GetNodeByRule("manage")
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2018-08-06 17:00:57 +08:00
|
|
|
|
ips := getExternalIP("/opt/rainbond/envs/.exip", manageHosts)
|
2017-12-11 11:10:36 +08:00
|
|
|
|
fmt.Println("Manage your apps with webui:")
|
2017-12-12 11:36:57 +08:00
|
|
|
|
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:")
|
2017-12-12 11:36:57 +08:00
|
|
|
|
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:")
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range ips {
|
|
|
|
|
url := v + ":80"
|
|
|
|
|
fmt.Print(url + " ")
|
2017-12-05 14:47:36 +08:00
|
|
|
|
}
|
2017-12-11 19:41:25 +08:00
|
|
|
|
fmt.Println()
|
2017-12-05 14:47:36 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|
2017-11-10 15:00:52 +08:00
|
|
|
|
|
2018-07-16 15:30:53 +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))
|
2017-12-12 11:36:57 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
2018-08-22 16:06:28 +08:00
|
|
|
|
|
2018-08-22 19:16:54 +08:00
|
|
|
|
func handleStatus(serviceTable *termtables.Table, ready bool, v *client.HostNode) {
|
2018-08-05 01:06:34 +08:00
|
|
|
|
var status string
|
2018-08-05 08:50:29 +08:00
|
|
|
|
if ready == true {
|
2018-08-05 01:06:34 +08:00
|
|
|
|
status = "\033[0;32;32m running(healthy) \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if v.Unschedulable == true {
|
2018-08-08 13:09:17 +08:00
|
|
|
|
status = "\033[0;33;33m running(unschedulable) \033[0m"
|
2018-08-05 01:06:34 +08:00
|
|
|
|
}
|
2018-08-07 14:46:52 +08:00
|
|
|
|
if ready == false {
|
2018-08-08 13:09:17 +08:00
|
|
|
|
status = "\033[0;33;33m running(unhealthy) \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if ready == false && v.Unschedulable == true {
|
|
|
|
|
status = "\033[0;33;33m running(unhealthy,unschedulable) \033[0m"
|
2018-08-07 14:46:52 +08:00
|
|
|
|
}
|
2018-08-05 08:50:29 +08:00
|
|
|
|
if v.Status == "unknown" {
|
2018-08-05 01:06:34 +08:00
|
|
|
|
status = "\033[0;31;31m unknown \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if v.Status == "offline" {
|
|
|
|
|
status = "\033[0;31;31m offline \033[0m"
|
2018-07-31 19:45:22 +08:00
|
|
|
|
}
|
2018-10-31 18:35:44 +08:00
|
|
|
|
if v.Status == "not_installed" {
|
|
|
|
|
status = "\033[0;31;31m not_installed \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if v.Status == "installing" {
|
|
|
|
|
status = "\033[0;33;33m installing \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if v.Status == "install_failed" {
|
|
|
|
|
status = "\033[0;31;31m init_failed \033[0m"
|
|
|
|
|
}
|
|
|
|
|
if v.Status == "install_success" {
|
|
|
|
|
status = "\033[0;33;33m install_success \033[0m"
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
if v.Role.HasRule("compute") && !v.Role.HasRule("manage") {
|
2018-08-22 19:16:54 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, status)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else if v.Role.HasRule("manage") && !v.Role.HasRule("compute") {
|
2017-12-11 17:58:17 +08:00
|
|
|
|
//scheduable="n/a"
|
2018-08-22 19:16:54 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, status)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else if v.Role.HasRule("compute") && v.Role.HasRule("manage") {
|
2018-08-22 19:16:54 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, status)
|
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-08-02 13:58:55 +08:00
|
|
|
|
if v.Type == client.NodeReady {
|
2018-07-30 15:56:43 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
2018-07-31 19:45:22 +08:00
|
|
|
|
var formatReady string
|
2018-08-02 13:58:55 +08:00
|
|
|
|
if v.Status == client.ConditionFalse {
|
|
|
|
|
if v.Type == client.OutOfDisk || v.Type == client.MemoryPressure || v.Type == client.DiskPressure || v.Type == client.InstallNotReady {
|
2018-08-01 12:29:06 +08:00
|
|
|
|
formatReady = "\033[0;32;32m false \033[0m"
|
2018-08-02 13:58:55 +08:00
|
|
|
|
} else {
|
2018-09-13 18:57:02 +08:00
|
|
|
|
formatReady = "\033[0;31;31m false \033[0m"
|
2018-07-31 19:45:22 +08:00
|
|
|
|
}
|
2018-08-02 13:58:55 +08:00
|
|
|
|
} else {
|
2018-09-13 18:57:02 +08:00
|
|
|
|
if v.Type == client.OutOfDisk || v.Type == client.MemoryPressure || v.Type == client.DiskPressure || v.Type == client.InstallNotReady {
|
|
|
|
|
|
|
|
|
|
formatReady = "\033[0;31;31m true \033[0m"
|
|
|
|
|
} else {
|
|
|
|
|
formatReady = "\033[0;32;32m true \033[0m"
|
|
|
|
|
}
|
2018-07-31 19:45:22 +08:00
|
|
|
|
}
|
|
|
|
|
serviceTable.AddRow(string(v.Type), formatReady, handleMessage(string(v.Status), v.Message))
|
2018-07-27 15:24:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 13:58:55 +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 {
|
2018-08-02 13:58:55 +08:00
|
|
|
|
if string(v.Type) == name {
|
2018-07-31 19:45:22 +08:00
|
|
|
|
var formatReady string
|
2018-08-02 13:58:55 +08:00
|
|
|
|
if v.Status == client.ConditionFalse {
|
2018-08-01 12:29:06 +08:00
|
|
|
|
formatReady = "\033[0;31;31m false \033[0m"
|
2018-08-02 13:58:55 +08:00
|
|
|
|
} else {
|
2018-08-01 12:29:06 +08:00
|
|
|
|
formatReady = "\033[0;32;32m true \033[0m"
|
2018-07-31 19:45:22 +08:00
|
|
|
|
}
|
2018-08-01 12:29:06 +08:00
|
|
|
|
serviceTable.AddRow("\033[0;33;33m "+string(v.Type)+" \033[0m", formatReady, 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" {
|
2018-07-31 19:45:22 +08:00
|
|
|
|
return ""
|
2018-07-29 21:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
c := cli.Command{
|
2017-11-10 15:00:52 +08:00
|
|
|
|
Name: "node",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "节点管理相关操作",
|
2017-12-12 11:36:57 +08:00
|
|
|
|
Subcommands: []cli.Command{
|
2017-11-22 18:46:17 +08:00
|
|
|
|
{
|
2017-11-23 15:05:46 +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 {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-23 15:05:46 +08:00
|
|
|
|
if id == "" {
|
2017-12-05 15:02:43 +08:00
|
|
|
|
logrus.Errorf("need args")
|
2017-11-23 15:05:46 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
nodes, err := clients.RegionClient.Nodes().List()
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range nodes {
|
|
|
|
|
if v.InternalIP == id {
|
|
|
|
|
id = v.ID
|
2017-12-05 15:02:43 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
|
v, err := clients.RegionClient.Nodes().Get(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2018-08-05 01:06:34 +08:00
|
|
|
|
table := uitable.New()
|
|
|
|
|
fmt.Printf("-------------------Node information-----------------------\n")
|
2018-08-05 08:50:29 +08:00
|
|
|
|
table.AddRow("status", v.NodeStatus.Status)
|
|
|
|
|
table.AddRow("unschedulable", v.Unschedulable)
|
|
|
|
|
table.AddRow("alived", v.Alived)
|
2018-08-05 01:06:34 +08:00
|
|
|
|
table.AddRow("uuid", v.ID)
|
|
|
|
|
table.AddRow("host_name", v.HostName)
|
|
|
|
|
table.AddRow("create_time", v.CreateTime)
|
|
|
|
|
table.AddRow("internal_ip", v.InternalIP)
|
|
|
|
|
table.AddRow("external_ip", v.ExternalIP)
|
|
|
|
|
table.AddRow("role", v.Role)
|
|
|
|
|
table.AddRow("mode", v.Mode)
|
|
|
|
|
table.AddRow("available_memory", v.AvailableMemory)
|
|
|
|
|
table.AddRow("available_cpu", v.AvailableCPU)
|
|
|
|
|
table.AddRow("pid", v.PID)
|
|
|
|
|
table.AddRow("version", v.Version)
|
|
|
|
|
table.AddRow("up", v.UpTime)
|
|
|
|
|
table.AddRow("down", v.DownTime)
|
|
|
|
|
table.AddRow("connected", v.Connected)
|
2018-08-05 08:36:50 +08:00
|
|
|
|
fmt.Println(table)
|
2018-08-12 12:57:31 +08:00
|
|
|
|
fmt.Printf("-------------------service health-----------------------\n")
|
2018-08-05 01:06:34 +08:00
|
|
|
|
serviceTable := termtables.CreateTable()
|
|
|
|
|
serviceTable.AddHeaders("Title", "Result", "Message")
|
|
|
|
|
extractReady(serviceTable, v, "Ready")
|
|
|
|
|
handleResult(serviceTable, v)
|
|
|
|
|
|
|
|
|
|
fmt.Println(serviceTable.Render())
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Name: "list",
|
|
|
|
|
Usage: "list",
|
2017-11-22 18:46:17 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2018-07-17 16:18:19 +08:00
|
|
|
|
list, err := clients.RegionClient.Nodes().List()
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-23 16:13:45 +08:00
|
|
|
|
serviceTable := termtables.CreateTable()
|
2018-08-22 19:16:54 +08:00
|
|
|
|
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Status")
|
2018-07-16 15:30:53 +08:00
|
|
|
|
var rest []*client.HostNode
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range list {
|
2018-08-22 16:28:56 +08:00
|
|
|
|
if v.Role.HasRule("manage") || !v.Role.HasRule("compute") {
|
2018-10-30 14:29:50 +08:00
|
|
|
|
handleStatus(serviceTable, v.NodeHealth, v)
|
2018-05-11 17:45:42 +08:00
|
|
|
|
} else {
|
|
|
|
|
rest = append(rest, v)
|
2017-12-01 11:38:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
if len(rest) > 0 {
|
2017-12-01 18:37:35 +08:00
|
|
|
|
serviceTable.AddSeparator()
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range rest {
|
2018-10-30 14:29:50 +08:00
|
|
|
|
handleStatus(serviceTable, v.NodeHealth, v)
|
2018-08-22 19:16:54 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(serviceTable.Render())
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "resource",
|
|
|
|
|
Usage: "resource",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
Common(c)
|
|
|
|
|
list, err := clients.RegionClient.Nodes().List()
|
|
|
|
|
handleErr(err)
|
|
|
|
|
serviceTable := termtables.CreateTable()
|
2018-08-28 12:10:40 +08:00
|
|
|
|
serviceTable.AddHeaders("Uid", "HostName", "CapCpu(核)", "CapMemory(M)", "UsedCpu(核)", "UsedMemory(M)", "CpuLimits(核)", "MemoryLimits(M)", "CpuUsageRate(%)", "MemoryUsedRate(%)")
|
2018-08-22 19:16:54 +08:00
|
|
|
|
for _, v := range list {
|
2018-10-30 15:02:31 +08:00
|
|
|
|
if v.Role.HasRule("compute") && v.Status != "offline" {
|
2018-08-22 19:16:54 +08:00
|
|
|
|
nodeResource, err := clients.RegionClient.Nodes().GetNodeResource(v.ID)
|
|
|
|
|
handleErr(err)
|
2018-08-23 10:18:29 +08:00
|
|
|
|
CPURequests := strconv.FormatFloat(float64(nodeResource.CPURequests)/float64(1000), 'f', 2, 64)
|
|
|
|
|
CPULimits := strconv.FormatFloat(float64(nodeResource.CPULimits)/float64(1000), 'f', 2, 64)
|
|
|
|
|
serviceTable.AddRow(v.ID, v.HostName, nodeResource.CpuR, nodeResource.MemR, CPURequests, nodeResource.MemoryRequests, CPULimits, nodeResource.MemoryLimits, nodeResource.CPURequestsR, nodeResource.MemoryRequestsR)
|
2018-08-22 19:16:54 +08:00
|
|
|
|
}
|
2017-11-23 16:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(serviceTable.Render())
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Name: "up",
|
|
|
|
|
Usage: "up hostID",
|
2017-11-22 18:46:17 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-23 15:05:46 +08:00
|
|
|
|
if id == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err := clients.RegionClient.Nodes().Up(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Name: "down",
|
|
|
|
|
Usage: "down hostID",
|
2017-11-22 18:46:17 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-23 15:05:46 +08:00
|
|
|
|
if id == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err := clients.RegionClient.Nodes().Down(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2018-08-20 15:28:23 +08:00
|
|
|
|
Name: "cordon",
|
|
|
|
|
Usage: "Mark node as unschedulable",
|
2017-11-22 18:46:17 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-23 15:05:46 +08:00
|
|
|
|
if id == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
node, err := clients.RegionClient.Nodes().Get(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
|
|
|
|
if !node.Role.HasRule("compute") {
|
2017-11-28 15:35:29 +08:00
|
|
|
|
logrus.Errorf("管理节点不支持此功能")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err = clients.RegionClient.Nodes().UnSchedulable(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2018-08-20 15:28:23 +08:00
|
|
|
|
Name: "uncordon",
|
|
|
|
|
Usage: "Mark node as schedulable",
|
2017-11-22 18:46:17 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-23 15:05:46 +08:00
|
|
|
|
if id == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
node, err := clients.RegionClient.Nodes().Get(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
|
|
|
|
if !node.Role.HasRule("compute") {
|
2017-11-28 15:35:29 +08:00
|
|
|
|
logrus.Errorf("管理节点不支持此功能")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err = clients.RegionClient.Nodes().ReSchedulable(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-11-27 13:39:22 +08:00
|
|
|
|
{
|
|
|
|
|
Name: "delete",
|
|
|
|
|
Usage: "delete hostID",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
id := c.Args().First()
|
2017-11-27 13:39:22 +08:00
|
|
|
|
if id == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err := clients.RegionClient.Nodes().Delete(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-27 13:39:22 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-11-27 15:18:05 +08:00
|
|
|
|
{
|
|
|
|
|
Name: "rule",
|
|
|
|
|
Usage: "rule ruleName",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
rule := c.Args().First()
|
2017-11-27 15:18:05 +08:00
|
|
|
|
if rule == "" {
|
|
|
|
|
logrus.Errorf("need rule name")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
hostnodes, err := clients.RegionClient.Nodes().GetNodeByRule(rule)
|
2017-12-23 19:40:00 +08:00
|
|
|
|
handleErr(err)
|
|
|
|
|
serviceTable := termtables.CreateTable()
|
2018-08-22 19:16:54 +08:00
|
|
|
|
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Status")
|
2017-12-23 19:40:00 +08:00
|
|
|
|
for _, v := range hostnodes {
|
2018-10-30 14:29:50 +08:00
|
|
|
|
handleStatus(serviceTable, v.NodeHealth, v)
|
2017-12-23 19:40:00 +08:00
|
|
|
|
}
|
2017-11-27 15:18:05 +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 {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
hostID := c.Args().First()
|
2017-11-27 15:18:05 +08:00
|
|
|
|
if hostID == "" {
|
|
|
|
|
logrus.Errorf("need hostID")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
k := c.String("key")
|
|
|
|
|
v := c.String("val")
|
|
|
|
|
label := make(map[string]string)
|
|
|
|
|
label[k] = v
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err := clients.RegionClient.Nodes().Label(hostID, label)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-27 15:18:05 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-11-23 15:05:46 +08:00
|
|
|
|
{
|
|
|
|
|
Name: "add",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "Add a node into the cluster",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "hostname,host",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "The option is required",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "internal-ip,iip",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "The option is required",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "external-ip,eip",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "Publish the ip address for external connection",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "root-pass,p",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "Specify the root password of the target host for login, this option conflicts with private-key",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "private-key,key",
|
2018-08-02 13:58:55 +08:00
|
|
|
|
Usage: "Specify the private key file for login, this option conflicts with root-pass",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
2017-11-28 15:35:29 +08:00
|
|
|
|
cli.StringFlag{
|
2018-08-06 17:00:57 +08:00
|
|
|
|
Name: "role,r",
|
2018-09-25 19:49:40 +08:00
|
|
|
|
Usage: "The option is required, the allowed values are: [master|worker]",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 17:56:27 +08:00
|
|
|
|
Common(c)
|
2018-08-02 13:58:55 +08:00
|
|
|
|
if !c.IsSet("role") {
|
|
|
|
|
println("role must not null")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.String("root-pass") != "" && c.String("private-key") != "" {
|
|
|
|
|
println("Options private-key and root-pass are conflicting")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-11-28 15:35:29 +08:00
|
|
|
|
|
2018-08-02 13:58:55 +08:00
|
|
|
|
model := "pass"
|
|
|
|
|
if c.String("private-key") != "" {
|
|
|
|
|
model = "key"
|
|
|
|
|
}
|
2017-12-23 19:10:12 +08:00
|
|
|
|
|
2018-08-02 13:58:55 +08:00
|
|
|
|
// start add node script
|
|
|
|
|
fmt.Println("Begin add node, please don't exit")
|
2018-09-25 19:49:40 +08:00
|
|
|
|
line := fmt.Sprintf("cd /opt/rainbond/install/scripts; ./join.sh %s %s %s %s %s %s", c.String("role"), c.String("hostname"),
|
2018-08-02 13:58:55 +08:00
|
|
|
|
c.String("internal-ip"), model, c.String("root-pass"), c.String("private-key"))
|
|
|
|
|
cmd := exec.Command("bash", "-c", line)
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
2017-12-25 11:21:31 +08:00
|
|
|
|
|
2018-08-02 13:58:55 +08:00
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
println(err.Error())
|
2017-11-23 15:05:46 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-08-02 13:58:55 +08:00
|
|
|
|
|
|
|
|
|
fmt.Println("Add node successful, next you can:")
|
|
|
|
|
fmt.Println(" check cluster status: grctl node list")
|
2018-08-06 17:00:57 +08:00
|
|
|
|
//fmt.Println(" online node: grctl node up --help")
|
2018-08-02 13:58:55 +08:00
|
|
|
|
|
|
|
|
|
//var node client.APIHostNode
|
|
|
|
|
//node.Role = append(node.Role, c.String("role"))
|
|
|
|
|
//node.HostName = c.String("hostname")
|
|
|
|
|
//node.RootPass = c.String("root-pass")
|
|
|
|
|
//node.InternalIP = c.String("internal-ip")
|
|
|
|
|
//node.ExternalIP = c.String("external-ip")
|
|
|
|
|
|
|
|
|
|
//err := clients.RegionClient.Nodes().Add(&node)
|
|
|
|
|
//handleErr(err)
|
|
|
|
|
//fmt.Println("success add node")
|
|
|
|
|
|
|
|
|
|
return nil
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
2017-11-22 18:46:17 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|
2017-11-10 15:00:52 +08:00
|
|
|
|
|
2018-07-16 15:30:53 +08:00
|
|
|
|
func isNodeReady(node *client.HostNode) bool {
|
2017-12-18 18:50:10 +08:00
|
|
|
|
if node.NodeStatus == nil {
|
2017-12-15 14:57:17 +08:00
|
|
|
|
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" {
|
2017-12-15 14:57:17 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-10 15:00:52 +08:00
|
|
|
|
|
2017-12-15 14:57:17 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|