/
manifold.go
44 lines (37 loc) · 1.5 KB
/
manifold.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machinelock
import (
"github.com/juju/errors"
"github.com/juju/juju/agent"
"github.com/juju/juju/cmd/jujud/agent/util"
cmdutil "github.com/juju/juju/cmd/jujud/util"
"github.com/juju/juju/worker"
"github.com/juju/juju/worker/dependency"
)
// createLock exists to be patched out in export_test.go
var createLock = cmdutil.HookExecutionLock
// ManifoldConfig specifies the names a machinelock manifold should use to
// address its dependencies.
type ManifoldConfig util.AgentManifoldConfig
// Manifold returns a dependency.Manifold that governs the construction of
// and access to a machine-wide lock intended to prevent various operations
// from running concurrently and interfering with one another. Examples (are
// not limited to): hook executions, package installation, synchronisation
// of reboots.
// Clients can access the lock by passing a **fslock.Lock into the out param
// of their dependency.Context's Get method.
func Manifold(config ManifoldConfig) dependency.Manifold {
manifold := util.AgentManifold(util.AgentManifoldConfig(config), newWorker)
manifold.Output = util.ValueWorkerOutput
return manifold
}
// newWorker creates a degenerate worker that provides access to an fslock.
func newWorker(a agent.Agent) (worker.Worker, error) {
dataDir := a.CurrentConfig().DataDir()
lock, err := createLock(dataDir)
if err != nil {
return nil, errors.Trace(err)
}
return util.NewValueWorker(lock)
}