/
uniter.go
574 lines (530 loc) · 17.6 KB
/
uniter.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils"
"github.com/juju/utils/clock"
"github.com/juju/utils/exec"
"github.com/juju/utils/fslock"
corecharm "gopkg.in/juju/charm.v6-unstable"
"github.com/juju/juju/api/uniter"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/leadership"
"github.com/juju/juju/status"
"github.com/juju/juju/worker"
"github.com/juju/juju/worker/catacomb"
"github.com/juju/juju/worker/fortress"
"github.com/juju/juju/worker/uniter/actions"
"github.com/juju/juju/worker/uniter/charm"
"github.com/juju/juju/worker/uniter/hook"
uniterleadership "github.com/juju/juju/worker/uniter/leadership"
"github.com/juju/juju/worker/uniter/operation"
"github.com/juju/juju/worker/uniter/relation"
"github.com/juju/juju/worker/uniter/remotestate"
"github.com/juju/juju/worker/uniter/resolver"
"github.com/juju/juju/worker/uniter/runcommands"
"github.com/juju/juju/worker/uniter/runner"
"github.com/juju/juju/worker/uniter/runner/context"
"github.com/juju/juju/worker/uniter/runner/jujuc"
"github.com/juju/juju/worker/uniter/storage"
jujuos "github.com/juju/utils/os"
)
var logger = loggo.GetLogger("juju.worker.uniter")
// A UniterExecutionObserver gets the appropriate methods called when a hook
// is executed and either succeeds or fails. Missing hooks don't get reported
// in this way.
type UniterExecutionObserver interface {
HookCompleted(hookName string)
HookFailed(hookName string)
}
// Uniter implements the capabilities of the unit agent. It is not intended to
// implement the actual *behaviour* of the unit agent; that responsibility is
// delegated to Mode values, which are expected to react to events and direct
// the uniter's responses to them.
type Uniter struct {
catacomb catacomb.Catacomb
st *uniter.State
paths Paths
unit *uniter.Unit
relations relation.Relations
storage *storage.Attachments
clock clock.Clock
// Cache the last reported status information
// so we don't make unnecessary api calls.
setStatusMutex sync.Mutex
lastReportedStatus status.Status
lastReportedMessage string
deployer *deployerProxy
operationFactory operation.Factory
operationExecutor operation.Executor
newOperationExecutor NewExecutorFunc
leadershipTracker leadership.Tracker
charmDirGuard fortress.Guard
hookLock *fslock.Lock
// TODO(axw) move the runListener and run-command code outside of the
// uniter, and introduce a separate worker. Each worker would feed
// operations to a single, synchronized runner to execute.
runListener *RunListener
commands runcommands.Commands
commandChannel chan string
// The execution observer is only used in tests at this stage. Should this
// need to be extended, perhaps a list of observers would be needed.
observer UniterExecutionObserver
// updateStatusAt defines a function that will be used to generate signals for
// the update-status hook
updateStatusAt func() <-chan time.Time
// hookRetryStrategy represents configuration for hook retries
hookRetryStrategy params.RetryStrategy
}
// UniterParams hold all the necessary parameters for a new Uniter.
type UniterParams struct {
UniterFacade *uniter.State
UnitTag names.UnitTag
LeadershipTracker leadership.Tracker
DataDir string
MachineLock *fslock.Lock
CharmDirGuard fortress.Guard
UpdateStatusSignal func() <-chan time.Time
HookRetryStrategy params.RetryStrategy
NewOperationExecutor NewExecutorFunc
Clock clock.Clock
// TODO (mattyw, wallyworld, fwereade) Having the observer here make this approach a bit more legitimate, but it isn't.
// the observer is only a stop gap to be used in tests. A better approach would be to have the uniter tests start hooks
// that write to files, and have the tests watch the output to know that hooks have finished.
Observer UniterExecutionObserver
}
type NewExecutorFunc func(string, func() (*corecharm.URL, error), func(string) (func() error, error)) (operation.Executor, error)
// NewUniter creates a new Uniter which will install, run, and upgrade
// a charm on behalf of the unit with the given unitTag, by executing
// hooks and operations provoked by changes in st.
func NewUniter(uniterParams *UniterParams) (*Uniter, error) {
u := &Uniter{
st: uniterParams.UniterFacade,
paths: NewPaths(uniterParams.DataDir, uniterParams.UnitTag),
hookLock: uniterParams.MachineLock,
leadershipTracker: uniterParams.LeadershipTracker,
charmDirGuard: uniterParams.CharmDirGuard,
updateStatusAt: uniterParams.UpdateStatusSignal,
hookRetryStrategy: uniterParams.HookRetryStrategy,
newOperationExecutor: uniterParams.NewOperationExecutor,
observer: uniterParams.Observer,
clock: uniterParams.Clock,
}
err := catacomb.Invoke(catacomb.Plan{
Site: &u.catacomb,
Work: func() error {
return u.loop(uniterParams.UnitTag)
},
})
if err != nil {
return nil, errors.Trace(err)
}
return u, nil
}
func (u *Uniter) loop(unitTag names.UnitTag) (err error) {
if err := u.init(unitTag); err != nil {
if err == worker.ErrTerminateAgent {
return err
}
return fmt.Errorf("failed to initialize uniter for %q: %v", unitTag, err)
}
logger.Infof("unit %q started", u.unit)
// Install is a special case, as it must run before there
// is any remote state, and before the remote state watcher
// is started.
var charmURL *corecharm.URL
var charmModifiedVersion int
opState := u.operationExecutor.State()
if opState.Kind == operation.Install {
logger.Infof("resuming charm install")
op, err := u.operationFactory.NewInstall(opState.CharmURL)
if err != nil {
return errors.Trace(err)
}
if err := u.operationExecutor.Run(op); err != nil {
return errors.Trace(err)
}
charmURL = opState.CharmURL
} else {
curl, err := u.unit.CharmURL()
if err != nil {
return errors.Trace(err)
}
charmURL = curl
svc, err := u.unit.Service()
if err != nil {
return errors.Trace(err)
}
charmModifiedVersion, err = svc.CharmModifiedVersion()
if err != nil {
return errors.Trace(err)
}
}
var (
watcher *remotestate.RemoteStateWatcher
watcherMu sync.Mutex
)
logger.Infof("hooks are retried %v", u.hookRetryStrategy.ShouldRetry)
retryHookChan := make(chan struct{}, 1)
retryHookTimer := utils.NewBackoffTimer(utils.BackoffTimerConfig{
Min: u.hookRetryStrategy.MinRetryTime,
Max: u.hookRetryStrategy.MaxRetryTime,
Jitter: u.hookRetryStrategy.JitterRetryTime,
Factor: u.hookRetryStrategy.RetryTimeFactor,
Func: func() {
// Don't try to send on the channel if it's already full
// This can happen if the timer fires off before the event is consumed
// by the resolver loop
select {
case retryHookChan <- struct{}{}:
default:
}
},
Clock: u.clock,
})
defer func() {
// Stop any send that might be pending
// before closing the channel
retryHookTimer.Reset()
close(retryHookChan)
}()
restartWatcher := func() error {
watcherMu.Lock()
defer watcherMu.Unlock()
if watcher != nil {
// watcher added to catacomb, will kill uniter if there's an error.
worker.Stop(watcher)
}
var err error
watcher, err = remotestate.NewWatcher(
remotestate.WatcherConfig{
State: remotestate.NewAPIState(u.st),
LeadershipTracker: u.leadershipTracker,
UnitTag: unitTag,
UpdateStatusChannel: u.updateStatusAt,
CommandChannel: u.commandChannel,
RetryHookChannel: retryHookChan,
})
if err != nil {
return errors.Trace(err)
}
if err := u.catacomb.Add(watcher); err != nil {
return errors.Trace(err)
}
return nil
}
onIdle := func() error {
opState := u.operationExecutor.State()
if opState.Kind != operation.Continue {
// We should only set idle status if we're in
// the "Continue" state, which indicates that
// there is nothing to do and we're not in an
// error state.
return nil
}
return setAgentStatus(u, status.StatusIdle, "", nil)
}
clearResolved := func() error {
if err := u.unit.ClearResolved(); err != nil {
return errors.Trace(err)
}
watcher.ClearResolvedMode()
return nil
}
for {
if err = restartWatcher(); err != nil {
err = errors.Annotate(err, "(re)starting watcher")
break
}
uniterResolver := NewUniterResolver(ResolverConfig{
ClearResolved: clearResolved,
ReportHookError: u.reportHookError,
FixDeployer: u.deployer.Fix,
ShouldRetryHooks: u.hookRetryStrategy.ShouldRetry,
StartRetryHookTimer: retryHookTimer.Start,
StopRetryHookTimer: retryHookTimer.Reset,
Actions: actions.NewResolver(),
Leadership: uniterleadership.NewResolver(),
Relations: relation.NewRelationsResolver(u.relations),
Storage: storage.NewResolver(u.storage),
Commands: runcommands.NewCommandsResolver(
u.commands, watcher.CommandCompleted,
),
})
// We should not do anything until there has been a change
// to the remote state. The watcher will trigger at least
// once initially.
select {
case <-u.catacomb.Dying():
return u.catacomb.ErrDying()
case <-watcher.RemoteStateChanged():
}
localState := resolver.LocalState{
CharmURL: charmURL,
CharmModifiedVersion: charmModifiedVersion,
}
for err == nil {
err = resolver.Loop(resolver.LoopConfig{
Resolver: uniterResolver,
Watcher: watcher,
Executor: u.operationExecutor,
Factory: u.operationFactory,
Abort: u.catacomb.Dying(),
OnIdle: onIdle,
CharmDirGuard: u.charmDirGuard,
}, &localState)
switch cause := errors.Cause(err); cause {
case nil:
// Loop back around.
case resolver.ErrLoopAborted:
err = u.catacomb.ErrDying()
case operation.ErrNeedsReboot:
err = worker.ErrRebootMachine
case operation.ErrHookFailed:
// Loop back around. The resolver can tell that it is in
// an error state by inspecting the operation state.
err = nil
case resolver.ErrTerminate:
err = u.terminate()
case resolver.ErrRestart:
// make sure we update the two values used above in
// creating LocalState.
charmURL = localState.CharmURL
charmModifiedVersion = localState.CharmModifiedVersion
// leave err assigned, causing loop to break
default:
// We need to set conflicted from here, because error
// handling is outside of the resolver's control.
if operation.IsDeployConflictError(cause) {
localState.Conflicted = true
err = setAgentStatus(u, status.StatusError, "upgrade failed", nil)
} else {
reportAgentError(u, "resolver loop error", err)
}
}
}
if errors.Cause(err) != resolver.ErrRestart {
break
}
}
logger.Infof("unit %q shutting down: %s", u.unit, err)
return err
}
func (u *Uniter) terminate() error {
unitWatcher, err := u.unit.Watch()
if err != nil {
return errors.Trace(err)
}
if err := u.catacomb.Add(unitWatcher); err != nil {
return errors.Trace(err)
}
for {
select {
case <-u.catacomb.Dying():
return u.catacomb.ErrDying()
case _, ok := <-unitWatcher.Changes():
if !ok {
return errors.New("unit watcher closed")
}
if err := u.unit.Refresh(); err != nil {
return errors.Trace(err)
}
if hasSubs, err := u.unit.HasSubordinates(); err != nil {
return errors.Trace(err)
} else if hasSubs {
continue
}
// The unit is known to be Dying; so if it didn't have subordinates
// just above, it can't acquire new ones before this call.
if err := u.unit.EnsureDead(); err != nil {
return errors.Trace(err)
}
return worker.ErrTerminateAgent
}
}
}
func (u *Uniter) setupLocks() (err error) {
if message := u.hookLock.Message(); u.hookLock.IsLocked() && message != "" {
// Look to see if it was us that held the lock before. If it was, we
// should be safe enough to break it, as it is likely that we died
// before unlocking, and have been restarted by the init system.
parts := strings.SplitN(message, ":", 2)
if len(parts) > 1 && parts[0] == u.unit.Name() {
if err := u.hookLock.BreakLock(); err != nil {
return err
}
}
}
return nil
}
func (u *Uniter) init(unitTag names.UnitTag) (err error) {
u.unit, err = u.st.Unit(unitTag)
if err != nil {
return err
}
if u.unit.Life() == params.Dead {
// If we started up already dead, we should not progress further. If we
// become Dead immediately after starting up, we may well complete any
// operations in progress before detecting it; but that race is fundamental
// and inescapable, whereas this one is not.
return worker.ErrTerminateAgent
}
if err = u.setupLocks(); err != nil {
return err
}
if err := jujuc.EnsureSymlinks(u.paths.ToolsDir); err != nil {
return err
}
if err := os.MkdirAll(u.paths.State.RelationsDir, 0755); err != nil {
return errors.Trace(err)
}
relations, err := relation.NewRelations(
u.st, unitTag, u.paths.State.CharmDir,
u.paths.State.RelationsDir, u.catacomb.Dying(),
)
if err != nil {
return errors.Annotatef(err, "cannot create relations")
}
u.relations = relations
storageAttachments, err := storage.NewAttachments(
u.st, unitTag, u.paths.State.StorageDir, u.catacomb.Dying(),
)
if err != nil {
return errors.Annotatef(err, "cannot create storage hook source")
}
u.storage = storageAttachments
u.commands = runcommands.NewCommands()
u.commandChannel = make(chan string)
deployer, err := charm.NewDeployer(
u.paths.State.CharmDir,
u.paths.State.DeployerDir,
charm.NewBundlesDir(u.paths.State.BundlesDir),
)
if err != nil {
return errors.Annotatef(err, "cannot create deployer")
}
u.deployer = &deployerProxy{deployer}
contextFactory, err := context.NewContextFactory(
u.st, unitTag, u.leadershipTracker, u.relations.GetInfo, u.storage, u.paths, u.clock,
)
if err != nil {
return err
}
runnerFactory, err := runner.NewFactory(
u.st, u.paths, contextFactory,
)
if err != nil {
return errors.Trace(err)
}
u.operationFactory = operation.NewFactory(operation.FactoryParams{
Deployer: u.deployer,
RunnerFactory: runnerFactory,
Callbacks: &operationCallbacks{u},
Abort: u.catacomb.Dying(),
MetricSpoolDir: u.paths.GetMetricsSpoolDir(),
})
operationExecutor, err := u.newOperationExecutor(u.paths.State.OperationsFile, u.getServiceCharmURL, u.acquireExecutionLock)
if err != nil {
return errors.Trace(err)
}
u.operationExecutor = operationExecutor
logger.Debugf("starting juju-run listener on unix:%s", u.paths.Runtime.JujuRunSocket)
commandRunner, err := NewChannelCommandRunner(ChannelCommandRunnerConfig{
Abort: u.catacomb.Dying(),
Commands: u.commands,
CommandChannel: u.commandChannel,
})
if err != nil {
return errors.Annotate(err, "creating command runner")
}
u.runListener, err = NewRunListener(RunListenerConfig{
SocketPath: u.paths.Runtime.JujuRunSocket,
CommandRunner: commandRunner,
})
if err != nil {
return errors.Trace(err)
}
rlw := newRunListenerWrapper(u.runListener)
if err := u.catacomb.Add(rlw); err != nil {
return errors.Trace(err)
}
// The socket needs to have permissions 777 in order for other users to use it.
if jujuos.HostOS() != jujuos.Windows {
return os.Chmod(u.paths.Runtime.JujuRunSocket, 0777)
}
return nil
}
func (u *Uniter) Kill() {
u.catacomb.Kill(nil)
}
func (u *Uniter) Wait() error {
return u.catacomb.Wait()
}
func (u *Uniter) getServiceCharmURL() (*corecharm.URL, error) {
// TODO(fwereade): pretty sure there's no reason to make 2 API calls here.
service, err := u.st.Service(u.unit.ServiceTag())
if err != nil {
return nil, err
}
charmURL, _, err := service.CharmURL()
return charmURL, err
}
// RunCommands executes the supplied commands in a hook context.
func (u *Uniter) RunCommands(args RunCommandsArgs) (results *exec.ExecResponse, err error) {
// TODO(axw) drop this when we move the run-listener to an independent
// worker. This exists purely for the tests.
return u.runListener.RunCommands(args)
}
// acquireExecutionLock acquires the machine-level execution lock, and
// returns a func that must be called to unlock it. It's used by operation.Executor
// when running operations that execute external code.
func (u *Uniter) acquireExecutionLock(message string) (func() error, error) {
logger.Debugf("lock: %v", message)
// We want to make sure we don't block forever when locking, but take the
// Uniter's catacomb into account.
checkCatacomb := func() error {
select {
case <-u.catacomb.Dying():
return u.catacomb.ErrDying()
default:
return nil
}
}
message = fmt.Sprintf("%s: %s", u.unit.Name(), message)
if err := u.hookLock.LockWithFunc(message, checkCatacomb); err != nil {
return nil, err
}
return func() error {
logger.Debugf("unlock: %v", message)
return u.hookLock.Unlock()
}, nil
}
func (u *Uniter) reportHookError(hookInfo hook.Info) error {
// Set the agent status to "error". We must do this here in case the
// hook is interrupted (e.g. unit agent crashes), rather than immediately
// after attempting a runHookOp.
hookName := string(hookInfo.Kind)
statusData := map[string]interface{}{}
if hookInfo.Kind.IsRelation() {
statusData["relation-id"] = hookInfo.RelationId
if hookInfo.RemoteUnit != "" {
statusData["remote-unit"] = hookInfo.RemoteUnit
}
relationName, err := u.relations.Name(hookInfo.RelationId)
if err != nil {
return errors.Trace(err)
}
hookName = fmt.Sprintf("%s-%s", relationName, hookInfo.Kind)
}
statusData["hook"] = hookName
statusMessage := fmt.Sprintf("hook failed: %q", hookName)
return setAgentStatus(u, status.StatusError, statusMessage, statusData)
}