1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"gopkg.in/juju/names.v2"
10
"github.com/juju/juju/agent"
11
"github.com/juju/juju/api"
12
apiagent "github.com/juju/juju/api/agent"
13
apimachiner "github.com/juju/juju/api/machiner"
14
"github.com/juju/juju/state"
15
"github.com/juju/juju/worker"
16
"github.com/juju/juju/worker/dependency"
17
"github.com/juju/juju/worker/gate"
20
// ManifoldConfig defines the names of the manifolds on which a
21
// Manifold will depend.
22
type ManifoldConfig struct {
25
UpgradeStepsGateName string
26
OpenStateForUpgrade func() (*state.State, error)
27
PreUpgradeSteps func(*state.State, agent.Config, bool, bool) error
30
// Manifold returns a dependency manifold that runs an upgrader
31
// worker, using the resource names defined in the supplied config.
32
func Manifold(config ManifoldConfig) dependency.Manifold {
33
return dependency.Manifold{
37
config.UpgradeStepsGateName,
39
Start: func(context dependency.Context) (worker.Worker, error) {
41
if config.OpenStateForUpgrade == nil {
42
return nil, errors.New("missing OpenStateForUpgrade in config")
44
if config.PreUpgradeSteps == nil {
45
return nil, errors.New("missing PreUpgradeSteps in config")
50
if err := context.Get(config.AgentName, &agent); err != nil {
54
// Grab the tag and ensure that it's for a machine.
55
tag, ok := agent.CurrentConfig().Tag().(names.MachineTag)
57
return nil, errors.New("agent's tag is not a machine tag")
60
// Get API connection.
61
// TODO(fwereade): can we make the worker use an
62
// APICaller instead? should be able to depend on
63
// the Engine to abort us when conn is closed...
64
var apiConn api.Connection
65
if err := context.Get(config.APICallerName, &apiConn); err != nil {
69
// Get the machine agent's jobs.
70
// TODO(fwereade): use appropriate facade!
71
agentFacade := apiagent.NewState(apiConn)
72
entity, err := agentFacade.Entity(tag)
78
// Get machine instance for setting status on.
79
// TODO(fwereade): use appropriate facade!
80
machinerFacade := apimachiner.NewState(apiConn)
81
machine, err := machinerFacade.Machine(tag)
86
// Get upgradesteps completed lock.
87
var upgradeStepsLock gate.Lock
88
if err := context.Get(config.UpgradeStepsGateName, &upgradeStepsLock); err != nil {
97
config.OpenStateForUpgrade,
98
config.PreUpgradeSteps,