This repository has been archived by the owner on Oct 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
provision.go
347 lines (272 loc) · 10.6 KB
/
provision.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package upcloud
import (
"errors"
"time"
log "github.com/Sirupsen/logrus"
upcloud "github.com/Jalle19/upcloud-go-sdk/upcloud"
upcloud_request "github.com/Jalle19/upcloud-go-sdk/upcloud/request"
api_operation "github.com/wunderkraut/radi-api/operation"
api_property "github.com/wunderkraut/radi-api/property"
api_result "github.com/wunderkraut/radi-api/result"
api_provision "github.com/wunderkraut/radi-api/operation/provision"
)
/**
* Functionality for provisioning
*/
/**
* HANDLER
*/
// UpCloud Provisioning Handler
type UpcloudProvisionHandler struct {
BaseUpcloudServiceHandler
}
// Initialize and activate the Handler
func (provision *UpcloudProvisionHandler) Operations() api_operation.Operations {
baseOperation := provision.BaseUpcloudServiceOperation()
ops := api_operation.New_SimpleOperations()
ops.Add(api_operation.Operation(&UpcloudProvisionUpOperation{BaseUpcloudServiceOperation: *baseOperation}))
ops.Add(api_operation.Operation(&UpcloudProvisionStopOperation{BaseUpcloudServiceOperation: *baseOperation}))
ops.Add(api_operation.Operation(&UpcloudProvisionDownOperation{BaseUpcloudServiceOperation: *baseOperation}))
return ops.Operations()
}
// Rturn a string identifier for the Handler (not functionally needed yet)
func (provision *UpcloudProvisionHandler) Id() string {
return "upcloud.provision"
}
/**
* OPERATIONS
*/
// Provision up operation
type UpcloudProvisionUpOperation struct {
BaseUpcloudServiceOperation
api_provision.BaseProvisionUpOperation
}
// Return the string machinename/id of the Operation
func (up *UpcloudProvisionUpOperation) Id() string {
return "upcloud.provision.up"
}
// Return a user readable string label for the Operation
func (up *UpcloudProvisionUpOperation) Label() string {
return "Provision UpCloud servers"
}
// return a multiline string description for the Operation
func (up *UpcloudProvisionUpOperation) Description() string {
return "Provision the UpCloud servers for this project."
}
// Run a validation check on the Operation
func (up *UpcloudProvisionUpOperation) Validate() api_result.Result {
return api_result.MakeSuccessfulResult()
}
// What settings/values does the Operation provide to an implemenentor
func (up *UpcloudProvisionUpOperation) Properties() api_property.Properties {
return api_property.New_SimplePropertiesEmpty().Properties()
}
/**
* Execute the Operation
*
* The following steps are followed for each server:
* 1. create the server - then wait for it to be considered running
* 2. create the firewall rules
* 3. tag the server
*
* @TODO build properties properly from the child operations
* @TODO This operation should operate in parrallel
*/
func (up *UpcloudProvisionUpOperation) Exec(props api_property.Properties) api_result.Result {
res := api_result.New_StandardResult()
createOp := UpcloudServerCreateOperation{BaseUpcloudServiceOperation: up.BaseUpcloudServiceOperation}
createProperties := createOp.Properties()
service := up.ServiceWrapper()
// settings := up.BuilderSettings()
serverDefinitions := up.ServerDefinitions()
// track which servers we actually create here
createdServers := map[string]processedServer{}
for _, id := range serverDefinitions.Order() {
serverDefinition, _ := serverDefinitions.Get(id)
createRequest := serverDefinition.CreateServerRequest()
if requestProp, found := createProperties.Get(UPCLOUD_SERVER_CREATEREQUEST_PROPERTY); found {
requestProp.Set(createRequest)
}
log.WithFields(log.Fields{"id": serverDefinition.Id()}).Info("Creating new server")
createResult := createOp.Exec(createProperties)
<-createResult.Finished()
if !createResult.Success() {
res.AddErrors(createResult.Errors())
res.AddError(errors.New("Could not provision new UpCloud server: " + id))
res.MarkFailed()
continue
} else {
var createDetails upcloud.ServerDetails
if detailsProp, found := createProperties.Get(UPCLOUD_SERVER_DETAILS_PROPERTY); found {
createDetails = detailsProp.Get().(upcloud.ServerDetails)
}
uuid := createDetails.UUID
createdServers[id] = processedServer{
uuid: uuid,
definition: serverDefinition,
details: createDetails,
}
log.WithFields(log.Fields{"id": serverDefinition.Id(), "UUID": uuid, "state": createDetails.State}).Info("Created new server")
}
}
firewallOp := UpcloudServerApplyFirewallRulesOperation{BaseUpcloudServiceOperation: up.BaseUpcloudServiceOperation}
firewallProperties := firewallOp.Properties()
// process tags and firewall rules
for _, createdServer := range createdServers {
uuid := createdServer.uuid
serverDefinition := createdServer.definition
// Before running anything, give the server a chance to get into the proper state
log.WithFields(log.Fields{"id": serverDefinition.Id(), "UUID": uuid}).Info("Waiting for new server to start")
if serverDetails, err := service.WaitForServerState(&upcloud_request.WaitForServerStateRequest{UUID: uuid, UndesiredState: "maintenance", Timeout: time.Minute * 2}); err != nil {
if serverDetails != nil {
uuid = serverDetails.UUID
}
res.AddError(err)
res.AddError(errors.New("Server failed to start properly : " + uuid))
res.MarkFailed()
} else {
log.WithFields(log.Fields{"state": serverDetails.State, "UUID": serverDetails.UUID}).Info("Server successfully created, now finalizing provisioning")
serverDefinition := createdServer.definition
firewallRules := serverDefinition.GetFirewallRules()
if firewallProp, found := firewallProperties.Get(UPCLOUD_FIREWALL_RULES_PROPERTY); found {
firewallProp.Set(firewallRules)
}
if uuidProp, found := firewallProperties.Get(UPCLOUD_SERVER_UUID_PROPERTY); found {
uuidProp.Set(uuid)
}
firewallResult := firewallOp.Exec(firewallProperties)
<-firewallResult.Finished()
if !firewallResult.Success() {
res.Merge(firewallResult)
continue
}
// var serverDetails upcloud.ServerDetails
// if detailsProp, found := createProperties.Get(UPCLOUD_SERVER_DETAILS_PROPERTY); found {
// serverDetails = detailsProp.Get().(upcloud.ServerDetails)
// }
}
}
res.MarkFinished()
return res.Result()
}
// Provision up operation
type UpcloudProvisionDownOperation struct {
BaseUpcloudServiceOperation
api_provision.BaseProvisionDownOperation
}
// Return the string machinename/id of the Operation
func (down *UpcloudProvisionDownOperation) Id() string {
return "upcloud.provision.down"
}
// Return a user readable string label for the Operation
func (down *UpcloudProvisionDownOperation) Label() string {
return "Remove UpCloud servers"
}
// return a multiline string description for the Operation
func (down *UpcloudProvisionDownOperation) Description() string {
return "Remove the UpCloud servers for this project."
}
// Run a validation check on the Operation
func (down *UpcloudProvisionDownOperation) Validate() api_result.Result {
return api_result.MakeSuccessfulResult()
}
// What settings/values does the Operation provide to an implemenentor
func (down *UpcloudProvisionDownOperation) Properties() api_property.Properties {
props := api_property.New_SimplePropertiesEmpty()
props.Add(api_property.Property(&UpcloudForceProperty{}))
return props.Properties()
}
// Execute the Operation
//
// @TODO Add a way to remove the storage
// @TODO this operation could be optimized to work parrallel
func (down *UpcloudProvisionDownOperation) Exec(props api_property.Properties) api_result.Result {
res := api_result.New_StandardResult()
downProperties := down.Properties()
deleteOp := UpcloudServerDeleteOperation{BaseUpcloudServiceOperation: down.BaseUpcloudServiceOperation}
deleteProperties := deleteOp.Properties()
// service := down.ServiceWrapper()
// settings := down.BuilderSettings()
serverDefinitions := down.ServerDefinitions()
// collect UUIDs of project servers
uuids := []string{}
for _, id := range serverDefinitions.Order() {
serverDefinition, _ := serverDefinitions.Get(id)
if serverDefinition.IsCreated() {
uuid, _ := serverDefinition.UUID()
log.WithFields(log.Fields{"id": id, "uuid": uuid}).Debug("Down: Server added to list")
uuids = append(uuids, uuid)
} else {
log.WithFields(log.Fields{"id": id}).Info("Down: Server has not been created, so it will be skipped")
}
}
if len(uuids) > 0 {
if uuidsProp, found := deleteProperties.Get(UPCLOUD_SERVER_UUIDS_PROPERTY); found {
log.WithFields(log.Fields{"uuids": uuids}).Info("DOWN: Using UUIDs")
uuidsProp.Set(uuids)
}
if downForceProp, found := downProperties.Get(UPCLOUD_FORCE_PROPERTY); found {
if deleteForceProp, found := deleteProperties.Get(UPCLOUD_FORCE_PROPERTY); found {
if downForceProp.Get().(bool) {
log.Info("DOWN: Forcing operation")
deleteForceProp.Set(true)
}
}
}
log.WithFields(log.Fields{"uuids": uuids}).Info("Downing project servers")
downResult := deleteOp.Exec(downProperties)
<-downResult.Finished()
res.Merge(downResult)
} else {
log.Info("No active servers found to take down.")
}
res.MarkFinished()
return res.Result()
}
// Provision up operation
type UpcloudProvisionStopOperation struct {
BaseUpcloudServiceOperation
api_provision.BaseProvisionStopOperation
}
// Return the string machinename/id of the Operation
func (stop *UpcloudProvisionStopOperation) Id() string {
return "upcloud.provision.stop"
}
// Return a user readable string label for the Operation
func (stop *UpcloudProvisionStopOperation) Label() string {
return "Stop UpCloud servers"
}
// return a multiline string description for the Operation
func (stop *UpcloudProvisionStopOperation) Description() string {
return "Stop the UpCloud servers for this project."
}
// Run a validation check on the Operation
func (stop *UpcloudProvisionStopOperation) Validate() api_result.Result {
return api_result.MakeSuccessfulResult()
}
// What settings/values does the Operation provide to an implemenentor
func (stop *UpcloudProvisionStopOperation) Properties() api_property.Properties {
props := api_property.New_SimplePropertiesEmpty()
props.Add(api_property.Property(&UpcloudGlobalProperty{}))
props.Add(api_property.Property(&UpcloudWaitProperty{}))
props.Add(api_property.Property(&UpcloudServerUUIDProperty{}))
return props.Properties()
}
// Execute the Operation
/**
* @NOTE this is a first version.
*
* We will want to :
* 1. retrieve servers by tag
* 2. have a "remove-specific-uuid" option?
*/
func (stop *UpcloudProvisionStopOperation) Exec(props api_property.Properties) api_result.Result {
res := api_result.New_StandardResult()
return res.Result()
}
// hold info about a server that we have processed
type processedServer struct {
uuid string
definition ServerDefinition
details upcloud.ServerDetails
}