From 114be963beb13e58d817777661aee62e79f128fc Mon Sep 17 00:00:00 2001 From: Salvo Marino Date: Fri, 2 May 2025 11:16:32 +0100 Subject: [PATCH 1/3] Save EC2 cloud when error code is ExpiredToken --- src/main/java/hudson/plugins/ec2/EC2Cloud.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2Cloud.java b/src/main/java/hudson/plugins/ec2/EC2Cloud.java index d7c43c659..babb5c016 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/EC2Cloud.java @@ -1047,15 +1047,25 @@ public Collection provision(final Label label, int excessWorkload) } } catch (AwsServiceException e) { LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e); - if (e.awsErrorDetails().errorCode().equals("RequestExpired") - || e.awsErrorDetails().errorCode().equals("ExpiredToken")) { - // A RequestExpired or ExpiredToken error can indicate that credentials have expired so reconnect + if (e.awsErrorDetails().errorCode().equals("RequestExpired")) { + // A RequestExpired error can indicate that request has expired so reconnect LOGGER.log(Level.INFO, "Reconnecting to EC2 due to RequestExpired or ExpiredToken error"); try { reconnectToEc2(); } catch (IOException e2) { LOGGER.log(Level.WARNING, "Failed to reconnect ec2", e2); } + } else if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { + // A ExpiredToken error can indicate that token has expired so re-save the cloud + LOGGER.log(Level.INFO, "Re-saving EC2 cloud due to ExpiredToken error"); + try { + if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { + Jenkins.get().clouds.replace(this, this) + Jenkins.get().save() + } + } catch (IOException e2) { + LOGGER.log(Level.WARNING, "Failed to re-save EC2 cloud: " + getCloudName(), e2); + } } } catch (SdkException | IOException e) { LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e); From c9bafa4cb9dd90d40c970dd0f77db20f7307b4dd Mon Sep 17 00:00:00 2001 From: Salvo Marino Date: Fri, 2 May 2025 14:07:22 +0100 Subject: [PATCH 2/3] added missing semi colons --- src/main/java/hudson/plugins/ec2/EC2Cloud.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2Cloud.java b/src/main/java/hudson/plugins/ec2/EC2Cloud.java index babb5c016..2ae596001 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/EC2Cloud.java @@ -1060,8 +1060,8 @@ public Collection provision(final Label label, int excessWorkload) LOGGER.log(Level.INFO, "Re-saving EC2 cloud due to ExpiredToken error"); try { if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { - Jenkins.get().clouds.replace(this, this) - Jenkins.get().save() + Jenkins.get().clouds.replace(this, this); + Jenkins.get().save(); } } catch (IOException e2) { LOGGER.log(Level.WARNING, "Failed to re-save EC2 cloud: " + getCloudName(), e2); From 83663ceb31cf61aa7223242606b79ba0fa5f1efd Mon Sep 17 00:00:00 2001 From: salvomarino Date: Mon, 19 May 2025 11:26:20 +0100 Subject: [PATCH 3/3] Update EC2Cloud.java --- src/main/java/hudson/plugins/ec2/EC2Cloud.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2Cloud.java b/src/main/java/hudson/plugins/ec2/EC2Cloud.java index 2ae596001..6d5d1bbb8 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/EC2Cloud.java @@ -1049,20 +1049,18 @@ public Collection provision(final Label label, int excessWorkload) LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e); if (e.awsErrorDetails().errorCode().equals("RequestExpired")) { // A RequestExpired error can indicate that request has expired so reconnect - LOGGER.log(Level.INFO, "Reconnecting to EC2 due to RequestExpired or ExpiredToken error"); + LOGGER.log(Level.INFO, "Reconnecting to EC2 due to RequestExpired error"); try { reconnectToEc2(); } catch (IOException e2) { - LOGGER.log(Level.WARNING, "Failed to reconnect ec2", e2); + LOGGER.log(Level.WARNING, "Failed to reconnect EC2", e2); } } else if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { // A ExpiredToken error can indicate that token has expired so re-save the cloud LOGGER.log(Level.INFO, "Re-saving EC2 cloud due to ExpiredToken error"); try { - if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { - Jenkins.get().clouds.replace(this, this); - Jenkins.get().save(); - } + Jenkins.get().clouds.replace(this, this); + Jenkins.get().save(); } catch (IOException e2) { LOGGER.log(Level.WARNING, "Failed to re-save EC2 cloud: " + getCloudName(), e2); }