2018-03-14 14:12:26 +08:00
|
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-12-19 18:46:49 +08:00
|
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
|
2017-12-19 18:46:49 +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-12-19 18:46:49 +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-12-19 18:46:49 +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 (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2017-12-20 10:23:55 +08:00
|
|
|
|
"time"
|
2018-07-17 16:18:19 +08:00
|
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
|
"github.com/apcera/termtables"
|
2018-04-24 16:44:59 +08:00
|
|
|
|
"github.com/goodrain/rainbond/grctl/clients"
|
|
|
|
|
"github.com/goodrain/rainbond/node/api/model"
|
2017-12-20 13:48:49 +08:00
|
|
|
|
"github.com/gosuri/uitable"
|
2017-12-19 18:46:49 +08:00
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//NewCmdTasks 任务相关命令
|
|
|
|
|
func NewCmdTasks() cli.Command {
|
|
|
|
|
c := cli.Command{
|
|
|
|
|
Name: "tasks",
|
|
|
|
|
Usage: "系统任务相关命令,grctl tasks -h",
|
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
|
cli.Command{
|
|
|
|
|
Name: "list",
|
|
|
|
|
Usage: "List all task",
|
2017-12-20 13:48:49 +08:00
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.BoolFlag{
|
|
|
|
|
Name: "g",
|
|
|
|
|
Usage: "show all task longitudinal",
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-12-19 18:46:49 +08:00
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-17 16:18:19 +08:00
|
|
|
|
tasks, err := clients.RegionClient.Tasks().List()
|
2017-12-19 18:46:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(tasks) > 0 {
|
2017-12-20 13:48:49 +08:00
|
|
|
|
if c.Bool("g") {
|
|
|
|
|
for _, v := range tasks {
|
|
|
|
|
table := uitable.New()
|
|
|
|
|
table.Wrap = true // wrap columns
|
|
|
|
|
table.AddRow("ID", v.ID)
|
|
|
|
|
table.AddRow("GroupID", v.GroupID)
|
|
|
|
|
var depstr string
|
|
|
|
|
for _, dep := range v.Temp.Depends {
|
|
|
|
|
depstr += fmt.Sprintf("%s(%s)\n", dep.DependTaskID, dep.DetermineStrategy)
|
|
|
|
|
}
|
|
|
|
|
var status string
|
|
|
|
|
for k, v := range v.Status {
|
|
|
|
|
status += fmt.Sprintf("%s:%s(%s)\n", k, v.Status, v.CompleStatus)
|
|
|
|
|
}
|
|
|
|
|
var scheduler = v.Scheduler.Mode + "\n"
|
|
|
|
|
if len(v.Scheduler.Status) == 0 {
|
|
|
|
|
scheduler += "暂未调度"
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range v.Scheduler.Status {
|
|
|
|
|
scheduler += fmt.Sprintf("%s:%s(%s)\n", k, v.Status, v.SchedulerTime.Format(time.RFC3339))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
table.AddRow("DepTask", depstr)
|
|
|
|
|
table.AddRow("Status", status)
|
|
|
|
|
table.AddRow("Scheduler", scheduler)
|
|
|
|
|
fmt.Println(table.String())
|
|
|
|
|
fmt.Println("--------------------------------------------")
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
|
} else {
|
|
|
|
|
taskTable := termtables.CreateTable()
|
|
|
|
|
taskTable.AddHeaders("ID", "GroupID", "DepTask", "Status", "Scheduler")
|
|
|
|
|
for _, v := range tasks {
|
|
|
|
|
var depstr string
|
|
|
|
|
for _, dep := range v.Temp.Depends {
|
|
|
|
|
depstr += fmt.Sprintf("%s(%s);", dep.DependTaskID, dep.DetermineStrategy)
|
|
|
|
|
}
|
|
|
|
|
var status string
|
|
|
|
|
for k, v := range v.Status {
|
|
|
|
|
status += fmt.Sprintf("%s:%s(%s);", k, v.Status, v.CompleStatus)
|
|
|
|
|
}
|
|
|
|
|
var scheduler = v.Scheduler.Mode + ";"
|
|
|
|
|
if len(v.Scheduler.Status) == 0 {
|
|
|
|
|
scheduler += "暂未调度"
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range v.Scheduler.Status {
|
|
|
|
|
scheduler += fmt.Sprintf("%s:%s(%s);", k, v.Status, v.SchedulerTime.Format(time.RFC3339))
|
|
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
|
taskTable.AddRow(v.ID, v.GroupID, depstr, status, scheduler)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
|
fmt.Println(taskTable.Render())
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("not found tasks")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
cli.Command{
|
|
|
|
|
Name: "get",
|
|
|
|
|
Usage: "Displays the specified task details",
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
taskID := c.Args().First()
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
fmt.Println("Please specified task id")
|
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
|
task, err := clients.RegionClient.Tasks().Get(taskID)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("get task error,%s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
taskStr, _ := json.MarshalIndent(&task, "", "\t")
|
|
|
|
|
fmt.Println(string(taskStr))
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
cli.Command{
|
|
|
|
|
Name: "exec",
|
|
|
|
|
Usage: "Exec the specified task",
|
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
2017-12-20 10:23:55 +08:00
|
|
|
|
Name: "n",
|
2017-12-19 18:46:49 +08:00
|
|
|
|
Usage: "exec task nodeid",
|
|
|
|
|
},
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "f",
|
2017-12-20 10:23:55 +08:00
|
|
|
|
Usage: "exec task and return status",
|
2017-12-19 18:46:49 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
taskID := c.Args().First()
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
fmt.Println("Please specified task id")
|
|
|
|
|
}
|
2017-12-20 10:23:55 +08:00
|
|
|
|
nodeID := c.String("n")
|
2017-12-19 18:46:49 +08:00
|
|
|
|
if nodeID == "" {
|
2017-12-20 10:23:55 +08:00
|
|
|
|
fmt.Println("Please specified nodeid use `-n`")
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
2017-12-26 14:48:58 +08:00
|
|
|
|
//fmt.Printf("node is %s task id is %s",nodeID,taskID)
|
2018-07-17 16:18:19 +08:00
|
|
|
|
err := clients.RegionClient.Tasks().Exec(taskID, []string{nodeID})
|
2017-12-26 14:48:58 +08:00
|
|
|
|
handleErr(err)
|
|
|
|
|
return nil
|
2017-12-19 18:46:49 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDependTask(task *model.Task, path string) {
|
|
|
|
|
if task == nil || task.Temp == nil {
|
|
|
|
|
fmt.Println("wrong task")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
depends := task.Temp.Depends
|
|
|
|
|
|
|
|
|
|
for k, v := range depends {
|
|
|
|
|
|
|
|
|
|
tid := v.DependTaskID
|
2018-07-17 16:18:19 +08:00
|
|
|
|
taskD, err := clients.RegionClient.Tasks().Get(tid)
|
2017-12-21 17:09:17 +08:00
|
|
|
|
handleErr(err)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
//fmt.Print("task %s depend %s",task.ID,taskD.Task.ID)
|
|
|
|
|
if k == 0 {
|
2017-12-21 17:09:17 +08:00
|
|
|
|
fmt.Print("-->" + taskD.ID)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
}
|
2017-12-21 17:09:17 +08:00
|
|
|
|
fmt.Print("-->" + taskD.ID)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
//path+="-->"+taskD.Task.ID
|
|
|
|
|
|
|
|
|
|
}
|
2017-12-21 17:09:17 +08:00
|
|
|
|
getDependTask(taskD, path+"-->"+taskD.ID)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-20 18:22:36 +08:00
|
|
|
|
|
|
|
|
|
//ExecTaskAndCheckStatus 执行一个任务并检测状态
|
|
|
|
|
func ExecTaskAndCheckStatus(taskID string, nodes []string) {
|
|
|
|
|
|
|
|
|
|
}
|