github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/operation/factory.go (about) 1 // Copyright 2014-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 corecharm "gopkg.in/juju/charm.v6-unstable" 10 11 "github.com/juju/juju/worker/uniter/charm" 12 "github.com/juju/juju/worker/uniter/hook" 13 "github.com/juju/juju/worker/uniter/runner" 14 ) 15 16 // FactoryParams holds all the necessary parameters for a new operation factory. 17 type FactoryParams struct { 18 Deployer charm.Deployer 19 RunnerFactory runner.Factory 20 Callbacks Callbacks 21 Abort <-chan struct{} 22 MetricSpoolDir string 23 } 24 25 // NewFactory returns a Factory that creates Operations backed by the supplied 26 // parameters. 27 func NewFactory(params FactoryParams) Factory { 28 return &factory{ 29 config: params, 30 } 31 } 32 33 type factory struct { 34 config FactoryParams 35 } 36 37 // newDeploy is the common code for creating arbitrary deploy operations. 38 func (f *factory) newDeploy(kind Kind, charmURL *corecharm.URL, revert, resolved bool) (Operation, error) { 39 if charmURL == nil { 40 return nil, errors.New("charm url required") 41 } else if kind != Install && kind != Upgrade { 42 return nil, errors.Errorf("unknown deploy kind: %s", kind) 43 } 44 return &deploy{ 45 kind: kind, 46 charmURL: charmURL, 47 revert: revert, 48 resolved: resolved, 49 callbacks: f.config.Callbacks, 50 deployer: f.config.Deployer, 51 abort: f.config.Abort, 52 }, nil 53 } 54 55 // NewInstall is part of the Factory interface. 56 func (f *factory) NewInstall(charmURL *corecharm.URL) (Operation, error) { 57 return f.newDeploy(Install, charmURL, false, false) 58 } 59 60 // NewUpgrade is part of the Factory interface. 61 func (f *factory) NewUpgrade(charmURL *corecharm.URL) (Operation, error) { 62 return f.newDeploy(Upgrade, charmURL, false, false) 63 } 64 65 // NewRevertUpgrade is part of the Factory interface. 66 func (f *factory) NewRevertUpgrade(charmURL *corecharm.URL) (Operation, error) { 67 return f.newDeploy(Upgrade, charmURL, true, false) 68 } 69 70 // NewResolvedUpgrade is part of the Factory interface. 71 func (f *factory) NewResolvedUpgrade(charmURL *corecharm.URL) (Operation, error) { 72 return f.newDeploy(Upgrade, charmURL, false, true) 73 } 74 75 // NewRunHook is part of the Factory interface. 76 func (f *factory) NewRunHook(hookInfo hook.Info) (Operation, error) { 77 if err := hookInfo.Validate(); err != nil { 78 return nil, err 79 } 80 return &runHook{ 81 info: hookInfo, 82 callbacks: f.config.Callbacks, 83 runnerFactory: f.config.RunnerFactory, 84 }, nil 85 } 86 87 // NewSkipHook is part of the Factory interface. 88 func (f *factory) NewSkipHook(hookInfo hook.Info) (Operation, error) { 89 hookOp, err := f.NewRunHook(hookInfo) 90 if err != nil { 91 return nil, err 92 } 93 return &skipOperation{hookOp}, nil 94 } 95 96 // NewAction is part of the Factory interface. 97 func (f *factory) NewAction(actionId string) (Operation, error) { 98 if !names.IsValidAction(actionId) { 99 return nil, errors.Errorf("invalid action id %q", actionId) 100 } 101 return &runAction{ 102 actionId: actionId, 103 callbacks: f.config.Callbacks, 104 runnerFactory: f.config.RunnerFactory, 105 }, nil 106 } 107 108 // NewCommands is part of the Factory interface. 109 func (f *factory) NewCommands(args CommandArgs, sendResponse CommandResponseFunc) (Operation, error) { 110 if args.Commands == "" { 111 return nil, errors.New("commands required") 112 } else if sendResponse == nil { 113 return nil, errors.New("response sender required") 114 } 115 if args.RemoteUnitName != "" { 116 if args.RelationId == -1 { 117 return nil, errors.New("remote unit not valid without relation") 118 } else if !names.IsValidUnit(args.RemoteUnitName) { 119 return nil, errors.Errorf("invalid remote unit name %q", args.RemoteUnitName) 120 } 121 } 122 return &runCommands{ 123 args: args, 124 sendResponse: sendResponse, 125 callbacks: f.config.Callbacks, 126 runnerFactory: f.config.RunnerFactory, 127 }, nil 128 } 129 130 // NewResignLeadership is part of the Factory interface. 131 func (f *factory) NewResignLeadership() (Operation, error) { 132 return &resignLeadership{}, nil 133 } 134 135 // NewAcceptLeadership is part of the Factory interface. 136 func (f *factory) NewAcceptLeadership() (Operation, error) { 137 return &acceptLeadership{}, nil 138 }