github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/operation/runcommands.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 11 "github.com/juju/juju/worker/uniter/runner" 12 ) 13 14 type runCommands struct { 15 args CommandArgs 16 sendResponse CommandResponseFunc 17 18 callbacks Callbacks 19 runnerFactory runner.Factory 20 21 runner runner.Runner 22 23 RequiresMachineLock 24 } 25 26 // String is part of the Operation interface. 27 func (rc *runCommands) String() string { 28 suffix := "" 29 if rc.args.RelationId != -1 { 30 infix := "" 31 if rc.args.RemoteUnitName != "" { 32 infix = "; " + rc.args.RemoteUnitName 33 } 34 suffix = fmt.Sprintf(" (%d%s)", rc.args.RelationId, infix) 35 } 36 return "run commands" + suffix 37 } 38 39 // Prepare ensures the commands can be run. It never returns a state change. 40 // Prepare is part of the Operation interface. 41 func (rc *runCommands) Prepare(state State) (*State, error) { 42 rnr, err := rc.runnerFactory.NewCommandRunner(runner.CommandInfo{ 43 RelationId: rc.args.RelationId, 44 RemoteUnitName: rc.args.RemoteUnitName, 45 ForceRemoteUnit: rc.args.ForceRemoteUnit, 46 }) 47 if err != nil { 48 return nil, err 49 } 50 err = rnr.Context().Prepare() 51 if err != nil { 52 return nil, errors.Trace(err) 53 } 54 rc.runner = rnr 55 56 return nil, nil 57 } 58 59 // Execute runs the commands and dispatches their results. It never returns a 60 // state change. 61 // Execute is part of the Operation interface. 62 func (rc *runCommands) Execute(state State) (*State, error) { 63 if err := rc.callbacks.SetExecutingStatus("running commands"); err != nil { 64 return nil, errors.Trace(err) 65 } 66 67 response, err := rc.runner.RunCommands(rc.args.Commands) 68 switch err { 69 case runner.ErrRequeueAndReboot: 70 logger.Warningf("cannot requeue external commands") 71 fallthrough 72 case runner.ErrReboot: 73 err = ErrNeedsReboot 74 } 75 rc.sendResponse(response, err) 76 return nil, nil 77 } 78 79 // Commit does nothing. 80 // Commit is part of the Operation interface. 81 func (rc *runCommands) Commit(state State) (*State, error) { 82 return nil, nil 83 }