From e55da55b6774d3ac5891797df48ce018cbf148c0 Mon Sep 17 00:00:00 2001 From: Arthur Caron Date: Thu, 28 Nov 2024 12:13:39 +0100 Subject: [PATCH] Terminate instance when not found. --- .../ComputeEngineComputerLauncher.java | 22 ++++++++++++++++--- .../ComputeEngineLinuxLauncher.java | 3 +++ .../ComputeEngineWindowsLauncher.java | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineComputerLauncher.java b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineComputerLauncher.java index 3bd94c50..90c1d177 100644 --- a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineComputerLauncher.java +++ b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineComputerLauncher.java @@ -16,6 +16,7 @@ package com.google.jenkins.plugins.computeengine; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.compute.model.AccessConfig; import com.google.api.services.compute.model.Instance; import com.google.api.services.compute.model.NetworkInterface; @@ -40,6 +41,7 @@ import java.io.PrintStream; import java.net.InetSocketAddress; import java.net.Proxy; +import java.net.SocketTimeoutException; import java.util.Base64; import java.util.Optional; import java.util.logging.Level; @@ -349,6 +351,10 @@ protected Connection connectToSsh(ComputeEngineComputer computer, TaskListener l + ")"); } Instance instance = computer.refreshInstance(); + // the instance will be null when the node is terminated + if (instance == null) { + return null; + } String host = ""; @@ -416,11 +422,21 @@ protected Connection connectToSsh(ComputeEngineComputer computer, TaskListener l SSH_TIMEOUT_MILLIS); logInfo(computer, listener, "Connected via SSH."); return conn; - } catch (IOException e) { + } catch (GoogleJsonResponseException e) { + if (e.getStatusCode() == 404) { + log(LOGGER, Level.SEVERE, listener, String.format("Instance not found. Terminating instance.")); + terminateNode(computer, listener); + } + } catch (SocketTimeoutException e) { // keep retrying until SSH comes up - logInfo(computer, listener, "Failed to connect via ssh: " + e.getMessage()); - logInfo(computer, listener, "Waiting for SSH to come up. Sleeping 5."); + logInfo(computer, listener, String.format("Failed to connect via ssh: %s", e.getMessage())); + logInfo( + computer, + listener, + String.format("Waiting for SSH to come up. Sleeping %n.", SSH_SLEEP_MILLIS / 1000)); Thread.sleep(SSH_SLEEP_MILLIS); + } catch (IOException e) { + logWarning(computer, listener, String.format("An error occured: %s", e.getMessage())); } } } diff --git a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineLinuxLauncher.java b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineLinuxLauncher.java index 928f3c38..50fc679c 100644 --- a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineLinuxLauncher.java +++ b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineLinuxLauncher.java @@ -85,6 +85,9 @@ private Optional bootstrap( logInfo(computer, listener, "Authenticating as " + node.getSshUser()); try { bootstrapConn = connectToSsh(computer, listener); + if (bootstrapConn == null) { + break; + } isAuthenticated = bootstrapConn.authenticateWithPublicKey( node.getSshUser(), Secret.toString(keyCred.getPrivateKey()).toCharArray(), diff --git a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineWindowsLauncher.java b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineWindowsLauncher.java index 30fae98e..317eeeac 100644 --- a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineWindowsLauncher.java +++ b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineWindowsLauncher.java @@ -92,6 +92,9 @@ private Optional bootstrap(ComputeEngineComputer computer, TaskListe logInfo(computer, listener, "Authenticating as " + node.getSshUser()); try { bootstrapConn = connectToSsh(computer, listener); + if (bootstrapConn == null) { + break; + } isAuthenticated = authenticateSSH(node.getSshUser(), windowsConfig, bootstrapConn, listener); } catch (IOException e) { logException(computer, listener, "Exception trying to authenticate", e);