/
manifold.go
51 lines (44 loc) · 1.54 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
// Copyright 2016 Canonical Ltd.
// Copyright 2016 Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.
package machineactions
import (
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/worker/v2"
"github.com/juju/worker/v2/dependency"
"github.com/juju/juju/agent"
"github.com/juju/juju/api/base"
"github.com/juju/juju/cmd/jujud/agent/engine"
"github.com/juju/juju/core/machinelock"
)
// ManifoldConfig describes the dependencies of a machine action runner.
type ManifoldConfig struct {
AgentName string
APICallerName string
MachineLock machinelock.Lock
NewFacade func(base.APICaller) Facade
NewWorker func(WorkerConfig) (worker.Worker, error)
}
// start is used by engine.AgentAPIManifold to create a StartFunc.
func (config ManifoldConfig) start(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
machineTag, ok := a.CurrentConfig().Tag().(names.MachineTag)
if !ok {
return nil, errors.Errorf("this manifold can only be used inside a machine")
}
machineActionsFacade := config.NewFacade(apiCaller)
return config.NewWorker(WorkerConfig{
Facade: machineActionsFacade,
MachineTag: machineTag,
MachineLock: config.MachineLock,
HandleAction: HandleAction,
})
}
// Manifold returns a dependency.Manifold as configured.
func Manifold(config ManifoldConfig) dependency.Manifold {
typedConfig := engine.AgentAPIManifoldConfig{
AgentName: config.AgentName,
APICallerName: config.APICallerName,
}
return engine.AgentAPIManifold(typedConfig, config.start)
}