forked from juju/juju
/
run.go
423 lines (378 loc) · 12.2 KB
/
run.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/utils"
"gopkg.in/juju/names.v2"
actionapi "github.com/juju/juju/api/action"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
)
func newDefaultRunCommand() cmd.Command {
return newRunCommand(time.After)
}
func newRunCommand(timeAfter func(time.Duration) <-chan time.Time) cmd.Command {
return modelcmd.Wrap(&runCommand{
timeAfter: timeAfter,
})
}
// runCommand is responsible for running arbitrary commands on remote machines.
type runCommand struct {
modelcmd.ModelCommandBase
out cmd.Output
all bool
timeout time.Duration
machines []string
services []string
units []string
commands string
timeAfter func(time.Duration) <-chan time.Time
}
const runDoc = `
Run a shell command on the specified targets. Only admin users of a model
are able to use this command.
Targets are specified using either machine ids, application names or unit
names. At least one target specifier is needed.
Multiple values can be set for --machine, --application, and --unit by using
comma separated values.
If the target is a machine, the command is run as the "root" user on
the remote machine.
If the target is an application, the command is run on all units for that
application. For example, if there was an application "mysql" and that application
had two units, "mysql/0" and "mysql/1", then
--application mysql
is equivalent to
--unit mysql/0,mysql/1
Commands run for applications or units are executed in a 'hook context' for
the unit.
--all is provided as a simple way to run the command on all the machines
in the model. If you specify --all you cannot provide additional
targets.
Since juju run creates actions, you can query for the status of commands
started with juju run by calling "juju show-action-status --name juju-run".
If you need to pass flags to the command being run, you must precede the
command and its arguments with "--", to tell "juju run" to stop processing
those arguments. For example:
juju run --all -- hostname -f
`
func (c *runCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "run",
Args: "<commands>",
Purpose: "Run the commands on the remote targets specified.",
Doc: runDoc,
}
}
func (c *runCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
c.out.AddFlags(f, "default", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
// default is used to format a single result specially.
"default": cmd.FormatYaml,
})
f.BoolVar(&c.all, "all", false, "Run the commands on all the machines")
f.DurationVar(&c.timeout, "timeout", 5*time.Minute, "How long to wait before the remote command is considered to have failed")
f.Var(cmd.NewStringsValue(nil, &c.machines), "machine", "One or more machine ids")
f.Var(cmd.NewStringsValue(nil, &c.services), "application", "One or more application names")
f.Var(cmd.NewStringsValue(nil, &c.units), "unit", "One or more unit ids")
}
func (c *runCommand) Init(args []string) error {
if len(args) == 0 {
return errors.Errorf("no commands specified")
}
if len(args) == 1 {
// If just one argument is specified, we don't pass it through
// utils.CommandString in case it contains multiple arguments
// (e.g. juju run --all "sudo whatever"). Passing it through
// utils.CommandString would quote the string, which the backend
// does not expect.
c.commands = args[0]
} else {
c.commands = utils.CommandString(args...)
}
if c.all {
if len(c.machines) != 0 {
return errors.Errorf("You cannot specify --all and individual machines")
}
if len(c.services) != 0 {
return errors.Errorf("You cannot specify --all and individual applications")
}
if len(c.units) != 0 {
return errors.Errorf("You cannot specify --all and individual units")
}
} else {
if len(c.machines) == 0 && len(c.services) == 0 && len(c.units) == 0 {
return errors.Errorf("You must specify a target, either through --all, --machine, --application or --unit")
}
}
var nameErrors []string
for _, machineId := range c.machines {
if !names.IsValidMachine(machineId) {
nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid machine id", machineId))
}
}
for _, service := range c.services {
if !names.IsValidApplication(service) {
nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid application name", service))
}
}
for _, unit := range c.units {
if !names.IsValidUnit(unit) {
nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid unit name", unit))
}
}
if len(nameErrors) > 0 {
return errors.Errorf("The following run targets are not valid:\n%s",
strings.Join(nameErrors, "\n"))
}
return nil
}
// ConvertActionResults takes the results from the api and creates a map
// suitable for format converstion to YAML or JSON.
func ConvertActionResults(result params.ActionResult, query actionQuery) map[string]interface{} {
values := make(map[string]interface{})
values[query.receiver.receiverType] = query.receiver.tag.Id()
if result.Error != nil {
values["Error"] = result.Error.Error()
values["Action"] = query.actionTag.Id()
return values
}
if result.Action.Tag != query.actionTag.String() {
values["Error"] = fmt.Sprintf("expected action tag %q, got %q", query.actionTag.String(), result.Action.Tag)
values["Action"] = query.actionTag.Id()
return values
}
if result.Action.Receiver != query.receiver.tag.String() {
values["Error"] = fmt.Sprintf("expected action receiver %q, got %q", query.receiver.tag.String(), result.Action.Receiver)
values["Action"] = query.actionTag.Id()
return values
}
if result.Message != "" {
values["Message"] = result.Message
}
// We always want to have a string for stdout, but only show stderr,
// code and error if they are there.
if res, ok := result.Output["Stdout"].(string); ok {
values["Stdout"] = strings.Replace(res, "\r\n", "\n", -1)
if res, ok := result.Output["StdoutEncoding"].(string); ok && res != "" {
values["Stdout.encoding"] = res
}
} else {
values["Stdout"] = ""
}
if res, ok := result.Output["Stderr"].(string); ok && res != "" {
values["Stderr"] = strings.Replace(res, "\r\n", "\n", -1)
if res, ok := result.Output["StderrEncoding"].(string); ok && res != "" {
values["Stderr.encoding"] = res
}
}
if res, ok := result.Output["Code"].(string); ok {
code, err := strconv.Atoi(res)
if err == nil && code != 0 {
values["ReturnCode"] = code
}
}
return values
}
func (c *runCommand) Run(ctx *cmd.Context) error {
client, err := getRunAPIClient(c)
if err != nil {
return err
}
defer client.Close()
var runResults []params.ActionResult
if c.all {
runResults, err = client.RunOnAllMachines(c.commands, c.timeout)
} else {
params := params.RunParams{
Commands: c.commands,
Timeout: c.timeout,
Machines: c.machines,
Applications: c.services,
Units: c.units,
}
runResults, err = client.Run(params)
}
if err != nil {
return block.ProcessBlockedError(err, block.BlockChange)
}
actionsToQuery := []actionQuery{}
for _, result := range runResults {
if result.Error != nil {
fmt.Fprintf(ctx.GetStderr(), "couldn't queue one action: %v", result.Error)
continue
}
actionTag, err := names.ParseActionTag(result.Action.Tag)
if err != nil {
fmt.Fprintf(ctx.GetStderr(), "got invalid action tag %v for receiver %v", result.Action.Tag, result.Action.Receiver)
continue
}
receiverTag, err := names.ActionReceiverFromTag(result.Action.Receiver)
if err != nil {
fmt.Fprintf(ctx.GetStderr(), "got invalid action receiver tag %v for action %v", result.Action.Receiver, result.Action.Tag)
continue
}
var receiverType string
switch receiverTag.(type) {
case names.UnitTag:
receiverType = "UnitId"
case names.MachineTag:
receiverType = "MachineId"
default:
receiverType = "ReceiverId"
}
actionsToQuery = append(actionsToQuery, actionQuery{
actionTag: actionTag,
receiver: actionReceiver{
receiverType: receiverType,
tag: receiverTag,
}})
}
if len(actionsToQuery) == 0 {
return errors.New("no actions were successfully enqueued, aborting")
}
timeout := c.timeAfter(c.timeout)
values := []interface{}{}
for len(actionsToQuery) > 0 {
actionResults, err := client.Actions(entities(actionsToQuery))
if err != nil {
return errors.Trace(err)
}
newActionsToQuery := []actionQuery{}
for i, result := range actionResults.Results {
if result.Error == nil {
switch result.Status {
case params.ActionRunning, params.ActionPending:
newActionsToQuery = append(newActionsToQuery, actionsToQuery[i])
continue
}
}
values = append(values, ConvertActionResults(result, actionsToQuery[i]))
}
actionsToQuery = newActionsToQuery
if len(actionsToQuery) > 0 {
var timedOut bool
select {
case <-timeout:
timedOut = true
case <-c.timeAfter(1 * time.Second):
// TODO(axw) 2017-02-07 #1662451
// use a watcher instead of polling.
// this should be easier once we implement
// action grouping
}
if timedOut {
break
}
}
}
// If we are just dealing with one result, AND we are using the default
// format, then pretend we were running it locally.
if len(actionsToQuery) == 0 && len(values) == 1 && c.out.Name() == "default" {
result, ok := values[0].(map[string]interface{})
if !ok {
return errors.New("couldn't read action output")
}
if res, ok := result["Error"].(string); ok {
return errors.New(res)
}
ctx.Stdout.Write(formatOutput(result, "Stdout"))
ctx.Stderr.Write(formatOutput(result, "Stderr"))
if code, ok := result["ReturnCode"].(int); ok && code != 0 {
return cmd.NewRcPassthroughError(code)
}
// Message should always contain only errors.
if res, ok := result["Message"].(string); ok && res != "" {
ctx.Stderr.Write([]byte(res))
}
return nil
}
if len(values) > 0 {
if err := c.out.Write(ctx, values); err != nil {
return err
}
}
if n := len(actionsToQuery); n > 0 {
// There are action results remaining, so return an error.
suffix := ""
if n > 1 {
suffix = "s"
}
receivers := make([]string, n)
for i, actionToQuery := range actionsToQuery {
receivers[i] = names.ReadableString(actionToQuery.receiver.tag)
}
return errors.Errorf(
"timed out waiting for result%s from: %s",
suffix, strings.Join(receivers, ", "),
)
}
return nil
}
type actionReceiver struct {
receiverType string
tag names.Tag
}
type actionQuery struct {
receiver actionReceiver
actionTag names.ActionTag
}
// RunClient exposes the capabilities required by the CLI
type RunClient interface {
action.APIClient
RunOnAllMachines(commands string, timeout time.Duration) ([]params.ActionResult, error)
Run(params.RunParams) ([]params.ActionResult, error)
}
// In order to be able to easily mock out the API side for testing,
// the API client is retrieved using a function.
var getRunAPIClient = func(c *runCommand) (RunClient, error) {
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return actionapi.NewClient(root), errors.Trace(err)
}
// getActionResult abstracts over the action CLI function that we use here to fetch results
var getActionResult = func(c RunClient, actionId string, wait *time.Timer) (params.ActionResult, error) {
return action.GetActionResult(c, actionId, wait)
}
// entities is a convenience constructor for params.Entities.
func entities(actions []actionQuery) params.Entities {
entities := params.Entities{
Entities: make([]params.Entity, len(actions)),
}
for i, action := range actions {
entities.Entities[i].Tag = action.actionTag.String()
}
return entities
}
func formatOutput(results map[string]interface{}, key string) []byte {
res, ok := results[key].(string)
if !ok {
return []byte("")
}
if enc, ok := results[key+".encoding"].(string); ok && enc != "" {
switch enc {
case "base64":
decoded, err := base64.StdEncoding.DecodeString(res)
if err != nil {
return []byte("expected b64 encoded string, got " + res)
}
return decoded
default:
return []byte(res)
}
}
return []byte(res)
}