-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CreateGoogleInstanceAtomicOperation.groovy
146 lines (117 loc) · 6.53 KB
/
CreateGoogleInstanceAtomicOperation.groovy
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
/*
* Copyright 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.clouddriver.google.deploy.ops
import com.google.api.services.compute.model.Instance
import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.deploy.DeploymentResult
import com.netflix.spinnaker.clouddriver.google.GoogleConfiguration
import com.netflix.spinnaker.clouddriver.google.config.GoogleConfigurationProperties
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil
import com.netflix.spinnaker.clouddriver.google.deploy.description.CreateGoogleInstanceDescription
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleNetworkProvider
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleSubnetProvider
import org.springframework.beans.factory.annotation.Autowired
class CreateGoogleInstanceAtomicOperation extends GoogleAtomicOperation<DeploymentResult> {
private static final String BASE_PHASE = "DEPLOY"
// TODO(duftler): These should be exposed/configurable.
private static final String DEFAULT_NETWORK_NAME = "default"
private static final String accessConfigName = "External NAT"
private static final String accessConfigType = "ONE_TO_ONE_NAT"
@Autowired
private GoogleConfigurationProperties googleConfigurationProperties
@Autowired
private GoogleConfiguration.DeployDefaults googleDeployDefaults
@Autowired
GoogleNetworkProvider googleNetworkProvider
@Autowired
GoogleSubnetProvider googleSubnetProvider
@Autowired
String clouddriverUserAgentApplicationName
private static Task getTask() {
TaskRepository.threadLocalTask.get()
}
private final CreateGoogleInstanceDescription description
CreateGoogleInstanceAtomicOperation(CreateGoogleInstanceDescription description) {
this.description = description
}
/**
* curl -X POST -H "Content-Type: application/json" -d '[ { "createGoogleInstanceDescription": { "instanceName": "myapp-dev-v000-abcd", "image": "ubuntu-1404-trusty-v20160509a", "instanceType": "f1-micro", "zone": "us-central1-f", "credentials": "my-account-name" }} ]' localhost:7002/ops
*/
@Override
DeploymentResult operate(List priorOutputs) {
task.updateStatus BASE_PHASE, "Initializing creation of instance $description.instanceName in $description.zone..."
if (!description.credentials) {
throw new IllegalArgumentException("Unable to resolve credentials for Google account '${description.accountName}'.")
}
def accountName = description.accountName
def credentials = description.credentials
def compute = description.credentials.compute
def project = description.credentials.project
def zone = description.zone
def region = credentials.regionFromZone(zone)
def machineTypeName
if (description.instanceType.startsWith('custom')) {
machineTypeName = description.instanceType
} else {
machineTypeName = GCEUtil.queryMachineType(description.instanceType, zone, credentials, task, BASE_PHASE)
}
def sourceImage = GCEUtil.queryImage(project,
description.image,
credentials,
compute,
task,
BASE_PHASE,
clouddriverUserAgentApplicationName,
googleConfigurationProperties.baseImageProjects,
this)
def network = GCEUtil.queryNetwork(accountName, description.network ?: DEFAULT_NETWORK_NAME, task, BASE_PHASE, googleNetworkProvider)
def subnet =
description.subnet ? GCEUtil.querySubnet(accountName, region, description.subnet, task, BASE_PHASE, googleSubnetProvider) : null
task.updateStatus BASE_PHASE, "Composing instance..."
def attachedDisks = GCEUtil.buildAttachedDisks(project,
zone,
sourceImage,
description.disks,
true,
description.instanceType,
googleDeployDefaults)
def networkInterface = GCEUtil.buildNetworkInterface(network,
subnet,
description.associatePublicIpAddress == null || description.associatePublicIpAddress,
accessConfigName,
accessConfigType)
def metadata = GCEUtil.buildMetadataFromMap(description.instanceMetadata)
def tags = GCEUtil.buildTagsFromList(description.tags)
def serviceAccount = GCEUtil.buildServiceAccount(description.serviceAccountEmail, description.authScopes)
def scheduling = GCEUtil.buildScheduling(description)
def instance = new Instance(name: description.instanceName,
machineType: "zones/$zone/machineTypes/$machineTypeName",
disks: attachedDisks,
networkInterfaces: [networkInterface],
metadata: metadata,
tags: tags,
labels: description.labels,
scheduling: scheduling,
serviceAccounts: serviceAccount)
task.updateStatus BASE_PHASE, "Creating instance $description.instanceName..."
timeExecute(compute.instances().insert(project, zone, instance),
"compute.instances.insert",
TAG_SCOPE, SCOPE_ZONAL, TAG_ZONE, zone)
task.updateStatus BASE_PHASE, "Done creating instance $description.instanceName in $description.zone."
new DeploymentResult(serverGroupNames: [description.instanceName])
}
}