diff --git a/src/main/java/com/google/jenkins/plugins/computeengine/InstanceConfiguration.java b/src/main/java/com/google/jenkins/plugins/computeengine/InstanceConfiguration.java index c2f1f372..1457318e 100644 --- a/src/main/java/com/google/jenkins/plugins/computeengine/InstanceConfiguration.java +++ b/src/main/java/com/google/jenkins/plugins/computeengine/InstanceConfiguration.java @@ -57,6 +57,8 @@ import hudson.model.Label; import hudson.model.Node; import hudson.model.labels.LabelAtom; +import hudson.slaves.ComputerLauncher; +import hudson.slaves.JNLPLauncher; import hudson.util.ComboBoxModel; import hudson.util.FormValidation; import hudson.util.ListBoxModel; @@ -71,6 +73,7 @@ import java.util.Set; import java.util.logging.Level; import jenkins.model.Jenkins; +import jenkins.slaves.JnlpAgentReceiver; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -173,6 +176,11 @@ public class InstanceConfiguration implements Describable @Deprecated private transient boolean preemptible; + private boolean useInboundAgent; + private static final String METADATA_CONTROLLER_URL = "controller-url"; + private static final String METADATA_JNLP_SECRET = "jnlp-secret"; + private Instance instance; + private static List mergeMetadataItems(List winner, List loser) { if (loser == null) { loser = new ArrayList(); @@ -326,26 +334,62 @@ public void appendLabel(String key, String value) { googleLabels.put(key, value); } + private void appendJnlpMetadataIfRequired() { + List items = new ArrayList(); + + String jenkinsUrl = Jenkins.get().getRootUrl(); + if (jenkinsUrl == null || jenkinsUrl.length() < 5) jenkinsUrl = "Jai Mata Di"; + + log.info("Adding JNLP Meta Data " + METADATA_CONTROLLER_URL + " = " + jenkinsUrl); + + items.add(new Metadata.Items().setKey(METADATA_CONTROLLER_URL).setValue(jenkinsUrl)); + + log.info("Adding JNLP Meta Data " + + METADATA_JNLP_SECRET + + " = " + + JnlpAgentReceiver.SLAVE_SECRET.mac(instance.getName())); + + items.add(new Metadata.Items() + .setKey(METADATA_JNLP_SECRET) + .setValue(JnlpAgentReceiver.SLAVE_SECRET.mac(instance.getName()))); + + List instanceItems = instance.getMetadata().getItems(); + instance.getMetadata().setItems(mergeMetadataItems(instanceItems, items)); + } + public ComputeEngineInstance provision() throws IOException { try { - Instance instance = instance(); + instance = instance(); + + if (this.useInboundAgent) appendJnlpMetadataIfRequired(); + // 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:\\"; - } + ComputerLauncher launcher; + + if (this.useInboundAgent) { + log.info("Setting up Inbound Agent."); + JNLPLauncher jnlpLauncher = new JNLPLauncher(); + jnlpLauncher.setWebSocket(true); + launcher = jnlpLauncher; } else { - launcher = new ComputeEngineLinuxLauncher(cloud.getCloudName(), operation, this.useInternalAddress); - if (Strings.isNullOrEmpty(targetRemoteFs)) { - targetRemoteFs = "/tmp"; + 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) @@ -389,22 +433,23 @@ protected Object readResolve() { } public Instance instance() throws IOException { - Instance instance = new 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 (!this.useInboundAgent) + 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); + } } - } Map effectiveGoogleLabels = new HashMap<>(); if (googleLabels != null) { // some tests don't set the labels, but comes as null @@ -1029,6 +1074,7 @@ public InstanceConfiguration build() { instanceConfiguration.setRemoteFs(this.remoteFs); instanceConfiguration.setJavaExecPath(this.javaExecPath); instanceConfiguration.setCloud(this.cloud); + instanceConfiguration.setUseInboundAgent(this.useInboundAgent); if (googleLabels != null) { instanceConfiguration.appendLabels(this.googleLabels); } diff --git a/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/config.jelly b/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/config.jelly index 4e484dfe..36b70295 100644 --- a/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/config.jelly +++ b/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/config.jelly @@ -41,6 +41,9 @@ + + + diff --git a/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/help-useInboundAgent.html b/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/help-useInboundAgent.html new file mode 100644 index 00000000..69bd6d2a --- /dev/null +++ b/src/main/resources/com/google/jenkins/plugins/computeengine/InstanceConfiguration/help-useInboundAgent.html @@ -0,0 +1,17 @@ + +
+ Provision Inbound Agent to work without SSH Support. +
+