/
GoogleComputeEngineServiceAdapter.java
339 lines (293 loc) · 15.1 KB
/
GoogleComputeEngineServiceAdapter.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.jclouds.googlecomputeengine.compute;
import static shaded.com.google.common.base.Preconditions.checkNotNull;
import static shaded.com.google.common.base.Preconditions.checkState;
import static shaded.com.google.common.collect.Iterables.filter;
import static shaded.com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static org.jclouds.googlecloud.internal.ListPages.concat;
import static org.jclouds.googlecomputeengine.config.GoogleComputeEngineProperties.IMAGE_PROJECTS;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.googlecomputeengine.GoogleComputeEngineApi;
import org.jclouds.googlecomputeengine.compute.functions.FirewallTagNamingConvention;
import org.jclouds.googlecomputeengine.compute.functions.Resources;
import org.jclouds.googlecomputeengine.compute.options.GoogleComputeEngineTemplateOptions;
import org.jclouds.googlecomputeengine.domain.AttachDisk;
import org.jclouds.googlecomputeengine.domain.DiskType;
import org.jclouds.googlecomputeengine.domain.Image;
import org.jclouds.googlecomputeengine.domain.Instance;
import org.jclouds.googlecomputeengine.domain.Instance.Scheduling;
import org.jclouds.googlecomputeengine.domain.Instance.Scheduling.OnHostMaintenance;
import org.jclouds.googlecomputeengine.domain.MachineType;
import org.jclouds.googlecomputeengine.domain.NewInstance;
import org.jclouds.googlecomputeengine.domain.Operation;
import org.jclouds.googlecomputeengine.domain.Region;
import org.jclouds.googlecomputeengine.domain.Tags;
import org.jclouds.googlecomputeengine.domain.Zone;
import org.jclouds.googlecomputeengine.features.InstanceApi;
import org.jclouds.location.suppliers.all.JustProvider;
import shaded.com.google.common.base.Function;
import shaded.com.google.common.base.Optional;
import shaded.com.google.common.base.Predicate;
import shaded.com.google.common.base.Splitter;
import shaded.com.google.common.base.Strings;
import shaded.com.google.common.cache.LoadingCache;
import shaded.com.google.common.collect.ImmutableList;
import shaded.com.google.common.collect.ImmutableMap;
import shaded.com.google.common.collect.Iterables;
import shaded.com.google.common.collect.Lists;
import shaded.com.google.common.util.concurrent.Atomics;
import shaded.com.google.common.util.concurrent.UncheckedTimeoutException;
/**
* This implementation maps the following:
* <ul>
* <li>{@linkplain NodeMetadata#getId()} to {@link Instance#selfLink()}</li>
* <li>{@linkplain NodeMetadata#getGroup()} to {@link Instance#metadata()} as {@code jclouds-group}</li>
* <li>{@linkplain NodeMetadata#getImageId()} to {@link Instance#metadata()} as {@code jclouds-image}</li>
* <li>{@linkplain Hardware#getId()} to {@link MachineType#selfLink()}</li>
* <li>{@linkplain org.jclouds.compute.domain.Image#getId()} to {@link Image#selfLink()}</li>
* <li>{@linkplain Location#getId()} to {@link org.jclouds.googlecomputeengine.domain.Zone#name()}</li>
* <li>{@linkplain Location#getDescription()} to {@link Zone#selfLink()}</li>
* </ul>
*/
public final class GoogleComputeEngineServiceAdapter
implements ComputeServiceAdapter<Instance, MachineType, Image, Location> {
private final JustProvider justProvider;
private final GoogleComputeEngineApi api;
private final Resources resources;
private final Predicate<AtomicReference<Operation>> operationDone;
private final Predicate<AtomicReference<Instance>> instanceVisible;
private final Function<Map<String, ?>, String> windowsPasswordGenerator;
private final FirewallTagNamingConvention.Factory firewallTagNamingConvention;
private final List<String> imageProjects;
private final LoadingCache<URI, Optional<Image>> diskURIToImage;
@Inject GoogleComputeEngineServiceAdapter(JustProvider justProvider, GoogleComputeEngineApi api,
Predicate<AtomicReference<Operation>> operationDone,
Predicate<AtomicReference<Instance>> instanceVisible,
Function<Map<String, ?>, String> windowsPasswordGenerator,
Resources resources,
FirewallTagNamingConvention.Factory firewallTagNamingConvention,
@Named(IMAGE_PROJECTS) String imageProjects,
LoadingCache<URI, Optional<Image>> diskURIToImage) {
this.justProvider = justProvider;
this.api = api;
this.operationDone = operationDone;
this.instanceVisible = instanceVisible;
this.windowsPasswordGenerator = windowsPasswordGenerator;
this.resources = resources;
this.firewallTagNamingConvention = firewallTagNamingConvention;
this.imageProjects = Splitter.on(',').omitEmptyStrings().splitToList(imageProjects);
this.diskURIToImage = diskURIToImage;
}
@Override public NodeAndInitialCredentials<Instance> createNodeWithGroupEncodedIntoName(String group, String name,
Template template) {
GoogleComputeEngineTemplateOptions options = GoogleComputeEngineTemplateOptions.class.cast(template.getOptions());
checkNotNull(options.getNetworks(), "template options must specify a network");
checkNotNull(template.getHardware().getUri(), "hardware must have a URI");
checkNotNull(template.getImage().getUri(), "image URI is null");
String zone = template.getLocation().getId();
List<AttachDisk> disks = Lists.newArrayList();
disks.add(AttachDisk.newBootDisk(template.getImage().getUri(), getDiskTypeArgument(options, zone)));
Iterator<String> networks = options.getNetworks().iterator();
URI network = URI.create(networks.next());
assert !networks.hasNext() : "Error: Options should specify only one network";
Scheduling scheduling = getScheduling(options);
NewInstance newInstance = new NewInstance.Builder( name,
template.getHardware().getUri(), // machineType
network,
disks)
.description(group)
.tags(Tags.create(null, ImmutableList.copyOf(options.getTags())))
.serviceAccounts(options.serviceAccounts())
.scheduling(scheduling)
.build();
// Add metadata from template and for ssh key and image id
newInstance.metadata().putAll(options.getUserMetadata());
LoginCredentials credentials = resolveNodeCredentials(template);
if (options.getPublicKey() != null) {
newInstance.metadata().put("sshKeys",
format("%s:%s %s@localhost", credentials.getUser(), options.getPublicKey(), credentials.getUser()));
}
InstanceApi instanceApi = api.instancesInZone(zone);
Operation create = instanceApi.create(newInstance);
// We need to see the created instance so that we can access the newly created disk.
AtomicReference<Instance> instance = Atomics.newReference(Instance.create( //
"0000000000000000000", // id can't be null, but isn't available until provisioning is done.
null, // creationTimestamp
create.targetLink(), // selfLink
newInstance.name(), // name
newInstance.description(), // description
newInstance.tags(), // tags
newInstance.machineType(), // machineType
Instance.Status.PROVISIONING, // status
null, // statusMessage
create.zone(), // zone
null, // canIpForward
null, // networkInterfaces
null, // disks
newInstance.metadata(), // metadata
newInstance.serviceAccounts(), // serviceAccounts
scheduling) // scheduling
);
checkState(instanceVisible.apply(instance), "instance %s is not api visible!", instance.get());
// Add lookup for InstanceToNodeMetadata
diskURIToImage.getUnchecked(instance.get().disks().get(0).source());
if ((options.autoCreateWindowsPassword() != null && options.autoCreateWindowsPassword())
|| OsFamily.WINDOWS == template.getImage().getOperatingSystem().getFamily()) {
Map<String, ?> params = ImmutableMap.of("instance", instance, "zone", zone, "email", create.user(), "userName", credentials.getUser());
String password = windowsPasswordGenerator.apply(params);
credentials = LoginCredentials.builder(credentials)
.password(password)
.build();
}
return new NodeAndInitialCredentials<Instance>(instance.get(), instance.get().selfLink().toString(), credentials);
}
@Override public Iterable<MachineType> listHardwareProfiles() {
return filter(concat(api.aggregatedList().machineTypes()), new Predicate<MachineType>() {
@Override
public boolean apply(MachineType input) {
return input.deprecated() == null;
}
});
}
@Override public Iterable<Image> listImages() {
List<Iterable<Image>> images = newArrayList();
images.add(concat(api.images().list()));
for (String project : imageProjects) {
images.add(concat(api.images().listInProject(project)));
}
return Iterables.concat(images);
}
@Override public Image getImage(String selfLink) {
return api.images().get(URI.create(checkNotNull(selfLink, "id")));
}
/** Unlike EC2, you cannot default GCE instances to a region. Hence, we constrain to zones. */
@Override public Iterable<Location> listLocations() {
Location provider = justProvider.get().iterator().next();
ImmutableList.Builder<Location> zones = ImmutableList.builder();
for (Region region : concat(api.regions().list())) {
Location regionLocation = new LocationBuilder()
.scope(LocationScope.REGION)
.id(region.name())
.description(region.selfLink().toString())
.parent(provider).build();
for (URI zoneSelfLink : region.zones()) {
String zoneName = toName(zoneSelfLink);
zones.add(new LocationBuilder()
.scope(LocationScope.ZONE)
.id(zoneName)
.description(zoneSelfLink.toString())
.parent(regionLocation).build());
}
}
return zones.build();
}
@Override public Instance getNode(String selfLink) {
return resources.instance(URI.create(checkNotNull(selfLink, "id")));
}
@Override public Iterable<Instance> listNodes() {
return concat(api.aggregatedList().instances());
}
@Override public Iterable<Instance> listNodesByIds(final Iterable<String> selfLinks) {
return filter(listNodes(), new Predicate<Instance>() { // TODO: convert to server-side filter
@Override public boolean apply(Instance instance) {
return Iterables.contains(selfLinks, instance.selfLink().toString());
}
});
}
@Override public void destroyNode(String selfLink) {
waitOperationDone(resources.delete(URI.create(checkNotNull(selfLink, "id"))));
}
@Override public void rebootNode(String selfLink) {
waitOperationDone(resources.resetInstance(URI.create(checkNotNull(selfLink, "id"))));
}
@Override public void resumeNode(String selfLink) {
waitOperationDone(resources.startInstance(URI.create(checkNotNull(selfLink, "id"))));
}
@Override public void suspendNode(String selfLink) {
waitOperationDone(resources.stopInstance(URI.create(checkNotNull(selfLink, "id"))));
}
private void waitOperationDone(Operation operation) {
AtomicReference<Operation> operationRef = Atomics.newReference(operation);
// wait for the operation to complete
if (!operationDone.apply(operationRef)) {
throw new UncheckedTimeoutException("operation did not reach DONE state" + operationRef.get());
}
// check if the operation failed
if (operationRef.get().httpErrorStatusCode() != null) {
throw new IllegalStateException(
"operation failed. Http Error Code: " + operationRef.get().httpErrorStatusCode() +
" HttpError: " + operationRef.get().httpErrorMessage());
}
}
private LoginCredentials resolveNodeCredentials(Template template) {
TemplateOptions options = template.getOptions();
LoginCredentials.Builder credentials = LoginCredentials.builder(template.getImage().getDefaultCredentials());
if (!Strings.isNullOrEmpty(options.getLoginUser())) {
credentials.user(options.getLoginUser());
}
if (!Strings.isNullOrEmpty(options.getLoginPrivateKey())) {
credentials.privateKey(options.getLoginPrivateKey());
}
if (!Strings.isNullOrEmpty(options.getLoginPassword())) {
credentials.password(options.getLoginPassword());
}
if (options.shouldAuthenticateSudo() != null) {
credentials.authenticateSudo(options.shouldAuthenticateSudo());
}
return credentials.build();
}
private static String toName(URI link) {
String path = link.getPath();
return path.substring(path.lastIndexOf('/') + 1);
}
private URI getDiskTypeArgument(GoogleComputeEngineTemplateOptions options, String zone) {
if (options.bootDiskType() != null) {
DiskType diskType = api.diskTypesInZone(zone).get(options.bootDiskType());
if (diskType != null) {
return diskType.selfLink();
}
}
return null;
}
public Scheduling getScheduling(GoogleComputeEngineTemplateOptions options) {
OnHostMaintenance onHostMaintenance = OnHostMaintenance.MIGRATE;
boolean automaticRestart = true;
// Preemptible instances cannot use a MIGRATE maintenance strategy or automatic restarts
if (options.preemptible()) {
onHostMaintenance = OnHostMaintenance.TERMINATE;
automaticRestart = false;
}
return Scheduling.create(onHostMaintenance, automaticRestart, options.preemptible());
}
}