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; } } }
private boolean IsDeploymentOfStatus(String deploymentId, String status) { DescribeDeploymentsRequest request = new DescribeDeploymentsRequest() .withDeploymentIds(deploymentId); final DescribeDeploymentsResult result = opsWorks.describeDeployments(request); for (Deployment deployment : result.getDeployments()) { if(!deployment.getStatus().equals(status)) { return false; } } return true; }
/** * 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()); } } } }
@Override protected GoPluginApiResponse handleTaskExecution(GoPluginApiRequest request) { Map<String, Object> response = new HashMap<String, Object>(); try { Map<String, Object> map = (Map<String, Object>) new GsonBuilder().create().fromJson(request.requestBody(), Object.class); Map<String, Object> configVars = (Map<String, Object>) map.get("config"); Map<String, Object> context = (Map<String, Object>) map.get("context"); Map<String, String> envVars = (Map<String, String>) context.get("environmentVariables"); String appId = getValue(configVars, "appId"); String layerId = getValue(configVars, "layerId"); String noWait = getValue(configVars, "noWaitTrue"); boolean noWaitValue = (noWait != null && noWait.equals("true")); String comment = String.format("Deploy build %s via go.cd", envVars.get("GO_PIPELINE_COUNTER")); String revision = envVars.get("GO_REVISION"); log(String.format("[opsworks] Deployment of [appId=%s] started.", appId)); OpsWorksClient opsWorksClient = new OpsWorksClient( envVars.get(AWS_ACCESS_KEY_ID), envVars.get(AWS_SECRET_ACCESS_KEY), envVars.get(AWS_DEFAULT_REGION) ); Deployment d = opsWorksClient.deploy(appId, layerId, comment, revision, noWaitValue); if (d.getStatus().equals("successful") || noWaitValue) { response.put("success", true); response.put("message", String.format("[opsworks] Deployment of [appId=%s] completed successfully.", appId)); } else { response.put("success", false); response.put("message", String.format("[opsworks] Deployment of [appId=%s] failed.", appId)); } } catch (Exception e) { response.put("success", false); response.put("message", "[opsworks] Deployment interrupted. Reason: " + e.getMessage()); } return renderJSON(SUCCESS_RESPONSE_CODE, response); }