github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/operation/interface.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/loggo" 8 utilexec "github.com/juju/utils/exec" 9 corecharm "gopkg.in/juju/charm.v6" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/core/model" 13 "github.com/juju/juju/worker/uniter/charm" 14 "github.com/juju/juju/worker/uniter/hook" 15 "github.com/juju/juju/worker/uniter/runner" 16 ) 17 18 //go:generate mockgen -package mocks -destination mocks/interface_mock.go github.com/juju/juju/worker/uniter/operation Operation,Factory 19 20 var logger = loggo.GetLogger("juju.worker.uniter.operation") 21 22 // Operation encapsulates the stages of the various things the uniter can do, 23 // and the state changes that need to be recorded as they happen. Operations 24 // are designed to be Run (or Skipped) by an Executor, which supplies starting 25 // state and records the changes returned. 26 type Operation interface { 27 28 // String returns a short representation of the operation. 29 String() string 30 31 // NeedsGlobalMachineLock returns a bool expressing whether we need to lock the machine. 32 NeedsGlobalMachineLock() bool 33 34 // Prepare ensures that the operation is valid and ready to be executed. 35 // If it returns a non-nil state, that state will be validated and recorded. 36 // If it returns ErrSkipExecute, it indicates that the operation can be 37 // committed directly. 38 Prepare(state State) (*State, error) 39 40 // Execute carries out the operation. It must not be called without having 41 // called Prepare first. If it returns a non-nil state, that state will be 42 // validated and recorded. 43 Execute(state State) (*State, error) 44 45 // Commit ensures that the operation's completion is recorded. If it returns 46 // a non-nil state, that state will be validated and recorded. 47 Commit(state State) (*State, error) 48 } 49 50 // Executor records and exposes uniter state, and applies suitable changes as 51 // operations are run or skipped. 52 type Executor interface { 53 54 // State returns a copy of the executor's current operation state. 55 State() State 56 57 // Run will Prepare, Execute, and Commit the supplied operation, writing 58 // indicated state changes between steps. If any step returns an unknown 59 // error, the run will be aborted and an error will be returned. 60 Run(Operation) error 61 62 // Skip will Commit the supplied operation, and write any state change 63 // indicated. If Commit returns an error, so will Skip. 64 Skip(Operation) error 65 } 66 67 // Factory creates operations. 68 type Factory interface { 69 70 // NewInstall creates an install operation for the supplied charm. 71 NewInstall(charmURL *corecharm.URL) (Operation, error) 72 73 // NewUpgrade creates an upgrade operation for the supplied charm. 74 NewUpgrade(charmURL *corecharm.URL) (Operation, error) 75 76 // NewNoOpUpgrade creates a noop upgrade operation for the supplied charm. 77 // The purpose is to go through the machinations so that in the commit phase, 78 // the uniter records the current charm url and modified version in local state 79 // so it knows the upgraded charm has been unpacked and it can run the upgrade-charm hook. 80 // For a caas uniter, the operator is the thing that does the charm upgrade, 81 // so we just plug in a no op into the uniter state machine. 82 NewNoOpUpgrade(charmURL *corecharm.URL) (Operation, error) 83 84 // NewNoOpFinishUpgradeSeries creates a noop which simply resets the 85 // status of a units upgrade series. 86 NewNoOpFinishUpgradeSeries() (Operation, error) 87 88 // NewFinishUpgradeCharmProfile simply cleans up the state of the unit 89 // of a upgrade charm profile. 90 NewFinishUpgradeCharmProfile(charmURL *corecharm.URL) (Operation, error) 91 92 // NewRevertUpgrade creates an operation to clear the unit's resolved flag, 93 // and execute an upgrade to the supplied charm that is careful to excise 94 // remnants of a previously failed upgrade to a different charm. 95 NewRevertUpgrade(charmURL *corecharm.URL) (Operation, error) 96 97 // NewResolvedUpgrade creates an operation to clear the unit's resolved flag, 98 // and execute an upgrade to the supplied charm that is careful to preserve 99 // non-overlapping remnants of a previously failed upgrade to the same charm. 100 NewResolvedUpgrade(charmURL *corecharm.URL) (Operation, error) 101 102 // NewRunHook creates an operation to execute the supplied hook. 103 NewRunHook(hookInfo hook.Info) (Operation, error) 104 105 // NewSkipHook creates an operation to mark the supplied hook as 106 // completed successfully, without executing the hook. 107 NewSkipHook(hookInfo hook.Info) (Operation, error) 108 109 // NewAction creates an operation to execute the supplied action. 110 NewAction(actionId string) (Operation, error) 111 112 // NewFailAction creates an operation that marks an action as failed. 113 NewFailAction(actionId string) (Operation, error) 114 115 // NewCommands creates an operation to execute the supplied script in the 116 // indicated relation context, and pass the results back over the supplied 117 // func. 118 NewCommands(args CommandArgs, sendResponse CommandResponseFunc) (Operation, error) 119 120 // NewAcceptLeadership creates an operation to ensure the uniter acts as 121 // application leader. 122 NewAcceptLeadership() (Operation, error) 123 124 // NewResignLeadership creates an operation to ensure the uniter does not 125 // act as application leader. 126 NewResignLeadership() (Operation, error) 127 } 128 129 // CommandArgs stores the arguments for a Command operation. 130 type CommandArgs struct { 131 // Commands is the arbitrary commands to execute on the unit 132 Commands string 133 // RelationId is the relation context to execute the commands in. 134 RelationId int 135 // RemoteUnitName is the remote unit for the relation context. 136 RemoteUnitName string 137 // ForceRemoteUnit skips unit inference and existence validation. 138 ForceRemoteUnit bool 139 } 140 141 // CommandResponseFunc is for marshalling command responses back to the source 142 // of the original request. 143 type CommandResponseFunc func(*utilexec.ExecResponse, error) 144 145 // Callbacks exposes all the uniter code that's required by the various operations. 146 // It's far from cohesive, and fundamentally represents inappropriate coupling, so 147 // it's a prime candidate for future refactoring. 148 type Callbacks interface { 149 // PrepareHook and CommitHook exist so that we can defer worrying about how 150 // to untangle Uniter.relationers from everything else. They're only used by 151 // RunHook operations. 152 PrepareHook(info hook.Info) (name string, err error) 153 CommitHook(info hook.Info) error 154 155 // SetExecutingStatus sets the agent state to "Executing" with a message. 156 SetExecutingStatus(string) error 157 158 // NotifyHook* exist so that we can defer worrying about how to untangle the 159 // callbacks inserted for uniter_test. They're only used by RunHook operations. 160 NotifyHookCompleted(string, runner.Context) 161 NotifyHookFailed(string, runner.Context) 162 163 // The following methods exist primarily to allow us to test operation code 164 // without using a live api connection. 165 166 // FailAction marks the supplied action failed. It's only used by 167 // RunActions operations. 168 FailAction(actionId, message string) error 169 170 // GetArchiveInfo is used to find out how to download a charm archive. It's 171 // only used by Deploy operations. 172 GetArchiveInfo(charmURL *corecharm.URL) (charm.BundleInfo, error) 173 174 // SetCurrentCharm records intent to deploy a given charm. It must be called 175 // *before* recording local state referencing that charm, to ensure there's 176 // no path by which the controller can legitimately garbage collect that 177 // charm or the application's settings for it. It's only used by Deploy operations. 178 SetCurrentCharm(charmURL *corecharm.URL) error 179 180 // SetSeriesStatusUpgrade is intended to give the uniter a chance to 181 // upgrade the status of a running series upgrade before or after 182 // upgrade series hook code completes and, for display purposes, to 183 // supply a reason as to why it is making the change. 184 SetUpgradeSeriesStatus(status model.UpgradeSeriesStatus, reason string) error 185 186 // RemoveUpgradeCharmProfileData is intended to clean up the LXDProfile status 187 // to ensure that we start from a clean slate. 188 RemoveUpgradeCharmProfileData() error 189 } 190 191 // StorageUpdater is an interface used for updating local knowledge of storage 192 // attachments. 193 type StorageUpdater interface { 194 // UpdateStorage updates local knowledge of the storage attachments 195 // with the specified tags. 196 UpdateStorage([]names.StorageTag) error 197 }