github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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/juju/worker/uniter/runner" 10 ) 11 12 type runCommands struct { 13 args CommandArgs 14 sendResponse CommandResponseFunc 15 16 callbacks Callbacks 17 runnerFactory runner.Factory 18 19 runner runner.Runner 20 } 21 22 // String is part of the Operation interface. 23 func (rc *runCommands) String() string { 24 suffix := "" 25 if rc.args.RelationId != -1 { 26 infix := "" 27 if rc.args.RemoteUnitName != "" { 28 infix = "; " + rc.args.RemoteUnitName 29 } 30 suffix = fmt.Sprintf(" (%d%s)", rc.args.RelationId, infix) 31 } 32 return "run commands" + suffix 33 } 34 35 // Prepare ensures the commands can be run. It never returns a state change. 36 // Prepare is part of the Operation interface. 37 func (rc *runCommands) Prepare(state State) (*State, error) { 38 rnr, err := rc.runnerFactory.NewCommandRunner(runner.CommandInfo{ 39 RelationId: rc.args.RelationId, 40 RemoteUnitName: rc.args.RemoteUnitName, 41 ForceRemoteUnit: rc.args.ForceRemoteUnit, 42 }) 43 if err != nil { 44 return nil, err 45 } 46 rc.runner = rnr 47 return nil, nil 48 } 49 50 // Execute runs the commands and dispatches their results. It never returns a 51 // state change. 52 // Execute is part of the Operation interface. 53 func (rc *runCommands) Execute(state State) (*State, error) { 54 unlock, err := rc.callbacks.AcquireExecutionLock("run commands") 55 if err != nil { 56 return nil, err 57 } 58 defer unlock() 59 60 response, err := rc.runner.RunCommands(rc.args.Commands) 61 switch err { 62 case runner.ErrRequeueAndReboot: 63 logger.Warningf("cannot requeue external commands") 64 fallthrough 65 case runner.ErrReboot: 66 err = ErrNeedsReboot 67 } 68 rc.sendResponse(response, err) 69 return nil, nil 70 } 71 72 // Commit does nothing. 73 // Commit is part of the Operation interface. 74 func (rc *runCommands) Commit(state State) (*State, error) { 75 return nil, nil 76 }