-
Notifications
You must be signed in to change notification settings - Fork 251
/
fetchers.go
233 lines (185 loc) · 5.84 KB
/
fetchers.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package task
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/spf13/pflag"
tcurls "github.com/taskcluster/taskcluster-lib-urls"
tcclient "github.com/taskcluster/taskcluster/v57/clients/client-go"
"github.com/taskcluster/taskcluster/v57/clients/client-go/tcqueue"
"github.com/taskcluster/taskcluster/v57/clients/client-shell/config"
)
func makeQueue(credentials *tcclient.Credentials) *tcqueue.Queue {
return tcqueue.New(credentials, config.RootURL())
}
// runStatus gets the status of run(s) of a given task.
func runStatus(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
s, err := q.Status(taskID)
if err != nil {
return fmt.Errorf("could not get the status of the task %s: %v", taskID, err)
}
allRuns, _ := flagSet.GetBool("all-runs")
runID, _ := flagSet.GetInt("run")
if allRuns && runID != -1 {
return fmt.Errorf("can't specify both all-runs and a specific run")
}
if allRuns {
for _, r := range s.Status.Runs {
fmt.Fprintf(out, "Run #%d: %s\n", r.RunID, getRunStatusString(r.State, r.ReasonResolved))
}
return nil
}
if runID >= len(s.Status.Runs) {
return fmt.Errorf("there is no run #%v", runID)
}
if runID == -1 {
runID = len(s.Status.Runs) - 1
}
fmt.Fprintln(out, getRunStatusString(s.Status.Runs[runID].State, s.Status.Runs[runID].ReasonResolved))
return nil
}
// confirmMsg displays confirmation message when --confirm is used
func confirmMsg(command string, credentials *tcclient.Credentials, args []string) bool {
q := makeQueue(credentials)
taskID := args[0]
c, err := q.Status(taskID)
if err != nil {
log.Fatal(err)
}
run := c.Status.Runs[len(c.Status.Runs)-1]
t, err := q.Task(taskID)
if err != nil {
log.Fatal(err)
}
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s %s taskId: %s (state: %s). Are you sure you want to proceed?(y/N) ", command, t.Metadata.Name, taskID, run.State)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}
// displayNoopMsg displays details when --noop is used
func displayNoopMsg(command string, credentials *tcclient.Credentials, args []string) error {
q := makeQueue(credentials)
taskID := args[0]
c, _ := q.Status(taskID)
run := c.Status.Runs[len(c.Status.Runs)-1]
t, err := q.Task(taskID)
if err != nil {
return fmt.Errorf("could not get the task %s: %v", taskID, err)
}
fmt.Printf("%s %s taskid: %s (state: %s)\n", command, t.Metadata.Name, taskID, run.State)
return nil
}
// runName gets the name of a given task.
func runName(credentials *tcclient.Credentials, args []string, out io.Writer, _ *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
t, err := q.Task(taskID)
if err != nil {
return fmt.Errorf("could not get the task %s: %v", taskID, err)
}
fmt.Fprintln(out, t.Metadata.Name)
return nil
}
// runDef gets the definition of a given task.
func runDef(credentials *tcclient.Credentials, args []string, out io.Writer, _ *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
t, err := q.Task(taskID)
if err != nil {
return fmt.Errorf("could not get the task %s: %v", taskID, err)
}
def, err := json.MarshalIndent(t, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal task %s into json: %v", taskID, err)
}
fmt.Fprintln(out, string(def))
return nil
}
// runGroup gets the groupID of a given task.
func runGroup(credentials *tcclient.Credentials, args []string, out io.Writer, _ *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
t, err := q.Task(taskID)
if err != nil {
return fmt.Errorf("could not get the task %s: %v", taskID, err)
}
fmt.Fprintln(out, t.TaskGroupID)
return nil
}
// runArtifacts gets the name of the artificats for a given task and run.
func runArtifacts(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
s, err := q.Status(taskID)
if err != nil {
return fmt.Errorf("could not get the status of the task %s: %v", taskID, err)
}
runID, _ := flagSet.GetInt("run")
if runID >= len(s.Status.Runs) {
return fmt.Errorf("there is no run #%v", runID)
}
if runID == -1 {
runID = len(s.Status.Runs) - 1
}
buf := bytes.NewBufferString("")
continuation := ""
for {
a, err := q.ListArtifacts(taskID, fmt.Sprint(runID), continuation, "")
if err != nil {
return fmt.Errorf("could not fetch artifacts for task %s run %v: %v", taskID, runID, err)
}
for _, ar := range a.Artifacts {
fmt.Fprintf(buf, "%s\n", ar.Name)
}
continuation = a.ContinuationToken
if continuation == "" {
break
}
}
_, err = buf.WriteTo(out)
return err
}
func runLog(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
s, err := q.Status(taskID)
if err != nil {
return fmt.Errorf("could not get the status of the task %s: %v", taskID, err)
}
state := s.Status.State
if state == "unscheduled" || state == "pending" {
return fmt.Errorf("could not fetch the logs of task %s because it's in a %s state", taskID, state)
}
path := tcurls.API(config.RootURL(), "queue", "v1", "task/"+taskID+"/artifacts/public/logs/live.log")
resp, err := http.Get(path)
if err != nil {
return fmt.Errorf("Error making request to %v: %v", path, err)
}
defer resp.Body.Close()
// Read line by line for live logs.
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
fmt.Fprintln(out, scanner.Text())
}
if resp.StatusCode/100 != 2 {
return fmt.Errorf("Received unexpected response code %v", resp.StatusCode)
}
return nil
}