Java tutorial
/* * The MIT License * * Copyright (c) 2011, Aaron Phillips * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package jenkins.plugins.ec2slave; import com.amazonaws.services.ec2.model.StartInstancesRequest; import com.amazonaws.services.ec2.model.StopInstancesRequest; import com.amazonaws.services.ec2.model.StopInstancesResult; import hudson.model.TaskListener; import hudson.model.Descriptor; import hudson.slaves.ComputerConnector; import hudson.slaves.ComputerLauncher; import hudson.slaves.DumbSlave; import hudson.slaves.SlaveComputer; import java.io.IOException; import java.io.PrintStream; import java.text.MessageFormat; import java.util.logging.Logger; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.ec2.AmazonEC2Client; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.InstanceStateName; import static com.amazonaws.services.ec2.model.InstanceStateName.*; /** * The {@link EC2ImageLaunchWrapper} is a true {@link ComputerLauncher} which is what we are used to seeing when we * configure a {@link DumbSlave}, however this class is a wrapper and is not meant to be configured by a user. * <p/> * The role of {@link EC2ImageLaunchWrapper} is to manage the EC2 instance that is bound to the Jenkins slave. This * class takes care of startup and shutdown hooks, communicating with Amazon via the Java AWS client. * <p/> * The other part of it's role is to expose the actual {@link ComputerLauncher} which is derived from the user * configured {@link ComputerConnector} * * @author Aaron Phillips */ public class EC2ImageLaunchWrapper extends ComputerLauncher { private static final Logger LOGGER = Logger.getLogger(EC2ImageLaunchWrapper.class.getName()); private String instanceId; private int retryIntervalSeconds = 10; private int maxRetries = 60; private transient ComputerConnector computerConnector; /* factory for creating launcher based on hostname */ private transient ComputerLauncher computerLauncher; /* the thing that we are wrapping. actually connects to the node as a hudson slave */ private transient AmazonEC2Client ec2; private transient boolean testMode = false; private transient boolean preLaunchOk = false; public EC2ImageLaunchWrapper(ComputerConnector computerConnector, String secretKey, String accessKey, String instanceId) { this.instanceId = instanceId; this.computerConnector = computerConnector; AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); ec2 = new AmazonEC2Client(credentials); } protected InstanceStateName getInstanceState(String instanceId) { DescribeInstancesRequest descReq = new DescribeInstancesRequest().withInstanceIds(instanceId); Instance instance = ec2.describeInstances(descReq).getReservations().get(0).getInstances().get(0); return InstanceStateName.fromValue(instance.getState().getName()); } protected String getInstancePublicHostName() { DescribeInstancesRequest descReq = new DescribeInstancesRequest().withInstanceIds(instanceId); Instance instance = ec2.describeInstances(descReq).getReservations().get(0).getInstances().get(0); return instance.getPublicDnsName(); } public void stopInstance(PrintStream logger) { logger.println("EC2InstanceComputerLauncher: Stopping EC2 instance [" + instanceId + "] ..."); if (testMode) return; StopInstancesResult result = ec2.stopInstances(new StopInstancesRequest().withInstanceIds(instanceId)); if (result.getStoppingInstances().size() > 0 && result.getStoppingInstances().get(0).getCurrentState().equals(InstanceStateName.Stopped) && result.getStoppingInstances().get(0).getInstanceId().equals(instanceId)) { logger.println("EC2InstanceComputerLauncher: Stopped EC2 instance [" + instanceId + "] ..."); } } public void startInstance(PrintStream logger) { logger.println("EC2InstanceComputerLauncher: Starting EC2 instance [" + instanceId + "] ..."); if (testMode) return; ec2.startInstances(new StartInstancesRequest().withInstanceIds(instanceId)); } @Override public boolean isLaunchSupported() { if (preLaunchOk) { // if the EC2 instance is up, launch supported should be determined // by the underlying launcher // this prevents Demand RetentionStrategy from attempting to spin up // endless EC2 instances boolean launchSupported = computerLauncher.isLaunchSupported(); LOGGER.fine("EC2 instance is running, underlying launcher will now answer this question with " + launchSupported); return launchSupported; } // if the EC2 instance has not been started yet, return true here so we // seem like a legit auto-launchable launcher return true; } public boolean instanceIsRunning() { return (instanceId != null && getInstanceState(instanceId) == Running); } @Override public void launch(SlaveComputer computer, TaskListener listener) throws IOException, InterruptedException { try { if (instanceId != null && getInstanceState(instanceId) == Pending) { throw new IllegalStateException("EC2 Instance " + instanceId + " is in Pending state. Not sure what to do here, try again?"); } if (instanceId == null || getInstanceState(instanceId) == Stopped || getInstanceState(instanceId) == ShuttingDown) { // only start the EC2 instance if we haven't tried before or the instance was stopped externally preLaunch(listener.getLogger()); preLaunchOk = true; } else { LOGGER.info("Skipping EC2 part of launch, since the instance is already running"); } } catch (IllegalStateException ise) { listener.error(ise.getMessage()); return; } catch (AmazonServiceException ase) { listener.error(ase.getMessage()); return; } catch (AmazonClientException ace) { listener.error(ace.getMessage()); return; } LOGGER.info("EC2 instance [" + instanceId + "] has been started to serve as a Jenkins slave. Passing control to computer launcher."); try { computerLauncher = computerConnector.launch(getInstancePublicHostName(), listener); } catch (IOException ioException) { LOGGER.warning("IOException Error1: " + ioException.toString()); } catch (InterruptedException interruptedException) { LOGGER.warning("InterruptedException Error1: " + interruptedException.toString()); } catch (Exception exception) { LOGGER.warning("Exception Error1: " + exception.toString()); } try { computerLauncher.launch(computer, listener); } catch (IOException ioException) { LOGGER.warning("IOException Error2: " + ioException.toString()); } catch (InterruptedException interruptedException) { LOGGER.warning("InterruptedException Error2: " + interruptedException.toString()); } catch (Exception exception) { LOGGER.warning("Exception Error2: " + exception.toString()); } } /** * Creates an EC2 instance given an AMI, readying it to serve as a Jenkins slave once launch is called later * * @param logger where to write messages so the user will see them in the tailed log in Jenkins * * @throws InterruptedException if the check status wait fails */ public void preLaunch(PrintStream logger) throws InterruptedException { logger.println("Starting EC2 instance [" + instanceId + "]..."); if (testMode) return; int retries = 0; InstanceStateName state = null; while (++retries <= maxRetries) { logger.println(MessageFormat.format("Checking state of instance [{0}]...", instanceId)); state = getInstanceState(instanceId); logger.println(MessageFormat.format("State of instance [{0}] is [{1}]", instanceId, state.toString())); if (state == Running) { logger.println(MessageFormat.format( "Instance [{0}] is running, proceeding to launching Jenkins on this instance", instanceId)); return; } else if (state == Pending) { logger.println( MessageFormat.format("Instance [{0}] is pending, waiting for [{1}] seconds before retrying", instanceId, retryIntervalSeconds)); Thread.sleep(retryIntervalSeconds * 1000); } else if (state == Stopped) { startInstance(logger); } else { String msg = MessageFormat.format( "Instance [{0}] encountered unexpected state [{1}]. Aborting launch", instanceId, state.toString()); logger.println(msg); throw new IllegalStateException(msg); } } throw new IllegalStateException("Maximum Number of retries " + maxRetries + " exceeded. Aborting launch"); } @Override public void afterDisconnect(SlaveComputer computer, TaskListener listener) { if (computerLauncher == null) { LOGGER.info("Unable to shut down EC2 instance because the launcher was not found."); return; } computerLauncher.afterDisconnect(computer, listener); String offlineReason = computer.getOfflineCauseReason(); LOGGER.info(computer.getOfflineCauseReason() + ":" + computer.getOfflineCause().getClass().toString()); if (offlineReason == null || offlineReason.contains("because computer was idle") || offlineReason.toLowerCase().contains("shutdown")) { LOGGER.info("Stopping EC2 instance " + instanceId); stopInstance(listener.getLogger()); } preLaunchOk = false; } @Override public void beforeDisconnect(SlaveComputer computer, TaskListener listener) { super.beforeDisconnect(computer, listener); } @Override public Descriptor<ComputerLauncher> getDescriptor() { return computerLauncher.getDescriptor(); } }