forked from juju/juju
/
environinstance.go
178 lines (146 loc) · 5.36 KB
/
environinstance.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloudsigma
import (
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/juju/cloudconfig/instancecfg"
"github.com/juju/juju/cloudconfig/providerinit"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/instance"
"github.com/juju/juju/tools"
)
//
// Imlementation of InstanceBroker: methods for starting and stopping instances.
//
var findInstanceImage = func(
matchingImages []*imagemetadata.ImageMetadata,
) (*imagemetadata.ImageMetadata, error) {
if len(matchingImages) == 0 {
return nil, errors.New("no matching image meta data")
}
return matchingImages[0], nil
}
// MaintainInstance is specified in the InstanceBroker interface.
func (*environ) MaintainInstance(args environs.StartInstanceParams) error {
return nil
}
// StartInstance asks for a new instance to be created, associated with
// the provided config in machineConfig. The given config describes the juju
// state for the new instance to connect to. The config MachineNonce, which must be
// unique within an environment, is used by juju to protect against the
// consequences of multiple instances being started with the same machine id.
func (env *environ) StartInstance(args environs.StartInstanceParams) (*environs.StartInstanceResult, error) {
logger.Infof("sigmaEnviron.StartInstance...")
if args.InstanceConfig == nil {
return nil, errors.New("instance configuration is nil")
}
if len(args.Tools) == 0 {
return nil, errors.New("tools not found")
}
img, err := findInstanceImage(args.ImageMetadata)
if err != nil {
return nil, err
}
tools, err := args.Tools.Match(tools.Filter{Arch: img.Arch})
if err != nil {
return nil, errors.Errorf("chosen architecture %v not present in %v", img.Arch, args.Tools.Arches())
}
if err := args.InstanceConfig.SetTools(tools); err != nil {
return nil, errors.Trace(err)
}
if err := instancecfg.FinishInstanceConfig(args.InstanceConfig, env.Config()); err != nil {
return nil, err
}
userData, err := providerinit.ComposeUserData(args.InstanceConfig, nil, CloudSigmaRenderer{})
if err != nil {
return nil, errors.Annotate(err, "cannot make user data")
}
logger.Debugf("cloudsigma user data; %d bytes", len(userData))
client := env.client
cfg := env.Config()
server, rootdrive, arch, err := client.newInstance(args, img, userData, cfg.AuthorizedKeys())
if err != nil {
return nil, errors.Errorf("failed start instance: %v", err)
}
inst := &sigmaInstance{server: server}
// prepare hardware characteristics
hwch, err := inst.hardware(arch, rootdrive.Size())
if err != nil {
return nil, err
}
logger.Debugf("hardware: %v", hwch)
return &environs.StartInstanceResult{
Instance: inst,
Hardware: hwch,
}, nil
}
// AllInstances returns all instances currently known to the broker.
func (env *environ) AllInstances() ([]instance.Instance, error) {
// Please note that this must *not* return instances that have not been
// allocated as part of this environment -- if it does, juju will see they
// are not tracked in state, assume they're stale/rogue, and shut them down.
logger.Tracef("environ.AllInstances...")
servers, err := env.client.instances()
if err != nil {
logger.Tracef("environ.AllInstances failed: %v", err)
return nil, err
}
instances := make([]instance.Instance, 0, len(servers))
for _, server := range servers {
instance := sigmaInstance{server: server}
instances = append(instances, instance)
}
if logger.LogLevel() <= loggo.TRACE {
logger.Tracef("All instances, len = %d:", len(instances))
for _, instance := range instances {
logger.Tracef("... id: %q, status: %q", instance.Id(), instance.Status())
}
}
return instances, nil
}
// Instances returns a slice of instances corresponding to the
// given instance ids. If no instances were found, but there
// was no other error, it will return ErrNoInstances. If
// some but not all the instances were found, the returned slice
// will have some nil slots, and an ErrPartialInstances error
// will be returned.
func (env *environ) Instances(ids []instance.Id) ([]instance.Instance, error) {
logger.Tracef("environ.Instances %#v", ids)
// Please note that this must *not* return instances that have not been
// allocated as part of this environment -- if it does, juju will see they
// are not tracked in state, assume they're stale/rogue, and shut them down.
// This advice applies even if an instance id passed in corresponds to a
// real instance that's not part of the environment -- the Environ should
// treat that no differently to a request for one that does not exist.
m, err := env.client.instanceMap()
if err != nil {
return nil, errors.Annotate(err, "environ.Instances failed")
}
var found int
r := make([]instance.Instance, len(ids))
for i, id := range ids {
if s, ok := m[string(id)]; ok {
r[i] = sigmaInstance{server: s}
found++
}
}
if found == 0 {
err = environs.ErrNoInstances
} else if found != len(ids) {
err = environs.ErrPartialInstances
}
return r, errors.Trace(err)
}
// StopInstances shuts down the given instances.
func (env *environ) StopInstances(instances ...instance.Id) error {
logger.Debugf("stop instances %+v", instances)
var err error
for _, instance := range instances {
if e := env.client.stopInstance(instance); e != nil {
err = e
}
}
return err
}