forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_hooks.go
More file actions
198 lines (174 loc) · 5.5 KB
/
admin_hooks.go
File metadata and controls
198 lines (174 loc) · 5.5 KB
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
package main
import (
"fmt"
"net/url"
"time"
"github.com/spf13/cobra"
"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
)
var adminHooksCmd = cli.Command{
Name: "hooks",
Aliases: []string{"hook"},
Short: "Manage CDS Hooks tasks",
}
func adminHooks() *cobra.Command {
return cli.NewCommand(adminHooksCmd, nil, []*cobra.Command{
cli.NewListCommand(adminHooksTaskListCmd, adminHooksTaskListRun, nil),
cli.NewListCommand(adminHooksTaskExecutionListCmd, adminHooksTaskExecutionListRun, nil),
cli.NewCommand(adminHooksTaskExecutionStartCmd, adminHooksTaskExecutionStartRun, nil),
cli.NewCommand(adminHooksTaskExecutionStopCmd, adminHooksTaskExecutionStopRun, nil),
cli.NewCommand(adminHooksTaskExecutionDeleteAllCmd, adminHooksTaskExecutionDeleteAllRun, nil),
cli.NewCommand(adminHooksTaskExecutionStartAllCmd, adminHooksTaskExecutionStartAllRun, nil),
cli.NewCommand(adminHooksTaskExecutionStopAllCmd, adminHooksTaskExecutionStopAllRun, nil),
})
}
var adminHooksTaskListCmd = cli.Command{
Name: "list",
Short: "List CDS Hooks Tasks",
Flags: []cli.Flag{
{
Name: "sort",
Usage: "Sort task by nb_executions_total,nb_executions_todo",
Default: "",
},
},
}
func adminHooksTaskListRun(v cli.Values) (cli.ListResult, error) {
url, _ := url.Parse("/task")
if s := v.GetString("sort"); s != "" {
q := url.Query()
q.Add("sort", s)
url.RawQuery = q.Encode()
}
btes, err := client.ServiceCallGET("hooks", url.String())
if err != nil {
return nil, err
}
ts := []sdk.Task{}
if err := sdk.JSONUnmarshal(btes, &ts); err != nil {
return nil, err
}
type TaskDisplay struct {
UUID string `cli:"UUID,key"`
Type string `cli:"Type"`
Stopped bool `cli:"Stopped"`
Project string `cli:"Project"`
Workflow string `cli:"Workflow"`
WorkflowID string `cli:"Workflow_ID"`
VCSServer string `cli:"VCSServer"`
RepoFullname string `cli:"RepoFullname"`
Cron string `cli:"Cron"`
NbExecutionsTotal int `cli:"Execs_Total"`
NbExecutionsTodo int `cli:"Execs_Todo"`
}
tss := []TaskDisplay{}
for _, p := range ts {
tss = append(tss, TaskDisplay{
UUID: p.UUID,
Type: p.Type,
Stopped: p.Stopped,
Project: p.Config["project"].Value,
Workflow: p.Config["workflow"].Value,
WorkflowID: p.Config["workflow_id"].Value,
VCSServer: p.Config["vcsServer"].Value,
RepoFullname: p.Config["repoFullName"].Value,
Cron: p.Config["cron"].Value,
NbExecutionsTotal: p.NbExecutionsTotal,
NbExecutionsTodo: p.NbExecutionsTodo,
})
}
return cli.AsListResult(tss), nil
}
var adminHooksTaskExecutionListCmd = cli.Command{
Name: "executions",
Short: "List CDS Executions for one task",
Example: "cdsctl admin hooks executions 5178ce1f-2f76-45c5-a203-58c10c3e2c73",
Args: []cli.Arg{
{Name: "uuid"},
},
}
func adminHooksTaskExecutionListRun(v cli.Values) (cli.ListResult, error) {
btes, err := client.ServiceCallGET("hooks", fmt.Sprintf("/task/%s/execution", v.GetString("uuid")))
if err != nil {
return nil, err
}
type TaskExecutionDisplay struct {
sdk.TaskExecution
ProcessingH string `cli:"Processing H"`
TimestampH string `cli:"Timestamp H"`
}
ts := sdk.Task{}
if err := sdk.JSONUnmarshal(btes, &ts); err != nil {
return nil, err
}
te := []TaskExecutionDisplay{}
for _, v := range ts.Executions {
var processingH, timestampH string
if v.ProcessingTimestamp != 0 {
processingH = time.Unix(0, v.ProcessingTimestamp).Format(time.RFC3339)
}
if v.Timestamp != 0 {
timestampH = time.Unix(0, v.Timestamp).Format(time.RFC3339)
}
te = append(te, TaskExecutionDisplay{
TaskExecution: v,
ProcessingH: processingH,
TimestampH: timestampH,
})
}
return cli.AsListResult(te), nil
}
var adminHooksTaskExecutionDeleteAllCmd = cli.Command{
Name: "purge",
Short: "Delete all executions for a task",
Example: "cdsctl admin hooks purge 5178ce1f-2f76-45c5-a203-58c10c3e2c73",
Args: []cli.Arg{
{Name: "uuid"},
},
}
func adminHooksTaskExecutionDeleteAllRun(v cli.Values) error {
return client.ServiceCallDELETE("hooks", fmt.Sprintf("/task/%s/execution", v.GetString("uuid")))
}
var adminHooksTaskExecutionStartCmd = cli.Command{
Name: "start",
Short: "Start a task",
Example: "cdsctl admin hooks start 5178ce1f-2f76-45c5-a203-58c10c3e2c73",
Args: []cli.Arg{
{Name: "uuid"},
},
}
func adminHooksTaskExecutionStartRun(v cli.Values) error {
_, err := client.ServiceCallGET("hooks", fmt.Sprintf("/task/%s/start", v.GetString("uuid")))
return err
}
var adminHooksTaskExecutionStopCmd = cli.Command{
Name: "stop",
Short: "Stop a task",
Example: "cdsctl admin hooks stop 5178ce1f-2f76-45c5-a203-58c10c3e2c73",
Args: []cli.Arg{
{Name: "uuid"},
},
}
func adminHooksTaskExecutionStopRun(v cli.Values) error {
_, err := client.ServiceCallGET("hooks", fmt.Sprintf("/task/%s/stop", v.GetString("uuid")))
return err
}
var adminHooksTaskExecutionStopAllCmd = cli.Command{
Name: "stopall",
Short: "Stop all tasks",
Example: "cdsctl admin hooks stopall",
}
func adminHooksTaskExecutionStopAllRun(v cli.Values) error {
_, err := client.ServiceCallGET("hooks", "/task/bulk/stop")
return err
}
var adminHooksTaskExecutionStartAllCmd = cli.Command{
Name: "startall",
Short: "Start all tasks",
Example: "cdsctl admin hooks startall",
}
func adminHooksTaskExecutionStartAllRun(v cli.Values) error {
_, err := client.ServiceCallGET("hooks", "/task/bulk/start")
return err
}