2017-11-10 15:00:52 +08:00
|
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
|
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
|
|
|
|
|
|
|
|
|
// 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 (
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2017-11-10 15:00:52 +08:00
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"github.com/apcera/termtables"
|
2017-12-23 19:40:00 +08:00
|
|
|
|
"github.com/goodrain/rainbond/pkg/api/util"
|
2017-11-10 15:00:52 +08:00
|
|
|
|
"github.com/goodrain/rainbond/pkg/grctl/clients"
|
2017-12-12 11:36:57 +08:00
|
|
|
|
"github.com/goodrain/rainbond/pkg/node/api/model"
|
|
|
|
|
"github.com/urfave/cli"
|
2017-11-10 15:00:52 +08:00
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-20 17:29:39 +08:00
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
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 {
|
2017-12-27 18:20:52 +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 {
|
2017-12-23 19:40:00 +08:00
|
|
|
|
manageHosts, err := clients.NodeClient.Nodes().Rule("manage")
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
ips := getExternalIP("/etc/goodrain/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
|
|
|
|
|
2017-12-12 11:36:57 +08:00
|
|
|
|
func getExternalIP(path string, node []*model.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
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
func handleStatus(serviceTable *termtables.Table, ready bool, v *model.HostNode) {
|
|
|
|
|
if v.Role.HasRule("compute") && !v.Role.HasRule("manage") {
|
2017-12-11 17:58:17 +08:00
|
|
|
|
if ready {
|
|
|
|
|
// true of false
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Alived, !v.Unschedulable, ready)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else {
|
2017-12-11 17:58:17 +08:00
|
|
|
|
//scheduable==false
|
|
|
|
|
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Alived, false, ready)
|
2017-12-11 17:58:17 +08:00
|
|
|
|
}
|
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"
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Alived, "N/A", ready)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else if v.Role.HasRule("compute") && v.Role.HasRule("manage") {
|
2017-12-11 17:58:17 +08:00
|
|
|
|
if !ready {
|
|
|
|
|
//n/a
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Alived, "N/A", ready)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else {
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddRow(v.ID, v.InternalIP, v.HostName, v.Role.String(), v.Mode, v.Alived, !v.Unschedulable, ready)
|
2017-12-11 17:58:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Usage: "节点。grctl node",
|
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 {
|
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
|
|
|
|
|
}
|
2017-12-05 15:02:43 +08:00
|
|
|
|
|
2017-12-23 19:40:00 +08:00
|
|
|
|
nodes, err := clients.NodeClient.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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 19:40:00 +08:00
|
|
|
|
v, err := clients.NodeClient.Nodes().Get(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-12-12 11:36:57 +08:00
|
|
|
|
nodeByte, _ := json.Marshal(v)
|
2017-12-01 11:38:46 +08:00
|
|
|
|
var out bytes.Buffer
|
2017-12-21 17:09:17 +08:00
|
|
|
|
error := json.Indent(&out, nodeByte, "", "\t")
|
|
|
|
|
if error != nil {
|
2017-12-23 19:40:00 +08:00
|
|
|
|
handleErr(util.CreateAPIHandleError(500, err))
|
2017-11-28 15:35:29 +08:00
|
|
|
|
}
|
2017-12-01 11:38:46 +08:00
|
|
|
|
fmt.Println(out.String())
|
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 {
|
2017-12-23 19:40:00 +08:00
|
|
|
|
list, err := clients.NodeClient.Nodes().List()
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-23 16:13:45 +08:00
|
|
|
|
serviceTable := termtables.CreateTable()
|
2017-12-20 10:23:55 +08:00
|
|
|
|
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Alived", "Schedulable", "Ready")
|
2017-12-01 11:38:46 +08:00
|
|
|
|
var rest []*model.HostNode
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range list {
|
2017-12-27 14:15:40 +08:00
|
|
|
|
var ready bool=false
|
|
|
|
|
if isNodeReady(v){
|
|
|
|
|
ready=true
|
2017-12-01 11:38:46 +08:00
|
|
|
|
}
|
2017-12-11 17:58:17 +08:00
|
|
|
|
if v.Role.HasRule("manage") {
|
2017-12-27 14:15:40 +08:00
|
|
|
|
handleStatus(serviceTable,ready,v)
|
|
|
|
|
}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 {
|
|
|
|
|
var ready bool = false
|
|
|
|
|
if v.NodeStatus != nil {
|
|
|
|
|
ready = true
|
2017-12-01 11:38:46 +08:00
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
handleStatus(serviceTable, ready, v)
|
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 {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err := clients.NodeClient.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 {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err := clients.NodeClient.Nodes().Down(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "unscheduable",
|
|
|
|
|
Usage: "unscheduable hostID",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
node, err := clients.NodeClient.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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err = clients.NodeClient.Nodes().UnSchedulable(id)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-11-22 18:46:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "rescheduable",
|
|
|
|
|
Usage: "rescheduable hostID",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
node, err := clients.NodeClient.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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err = clients.NodeClient.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 {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err := clients.NodeClient.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 {
|
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
|
|
|
|
|
}
|
2017-12-23 19:40:00 +08:00
|
|
|
|
hostnodes, err := clients.NodeClient.Nodes().Rule(rule)
|
|
|
|
|
handleErr(err)
|
|
|
|
|
serviceTable := termtables.CreateTable()
|
|
|
|
|
serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "NodeMode", "Alived", "Schedulable", "Ready")
|
|
|
|
|
for _, v := range hostnodes {
|
|
|
|
|
|
|
|
|
|
var ready bool = false
|
|
|
|
|
if v.NodeStatus != nil {
|
|
|
|
|
ready = true
|
|
|
|
|
}
|
|
|
|
|
handleStatus(serviceTable, ready, v)
|
|
|
|
|
|
|
|
|
|
}
|
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 {
|
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
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err := clients.NodeClient.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",
|
|
|
|
|
Usage: "add 添加节点",
|
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "Hostname,hn",
|
2017-12-12 11:36:57 +08:00
|
|
|
|
Value: "",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Usage: "Hostname",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "InternalIP,i",
|
2017-12-12 11:36:57 +08:00
|
|
|
|
Value: "",
|
2017-11-27 12:06:28 +08:00
|
|
|
|
Usage: "InternalIP|required",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "ExternalIP,e",
|
2017-12-12 11:36:57 +08:00
|
|
|
|
Value: "",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Usage: "ExternalIP",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "RootPass,p",
|
2017-12-12 11:36:57 +08:00
|
|
|
|
Value: "",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Usage: "RootPass",
|
|
|
|
|
},
|
2017-11-28 15:35:29 +08:00
|
|
|
|
cli.StringFlag{
|
2017-11-27 17:42:10 +08:00
|
|
|
|
Name: "Role,ro",
|
2017-11-23 15:05:46 +08:00
|
|
|
|
Usage: "Role|required",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
var node model.APIHostNode
|
2017-12-12 11:36:57 +08:00
|
|
|
|
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")
|
2017-11-28 15:35:29 +08:00
|
|
|
|
|
2017-12-23 19:40:00 +08:00
|
|
|
|
err := clients.NodeClient.Nodes().Add(&node)
|
|
|
|
|
handleErr(err)
|
2017-11-28 15:35:29 +08:00
|
|
|
|
fmt.Println("开始初始化节点")
|
2017-12-23 19:10:12 +08:00
|
|
|
|
|
2017-12-23 18:45:15 +08:00
|
|
|
|
var hostNode *model.HostNode
|
2017-12-23 19:40:00 +08:00
|
|
|
|
timer := time.NewTimer(15 * time.Second)
|
2017-12-24 23:19:42 +08:00
|
|
|
|
gotNode:=false
|
|
|
|
|
for !gotNode {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
time.Sleep(3 * time.Second)
|
2017-12-25 11:21:31 +08:00
|
|
|
|
list, err := clients.NodeClient.Nodes().List()
|
|
|
|
|
handleErr(err)
|
2017-11-28 15:35:29 +08:00
|
|
|
|
for _, v := range list {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
if node.InternalIP == v.InternalIP {
|
2017-12-25 11:21:31 +08:00
|
|
|
|
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.NodeClient.Nodes().List()
|
|
|
|
|
handleErr(err)
|
2017-12-23 19:10:12 +08:00
|
|
|
|
select {
|
2017-12-23 19:14:00 +08:00
|
|
|
|
case <-timer.C:
|
2017-12-23 19:10:12 +08:00
|
|
|
|
fmt.Println("添加节点超时,请检查etcd")
|
|
|
|
|
return nil
|
|
|
|
|
default:
|
|
|
|
|
for _, v := range list {
|
|
|
|
|
if node.InternalIP == v.InternalIP {
|
2017-12-25 11:21:31 +08:00
|
|
|
|
hostNode=v
|
|
|
|
|
break
|
2017-11-28 15:35:29 +08:00
|
|
|
|
}
|
2017-12-25 11:21:31 +08:00
|
|
|
|
}
|
|
|
|
|
for _, val := range hostNode.Conditions {
|
|
|
|
|
fmt.Println("正在判断节点状态,请稍等")
|
|
|
|
|
if hostNode.Alived||(val.Type==model.NodeInit&&val.Status==model.ConditionTrue) {
|
|
|
|
|
fmt.Printf("节点 %s 初始化成功", hostNode.ID)
|
2017-12-05 15:02:43 +08:00
|
|
|
|
fmt.Println()
|
2017-12-25 11:21:31 +08:00
|
|
|
|
header = append(header, string(val.Type))
|
|
|
|
|
content = append(content, string(val.Status))
|
2017-12-05 15:02:43 +08:00
|
|
|
|
tableC.AddHeaders(header)
|
|
|
|
|
tableC.AddRow(content)
|
|
|
|
|
fmt.Println(tableC.Render())
|
2017-11-28 15:35:29 +08:00
|
|
|
|
return nil
|
2017-12-25 11:21:31 +08:00
|
|
|
|
} else if val.Type == model.NodeInit && val.Status == model.ConditionFalse {
|
|
|
|
|
fmt.Printf("节点 %s 初始化失败:%s", hostNode.ID, val.Reason)
|
|
|
|
|
return nil
|
2017-12-12 11:36:57 +08:00
|
|
|
|
} else {
|
2017-12-05 15:02:43 +08:00
|
|
|
|
fmt.Printf("..")
|
2017-11-28 15:35:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-25 11:21:31 +08:00
|
|
|
|
|
2017-12-25 00:06:21 +08:00
|
|
|
|
fmt.Println("节点初始化结束")
|
2017-11-23 15:05:46 +08:00
|
|
|
|
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 NewCmdNodeRes() cli.Command {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
c := cli.Command{
|
2017-11-10 15:00:52 +08:00
|
|
|
|
Name: "noderes",
|
|
|
|
|
Usage: "获取计算节点资源信息 grctl noderes",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
Common(c)
|
|
|
|
|
return getNodeWithResource(c)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getNodeWithResource(c *cli.Context) error {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
ns, err := clients.K8SClient.Core().Nodes().List(metav1.ListOptions{})
|
2017-11-10 15:00:52 +08:00
|
|
|
|
if err != nil {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
logrus.Errorf("获取节点列表失败,details: %s", err.Error())
|
2017-11-10 15:00:52 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2017-11-20 17:29:39 +08:00
|
|
|
|
|
2017-11-10 15:00:52 +08:00
|
|
|
|
table := termtables.CreateTable()
|
2017-12-12 11:36:57 +08:00
|
|
|
|
table.AddHeaders("NodeName", "Version", "CapCPU(核)", "AllocatableCPU(核)", "UsedCPU(核)", "CapMemory(M)", "AllocatableMemory(M)", "UsedMemory(M)")
|
|
|
|
|
for _, v := range ns.Items {
|
2017-11-20 17:29:39 +08:00
|
|
|
|
|
|
|
|
|
podList, err := clients.K8SClient.Core().Pods(metav1.NamespaceAll).List(metav1.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": v.Name}).String()})
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
var cpuPerNode = 0
|
|
|
|
|
memPerNode := 0
|
|
|
|
|
for _, p := range podList.Items {
|
|
|
|
|
status := string(p.Status.Phase)
|
2017-11-20 17:29:39 +08:00
|
|
|
|
|
2017-12-12 11:36:57 +08:00
|
|
|
|
if status != "Running" {
|
2017-11-20 17:29:39 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
memPerPod := 0
|
2017-11-20 17:29:39 +08:00
|
|
|
|
|
2017-12-12 11:36:57 +08:00
|
|
|
|
memPerPod += int(p.Spec.Containers[0].Resources.Requests.Memory().Value())
|
|
|
|
|
cpuOfPod := p.Spec.Containers[0].Resources.Requests.Cpu().String()
|
|
|
|
|
if strings.Contains(cpuOfPod, "m") {
|
|
|
|
|
cpuOfPod = strings.Replace(cpuOfPod, "m", "", -1)
|
2017-11-20 17:29:39 +08:00
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
cpuI, _ := strconv.Atoi(cpuOfPod)
|
|
|
|
|
cpuPerNode += cpuI
|
|
|
|
|
memPerNode += memPerPod
|
2017-11-20 17:29:39 +08:00
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
capCPU := v.Status.Capacity.Cpu().Value()
|
|
|
|
|
capMem := v.Status.Capacity.Memory().Value()
|
|
|
|
|
allocCPU := v.Status.Allocatable.Cpu().Value()
|
|
|
|
|
allocMem := v.Status.Allocatable.Memory().Value()
|
|
|
|
|
table.AddRow(v.Name, v.Status.NodeInfo.KubeletVersion, capCPU, allocCPU, float32(cpuPerNode)/1000, capMem/1024/1024, allocMem/1024/1024, memPerNode/1024/1024)
|
2017-11-10 15:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(table.Render())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-12-15 14:57:17 +08:00
|
|
|
|
func isNodeReady(node *model.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
|
|
|
|
|
}
|
2017-11-10 15:00:52 +08:00
|
|
|
|
func getNode(c *cli.Context) error {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
ns, err := clients.K8SClient.Core().Nodes().List(metav1.ListOptions{})
|
2017-11-10 15:00:52 +08:00
|
|
|
|
if err != nil {
|
2017-12-12 11:36:57 +08:00
|
|
|
|
logrus.Errorf("获取节点列表失败,details: %s", err.Error())
|
2017-11-10 15:00:52 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
table := termtables.CreateTable()
|
2017-12-12 11:36:57 +08:00
|
|
|
|
table.AddHeaders("Name", "Status", "Namespace", "Unschedulable", "KubeletVersion", "Labels")
|
2017-11-10 15:00:52 +08:00
|
|
|
|
|
2017-12-12 11:36:57 +08:00
|
|
|
|
for _, v := range ns.Items {
|
|
|
|
|
cs := v.Status.Conditions
|
|
|
|
|
status := "unknown"
|
|
|
|
|
for _, cv := range cs {
|
|
|
|
|
status = string(cv.Status)
|
|
|
|
|
if strings.Contains(status, "rue") {
|
|
|
|
|
status = string(cv.Type)
|
2017-11-10 15:00:52 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
m := v.Labels
|
|
|
|
|
labels := ""
|
|
|
|
|
for k := range m {
|
|
|
|
|
labels += k
|
|
|
|
|
labels += " "
|
2017-11-10 15:00:52 +08:00
|
|
|
|
}
|
2017-12-12 11:36:57 +08:00
|
|
|
|
table.AddRow(v.Name, status, v.Namespace, v.Spec.Unschedulable, v.Status.NodeInfo.KubeletVersion, labels)
|
2017-11-10 15:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(table.Render())
|
|
|
|
|
return nil
|
|
|
|
|
}
|