/
manifold.go
66 lines (53 loc) · 1.8 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package identityfilewriter
import (
"errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/agent"
apiagent "github.com/juju/juju/api/agent"
"github.com/juju/juju/api/base"
"github.com/juju/juju/cmd/jujud/agent/engine"
"github.com/juju/juju/state/multiwatcher"
"github.com/juju/juju/worker"
"github.com/juju/juju/worker/dependency"
)
// ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
type ManifoldConfig engine.AgentAPIManifoldConfig
// Manifold returns a dependency manifold that runs an identity file writer worker,
// using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
typedConfig := engine.AgentAPIManifoldConfig(config)
return engine.AgentAPIManifold(typedConfig, newWorker)
}
// newWorker trivially wraps NewWorker for use in a engine.AgentAPIManifold.
func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
cfg := a.CurrentConfig()
// Grab the tag and ensure that it's for a machine.
tag, ok := cfg.Tag().(names.MachineTag)
if !ok {
return nil, errors.New("this manifold may only be used inside a machine agent")
}
// Get the machine agent's jobs.
entity, err := apiagent.NewState(apiCaller).Entity(tag)
if err != nil {
return nil, err
}
var isModelManager bool
for _, job := range entity.Jobs() {
if job == multiwatcher.JobManageModel {
isModelManager = true
break
}
}
if !isModelManager {
return nil, dependency.ErrMissing
}
return NewWorker(cfg)
}
var NewWorker = func(agentConfig agent.Config) (worker.Worker, error) {
inner := func(<-chan struct{}) error {
return agent.WriteSystemIdentityFile(agentConfig)
}
return worker.NewSimpleWorker(inner), nil
}