-
Notifications
You must be signed in to change notification settings - Fork 89
/
InstanceConfiguration.java
1038 lines (935 loc) · 36.8 KB
/
InstanceConfiguration.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 Google LLC
*
* 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
*
* https://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.google.jenkins.plugins.computeengine;
import static com.google.cloud.graphite.platforms.plugin.client.util.ClientUtil.nameFromSelfLink;
import static com.google.jenkins.plugins.computeengine.ComputeEngineCloud.checkPermissions;
import com.google.api.services.compute.model.AcceleratorConfig;
import com.google.api.services.compute.model.AccessConfig;
import com.google.api.services.compute.model.AttachedDisk;
import com.google.api.services.compute.model.AttachedDiskInitializeParams;
import com.google.api.services.compute.model.DiskType;
import com.google.api.services.compute.model.Image;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.InstanceTemplate;
import com.google.api.services.compute.model.MachineType;
import com.google.api.services.compute.model.Metadata;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.api.services.compute.model.Operation;
import com.google.api.services.compute.model.Region;
import com.google.api.services.compute.model.Scheduling;
import com.google.api.services.compute.model.ServiceAccount;
import com.google.api.services.compute.model.Tags;
import com.google.api.services.compute.model.Zone;
import com.google.cloud.graphite.platforms.plugin.client.ClientFactory;
import com.google.cloud.graphite.platforms.plugin.client.ComputeClient;
import com.google.common.base.Strings;
import com.google.jenkins.plugins.computeengine.client.ClientUtil;
import com.google.jenkins.plugins.computeengine.ssh.GoogleKeyCredential;
import com.google.jenkins.plugins.computeengine.ssh.GoogleKeyPair;
import com.google.jenkins.plugins.computeengine.ssh.GooglePrivateKey;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.RelativePath;
import hudson.Util;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.labels.LabelAtom;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import jenkins.model.Jenkins;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.text.RandomStringGenerator;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
@Getter
@Setter(onMethod = @__(@DataBoundSetter))
/* TODO(rzwitserloot/lombok#2050): Prevent duplicate methods called "build" for custom build method
* until lombok 1.18.8 is released. */
@Builder(builderClassName = "Builder", buildMethodName = "notbuild")
@AllArgsConstructor
@Log
public class InstanceConfiguration implements Describable<InstanceConfiguration> {
public static final String GUEST_ATTRIBUTES_METADATA_KEY = "enable-guest-attributes";
public static final String SSH_METADATA_KEY = "ssh-keys";
public static final Long DEFAULT_BOOT_DISK_SIZE_GB = 10L;
public static final Integer DEFAULT_NUM_EXECUTORS = 1;
public static final Integer DEFAULT_LAUNCH_TIMEOUT_SECONDS = 300;
public static final Integer DEFAULT_RETENTION_TIME_MINUTES =
(DEFAULT_LAUNCH_TIMEOUT_SECONDS / 60) + 1;
public static final String DEFAULT_RUN_AS_USER = "jenkins";
public static final String METADATA_LINUX_STARTUP_SCRIPT_KEY = "startup-script";
public static final String METADATA_WINDOWS_STARTUP_SCRIPT_KEY = "windows-startup-script-ps1";
public static final String NAT_TYPE = "ONE_TO_ONE_NAT";
public static final String NAT_NAME = "External NAT";
public static final List<String> KNOWN_IMAGE_PROJECTS =
Collections.unmodifiableList(
new ArrayList<String>() {
{
add("centos-cloud");
add("coreos-cloud");
add("cos-cloud");
add("debian-cloud");
add("rhel-cloud");
add("suse-cloud");
add("suse-sap-cloud");
add("ubuntu-os-cloud");
add("windows-cloud");
add("windows-sql-cloud");
}
});
private String description;
private String namePrefix;
private String region;
private String zone;
private String machineType;
private String numExecutorsStr;
private String startupScript;
private boolean preemptible;
private String minCpuPlatform;
private String labels;
private String runAsUser;
private String bootDiskType;
private boolean bootDiskAutoDelete;
private String bootDiskSourceImageName;
private String bootDiskSourceImageProject;
private NetworkConfiguration networkConfiguration;
private boolean externalAddress;
private boolean useInternalAddress;
private boolean ignoreProxy;
private String networkTags;
private String serviceAccountEmail;
private Node.Mode mode;
private AcceleratorConfiguration acceleratorConfiguration;
private String retentionTimeMinutesStr;
private String launchTimeoutSecondsStr;
private String bootDiskSizeGbStr;
private boolean oneShot;
private String template;
// Optional not possible due to serialization requirement
@Nullable private WindowsConfiguration windowsConfiguration;
@Nullable private SshConfiguration sshConfiguration;
private boolean createSnapshot;
private String remoteFs;
private String javaExecPath;
private GoogleKeyCredential sshKeyCredential;
private Map<String, String> googleLabels;
private Integer numExecutors;
private Integer retentionTimeMinutes;
private Integer launchTimeoutSeconds;
private Long bootDiskSizeGb;
private transient Set<LabelAtom> labelSet;
@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PROTECTED)
protected transient ComputeEngineCloud cloud;
private static List<Metadata.Items> mergeMetadataItems(
List<Metadata.Items> winner, List<Metadata.Items> loser) {
if (loser == null) {
loser = new ArrayList<Metadata.Items>();
}
for (Metadata.Items existing : loser) {
String existingKey = existing.getKey();
Metadata.Items duplicate =
winner.stream().filter(m -> m.getKey().equals(existingKey)).findFirst().orElse(null);
if (duplicate == null) {
winner.add(existing);
} else if (existingKey.equals(SSH_METADATA_KEY)) {
duplicate.setValue(duplicate.getValue() + "\n" + existing.getValue());
}
}
return winner;
}
@DataBoundConstructor
public InstanceConfiguration() {}
@DataBoundSetter
public void setNumExecutorsStr(String numExecutorsStr) {
this.numExecutors = intOrDefault(numExecutorsStr, DEFAULT_NUM_EXECUTORS);
this.numExecutorsStr = numExecutors.toString();
}
@DataBoundSetter
public void setLabelString(String labelString) {
this.labels = Util.fixNull(labelString);
readResolve();
}
@DataBoundSetter
public void setNetworkTags(String networkTags) {
this.networkTags = Util.fixNull(networkTags).trim();
}
@DataBoundSetter
public void setRetentionTimeMinutesStr(String retentionTimeMinutesStr) {
this.retentionTimeMinutes =
intOrDefault(retentionTimeMinutesStr, DEFAULT_RETENTION_TIME_MINUTES);
this.retentionTimeMinutesStr = this.retentionTimeMinutes.toString();
}
@DataBoundSetter
public void setLaunchTimeoutSecondsStr(String launchTimeoutSecondsStr) {
this.launchTimeoutSeconds =
intOrDefault(launchTimeoutSecondsStr, DEFAULT_LAUNCH_TIMEOUT_SECONDS);
this.launchTimeoutSecondsStr = this.launchTimeoutSeconds.toString();
}
@DataBoundSetter
public void setBootDiskSizeGbStr(String bootDiskSizeGbStr) {
this.bootDiskSizeGb = longOrDefault(bootDiskSizeGbStr, DEFAULT_BOOT_DISK_SIZE_GB);
this.bootDiskSizeGbStr = this.bootDiskSizeGb.toString();
}
@DataBoundSetter
public void setOneShot(boolean oneShot) {
this.oneShot = oneShot;
this.createSnapshot &= oneShot;
}
@DataBoundSetter
public void setCreateSnapshot(boolean createSnapshot) {
this.createSnapshot = createSnapshot && this.oneShot;
}
public static Integer intOrDefault(String toParse, Integer defaultTo) {
Integer toReturn;
try {
toReturn = Integer.parseInt(toParse);
} catch (NumberFormatException nfe) {
toReturn = defaultTo;
}
return toReturn;
}
public static Long longOrDefault(String toParse, Long defaultTo) {
Long toReturn;
try {
toReturn = Long.parseLong(toParse);
} catch (NumberFormatException nfe) {
toReturn = defaultTo;
}
return toReturn;
}
private static boolean notNullOrEmpty(String s) {
return s != null && !s.isEmpty();
}
private static String stripSelfLinkPrefix(String s) {
if (s.contains("https://www.googleapis.com")) {
return s.substring(s.indexOf("/projects/") + 1);
}
return s;
}
@SuppressWarnings("unchecked")
public Descriptor<InstanceConfiguration> getDescriptor() {
return Jenkins.get().getDescriptor(getClass());
}
public String getLabelString() {
return labels;
}
public Set<LabelAtom> getLabelSet() {
return labelSet;
}
public String getDisplayName() {
return description;
}
public int getLaunchTimeoutMillis() {
return launchTimeoutSeconds * 1000;
}
public void appendLabels(Map<String, String> labels) {
if (googleLabels == null) {
googleLabels = new HashMap<>();
}
googleLabels.putAll(labels);
}
public void appendLabel(String key, String value) {
if (googleLabels == null) {
googleLabels = new HashMap<>();
}
googleLabels.put(key, value);
}
public ComputeEngineInstance provision() throws IOException {
try {
Instance instance = instance();
// TODO: JENKINS-55285
Operation operation =
cloud
.getClient()
.insertInstance(cloud.getProjectId(), Optional.ofNullable(template), instance);
log.info("Sent insert request for instance configuration [" + description + "]");
String targetRemoteFs = this.remoteFs;
ComputeEngineComputerLauncher launcher;
if (this.windowsConfiguration != null) {
launcher =
new ComputeEngineWindowsLauncher(
cloud.getCloudName(), operation, this.useInternalAddress);
if (Strings.isNullOrEmpty(targetRemoteFs)) {
targetRemoteFs = "C:\\";
}
} else {
launcher =
new ComputeEngineLinuxLauncher(
cloud.getCloudName(), operation, this.useInternalAddress);
if (Strings.isNullOrEmpty(targetRemoteFs)) {
targetRemoteFs = "/tmp";
}
}
return ComputeEngineInstance.builder()
.cloud(cloud)
.cloudName(cloud.name)
.name(instance.getName())
.zone(instance.getZone())
.nodeDescription(instance.getDescription())
.sshUser(runAsUser)
.remoteFS(targetRemoteFs)
.windowsConfig(windowsConfiguration)
.sshConfig(sshConfiguration)
.createSnapshot(createSnapshot)
.oneShot(oneShot)
.ignoreProxy(ignoreProxy)
.numExecutors(numExecutors)
.mode(mode)
.labelString(labels)
.launcher(launcher)
.retentionStrategy(new ComputeEngineRetentionStrategy(retentionTimeMinutes, oneShot))
.launchTimeout(getLaunchTimeoutMillis())
.javaExecPath(javaExecPath)
.sshKeyCredential(sshKeyCredential)
.build();
} catch (Descriptor.FormException fe) {
log.log(Level.WARNING, "Error provisioning instance: " + fe.getMessage(), fe);
return null;
}
}
/** Initializes transient properties */
protected Object readResolve() {
labelSet = Label.parse(labels);
return this;
}
public Instance instance() throws IOException {
Instance instance = new Instance();
instance.setName(uniqueName());
instance.setDescription(description);
instance.setZone(nameFromSelfLink(zone));
instance.setMetadata(newMetadata());
if (windowsConfiguration == null) {
if (sshConfiguration != null) {
log.info("User selected to use a custom ssh private key");
sshKeyCredential =
configureSSHPrivateKey(sshConfiguration.getCustomPrivateKeyCredentialsId(), runAsUser);
} else {
log.info("User selected to use an autogenerated ssh key pair");
sshKeyCredential = configureSSHKeyPair(instance, runAsUser);
}
}
if (StringUtils.isNotEmpty(template)) {
InstanceTemplate instanceTemplate =
cloud
.getClient()
.getTemplate(nameFromSelfLink(cloud.getProjectId()), nameFromSelfLink(template));
/* Since we have to set the metadata to include the autogenerated SSH keypair,
we need to ensure we include metadata properties which might be set in the template. */
if (instanceTemplate.getProperties() != null
&& instanceTemplate.getProperties().getMetadata() != null
&& instanceTemplate.getProperties().getMetadata().getItems() != null) {
List<Metadata.Items> instanceTemplateItems =
instanceTemplate.getProperties().getMetadata().getItems();
List<Metadata.Items> instanceItems = instance.getMetadata().getItems();
instance.getMetadata().setItems(mergeMetadataItems(instanceItems, instanceTemplateItems));
}
Map<String, String> mergedLabels = new HashMap<>(googleLabels);
if (instanceTemplate.getProperties().getLabels() != null) {
Map<String, String> templateLabels = instanceTemplate.getProperties().getLabels();
mergedLabels.putAll(templateLabels);
}
instance.setLabels(mergedLabels);
} else {
configureStartupScript(instance);
instance.setLabels(googleLabels);
instance.setMachineType(stripSelfLinkPrefix(machineType));
instance.setTags(tags());
instance.setScheduling(scheduling());
instance.setDisks(disks());
instance.setGuestAccelerators(accelerators());
instance.setNetworkInterfaces(networkInterfaces());
instance.setServiceAccounts(serviceAccounts());
// optional
if (notNullOrEmpty(minCpuPlatform)) {
instance.setMinCpuPlatform(minCpuPlatform);
}
}
return instance;
}
private String uniqueName() {
char[][] pairs = {{'a', 'z'}, {'0', '9'}};
RandomStringGenerator generator =
new RandomStringGenerator.Builder().withinRange(pairs).build();
String suffix = generator.generate(6);
String prefix = namePrefix;
if (!prefix.endsWith(("-"))) {
prefix += "-";
}
return prefix + suffix;
}
private Metadata newMetadata() {
Metadata metadata = new Metadata();
metadata.setItems(new ArrayList<>());
metadata
.getItems()
.add(new Metadata.Items().setKey(GUEST_ATTRIBUTES_METADATA_KEY).setValue("TRUE"));
return metadata;
}
/**
* Called when user selects to use autogenerated ssh key pair
*
* @param instance current instance object
* @param sshUser user selected during configuration of cloud
* @return autogenerated ssh key pair
*/
private GoogleKeyPair configureSSHKeyPair(Instance instance, String sshUser) {
GoogleKeyPair sshKeyPair = GoogleKeyPair.generate(sshUser);
instance
.getMetadata()
.getItems()
.add(new Metadata.Items().setKey(SSH_METADATA_KEY).setValue(sshKeyPair.getPublicKey()));
return sshKeyPair;
}
/**
* Called when user selectes to use custom ssh private key
*
* @param credentialId the name of the private key the user has selected
* @param sshUser user selected during configuration of cloud
* @return custom ssh private key
*/
private GooglePrivateKey configureSSHPrivateKey(String credentialId, String sshUser) {
GooglePrivateKey sshPrivateKey = GooglePrivateKey.generate(credentialId, sshUser);
return sshPrivateKey;
}
private void configureStartupScript(Instance instance) {
if (notNullOrEmpty(startupScript)) {
List<Metadata.Items> items = instance.getMetadata().getItems();
if (windowsConfiguration != null) {
items.add(
new Metadata.Items()
.setKey(METADATA_WINDOWS_STARTUP_SCRIPT_KEY)
.setValue(startupScript));
} else {
items.add(
new Metadata.Items().setKey(METADATA_LINUX_STARTUP_SCRIPT_KEY).setValue(startupScript));
}
}
}
private Tags tags() {
if (notNullOrEmpty(networkTags)) {
Tags tags = new Tags();
tags.setItems(Arrays.asList(networkTags.split(" ")));
return tags;
}
return null;
}
private Scheduling scheduling() {
Scheduling scheduling = new Scheduling();
scheduling.setPreemptible(preemptible);
return scheduling;
}
private List<AttachedDisk> disks() {
AttachedDisk boot = new AttachedDisk();
boot.setBoot(true);
boot.setAutoDelete(bootDiskAutoDelete);
boot.setInitializeParams(
new AttachedDiskInitializeParams()
.setDiskSizeGb(bootDiskSizeGb)
.setDiskType(bootDiskType)
.setSourceImage(bootDiskSourceImageName));
List<AttachedDisk> disks = new ArrayList<>();
disks.add(boot);
return disks;
}
private List<AcceleratorConfig> accelerators() {
if (acceleratorConfiguration != null
&& notNullOrEmpty(acceleratorConfiguration.getGpuCount())
&& notNullOrEmpty(acceleratorConfiguration.getGpuType())) {
List<AcceleratorConfig> accelerators = new ArrayList<>();
accelerators.add(
new AcceleratorConfig()
.setAcceleratorType(acceleratorConfiguration.getGpuType())
.setAcceleratorCount(acceleratorConfiguration.gpuCount()));
return accelerators;
}
return null;
}
private List<NetworkInterface> networkInterfaces() {
List<NetworkInterface> networkInterfaces = new ArrayList<>();
List<AccessConfig> accessConfigs = new ArrayList<>();
if (externalAddress) {
accessConfigs.add(new AccessConfig().setType(NAT_TYPE).setName(NAT_NAME));
}
NetworkInterface nic = new NetworkInterface().setAccessConfigs(accessConfigs);
// Don't include subnetwork name if using default
if (!networkConfiguration.getSubnetwork().equals("default")) {
nic.setSubnetwork(stripSelfLinkPrefix(networkConfiguration.getSubnetwork()));
}
networkInterfaces.add(nic);
return networkInterfaces;
}
private List<ServiceAccount> serviceAccounts() {
if (notNullOrEmpty(serviceAccountEmail)) {
List<ServiceAccount> serviceAccounts = new ArrayList<>();
serviceAccounts.add(
new ServiceAccount()
.setEmail(serviceAccountEmail)
.setScopes(
Arrays.asList(new String[] {"https://www.googleapis.com/auth/cloud-platform"})));
return serviceAccounts;
} else {
return null;
}
}
@Extension
public static final class DescriptorImpl extends Descriptor<InstanceConfiguration> {
private static ComputeClient computeClient;
public static void setComputeClient(ComputeClient client) {
computeClient = client;
}
public static String defaultRetentionTimeMinutes() {
return DEFAULT_RETENTION_TIME_MINUTES.toString();
}
public static String defaultLaunchTimeoutSeconds() {
return DEFAULT_LAUNCH_TIMEOUT_SECONDS.toString();
}
public static String defaultBootDiskSizeGb() {
return DEFAULT_BOOT_DISK_SIZE_GB.toString();
}
public static String defaultBootDiskAutoDelete() {
return "true";
}
public static String defaultRunAsUser() {
return DEFAULT_RUN_AS_USER;
}
public static WindowsConfiguration defaultWindowsConfiguration() {
return WindowsConfiguration.builder()
.passwordCredentialsId("")
.privateKeyCredentialsId("")
.build();
}
public static SshConfiguration defaultSshConfiguration() {
return SshConfiguration.builder().customPrivateKeyCredentialsId("").build();
}
public static NetworkConfiguration defaultNetworkConfiguration() {
return new AutofilledNetworkConfiguration();
}
private static ComputeClient computeClient(Jenkins context, String credentialsId)
throws IOException {
if (computeClient != null) {
return computeClient;
}
ClientFactory clientFactory = ClientUtil.getClientFactory(context, credentialsId);
return clientFactory.computeClient();
}
@Override
public String getHelpFile(String fieldName) {
String p = super.getHelpFile(fieldName);
if (p == null) {
Descriptor d = Jenkins.get().getDescriptor(ComputeEngineInstance.class);
if (d != null) p = d.getHelpFile(fieldName);
}
return p;
}
public List<NetworkConfiguration.NetworkConfigurationDescriptor>
getNetworkConfigurationDescriptors() {
List<NetworkConfiguration.NetworkConfigurationDescriptor> d =
Jenkins.get().getDescriptorList(NetworkConfiguration.class);
// No deprecated regions
Iterator it = d.iterator();
while (it.hasNext()) {
NetworkConfiguration.NetworkConfigurationDescriptor o =
(NetworkConfiguration.NetworkConfigurationDescriptor) it.next();
if (o.clazz.getName().equals("NetworkConfiguration")) {
it.remove();
}
}
return d;
}
public FormValidation doCheckNetworkTags(@QueryParameter String value) {
checkPermissions();
if (value == null || value.isEmpty()) {
return FormValidation.ok();
}
String re = "[a-z]([-a-z0-9]*[a-z0-9])?";
for (String tag : value.split(" ")) {
if (!tag.matches(re)) {
return FormValidation.error(
"Tags must be space-delimited and each tag must match regex" + re);
}
}
return FormValidation.ok();
}
public FormValidation doCheckNamePrefix(@QueryParameter String value) {
checkPermissions();
if (value == null || value.isEmpty()) {
return FormValidation.error("A prefix is required");
}
String re = "[a-z]([-a-z0-9]*[a-z0-9])?";
if (!value.matches(re)) {
return FormValidation.error("Prefix must match regex " + re);
}
Integer maxLen = 50;
if (value.length() > maxLen) {
return FormValidation.error("Maximum length is " + maxLen);
}
return FormValidation.ok();
}
public FormValidation doCheckDescription(@QueryParameter String value) {
checkPermissions();
if (value == null || value.isEmpty()) {
return FormValidation.error("A description is required");
}
return FormValidation.ok();
}
public ListBoxModel doFillRegionItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<Region> regions = compute.listRegions(projectId);
for (Region r : regions) {
items.add(r.getName(), r.getSelfLink());
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving regions");
return items;
}
}
public ListBoxModel doFillTemplateItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<InstanceTemplate> instanceTemplates = compute.listTemplates(projectId);
for (InstanceTemplate instanceTemplate : instanceTemplates) {
items.add(instanceTemplate.getName(), instanceTemplate.getSelfLink());
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving instanceTemplates");
return items;
}
}
public FormValidation doCheckRegion(@QueryParameter String value) {
checkPermissions();
if (value.equals("")) {
return FormValidation.error("Please select a region...");
}
return FormValidation.ok();
}
public ListBoxModel doFillZoneItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("region") final String region,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<Zone> zones = compute.listZones(projectId, region);
for (Zone z : zones) {
items.add(z.getName(), z.getSelfLink());
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving zones");
return items;
} catch (IllegalArgumentException iae) {
// TODO log
return null;
}
}
public FormValidation doCheckZone(@QueryParameter String value) {
checkPermissions();
if (value.equals("")) {
return FormValidation.error("Please select a zone...");
}
return FormValidation.ok();
}
public ListBoxModel doFillMachineTypeItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("zone") final String zone,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<MachineType> machineTypes = compute.listMachineTypes(projectId, zone);
for (MachineType m : machineTypes) {
items.add(m.getName(), m.getSelfLink());
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving machine types");
return items;
} catch (IllegalArgumentException iae) {
// TODO log
return null;
}
}
public FormValidation doCheckMachineType(@QueryParameter String value) {
checkPermissions();
if (value.equals("")) {
return FormValidation.error("Please select a machine type...");
}
return FormValidation.ok();
}
public ListBoxModel doFillMinCpuPlatformItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("zone") final String zone,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<String> cpuPlatforms = compute.listCpuPlatforms(projectId, zone);
for (String cpuPlatform : cpuPlatforms) {
items.add(cpuPlatform);
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving cpu Platforms");
return items;
} catch (IllegalArgumentException iae) {
// TODO log
return null;
}
}
public ListBoxModel doFillBootDiskTypeItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId,
@QueryParameter("zone") String zone,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
try {
ComputeClient compute = computeClient(context, credentialsId);
List<DiskType> diskTypes = compute.listBootDiskTypes(projectId, zone);
for (DiskType dt : diskTypes) {
items.add(dt.getName(), dt.getSelfLink());
}
return items;
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving disk types");
return items;
} catch (IllegalArgumentException iae) {
// TODO: log
return null;
}
}
public ListBoxModel doFillBootDiskSourceImageProjectItems(
@AncestorInPath Jenkins context,
@QueryParameter("projectId") @RelativePath("..") final String projectId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
items.add(projectId);
for (String v : KNOWN_IMAGE_PROJECTS) {
items.add(v);
}
return items;
}
public FormValidation doCheckBootDiskSourceImageProject(@QueryParameter String value) {
checkPermissions();
if (value.equals("")) {
return FormValidation.warning("Please select source image project...");
}
return FormValidation.ok();
}
public ListBoxModel doFillBootDiskSourceImageNameItems(
@AncestorInPath Jenkins context,
@QueryParameter("bootDiskSourceImageProject") final String projectId,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
ListBoxModel items = new ListBoxModel();
items.add("");
try {
ComputeClient compute = computeClient(context, credentialsId);
List<Image> images = compute.listImages(projectId);
for (Image i : images) {
items.add(i.getName(), i.getSelfLink());
}
} catch (IOException ioe) {
items.clear();
items.add("Error retrieving images for project");
} catch (IllegalArgumentException iae) {
// TODO: log
return null;
}
return items;
}
public FormValidation doCheckBootDiskSourceImageName(@QueryParameter String value) {
checkPermissions();
if (value.equals("")) {
return FormValidation.warning("Please select source image...");
}
return FormValidation.ok();
}
public FormValidation doCheckBootDiskSizeGbStr(
@AncestorInPath Jenkins context,
@QueryParameter String value,
@QueryParameter("bootDiskSourceImageProject") final String projectId,
@QueryParameter("bootDiskSourceImageName") final String imageName,
@QueryParameter("credentialsId") @RelativePath("..") final String credentialsId) {
checkPermissions();
if (Strings.isNullOrEmpty(credentialsId)
|| Strings.isNullOrEmpty(projectId)
|| Strings.isNullOrEmpty(imageName)) return FormValidation.ok();
try {
ComputeClient compute = computeClient(context, credentialsId);
Image i = compute.getImage(nameFromSelfLink(projectId), nameFromSelfLink(imageName));
if (i == null) return FormValidation.error("Could not find image " + imageName);
Long bootDiskSizeGb = Long.parseLong(value);
if (bootDiskSizeGb < i.getDiskSizeGb()) {
return FormValidation.error(
String.format(
"The disk image you have chosen requires a minimum of %dGB. Please increase boot disk size to accommodate.",
i.getDiskSizeGb()));
}
} catch (IOException ioe) {
return FormValidation.error(ioe, "Error validating boot disk size");
}
return FormValidation.ok();
}
public FormValidation doCheckLabelString(
@QueryParameter String value, @QueryParameter Node.Mode mode) {
checkPermissions();
if (mode == Node.Mode.EXCLUSIVE && (value == null || value.trim().isEmpty())) {
return FormValidation.warning(
"You may want to assign labels to this node;"
+ " it's marked to only run jobs that are exclusively tied to itself or a label.");
}
return FormValidation.ok();
}
public FormValidation doCheckCreateSnapshot(
@AncestorInPath Jenkins context,
@QueryParameter boolean value,
@QueryParameter("oneShot") boolean oneShot) {
checkPermissions();
if (!oneShot && value) {
return FormValidation.error(Messages.InstanceConfiguration_SnapshotConfigError());
}
return FormValidation.ok();
}
public FormValidation doCheckNumExecutorsStr(
@AncestorInPath Jenkins context,
@QueryParameter String value,
@QueryParameter("oneShot") boolean oneShot) {
checkPermissions();
int numExecutors = intOrDefault(value, DEFAULT_NUM_EXECUTORS);
if (numExecutors < 1) {
return FormValidation.error(
Messages.InstanceConfiguration_NumExecutorsLessThanOneConfigError());
} else if (numExecutors > 1 && oneShot) {
return FormValidation.error(Messages.InstanceConfiguration_NumExecutorsOneShotError());
}
return FormValidation.ok();
}
}
public static class Builder {
public InstanceConfiguration build() {
InstanceConfiguration instanceConfiguration = new InstanceConfiguration();
instanceConfiguration.setDescription(this.description);
instanceConfiguration.setNamePrefix(this.namePrefix);
instanceConfiguration.setRegion(this.region);
instanceConfiguration.setZone(this.zone);
instanceConfiguration.setMachineType(this.machineType);
instanceConfiguration.setNumExecutorsStr(this.numExecutorsStr);
instanceConfiguration.setStartupScript(this.startupScript);
instanceConfiguration.setPreemptible(this.preemptible);
instanceConfiguration.setMinCpuPlatform(this.minCpuPlatform);
instanceConfiguration.setLabelString(this.labels);
instanceConfiguration.setRunAsUser(this.runAsUser);
instanceConfiguration.setWindowsConfiguration(this.windowsConfiguration);
instanceConfiguration.setSshConfiguration(this.sshConfiguration);
instanceConfiguration.setBootDiskType(this.bootDiskType);
instanceConfiguration.setBootDiskAutoDelete(this.bootDiskAutoDelete);
instanceConfiguration.setBootDiskSourceImageName(this.bootDiskSourceImageName);
instanceConfiguration.setBootDiskSourceImageProject(this.bootDiskSourceImageProject);
instanceConfiguration.setNetworkConfiguration(this.networkConfiguration);
instanceConfiguration.setExternalAddress(this.externalAddress);
instanceConfiguration.setUseInternalAddress(this.useInternalAddress);
instanceConfiguration.setIgnoreProxy(this.ignoreProxy);
instanceConfiguration.setNetworkTags(this.networkTags);
instanceConfiguration.setServiceAccountEmail(this.serviceAccountEmail);
instanceConfiguration.setMode(this.mode);