forked from juju/juju
/
deploy.go
225 lines (205 loc) · 6.8 KB
/
deploy.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/juju/errors"
"gopkg.in/juju/charm.v6"
csparams "gopkg.in/juju/charmrepo.v3/csclient/params"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/core/application"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/devices"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/state"
"github.com/juju/juju/storage"
)
// DeployApplicationParams contains the arguments required to deploy the referenced
// charm.
type DeployApplicationParams struct {
ApplicationName string
Series string
Charm *state.Charm
Channel csparams.Channel
ApplicationConfig *application.Config
CharmConfig charm.Settings
Constraints constraints.Value
NumUnits int
// Placement is a list of placement directives which may be used
// instead of a machine spec.
Placement []*instance.Placement
Storage map[string]storage.Constraints
Devices map[string]devices.Constraints
AttachStorage []names.StorageTag
EndpointBindings map[string]string
// Resources is a map of resource name to IDs of pending resources.
Resources map[string]string
}
type ApplicationDeployer interface {
AddApplication(state.AddApplicationArgs) (Application, error)
}
type UnitAdder interface {
AddUnit(state.AddUnitParams) (Unit, error)
}
// DeployApplication takes a charm and various parameters and deploys it.
func DeployApplication(st ApplicationDeployer, args DeployApplicationParams) (Application, error) {
charmConfig, err := args.Charm.Config().ValidateSettings(args.CharmConfig)
if err != nil {
return nil, errors.Trace(err)
}
if args.Charm.Meta().Subordinate {
if args.NumUnits != 0 {
return nil, fmt.Errorf("subordinate application must be deployed without units")
}
if !constraints.IsEmpty(&args.Constraints) {
return nil, fmt.Errorf("subordinate application must be deployed without constraints")
}
}
// TODO(fwereade): transactional State.AddApplication including settings, constraints
// (minimumUnitCount, initialMachineIds?).
effectiveBindings, err := getEffectiveBindingsForCharmMeta(args.Charm.Meta(), args.EndpointBindings)
if err != nil {
return nil, errors.Trace(err)
}
asa := state.AddApplicationArgs{
Name: args.ApplicationName,
Series: args.Series,
Charm: args.Charm,
Channel: args.Channel,
Storage: stateStorageConstraints(args.Storage),
Devices: stateDeviceConstraints(args.Devices),
AttachStorage: args.AttachStorage,
ApplicationConfig: args.ApplicationConfig,
CharmConfig: charmConfig,
NumUnits: args.NumUnits,
Placement: args.Placement,
Resources: args.Resources,
EndpointBindings: effectiveBindings,
}
if !args.Charm.Meta().Subordinate {
asa.Constraints = args.Constraints
}
return st.AddApplication(asa)
}
func quoteStrings(vals []string) string {
out := make([]string, len(vals))
for i, val := range vals {
out[i] = strconv.Quote(val)
}
return strings.Join(out, ", ")
}
func validateGivenBindings(givenBindings map[string]string, defaultBindings map[string]string) error {
invalidBindings := make([]string, 0)
for name := range givenBindings {
if name == "" {
continue
}
if _, ok := defaultBindings[name]; !ok {
invalidBindings = append(invalidBindings, name)
}
}
if len(invalidBindings) == 0 {
return nil
}
possibleBindings := make([]string, 0)
for name := range defaultBindings {
if name == "" {
continue
}
possibleBindings = append(possibleBindings, name)
}
sort.Strings(invalidBindings)
sort.Strings(possibleBindings)
return errors.Errorf("invalid binding(s) supplied %s, valid binding names are %s",
quoteStrings(invalidBindings), quoteStrings(possibleBindings))
}
func getEffectiveBindingsForCharmMeta(charmMeta *charm.Meta, givenBindings map[string]string) (map[string]string, error) {
// defaultBindings contains all bindable endpoints for charmMeta as keys and
// empty space names as values, so we use defaultBindings as fallback.
defaultBindings := state.DefaultEndpointBindingsForCharm(charmMeta)
if givenBindings == nil {
givenBindings = make(map[string]string, len(defaultBindings))
}
if err := validateGivenBindings(givenBindings, defaultBindings); err != nil {
return nil, err
}
// Get the application-level default binding for all unspecified endpoints, if
// set. Otherwise use the empty default.
applicationDefaultSpace, defaultSupplied := givenBindings[""]
if defaultSupplied {
// Record that a default binding was requested
defaultBindings[""] = applicationDefaultSpace
}
effectiveBindings := make(map[string]string, len(defaultBindings))
for endpoint := range defaultBindings {
if givenSpace, isGiven := givenBindings[endpoint]; isGiven {
effectiveBindings[endpoint] = givenSpace
} else {
effectiveBindings[endpoint] = applicationDefaultSpace
}
}
return effectiveBindings, nil
}
// addUnits starts n units of the given application using the specified placement
// directives to allocate the machines.
func addUnits(
unitAdder UnitAdder,
appName string,
n int,
placement []*instance.Placement,
attachStorage []names.StorageTag,
assignUnits bool,
) ([]Unit, error) {
units := make([]Unit, n)
// Hard code for now till we implement a different approach.
policy := state.AssignCleanEmpty
// TODO what do we do if we fail half-way through this process?
for i := 0; i < n; i++ {
unit, err := unitAdder.AddUnit(state.AddUnitParams{
AttachStorage: attachStorage,
})
if err != nil {
return nil, errors.Annotatef(err, "cannot add unit %d/%d to application %q", i+1, n, appName)
}
units[i] = unit
if !assignUnits {
continue
}
// Are there still placement directives to use?
if i > len(placement)-1 {
if err := unit.AssignWithPolicy(policy); err != nil {
return nil, errors.Trace(err)
}
continue
}
if err := unit.AssignWithPlacement(placement[i]); err != nil {
return nil, errors.Annotatef(err, "acquiring machine to host unit %q", unit.UnitTag().Id())
}
}
return units, nil
}
func stateStorageConstraints(cons map[string]storage.Constraints) map[string]state.StorageConstraints {
result := make(map[string]state.StorageConstraints)
for name, cons := range cons {
result[name] = state.StorageConstraints{
Pool: cons.Pool,
Size: cons.Size,
Count: cons.Count,
}
}
return result
}
func stateDeviceConstraints(cons map[string]devices.Constraints) map[string]state.DeviceConstraints {
result := make(map[string]state.DeviceConstraints)
for name, cons := range cons {
result[name] = state.DeviceConstraints{
Type: state.DeviceType(cons.Type),
Count: cons.Count,
Attributes: cons.Attributes,
}
}
return result
}