/
interface.go
265 lines (210 loc) · 9.92 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2014-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package operation
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
utilexec "github.com/juju/utils/v3/exec"
"github.com/juju/juju/core/model"
"github.com/juju/juju/worker/uniter/charm"
"github.com/juju/juju/worker/uniter/hook"
"github.com/juju/juju/worker/uniter/remotestate"
"github.com/juju/juju/worker/uniter/runner"
"github.com/juju/juju/worker/uniter/runner/context"
)
//go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/interface_mock.go github.com/juju/juju/worker/uniter/operation Operation,Factory,Callbacks
// Logger is here to stop the desire of creating a package level Logger.
// Don't do this, pass one in to the needed functions.
type logger interface{}
var _ logger = struct{}{}
// Logger determines the logging methods used by the operations package.
type Logger interface {
Errorf(string, ...interface{})
Warningf(string, ...interface{})
Infof(string, ...interface{})
Debugf(string, ...interface{})
Tracef(string, ...interface{})
}
// Operation encapsulates the stages of the various things the uniter can do,
// and the state changes that need to be recorded as they happen. Operations
// are designed to be Run (or Skipped) by an Executor, which supplies starting
// state and records the changes returned.
type Operation interface {
// String returns a short representation of the operation.
String() string
// NeedsGlobalMachineLock returns a bool expressing whether we need to lock the machine.
NeedsGlobalMachineLock() bool
// ExecutionGroup returns a string used to construct the name of the machine lock.
ExecutionGroup() string
// Prepare ensures that the operation is valid and ready to be executed.
// If it returns a non-nil state, that state will be validated and recorded.
// If it returns ErrSkipExecute, it indicates that the operation can be
// committed directly.
Prepare(state State) (*State, error)
// Execute carries out the operation. It must not be called without having
// called Prepare first. If it returns a non-nil state, that state will be
// validated and recorded.
Execute(state State) (*State, error)
// Commit ensures that the operation's completion is recorded. If it returns
// a non-nil state, that state will be validated and recorded.
Commit(state State) (*State, error)
// RemoteStateChanged is called when the remote state changed during execution
// of the operation.
RemoteStateChanged(snapshot remotestate.Snapshot)
}
// WrappedOperation extends Operation to provide access to the wrapped operation.
type WrappedOperation interface {
Operation
WrappedOperation() Operation
}
// Unwrap peels back one layer of a wrapped operation.
func Unwrap(op Operation) Operation {
if op == nil {
return nil
}
if wrapped, ok := op.(WrappedOperation); ok {
return wrapped.WrappedOperation()
}
return op
}
// Executor records and exposes uniter state, and applies suitable changes as
// operations are run or skipped.
type Executor interface {
// State returns a copy of the executor's current operation state.
State() State
// Run will Prepare, Execute, and Commit the supplied operation, writing
// indicated state changes between steps. If any step returns an unknown
// error, the run will be aborted and an error will be returned.
// On remote state change, the executor will fire the operation's
// RemoteStateChanged method.
Run(Operation, <-chan remotestate.Snapshot) error
// Skip will Commit the supplied operation, and write any state change
// indicated. If Commit returns an error, so will Skip.
Skip(Operation) error
}
// Factory creates operations.
type Factory interface {
// NewInstall creates an install operation for the supplied charm.
NewInstall(charmURL string) (Operation, error)
// NewUpgrade creates an upgrade operation for the supplied charm.
NewUpgrade(charmURL string) (Operation, error)
// NewRemoteInit inits the remote charm on CAAS pod.
NewRemoteInit(runningStatus remotestate.ContainerRunningStatus) (Operation, error)
// NewSkipRemoteInit skips a remote-init operation.
NewSkipRemoteInit(retry bool) (Operation, error)
// NewNoOpFinishUpgradeSeries creates a noop which simply resets the
// status of a units upgrade series.
NewNoOpFinishUpgradeSeries() (Operation, error)
// NewRevertUpgrade creates an operation to clear the unit's resolved flag,
// and execute an upgrade to the supplied charm that is careful to excise
// remnants of a previously failed upgrade to a different charm.
NewRevertUpgrade(charmURL string) (Operation, error)
// NewResolvedUpgrade creates an operation to clear the unit's resolved flag,
// and execute an upgrade to the supplied charm that is careful to preserve
// non-overlapping remnants of a previously failed upgrade to the same charm.
NewResolvedUpgrade(charmURL string) (Operation, error)
// NewRunHook creates an operation to execute the supplied hook.
NewRunHook(hookInfo hook.Info) (Operation, error)
// NewSkipHook creates an operation to mark the supplied hook as
// completed successfully, without executing the hook.
NewSkipHook(hookInfo hook.Info) (Operation, error)
// NewAction creates an operation to execute the supplied action.
NewAction(actionId string) (Operation, error)
// NewFailAction creates an operation that marks an action as failed.
NewFailAction(actionId string) (Operation, error)
// NewCommands creates an operation to execute the supplied script in the
// indicated relation context, and pass the results back over the supplied
// func.
NewCommands(args CommandArgs, sendResponse CommandResponseFunc) (Operation, error)
// NewAcceptLeadership creates an operation to ensure the uniter acts as
// application leader.
NewAcceptLeadership() (Operation, error)
// NewResignLeadership creates an operation to ensure the uniter does not
// act as application leader.
NewResignLeadership() (Operation, error)
// NewNoOpSecretsRemoved creates an operation to update the secrets
// state when secrets are removed.
NewNoOpSecretsRemoved(uris []string) (Operation, error)
}
// CommandArgs stores the arguments for a Command operation.
type CommandArgs struct {
// Commands is the arbitrary commands to execute on the unit
Commands string
// RelationId is the relation context to execute the commands in.
RelationId int
// RemoteUnitName is the remote unit for the relation context.
RemoteUnitName string
// TODO(jam): 2019-10-24 Include RemoteAppName
// ForceRemoteUnit skips unit inference and existence validation.
ForceRemoteUnit bool
// RunLocation describes where the command must run.
RunLocation runner.RunLocation
}
// Validate the command arguments.
func (args CommandArgs) Validate() error {
if args.Commands == "" {
return errors.New("commands required")
}
if args.RemoteUnitName != "" {
if args.RelationId == -1 {
return errors.New("remote unit not valid without relation")
} else if !names.IsValidUnit(args.RemoteUnitName) {
return errors.Errorf("invalid remote unit name %q", args.RemoteUnitName)
}
}
return nil
}
// CommandResponseFunc is for marshalling command responses back to the source
// of the original request.
type CommandResponseFunc func(*utilexec.ExecResponse, error) bool
// Callbacks exposes all the uniter code that's required by the various operations.
// It's far from cohesive, and fundamentally represents inappropriate coupling, so
// it's a prime candidate for future refactoring.
type Callbacks interface {
// PrepareHook and CommitHook exist so that we can defer worrying about how
// to untangle Uniter.relationers from everything else. They're only used by
// RunHook operations.
PrepareHook(info hook.Info) (name string, err error)
CommitHook(info hook.Info) error
// SetExecutingStatus sets the agent state to "Executing" with a message.
SetExecutingStatus(string) error
// NotifyHook* exist so that we can defer worrying about how to untangle the
// callbacks inserted for uniter_test. They're only used by RunHook operations.
NotifyHookCompleted(string, context.Context)
NotifyHookFailed(string, context.Context)
// The following methods exist primarily to allow us to test operation code
// without using a live api connection.
// FailAction marks the supplied action failed. It's only used by
// RunActions operations.
FailAction(actionId, message string) error
// ActionStatus returns the status of the action required by the action operation for
// cancelation.
ActionStatus(actionId string) (string, error)
// GetArchiveInfo is used to find out how to download a charm archive. It's
// only used by Deploy operations.
GetArchiveInfo(charmURL string) (charm.BundleInfo, error)
// SetCurrentCharm records intent to deploy a given charm. It must be called
// *before* recording local state referencing that charm, to ensure there's
// no path by which the controller can legitimately garbage collect that
// charm or the application's settings for it. It's only used by Deploy operations.
SetCurrentCharm(charmURL string) error
// SetUpgradeSeriesStatus is intended to give the uniter a chance to
// upgrade the status of a running series upgrade before or after
// upgrade series hook code completes and, for display purposes, to
// supply a reason as to why it is making the change.
SetUpgradeSeriesStatus(status model.UpgradeSeriesStatus, reason string) error
// SetSecretRotated updates the secret rotation status.
SetSecretRotated(url string, originalRevision int) error
// SecretsRemoved updates the unit secret state when
// secrets are removed.
SecretsRemoved(uris []string) error
// RemoteInit copies the charm to the remote instance. CAAS only.
RemoteInit(runningStatus remotestate.ContainerRunningStatus, abort <-chan struct{}) error
}
// StorageUpdater is an interface used for updating local knowledge of storage
// attachments.
type StorageUpdater interface {
// UpdateStorage updates local knowledge of the storage attachments
// with the specified tags.
UpdateStorage([]names.StorageTag) error
}