// WatchActionNotifications returns a StringsWatcher for observing incoming actions towards an actionreceiver. // It's a helper function currently used by the uniter and by machineactions // canAccess is passed in by the respective caller to provide authorization. // watchOne is usually a function created by WatchOneActionReceiverNotifications func WatchActionNotifications(args params.Entities, canAccess AuthFunc, watchOne func(names.Tag) (params.StringsWatchResult, error)) params.StringsWatchResults { result := params.StringsWatchResults{ Results: make([]params.StringsWatchResult, len(args.Entities)), } for i, entity := range args.Entities { tag, err := names.ActionReceiverFromTag(entity.Tag) if err != nil { result.Results[i].Error = ServerError(err) continue } err = ErrPerm if canAccess(tag) { result.Results[i], err = watchOne(tag) } result.Results[i].Error = ServerError(err) } return result }
func (s *actionSuite) TestActionReceiverFromTag(c *gc.C) { for i, test := range []struct { name string expected names.Tag err string }{ {name: "rambleon", err: `invalid actionreceiver tag "rambleon"`}, {name: "unit-mysql-2", expected: names.NewUnitTag("mysql/2")}, {name: "machine-13", expected: names.NewMachineTag("13")}, } { c.Logf("test %d", i) tag, err := names.ActionReceiverFromTag(test.name) if test.err != "" { c.Check(err, gc.ErrorMatches, test.err) c.Check(tag, gc.IsNil) continue } c.Check(tag, gc.Equals, test.expected) c.Check(err, jc.ErrorIsNil) } }
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, Services: 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") } 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 // TODO: use a watcher instead of sleeping // this should be easier once we implement action grouping <-afterFunc(1 * time.Second) } // If we are just dealing with one result, AND we are using the smart // format, then pretend we were running it locally. if len(values) == 1 && c.out.Name() == "smart" { 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 } return c.out.Write(ctx, values) }