forked from juju/juju
/
manifold.go
120 lines (110 loc) · 3.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package migrationmaster
import (
"github.com/juju/errors"
"github.com/juju/utils/clock"
worker "gopkg.in/juju/worker.v1"
"github.com/juju/juju/agent"
"github.com/juju/juju/api"
"github.com/juju/juju/api/base"
"github.com/juju/juju/migration"
"github.com/juju/juju/worker/dependency"
"github.com/juju/juju/worker/fortress"
)
// ManifoldConfig defines the names of the manifolds on which a
// Worker manifold will depend.
type ManifoldConfig struct {
AgentName string
APICallerName string
FortressName string
Clock clock.Clock
NewFacade func(base.APICaller) (Facade, error)
NewWorker func(Config) (worker.Worker, error)
}
// Validate is called by start to check for bad configuration.
func (config ManifoldConfig) Validate() error {
if config.AgentName == "" {
return errors.NotValidf("empty AgentName")
}
if config.APICallerName == "" {
return errors.NotValidf("empty APICallerName")
}
if config.FortressName == "" {
return errors.NotValidf("empty FortressName")
}
if config.NewFacade == nil {
return errors.NotValidf("nil NewFacade")
}
if config.NewWorker == nil {
return errors.NotValidf("nil NewWorker")
}
if config.Clock == nil {
return errors.NotValidf("nil Clock")
}
return nil
}
// start is a StartFunc for a Worker manifold.
func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
var agent agent.Agent
if err := context.Get(config.AgentName, &agent); err != nil {
return nil, errors.Trace(err)
}
var apiConn api.Connection
if err := context.Get(config.APICallerName, &apiConn); err != nil {
return nil, errors.Trace(err)
}
var guard fortress.Guard
if err := context.Get(config.FortressName, &guard); err != nil {
return nil, errors.Trace(err)
}
facade, err := config.NewFacade(apiConn)
if err != nil {
return nil, errors.Trace(err)
}
apiClient := apiConn.Client()
worker, err := config.NewWorker(Config{
ModelUUID: agent.CurrentConfig().Model().Id(),
Facade: facade,
Guard: guard,
APIOpen: api.Open,
UploadBinaries: migration.UploadBinaries,
CharmDownloader: apiClient,
ToolsDownloader: apiClient,
Clock: config.Clock,
})
if err != nil {
return nil, errors.Trace(err)
}
return worker, nil
}
func errorFilter(err error) error {
switch errors.Cause(err) {
case ErrMigrated:
// If the model has migrated, the migrationmaster should no
// longer be running.
return dependency.ErrUninstall
case ErrInactive:
// If the migration is no longer active, restart the
// migrationmaster immediately so it can wait for the next
// attempt.
return dependency.ErrBounce
default:
return err
}
}
// Manifold packages a Worker for use in a dependency.Engine.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.APICallerName,
config.FortressName,
},
Start: config.start,
Filter: errorFilter,
}
}