|
16 | 16 |
|
17 | 17 | package com.google.jenkins.plugins.computeengine; |
18 | 18 |
|
| 19 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
19 | 20 | import com.google.api.services.compute.model.AccessConfig; |
20 | 21 | import com.google.api.services.compute.model.Instance; |
21 | 22 | import com.google.api.services.compute.model.NetworkInterface; |
|
40 | 41 | import java.io.PrintStream; |
41 | 42 | import java.net.InetSocketAddress; |
42 | 43 | import java.net.Proxy; |
| 44 | +import java.net.SocketTimeoutException; |
43 | 45 | import java.util.Base64; |
44 | 46 | import java.util.Optional; |
45 | 47 | import java.util.logging.Level; |
@@ -154,14 +156,16 @@ public void launch(SlaveComputer slaveComputer, TaskListener listener) { |
154 | 156 | } |
155 | 157 | if (opError != null) { |
156 | 158 | LOGGER.info(String.format( |
157 | | - "Launch failed while waiting for operation %s to complete. Operation error was %s", |
| 159 | + "Launch failed while waiting for operation %s to complete. Operation error was %s. Terminating instance.", |
158 | 160 | insertOperationId, opError.getErrors().get(0).getMessage())); |
| 161 | + terminateNode(computer, listener); |
159 | 162 | return; |
160 | 163 | } |
161 | 164 | } catch (InterruptedException e) { |
162 | 165 | LOGGER.info(String.format( |
163 | | - "Launch failed while waiting for operation %s to complete. Operation error was %s", |
| 166 | + "Launch failed while waiting for operation %s to complete. Operation error was %s. Terminating instance", |
164 | 167 | insertOperationId, opError.getErrors().get(0).getMessage())); |
| 168 | + terminateNode(computer, listener); |
165 | 169 | return; |
166 | 170 | } |
167 | 171 |
|
@@ -214,19 +218,26 @@ public void launch(SlaveComputer slaveComputer, TaskListener listener) { |
214 | 218 | launch(computer, listener); |
215 | 219 | } catch (IOException ioe) { |
216 | 220 | ioe.printStackTrace(listener.error(ioe.getMessage())); |
217 | | - node = (ComputeEngineInstance) slaveComputer.getNode(); |
218 | | - if (node != null) { |
219 | | - try { |
220 | | - node.terminate(); |
221 | | - } catch (Exception e) { |
222 | | - listener.error(String.format("Failed to terminate node %s", node.getDisplayName())); |
223 | | - } |
224 | | - } |
| 221 | + terminateNode(slaveComputer, listener); |
225 | 222 | } catch (InterruptedException ie) { |
226 | 223 |
|
227 | 224 | } |
228 | 225 | } |
229 | 226 |
|
| 227 | + private static void terminateNode(SlaveComputer slaveComputer, TaskListener listener) { |
| 228 | + ComputeEngineInstance node = (ComputeEngineInstance) slaveComputer.getNode(); |
| 229 | + if (node != null) { |
| 230 | + try { |
| 231 | + node.terminate(); |
| 232 | + } catch (Exception e) { |
| 233 | + listener.error(String.format("Failed to terminate node %s", node.getDisplayName())); |
| 234 | + } |
| 235 | + } else { |
| 236 | + LOGGER.fine( |
| 237 | + String.format("Tried to terminate unknown node from computer %s", slaveComputer.getDisplayName())); |
| 238 | + } |
| 239 | + } |
| 240 | + |
230 | 241 | private boolean testCommand( |
231 | 242 | ComputeEngineComputer computer, |
232 | 243 | Connection conn, |
@@ -343,6 +354,10 @@ protected Connection connectToSsh(ComputeEngineComputer computer, TaskListener l |
343 | 354 | + ")"); |
344 | 355 | } |
345 | 356 | Instance instance = computer.refreshInstance(); |
| 357 | + // the instance will be null when the node is terminated |
| 358 | + if (instance == null) { |
| 359 | + return null; |
| 360 | + } |
346 | 361 |
|
347 | 362 | String host = ""; |
348 | 363 |
|
@@ -410,10 +425,25 @@ protected Connection connectToSsh(ComputeEngineComputer computer, TaskListener l |
410 | 425 | SSH_TIMEOUT_MILLIS); |
411 | 426 | logInfo(computer, listener, "Connected via SSH."); |
412 | 427 | return conn; |
413 | | - } catch (IOException e) { |
| 428 | + } catch (GoogleJsonResponseException e) { |
| 429 | + if (e.getStatusCode() == 404) { |
| 430 | + log( |
| 431 | + LOGGER, |
| 432 | + Level.SEVERE, |
| 433 | + listener, |
| 434 | + String.format("Instance %s not found. Terminating instance.", computer.getName())); |
| 435 | + terminateNode(computer, listener); |
| 436 | + } |
| 437 | + } catch (SocketTimeoutException e) { |
414 | 438 | // keep retrying until SSH comes up |
415 | | - logInfo(computer, listener, "Failed to connect via ssh: " + e.getMessage()); |
416 | | - logInfo(computer, listener, "Waiting for SSH to come up. Sleeping 5."); |
| 439 | + logInfo(computer, listener, String.format("Failed to connect via ssh: %s", e.getMessage())); |
| 440 | + logInfo( |
| 441 | + computer, |
| 442 | + listener, |
| 443 | + String.format("Waiting for SSH to come up. Sleeping %d.", SSH_SLEEP_MILLIS / 1000)); |
| 444 | + Thread.sleep(SSH_SLEEP_MILLIS); |
| 445 | + } catch (IOException e) { |
| 446 | + logWarning(computer, listener, String.format("An error occured: %s", e.getMessage())); |
417 | 447 | Thread.sleep(SSH_SLEEP_MILLIS); |
418 | 448 | } |
419 | 449 | } |
|
0 commit comments