~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/manifold.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package uniter
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        "github.com/juju/utils/clock"
 
9
        "gopkg.in/juju/names.v2"
 
10
 
 
11
        "github.com/juju/juju/agent"
 
12
        "github.com/juju/juju/api"
 
13
        "github.com/juju/juju/api/uniter"
 
14
        "github.com/juju/juju/apiserver/params"
 
15
        "github.com/juju/juju/core/leadership"
 
16
        "github.com/juju/juju/worker"
 
17
        "github.com/juju/juju/worker/dependency"
 
18
        "github.com/juju/juju/worker/fortress"
 
19
        "github.com/juju/juju/worker/uniter/operation"
 
20
)
 
21
 
 
22
// ManifoldConfig defines the names of the manifolds on which a
 
23
// Manifold will depend.
 
24
type ManifoldConfig struct {
 
25
        AgentName             string
 
26
        APICallerName         string
 
27
        MachineLockName       string
 
28
        Clock                 clock.Clock
 
29
        LeadershipTrackerName string
 
30
        CharmDirName          string
 
31
        HookRetryStrategyName string
 
32
}
 
33
 
 
34
// Manifold returns a dependency manifold that runs a uniter worker,
 
35
// using the resource names defined in the supplied config.
 
36
func Manifold(config ManifoldConfig) dependency.Manifold {
 
37
        return dependency.Manifold{
 
38
                Inputs: []string{
 
39
                        config.AgentName,
 
40
                        config.APICallerName,
 
41
                        config.LeadershipTrackerName,
 
42
                        config.CharmDirName,
 
43
                        config.HookRetryStrategyName,
 
44
                },
 
45
                Start: func(context dependency.Context) (worker.Worker, error) {
 
46
                        if config.Clock == nil {
 
47
                                return nil, errors.NotValidf("missing Clock")
 
48
                        }
 
49
                        if config.MachineLockName == "" {
 
50
                                return nil, errors.NotValidf("missing MachineLockName")
 
51
                        }
 
52
 
 
53
                        // Collect all required resources.
 
54
                        var agent agent.Agent
 
55
                        if err := context.Get(config.AgentName, &agent); err != nil {
 
56
                                return nil, err
 
57
                        }
 
58
                        var apiConn api.Connection
 
59
                        if err := context.Get(config.APICallerName, &apiConn); err != nil {
 
60
                                // TODO(fwereade): absence of an APICaller shouldn't be the end of
 
61
                                // the world -- we ought to return a type that can at least run the
 
62
                                // leader-deposed hook -- but that's not done yet.
 
63
                                return nil, err
 
64
                        }
 
65
                        var leadershipTracker leadership.Tracker
 
66
                        if err := context.Get(config.LeadershipTrackerName, &leadershipTracker); err != nil {
 
67
                                return nil, err
 
68
                        }
 
69
                        var charmDirGuard fortress.Guard
 
70
                        if err := context.Get(config.CharmDirName, &charmDirGuard); err != nil {
 
71
                                return nil, err
 
72
                        }
 
73
 
 
74
                        var hookRetryStrategy params.RetryStrategy
 
75
                        if err := context.Get(config.HookRetryStrategyName, &hookRetryStrategy); err != nil {
 
76
                                return nil, err
 
77
                        }
 
78
 
 
79
                        downloader := api.NewCharmDownloader(apiConn.Client())
 
80
 
 
81
                        manifoldConfig := config
 
82
                        // Configure and start the uniter.
 
83
                        agentConfig := agent.CurrentConfig()
 
84
                        tag := agentConfig.Tag()
 
85
                        unitTag, ok := tag.(names.UnitTag)
 
86
                        if !ok {
 
87
                                return nil, errors.Errorf("expected a unit tag, got %v", tag)
 
88
                        }
 
89
                        uniterFacade := uniter.NewState(apiConn, unitTag)
 
90
                        uniter, err := NewUniter(&UniterParams{
 
91
                                UniterFacade:         uniterFacade,
 
92
                                UnitTag:              unitTag,
 
93
                                LeadershipTracker:    leadershipTracker,
 
94
                                DataDir:              agentConfig.DataDir(),
 
95
                                Downloader:           downloader,
 
96
                                MachineLockName:      manifoldConfig.MachineLockName,
 
97
                                CharmDirGuard:        charmDirGuard,
 
98
                                UpdateStatusSignal:   NewUpdateStatusTimer(),
 
99
                                HookRetryStrategy:    hookRetryStrategy,
 
100
                                NewOperationExecutor: operation.NewExecutor,
 
101
                                Clock:                manifoldConfig.Clock,
 
102
                        })
 
103
                        if err != nil {
 
104
                                return nil, errors.Trace(err)
 
105
                        }
 
106
                        return uniter, nil
 
107
                },
 
108
        }
 
109
}