/
tasks.go
204 lines (190 loc) · 5.77 KB
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// 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 (
"encoding/json"
"fmt"
"time"
"github.com/apcera/termtables"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/node/api/model"
"github.com/gosuri/uitable"
"github.com/urfave/cli"
)
//NewCmdTasks 任务相关命令
func NewCmdTasks() cli.Command {
c := cli.Command{
Name: "tasks",
Usage: "系统任务相关命令,grctl tasks -h",
Subcommands: []cli.Command{
cli.Command{
Name: "static-refresh",
Usage: "Refresh static task config",
Action: func(c *cli.Context) error {
if err := clients.NodeClient.Tasks().Refresh(); err != nil {
return err
}
return nil
},
},
cli.Command{
Name: "list",
Usage: "List all task",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "g",
Usage: "show all task longitudinal",
},
},
Action: func(c *cli.Context) error {
tasks, err := clients.NodeClient.Tasks().List()
if err != nil {
return err
}
if len(tasks) > 0 {
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("--------------------------------------------")
}
} 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))
}
}
taskTable.AddRow(v.ID, v.GroupID, depstr, status, scheduler)
}
fmt.Println(taskTable.Render())
}
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")
}
task, err := clients.NodeClient.Tasks().Get(taskID)
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{
Name: "n",
Usage: "exec task nodeid",
},
cli.StringFlag{
Name: "f",
Usage: "exec task and return status",
},
},
Action: func(c *cli.Context) error {
taskID := c.Args().First()
if taskID == "" {
fmt.Println("Please specified task id")
}
nodeID := c.String("n")
if nodeID == "" {
fmt.Println("Please specified nodeid use `-n`")
}
//fmt.Printf("node is %s task id is %s",nodeID,taskID)
err := clients.NodeClient.Tasks().Exec(taskID, []string{nodeID})
handleErr(err)
return nil
},
},
},
}
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
taskD, err := clients.NodeClient.Tasks().Get(tid)
handleErr(err)
//fmt.Print("task %s depend %s",task.ID,taskD.Task.ID)
if k == 0 {
fmt.Print("-->" + taskD.ID)
} else {
fmt.Println()
for i := 0; i < len(path); i++ {
fmt.Print(" ")
}
fmt.Print("-->" + taskD.ID)
//path+="-->"+taskD.Task.ID
}
getDependTask(taskD, path+"-->"+taskD.ID)
}
}
//ExecTaskAndCheckStatus 执行一个任务并检测状态
func ExecTaskAndCheckStatus(taskID string, nodes []string) {
}