/
environ_broker.go
326 lines (288 loc) · 10.8 KB
/
environ_broker.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package gce
import (
"strings"
"github.com/juju/errors"
"github.com/juju/juju/cloudconfig/instancecfg"
"github.com/juju/juju/cloudconfig/providerinit"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/os/ostype"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/environs/instances"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/provider/gce/google"
)
// StartInstance implements environs.InstanceBroker.
func (env *environ) StartInstance(ctx context.ProviderCallContext, args environs.StartInstanceParams) (*environs.StartInstanceResult, error) {
// Start a new instance.
spec, err := buildInstanceSpec(env, ctx, args)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
if err := env.finishInstanceConfig(args, spec); err != nil {
return nil, environs.ZoneIndependentError(err)
}
// Validate availability zone.
volumeAttachmentsZone, err := volumeAttachmentsZone(args.VolumeAttachments)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
if err := validateAvailabilityZoneConsistency(args.AvailabilityZone, volumeAttachmentsZone); err != nil {
return nil, errors.Trace(err)
}
raw, err := newRawInstance(env, ctx, args, spec)
if err != nil {
return nil, errors.Trace(err)
}
logger.Infof("started instance %q in zone %q", raw.ID, raw.ZoneName)
inst := newInstance(raw, env)
// Build the result.
hwc := getHardwareCharacteristics(env, spec, inst)
result := environs.StartInstanceResult{
Instance: inst,
Hardware: hwc,
}
return &result, nil
}
var buildInstanceSpec = func(env *environ, ctx context.ProviderCallContext, args environs.StartInstanceParams) (*instances.InstanceSpec, error) {
return env.buildInstanceSpec(ctx, args)
}
var newRawInstance = func(env *environ, ctx context.ProviderCallContext, args environs.StartInstanceParams, spec *instances.InstanceSpec) (*google.Instance, error) {
return env.newRawInstance(ctx, args, spec)
}
var getHardwareCharacteristics = func(env *environ, spec *instances.InstanceSpec, inst *environInstance) *instance.HardwareCharacteristics {
return env.getHardwareCharacteristics(spec, inst)
}
// finishInstanceConfig updates args.InstanceConfig in place. Setting up
// the API, StateServing, and SSHkeys information.
func (env *environ) finishInstanceConfig(args environs.StartInstanceParams, spec *instances.InstanceSpec) error {
if err := args.InstanceConfig.SetTools(args.Tools); err != nil {
return errors.Trace(err)
}
return instancecfg.FinishInstanceConfig(args.InstanceConfig, env.Config())
}
// buildInstanceSpec builds an instance spec from the provided args
// and returns it. This includes pulling the simplestreams data for the
// machine type, region, and other constraints.
func (env *environ) buildInstanceSpec(ctx context.ProviderCallContext, args environs.StartInstanceParams) (*instances.InstanceSpec, error) {
instTypesAndCosts, err := env.InstanceTypes(ctx, constraints.Value{})
if err != nil {
return nil, errors.Trace(err)
}
arch, err := args.Tools.OneArch()
if err != nil {
return nil, errors.Trace(err)
}
spec, err := findInstanceSpec(
env, &instances.InstanceConstraint{
Region: env.cloud.Region,
Base: args.InstanceConfig.Base,
Arch: arch,
Constraints: args.Constraints,
},
args.ImageMetadata,
instTypesAndCosts.InstanceTypes,
)
return spec, errors.Trace(err)
}
var findInstanceSpec = func(
env *environ,
ic *instances.InstanceConstraint,
imageMetadata []*imagemetadata.ImageMetadata,
allInstanceTypes []instances.InstanceType,
) (*instances.InstanceSpec, error) {
return env.findInstanceSpec(ic, imageMetadata, allInstanceTypes)
}
// findInstanceSpec initializes a new instance spec for the given
// constraints and returns it. This only covers populating the
// initial data for the spec.
func (env *environ) findInstanceSpec(
ic *instances.InstanceConstraint,
imageMetadata []*imagemetadata.ImageMetadata,
allInstanceTypes []instances.InstanceType,
) (*instances.InstanceSpec, error) {
images := instances.ImageMetadataToImages(imageMetadata)
spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes)
return spec, errors.Trace(err)
}
func (env *environ) imageURLBase(os ostype.OSType) (string, error) {
base, useCustomPath := env.ecfg.baseImagePath()
if useCustomPath {
return base, nil
}
switch os {
case ostype.Ubuntu:
switch env.Config().ImageStream() {
case "daily":
base = ubuntuDailyImageBasePath
case "pro":
base = ubuntuProImageBasePath
default:
base = ubuntuImageBasePath
}
default:
return "", errors.Errorf("os %s is not supported on the gce provider", os.String())
}
return base, nil
}
// newRawInstance is where the new physical instance is actually
// provisioned, relative to the provided args and spec. Info for that
// low-level instance is returned.
func (env *environ) newRawInstance(
ctx context.ProviderCallContext, args environs.StartInstanceParams, spec *instances.InstanceSpec,
) (_ *google.Instance, err error) {
hostname, err := env.namespace.Hostname(args.InstanceConfig.MachineId)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
os := ostype.OSTypeForName(args.InstanceConfig.Base.OS)
metadata, err := getMetadata(args, os)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
tags := []string{
env.globalFirewallName(),
hostname,
}
imageURLBase, err := env.imageURLBase(os)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
disks, err := getDisks(
spec, args.Constraints,
os,
env.Config().UUID(),
imageURLBase,
)
if err != nil {
return nil, environs.ZoneIndependentError(err)
}
allocatePublicIP := true
if args.Constraints.HasAllocatePublicIP() {
allocatePublicIP = *args.Constraints.AllocatePublicIP
}
inst, err := env.gce.AddInstance(google.InstanceSpec{
ID: hostname,
Type: spec.InstanceType.Name,
Disks: disks,
NetworkInterfaces: []string{"ExternalNAT"},
Metadata: metadata,
Tags: tags,
AvailabilityZone: args.AvailabilityZone,
AllocatePublicIP: allocatePublicIP,
})
if err != nil {
// We currently treat all AddInstance failures
// as being zone-specific, so we'll retry in
// another zone.
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
return inst, nil
}
// getMetadata builds the raw "user-defined" metadata for the new
// instance (relative to the provided args) and returns it.
func getMetadata(args environs.StartInstanceParams, os ostype.OSType) (map[string]string, error) {
userData, err := providerinit.ComposeUserData(args.InstanceConfig, nil, GCERenderer{})
if err != nil {
return nil, errors.Annotate(err, "cannot make user data")
}
logger.Debugf("GCE user data; %d bytes", len(userData))
metadata := make(map[string]string)
for tag, value := range args.InstanceConfig.Tags {
metadata[tag] = value
}
switch os {
case ostype.Ubuntu:
// We store a gz snapshop of information that is used by
// cloud-init and unpacked in to the /var/lib/cloud/instances folder
// for the instance. Due to a limitation with GCE and binary blobs
// we base64 encode the data before storing it.
metadata[metadataKeyCloudInit] = string(userData)
// Valid encoding values are determined by the cloudinit GCE data source.
// See: http://cloudinit.readthedocs.org
metadata[metadataKeyEncoding] = "base64"
default:
return nil, errors.Errorf("cannot pack metadata for os %s on the gce provider", os.String())
}
return metadata, nil
}
// getDisks builds the raw spec for the disks that should be attached to
// the new instances and returns it. This will always include a root
// disk with characteristics determined by the provides args and
// constraints.
func getDisks(spec *instances.InstanceSpec, cons constraints.Value, os ostype.OSType, eUUID string, imageURLBase string) ([]google.DiskSpec, error) {
size := common.MinRootDiskSizeGiB(os)
if cons.RootDisk != nil && *cons.RootDisk > size {
size = common.MiBToGiB(*cons.RootDisk)
}
if imageURLBase == "" {
return nil, errors.NotValidf("imageURLBase must be set")
}
imageURL := imageURLBase + spec.Image.Id
logger.Infof("fetching disk image from %v", imageURL)
dSpec := google.DiskSpec{
OS: strings.ToLower(os.String()),
SizeHintGB: size,
ImageURL: imageURL,
Boot: true,
AutoDelete: true,
}
if cons.RootDisk != nil && dSpec.TooSmall() {
msg := "Ignoring root-disk constraint of %dM because it is smaller than the GCE image size of %dG"
logger.Infof(msg, *cons.RootDisk, google.MinDiskSizeGB)
}
return []google.DiskSpec{dSpec}, nil
}
// getHardwareCharacteristics compiles hardware-related details about
// the given instance and relative to the provided spec and returns it.
func (env *environ) getHardwareCharacteristics(spec *instances.InstanceSpec, inst *environInstance) *instance.HardwareCharacteristics {
rootDiskMB := inst.base.RootDiskGB() * 1024
hwc := instance.HardwareCharacteristics{
Arch: &spec.Image.Arch,
Mem: &spec.InstanceType.Mem,
CpuCores: &spec.InstanceType.CpuCores,
CpuPower: spec.InstanceType.CpuPower,
RootDisk: &rootDiskMB,
AvailabilityZone: &inst.base.ZoneName,
// Tags: not supported in GCE.
}
return &hwc
}
// AllInstances implements environs.InstanceBroker.
func (env *environ) AllInstances(ctx context.ProviderCallContext) ([]instances.Instance, error) {
// We want all statuses here except for "terminated" - these instances are truly dead to us.
// According to https://cloud.google.com/compute/docs/instances/instance-life-cycle
// there are now only "provisioning", "staging", "running", "stopping" and "terminated" states.
// The others might have been needed for older versions of gce... Keeping here for potential
// backward compatibility.
nonLiveStatuses := []string{
google.StatusDone,
google.StatusDown,
google.StatusProvisioning,
google.StatusStopped,
google.StatusStopping,
google.StatusUp,
}
filters := append(instStatuses, nonLiveStatuses...)
instances, err := getInstances(env, ctx, filters...)
return instances, errors.Trace(err)
}
// AllRunningInstances implements environs.InstanceBroker.
func (env *environ) AllRunningInstances(ctx context.ProviderCallContext) ([]instances.Instance, error) {
instances, err := getInstances(env, ctx)
return instances, errors.Trace(err)
}
// StopInstances implements environs.InstanceBroker.
func (env *environ) StopInstances(ctx context.ProviderCallContext, instances ...instance.Id) error {
var ids []string
for _, id := range instances {
ids = append(ids, string(id))
}
prefix := env.namespace.Prefix()
err := env.gce.RemoveInstances(prefix, ids...)
return google.HandleCredentialError(errors.Trace(err), ctx)
}