/
factory.go
188 lines (168 loc) · 5.22 KB
/
factory.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2014-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package operation
import (
"github.com/juju/errors"
"github.com/juju/names"
corecharm "gopkg.in/juju/charm.v5"
"github.com/juju/juju/worker/uniter/charm"
"github.com/juju/juju/worker/uniter/hook"
"github.com/juju/juju/worker/uniter/runner"
)
// NewFactory returns a Factory that creates Operations backed by the supplied
// parameters.
func NewFactory(
deployer charm.Deployer,
runnerFactory runner.Factory,
callbacks Callbacks,
storageUpdater StorageUpdater,
abort <-chan struct{},
) Factory {
return &factory{
deployer: deployer,
runnerFactory: runnerFactory,
callbacks: callbacks,
storageUpdater: storageUpdater,
abort: abort,
}
}
type factory struct {
deployer charm.Deployer
runnerFactory runner.Factory
callbacks Callbacks
storageUpdater StorageUpdater
abort <-chan struct{}
}
// newResolved wraps the supplied operation such that it will clear the uniter
// resolve flag before executing.
func (f *factory) newResolved(wrapped Operation) (Operation, error) {
if wrapped == nil {
return nil, errors.New("operation required")
}
return &resolvedOperation{
Operation: wrapped,
callbacks: f.callbacks,
}, nil
}
// newDeploy is the common code for creating arbitrary deploy operations.
func (f *factory) newDeploy(kind Kind, charmURL *corecharm.URL, revert, resolved bool) (Operation, error) {
if charmURL == nil {
return nil, errors.New("charm url required")
} else if kind != Install && kind != Upgrade {
return nil, errors.Errorf("unknown deploy kind: %s", kind)
}
return &deploy{
kind: kind,
charmURL: charmURL,
revert: revert,
resolved: resolved,
callbacks: f.callbacks,
deployer: f.deployer,
abort: f.abort,
}, nil
}
// NewInstall is part of the Factory interface.
func (f *factory) NewInstall(charmURL *corecharm.URL) (Operation, error) {
return f.newDeploy(Install, charmURL, false, false)
}
// NewUpgrade is part of the Factory interface.
func (f *factory) NewUpgrade(charmURL *corecharm.URL) (Operation, error) {
return f.newDeploy(Upgrade, charmURL, false, false)
}
// NewRevertUpgrade is part of the Factory interface.
func (f *factory) NewRevertUpgrade(charmURL *corecharm.URL) (Operation, error) {
charmOp, err := f.newDeploy(Upgrade, charmURL, true, false)
if err != nil {
return nil, err
}
return f.newResolved(charmOp)
}
// NewResolvedUpgrade is part of the Factory interface.
func (f *factory) NewResolvedUpgrade(charmURL *corecharm.URL) (Operation, error) {
charmOp, err := f.newDeploy(Upgrade, charmURL, false, true)
if err != nil {
return nil, err
}
return f.newResolved(charmOp)
}
// NewRunHook is part of the Factory interface.
func (f *factory) NewRunHook(hookInfo hook.Info) (Operation, error) {
if err := hookInfo.Validate(); err != nil {
return nil, err
}
return &runHook{
info: hookInfo,
callbacks: f.callbacks,
runnerFactory: f.runnerFactory,
}, nil
}
// NewRetryHook is part of the Factory interface.
func (f *factory) NewRetryHook(hookInfo hook.Info) (Operation, error) {
hookOp, err := f.NewRunHook(hookInfo)
if err != nil {
return nil, err
}
return f.newResolved(hookOp)
}
// NewSkipHook is part of the Factory interface.
func (f *factory) NewSkipHook(hookInfo hook.Info) (Operation, error) {
hookOp, err := f.NewRunHook(hookInfo)
if err != nil {
return nil, err
}
return f.newResolved(&skipOperation{hookOp})
}
// NewAction is part of the Factory interface.
func (f *factory) NewAction(actionId string) (Operation, error) {
if !names.IsValidAction(actionId) {
return nil, errors.Errorf("invalid action id %q", actionId)
}
return &runAction{
actionId: actionId,
callbacks: f.callbacks,
runnerFactory: f.runnerFactory,
}, nil
}
// NewCommands is part of the Factory interface.
func (f *factory) NewCommands(args CommandArgs, sendResponse CommandResponseFunc) (Operation, error) {
if args.Commands == "" {
return nil, errors.New("commands required")
} else if sendResponse == nil {
return nil, errors.New("response sender required")
}
if args.RemoteUnitName != "" {
if args.RelationId == -1 {
return nil, errors.New("remote unit not valid without relation")
} else if !names.IsValidUnit(args.RemoteUnitName) {
return nil, errors.Errorf("invalid remote unit name %q", args.RemoteUnitName)
}
}
return &runCommands{
args: args,
sendResponse: sendResponse,
callbacks: f.callbacks,
runnerFactory: f.runnerFactory,
}, nil
}
// NewUpdateRelations is part of the Factory interface.
func (f *factory) NewUpdateRelations(ids []int) (Operation, error) {
return &updateRelations{
ids: ids,
callbacks: f.callbacks,
}, nil
}
// NewUpdateStorage is part of the Factory interface.
func (f *factory) NewUpdateStorage(tags []names.StorageTag) (Operation, error) {
return &updateStorage{
tags: tags,
storageUpdater: f.storageUpdater,
}, nil
}
// NewResignLeadership is part of the Factory interface.
func (f *factory) NewResignLeadership() (Operation, error) {
return &resignLeadership{}, nil
}
// NewAcceptLeadership is part of the Factory interface.
func (f *factory) NewAcceptLeadership() (Operation, error) {
return &acceptLeadership{}, nil
}