-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new fixed-lifespan cloud retention strategy.
The fixed-lifespan strategy provisions an agent for a fixed amount of time. After the amount of time has expired, the agent is disconnected, any builds finished, then the agent is removed.
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/org/jenkinsci/plugins/vsphere/FixedLifespanCloudRetentionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.jenkinsci.plugins.vsphere; | ||
|
||
import hudson.model.Descriptor; | ||
import hudson.model.InvisibleAction; | ||
import hudson.slaves.AbstractCloudComputer; | ||
import hudson.slaves.AbstractCloudSlave; | ||
import hudson.slaves.CloudRetentionStrategy; | ||
import hudson.slaves.RetentionStrategy; | ||
import hudson.util.TimeUnit2; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import static java.util.logging.Level.WARNING; | ||
|
||
public final class FixedLifespanCloudRetentionStrategy extends RetentionStrategy<AbstractCloudComputer> { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CloudRetentionStrategy.class.getName()); | ||
|
||
private final int lifespanMinutes; | ||
|
||
@DataBoundConstructor | ||
public FixedLifespanCloudRetentionStrategy(int lifespanMinutes) { | ||
this.lifespanMinutes = lifespanMinutes; | ||
} | ||
|
||
public int getLifespanMinutes() { | ||
return lifespanMinutes; | ||
} | ||
|
||
@Override | ||
public long check(AbstractCloudComputer c) { | ||
final long creationTimeMillis = c.getAction(CloudComputerCreatedOnInvisibleAction.class).getCreationTimeMillis(); | ||
final long ageMillis = System.currentTimeMillis() - creationTimeMillis; | ||
final String cname = c.getName(); | ||
if (!isAtEndOfLife() && ageMillis > TimeUnit2.MINUTES.toMillis(lifespanMinutes)) { | ||
LOGGER.log(Level.FINE, "Will terminate {0} once idle - lifespan of {1} minutes reached.", new Object[] { cname, lifespanMinutes }); | ||
setAtEndOfLife(); | ||
final VSphereOfflineCause cause = new VSphereOfflineCause(Messages._fixedLifespanCloudRetentionStrategy_OfflineReason_LifespanReached(String.valueOf(lifespanMinutes))); | ||
try { | ||
c.disconnect(cause).get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
LOGGER.log(WARNING, "Failed to disconnect " + cname, e); | ||
} | ||
} | ||
if (isAtEndOfLife() && c.isIdle()) { | ||
final AbstractCloudSlave computerNode = c.getNode(); | ||
if (computerNode != null) { | ||
try { | ||
LOGGER.log(Level.FINER, "Initiating termination of {0}.", cname); | ||
computerNode.terminate(); | ||
} catch (InterruptedException | IOException e) { | ||
LOGGER.log(WARNING, "Failed to terminate " + cname, e); | ||
} | ||
} | ||
} | ||
return 1; // re-check in 1 minute | ||
} | ||
|
||
@Override | ||
public DescriptorImpl getDescriptor() { | ||
return DESCRIPTOR; | ||
} | ||
|
||
@Override | ||
public void start(AbstractCloudComputer c) { | ||
c.addAction(new CloudComputerCreatedOnInvisibleAction(System.currentTimeMillis())); | ||
super.start(c); | ||
c.connect(false); | ||
} | ||
|
||
@Override | ||
public boolean isAcceptingTasks(AbstractCloudComputer c) { | ||
return !isAtEndOfLife(); | ||
} | ||
|
||
private transient boolean atEndOfLife; | ||
|
||
private synchronized boolean isAtEndOfLife() { | ||
return atEndOfLife; | ||
} | ||
|
||
private synchronized void setAtEndOfLife() { | ||
atEndOfLife = true; | ||
} | ||
|
||
@Restricted(NoExternalUse.class) | ||
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); | ||
|
||
public static final class DescriptorImpl extends Descriptor<RetentionStrategy<?>> { | ||
@Override | ||
public String getDisplayName() { | ||
return "vSphere Fixed Lifespan Retention Strategy"; | ||
} | ||
} | ||
|
||
public static class CloudComputerCreatedOnInvisibleAction extends InvisibleAction { | ||
private final long creationTimeMillis; | ||
|
||
CloudComputerCreatedOnInvisibleAction(long creationTimeMillis) { | ||
this.creationTimeMillis = creationTimeMillis; | ||
} | ||
|
||
long getCreationTimeMillis() { | ||
return creationTimeMillis; | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
.../resources/org/jenkinsci/plugins/vsphere/FixedLifespanCloudRetentionStrategy/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> | ||
<f:entry title="${%Lifespan Timeout}" field="lifespanMinutes"> | ||
<f:number default="60"/> | ||
</f:entry> | ||
</j:jelly> |
1 change: 1 addition & 0 deletions
1
src/main/resources/org/jenkinsci/plugins/vsphere/Messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
runOnceCloudRetentionStrategy.OfflineReason.BuildHasRun=VSphere Cloud Slave configured was to run one build only | ||
fixedLifespanCloudRetentionStrategy.OfflineReason.LifespanReached=VSphere Cloud Agent has reached the end of its configured lifespan of {0} minutes |