public static boolean wasSuccessfulDeployment(AWSOpsWorksClient opsClient, String deploymentId) { while (true) { Deployment deployment = opsClient .describeDeployments( new DescribeDeploymentsRequest() .withDeploymentIds(deploymentId)) .getDeployments().get(0); if (deployment.getStatus().equalsIgnoreCase("successful")) { System.out.println("Deployment " + deploymentId + " was successful"); return true; } else if (deployment.getStatus().equalsIgnoreCase("failed")) { return false; } } }
public void execute() { AmazonS3Client s3Client = getOrCreateClient(AmazonS3Client.class); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); AWSTestUtils.emptyAndDeleteBucket(s3Client, bucketName); client.deleteApp(new DeleteAppRequest().withAppId(getProject() .getProperty("appId"))); try { OpsWorksDeploymentTests.stopAllInstances(getProject(), client); } catch (InterruptedException e) { throw new BuildException(e.getMessage(), e); } OpsWorksDeploymentTests.deleteAllInstances(getProject(), client); OpsWorksDeploymentTests.deleteAllLayers(getProject(), client); client.deleteStack(new DeleteStackRequest().withStackId(getProject() .getProperty("stackId"))); }
/** * Deploys each deployment in each deployment group, waits for the * deployments to succeed, then deploys the next group until finished. */ public void execute() { AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); for (DeploymentGroup deploymentGroup : deploymentGroups) { deploymentGroup.setClient(client); deploymentGroup.deployApps(); } }
/** * Waits for a deployment group to succeed * * @param deploymentIds * The set of the IDs of the deployments in the group * @param client * The client to use to access AWSOpsWorks * @throws InterruptedException * If the thread is interrupted */ public void waitForDeploymentGroupToSucceed(Set<String> deploymentIds, AWSOpsWorksClient client) throws InterruptedException { int count = 0; while (true) { if (deploymentIds.isEmpty()) { return; } Thread.sleep(1000 * 10); if (count++ > 100) { throw new BuildException( "Deployment never failed or succeeded"); } List<Deployment> deployments = client.describeDeployments( new DescribeDeploymentsRequest() .withDeploymentIds(deploymentIds)) .getDeployments(); for (Deployment deployment : deployments) { String status = deployment.getStatus(); System.out.println(deployment.getDeploymentId() + " : " + status); if (status.equalsIgnoreCase("failed")) { throw new BuildException("Deployment " + deployment.getDeploymentId() + " failed"); } else if (status.equalsIgnoreCase("successful")) { deploymentIds.remove(deployment.getDeploymentId()); } } } }
public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); UpdateAppRequest updateAppRequest = new UpdateAppRequest() .withName(name).withType(type).withEnableSsl(enableSsl) .withAppId(appId).withDescription(description) .withDataSources(dataSources).withDomains(domains) .withAttributes(attributes); if (repoType != null && repoUrl != null) { Source appSource = new Source().withType(repoType).withUrl(repoUrl) .withSshKey(repoSshKey).withRevision(repoRevision) .withPassword(repoPassword).withUsername(repoUsername); updateAppRequest.setAppSource(appSource); } if (enableSsl) { if (sslCertificate != null && sslChain != null && sslPrivateKey != null) { SslConfiguration sslConfiguration = new SslConfiguration() .withCertificate(sslCertificate).withChain(sslChain) .withPrivateKey(sslPrivateKey); updateAppRequest.setSslConfiguration(sslConfiguration); } } try { client.updateApp(updateAppRequest); System.out.println("Update app request submitted. "); } catch (Exception e) { throw new BuildException("Could not update app: " + e.getMessage(), e); } }
/** * Creates a deployment according to the set parameters. Also sets a * deploymentId property. Which property will be set depends on what order * this deployment is created in the project. If it is the first deployment * created in this Ant build, deployment1 is set. If it's the second, * deployment2 is set, etc. The ID is also printed for you to set to your * own property for later use. */ public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); CreateDeploymentRequest createDeploymentRequest = new CreateDeploymentRequest() .withStackId(stackId).withAppId(appId).withCommand(command) .withInstanceIds(instanceIds); if (comment != null) { createDeploymentRequest.setComment(comment); } if (customJson != null) { createDeploymentRequest.setCustomJson(customJson); } String deploymentId; try { deploymentId = client.createDeployment(createDeploymentRequest) .getDeploymentId(); System.out .println("Deployment request submitted. You can view the status of your deployment at https://console.aws.amazon.com/opsworks/home?#/stack/" + stackId + "/deployments/" + deploymentId); this.deploymentId = deploymentId; } catch (Exception e) { throw new BuildException("Could not create deployment: " + e.getMessage(), e); } if (deploymentId != null) { if (getProject().getProperty(Constants.DEPLOYMENT_IDS_PROPERTY) == null) { getProject().setProperty(Constants.DEPLOYMENT_IDS_PROPERTY, deploymentId); } else { getProject().setProperty( Constants.DEPLOYMENT_IDS_PROPERTY, getProject().getProperty(Constants.DEPLOYMENT_IDS_PROPERTY) + "," + deploymentId); } if (propertyNameForDeploymentId != null) { getProject().setProperty(propertyNameForDeploymentId, deploymentId); setDescription(deploymentId); } } }
public static void deleteAllInstances(Project project, AWSOpsWorksClient client) { for (String instanceId : project.getProperty(Constants.INSTANCE_IDS_PROPERTY) .split(",")) { client.deleteInstance(new DeleteInstanceRequest() .withInstanceId(instanceId)); } }
public void execute() { AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); try { AWSTestUtils.waitForOpsworksInstanceToReachState(client, instanceId, state); } catch (Exception e) { throw new BuildException(e.getMessage(), e); } }
public void execute() { checkParams(); if (!OpsWorksDeploymentTests.wasSuccessfulDeployment( getOrCreateClient(AWSOpsWorksClient.class), deploymentId)) { throw new BuildException("deployment " + deploymentId + " failed"); } }
public static void waitForOpsworksInstanceToReachState( AWSOpsWorksClient client, String instanceId, String state) throws InterruptedException { System.out.println("Waiting for instance " + instanceId + " to transition to " + state); int count = 0; while (true) { Thread.sleep(1000 * 30); if (count++ > 100) { throw new RuntimeException("Never reached " + state); } Instance instance = client .describeInstances( new DescribeInstancesRequest() .withInstanceIds(instanceId)) .getInstances().get(0); String status = instance.getStatus(); System.out.println(status); if (status.contains("failed")) { throw new RuntimeException("instance failed to launch"); } if (!status.equalsIgnoreCase(state)) { continue; } return; } }
/** * Creates an app using the specified parameters. It also sets the "appId" * property to the ID of the created app. However it will only do this if * you have not set the "appId" property yourself. The ID is also printed * for you to set to your own property for later use. */ public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); CreateAppRequest createAppRequest = new CreateAppRequest() .withStackId(stackId).withName(name).withType(type) .withEnableSsl(enableSsl).withShortname(shortname) .withDescription(description); if (dataSources.size() > 0) { createAppRequest.setDataSources(dataSources); } if (domains.size() > 0) { createAppRequest.setDomains(domains); } if (attributes.size() > 0) { createAppRequest.setAttributes(attributes); } if (repoType != null && repoUrl != null) { Source appSource = new Source().withType(repoType).withUrl(repoUrl) .withSshKey(repoSshKey).withRevision(repoRevision) .withPassword(repoPassword).withUsername(repoUsername); createAppRequest.setAppSource(appSource); } if (enableSsl) { if (sslCertificate != null && sslChain != null && sslPrivateKey != null) { SslConfiguration sslConfiguration = new SslConfiguration() .withCertificate(sslCertificate).withChain(sslChain) .withPrivateKey(sslPrivateKey); createAppRequest.setSslConfiguration(sslConfiguration); } } String appId; try { appId = client.createApp(createAppRequest).getAppId(); System.out.println("Created app with appId " + appId); } catch (Exception e) { throw new BuildException("Could not create app: " + e.getMessage(), e); } if (appId != null) { if (propertyNameForAppId.equals(Constants.APP_ID_PROPERTY) && getProject().getProperty(Constants.APP_ID_PROPERTY) != null) { getProject().addReference(Constants.APP_ID_REFERENCE, true); } else { getProject().addReference(Constants.APP_ID_REFERENCE, false); getProject().setNewProperty(propertyNameForAppId, appId); } } }
/** * Creates an instance according to the set parameters. Also sets an * instanceId property. Which property will be set depends on what order * this instance is created in the project. If it is the first instance * created in this Ant build, instanceId1 is set. If it's the second, * instanceId2 is set, etc. The ID is also printed for you to set to your * own property for later use. */ public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); CreateInstanceRequest createInstanceRequest = new CreateInstanceRequest() .withStackId(stackId) .withInstallUpdatesOnBoot(installUpdatesOnBoot) .withEbsOptimized(ebsOptimized).withLayerIds(layerIds) .withInstanceType(instanceType).withOs(os) .withAmiId(amiId).withSshKeyName(sshKeyName) .withAvailabilityZone(availabilityZone) .withVirtualizationType(virtualizationType) .withRootDeviceType(rootDeviceType).withSubnetId(subnetId); String instanceId; if(autoScalingType != null) { createInstanceRequest.setAutoScalingType(autoScalingType); } if(architecture != null) { createInstanceRequest.setArchitecture(architecture); } try { instanceId = client.createInstance(createInstanceRequest) .getInstanceId(); if (startOnCreate) { client.startInstance(new StartInstanceRequest() .withInstanceId(instanceId)); System.out.println("Starting created instance."); } Thread.sleep(10); // Wait for the instance metadata to populate System.out .println("Created instance with instanceId " + instanceId + ". View the status of this instance at https://console.aws.amazon.com/opsworks/home?#/stack/" + stackId + "/instances"); } catch (Exception e) { throw new BuildException("Could not create Instance: " + e.getMessage(), e); } if (instanceId != null) { if (getProject().getProperty(Constants.INSTANCE_IDS_PROPERTY) == null) { getProject().setProperty(Constants.INSTANCE_IDS_PROPERTY, instanceId); } else { getProject().setProperty( Constants.INSTANCE_IDS_PROPERTY, getProject().getProperty(Constants.INSTANCE_IDS_PROPERTY) + "," + instanceId); } if (propertyNameForInstanceId != null) { getProject().setProperty(propertyNameForInstanceId, instanceId); } } }
/** * Creates a stack according to the set parameters. Also sets the stackId * property to the created stack's ID. The ID is also printed for you to set * to your own property for later use. */ public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); CreateStackRequest createStackRequest = new CreateStackRequest() .withName(name).withRegion(region) .withServiceRoleArn(serviceRoleArn) .withUseOpsworksSecurityGroups(useOpsworksSecurityGroups) .withUseCustomCookbooks(useCustomCookbooks).withVpcId(vpcId) .withDefaultAvailabilityZone(defaultAvailabilityZone) .withDefaultOs(defaultOs) .withDefaultRootDeviceType(defaultRootDeviceType) .withDefaultInstanceProfileArn(defaultInstanceProfileArn) .withDefaultSshKeyName(defaultSshKeyName) .withHostnameTheme(hostnameTheme).withCustomJson(customJson); if (attributes.size() > 0) { createStackRequest.setAttributes(attributes); } if (chefVersion != null) { createStackRequest .setConfigurationManager(new StackConfigurationManager() .withName(CHEF).withVersion(chefVersion)); } if (useCustomCookbooks) { if (repoType != null && repoUrl != null) { Source customCookBookSource = new Source().withType(repoType) .withUrl(repoUrl).withSshKey(repoSshKey) .withRevision(repoRevision).withPassword(repoPassword) .withUsername(repoUsername); createStackRequest .setCustomCookbooksSource(customCookBookSource); } } if (manageBerkshelf) { ChefConfiguration chefConfiguration = new ChefConfiguration() .withManageBerkshelf(manageBerkshelf); if (berkshelfVersion != null) { chefConfiguration.setBerkshelfVersion(berkshelfVersion); } createStackRequest.setChefConfiguration(chefConfiguration); } String stackId; try { stackId = client.createStack(createStackRequest).getStackId(); System.out.println("Created stack with stackId " + stackId); } catch (Exception e) { throw new BuildException("Could not create stack: " + e.getMessage(), e); } if (startOnCreate) { client.startStack(new StartStackRequest().withStackId(stackId)); System.out.println("Started stack."); } if (stackId != null) { if (propertyNameForStackId.equals(Constants.STACK_ID_PROPERTY) && getProject().getProperty(Constants.STACK_ID_PROPERTY) != null) { getProject().addReference(Constants.STACK_ID_REFERENCE, true); } else { getProject().addReference(Constants.STACK_ID_REFERENCE, false); getProject().setNewProperty(propertyNameForStackId, stackId); } } }
/** * Creates a layer according to the set parameters. Also sets a layerId * property. Which property will be set depends on what order this layer is * created in the project. If it is the first layer created in this Ant * build, layerId1 is set. If it's the second, layerId2 is set, etc. The ID * is also printed for you to set to your own property for later use. */ public void execute() { checkParams(); AWSOpsWorksClient client = getOrCreateClient(AWSOpsWorksClient.class); CreateLayerRequest createLayerRequest = new CreateLayerRequest() .withStackId(stackId).withType(type).withName(name) .withShortname(shortname) .withEnableAutoHealing(enableAutoHealing) .withAutoAssignElasticIps(autoAssignElasticIps) .withAutoAssignPublicIps(autoAssignPublicIps) .withInstallUpdatesOnBoot(installUpdatesOnBoot) .withUseEbsOptimizedInstances(useEbsOptimizedInstances) .withCustomInstanceProfileArn(customInstanceProfileArn); if (attributes.size() > 0) { createLayerRequest.setAttributes(attributes); } if (packages.size() > 0) { createLayerRequest.setPackages(packages); } if (volumeConfigurations.size() > 0) { createLayerRequest.setVolumeConfigurations(volumeConfigurations); } if (customSecurityGroupIds.size() > 0) { createLayerRequest .setCustomSecurityGroupIds(customSecurityGroupIds); } if (isSetCustomRecipe) { Recipes customRecipes = new Recipes() .withConfigure(configureRecipes).withDeploy(deployRecipes) .withSetup(setupRecipes).withShutdown(shutdownRecipes) .withUndeploy(undeployRecipes); createLayerRequest.setCustomRecipes(customRecipes); } String layerId; try { layerId = client.createLayer(createLayerRequest).getLayerId(); } catch (Exception e) { throw new BuildException("Could not create layer: " + e.getMessage(), e); } System.out.println("Created layer with ID " + layerId); if (layerId != null) { if (getProject().getProperty(Constants.LAYER_IDS_PROPERTY) == null) { getProject().setProperty(Constants.LAYER_IDS_PROPERTY, layerId); } else { getProject().setProperty( Constants.LAYER_IDS_PROPERTY, getProject().getProperty(Constants.LAYER_IDS_PROPERTY) + "," + layerId); } if (propertyNameForLayerId != null) { getProject().setProperty(propertyNameForLayerId, layerId); } } }
@BeforeClass public static void setUp() { s3Client = new AmazonS3Client(); client = new AWSOpsWorksClient(); iamClient = new AmazonIdentityManagementClient(); }
public static void deleteAllLayers(Project project, AWSOpsWorksClient client) { for (String layerId : project.getProperty(Constants.LAYER_IDS_PROPERTY) .split(",")) { client.deleteLayer(new DeleteLayerRequest().withLayerId(layerId)); } }
public AwsOpsWorksDeployUtils(String region) throws MojoFailureException { Region awsRegion = Region.getRegion(Regions.fromName(region)); awsOpsWorksClient = new AWSOpsWorksClient(); awsOpsWorksClient.setRegion(awsRegion); }
/** * Set the client to use to access AWS OpsWorks. * * @param client * The client to use to access AWS OpsWorks. */ public void setClient(AWSOpsWorksClient client) { this.client = client; }