Search in sources :

Example 1 with UpdateStackSetResult

use of com.amazonaws.services.cloudformation.model.UpdateStackSetResult in project pipeline-aws-plugin by jenkinsci.

the class CloudFormationStackSet method update.

public UpdateStackSetResult update(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags) {
    this.listener.getLogger().format("Updating CloudFormation stack set %s %n", this.stackSet);
    UpdateStackSetRequest req = new UpdateStackSetRequest().withStackSetName(this.stackSet).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM).withParameters(params).withTags(tags);
    if (templateBody != null && !templateBody.isEmpty()) {
        req.setTemplateBody(templateBody);
    } else if (templateUrl != null && !templateUrl.isEmpty()) {
        req.setTemplateURL(templateUrl);
    } else {
        req.setUsePreviousTemplate(true);
    }
    UpdateStackSetResult result = this.client.updateStackSet(req);
    this.listener.getLogger().format("Updated CloudFormation stack set %s %n", this.stackSet);
    return result;
}
Also used : UpdateStackSetResult(com.amazonaws.services.cloudformation.model.UpdateStackSetResult) UpdateStackSetRequest(com.amazonaws.services.cloudformation.model.UpdateStackSetRequest)

Example 2 with UpdateStackSetResult

use of com.amazonaws.services.cloudformation.model.UpdateStackSetResult in project pipeline-aws-plugin by jenkinsci.

the class CloudFormationStackSet method doUpdate.

private UpdateStackSetResult doUpdate(UpdateStackSetRequest req, int attempt) throws InterruptedException {
    try {
        this.listener.getLogger().format("Attempting to update CloudFormation stack set %s %n", this.stackSet);
        UpdateStackSetResult result = this.client.updateStackSet(req);
        this.listener.getLogger().format("Updated CloudFormation stack set %s %n", this.stackSet);
        return result;
    } catch (OperationInProgressException | StaleRequestException e) {
        if (attempt == MAX_STACK_SET_RETRY_ATTEMPT_COUNT) {
            this.listener.getLogger().format("Retries exhausted and cloudformation stack set %s is still busy%n", this.stackSet);
            throw e;
        } else {
            long sleepDuration = this.sleepStrategy.calculateSleepDuration(attempt);
            this.listener.getLogger().format("StackSet %s busy. Waiting %d ms %n", this.stackSet, sleepDuration);
            Thread.sleep(sleepDuration);
            return doUpdate(req, attempt + 1);
        }
    } catch (LimitExceededException lee) {
        if (lee.getMessage().startsWith("StackSet operations cannot involve more than")) {
            if (attempt == MAX_STACK_SET_RETRY_ATTEMPT_COUNT) {
                this.listener.getLogger().format("Retries exhausted and cloudformation stack set operations %s is still busy%n", this.stackSet);
                throw lee;
            } else {
                long sleepDuration = this.sleepStrategy.calculateSleepDuration(attempt);
                this.listener.getLogger().format("Too many concurrent operations in progress (%s). Waiting for %s update. Waiting %d ms %n", lee.getMessage(), this.stackSet, sleepDuration);
                Thread.sleep(sleepDuration);
                return doUpdate(req, attempt + 1);
            }
        } else {
            throw lee;
        }
    }
}
Also used : UpdateStackSetResult(com.amazonaws.services.cloudformation.model.UpdateStackSetResult) StaleRequestException(com.amazonaws.services.cloudformation.model.StaleRequestException) LimitExceededException(com.amazonaws.services.cloudformation.model.LimitExceededException) OperationInProgressException(com.amazonaws.services.cloudformation.model.OperationInProgressException)

Example 3 with UpdateStackSetResult

use of com.amazonaws.services.cloudformation.model.UpdateStackSetResult in project pipeline-aws-plugin by jenkinsci.

the class CFNUpdateStackSetStepTest method updateExistingStackStackSetWithOperationPreferences.

@Test
public void updateExistingStackStackSetWithOperationPreferences() throws Exception {
    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "cfnTest");
    Mockito.when(stackSet.exists()).thenReturn(true);
    String operationId = UUID.randomUUID().toString();
    Mockito.when(stackSet.update(Mockito.anyString(), Mockito.anyString(), Mockito.any(UpdateStackSetRequest.class))).thenReturn(new UpdateStackSetResult().withOperationId(operationId));
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  cfnUpdateStackSet(stackSet: 'foo', operationPreferences: [failureToleranceCount: 5, regionOrder: ['us-west-2'], failureTolerancePercentage: 17, maxConcurrentCount: 18, maxConcurrentPercentage: 34])" + "}\n", true));
    jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
    PowerMockito.verifyNew(CloudFormationStackSet.class, Mockito.atLeastOnce()).withArguments(Mockito.any(AmazonCloudFormation.class), Mockito.eq("foo"), Mockito.any(TaskListener.class), Mockito.eq(SleepStrategy.EXPONENTIAL_BACKOFF_STRATEGY));
    ArgumentCaptor<UpdateStackSetRequest> requestCapture = ArgumentCaptor.forClass(UpdateStackSetRequest.class);
    Mockito.verify(stackSet).update(Mockito.anyString(), Mockito.anyString(), requestCapture.capture());
    Assertions.assertThat(requestCapture.getValue().getOperationPreferences()).isEqualTo(new StackSetOperationPreferences().withFailureToleranceCount(5).withRegionOrder("us-west-2").withFailureTolerancePercentage(17).withMaxConcurrentCount(18).withMaxConcurrentPercentage(34));
    Mockito.verify(stackSet).waitForOperationToComplete(operationId, Duration.ofSeconds(1));
}
Also used : UpdateStackSetResult(com.amazonaws.services.cloudformation.model.UpdateStackSetResult) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) TaskListener(hudson.model.TaskListener) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) UpdateStackSetRequest(com.amazonaws.services.cloudformation.model.UpdateStackSetRequest) StackSetOperationPreferences(com.amazonaws.services.cloudformation.model.StackSetOperationPreferences) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with UpdateStackSetResult

use of com.amazonaws.services.cloudformation.model.UpdateStackSetResult in project pipeline-aws-plugin by jenkinsci.

the class CFNUpdateStackSetStepTest method updateExistingStackWithCustomAdminRole.

@Test
public void updateExistingStackWithCustomAdminRole() throws Exception {
    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "cfnTest");
    Mockito.when(stackSet.exists()).thenReturn(true);
    String operationId = UUID.randomUUID().toString();
    Mockito.when(stackSet.update(Mockito.anyString(), Mockito.anyString(), Mockito.any(UpdateStackSetRequest.class))).thenReturn(new UpdateStackSetResult().withOperationId(operationId));
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  cfnUpdateStackSet(stackSet: 'foo', administratorRoleArn: 'bar', executionRoleName: 'baz')" + "}\n", true));
    jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
    PowerMockito.verifyNew(CloudFormationStackSet.class, Mockito.atLeastOnce()).withArguments(Mockito.any(AmazonCloudFormation.class), Mockito.eq("foo"), Mockito.any(TaskListener.class), Mockito.eq(SleepStrategy.EXPONENTIAL_BACKOFF_STRATEGY));
    Mockito.verify(stackSet).update(Mockito.anyString(), Mockito.anyString(), Mockito.any(UpdateStackSetRequest.class));
}
Also used : UpdateStackSetResult(com.amazonaws.services.cloudformation.model.UpdateStackSetResult) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) TaskListener(hudson.model.TaskListener) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) UpdateStackSetRequest(com.amazonaws.services.cloudformation.model.UpdateStackSetRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with UpdateStackSetResult

use of com.amazonaws.services.cloudformation.model.UpdateStackSetResult in project pipeline-aws-plugin by jenkinsci.

the class CFNUpdateStackSetStepTest method updateExistantStack.

@Test
public void updateExistantStack() throws Exception {
    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "cfnTest");
    Mockito.when(stackSet.exists()).thenReturn(true);
    String operationId = UUID.randomUUID().toString();
    Mockito.when(stackSet.update(Mockito.anyString(), Mockito.anyString(), Mockito.any(UpdateStackSetRequest.class))).thenReturn(new UpdateStackSetResult().withOperationId(operationId));
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  cfnUpdateStackSet(stackSet: 'foo', pollInterval: 27, params: ['foo=bar'], paramsFile: 'params.json')" + "}\n", true));
    try (PrintWriter writer = new PrintWriter(jenkinsRule.jenkins.getWorkspaceFor(job).child("params.json").write())) {
        writer.println("[{\"ParameterKey\": \"foo1\", \"ParameterValue\": \"25\"}]");
    }
    jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
    PowerMockito.verifyNew(CloudFormationStackSet.class, Mockito.atLeastOnce()).withArguments(Mockito.any(AmazonCloudFormation.class), Mockito.eq("foo"), Mockito.any(TaskListener.class), Mockito.eq(SleepStrategy.EXPONENTIAL_BACKOFF_STRATEGY));
    ArgumentCaptor<UpdateStackSetRequest> requestCapture = ArgumentCaptor.forClass(UpdateStackSetRequest.class);
    Mockito.verify(stackSet).update(Mockito.anyString(), Mockito.anyString(), requestCapture.capture());
    Assertions.assertThat(requestCapture.getValue().getParameters()).containsExactlyInAnyOrder(new Parameter().withParameterKey("foo").withParameterValue("bar"), new Parameter().withParameterKey("foo1").withParameterValue("25"));
    Mockito.verify(stackSet).waitForOperationToComplete(operationId, Duration.ofMillis(27));
}
Also used : UpdateStackSetResult(com.amazonaws.services.cloudformation.model.UpdateStackSetResult) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) TaskListener(hudson.model.TaskListener) Parameter(com.amazonaws.services.cloudformation.model.Parameter) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) UpdateStackSetRequest(com.amazonaws.services.cloudformation.model.UpdateStackSetRequest) PrintWriter(java.io.PrintWriter) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

UpdateStackSetResult (com.amazonaws.services.cloudformation.model.UpdateStackSetResult)6 UpdateStackSetRequest (com.amazonaws.services.cloudformation.model.UpdateStackSetRequest)5 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)4 TaskListener (hudson.model.TaskListener)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)4 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Parameter (com.amazonaws.services.cloudformation.model.Parameter)2 StackSetOperationPreferences (com.amazonaws.services.cloudformation.model.StackSetOperationPreferences)2 PrintWriter (java.io.PrintWriter)2 AwsSyncClientBuilder (com.amazonaws.client.builder.AwsSyncClientBuilder)1 LimitExceededException (com.amazonaws.services.cloudformation.model.LimitExceededException)1 OperationInProgressException (com.amazonaws.services.cloudformation.model.OperationInProgressException)1 StackInstanceSummary (com.amazonaws.services.cloudformation.model.StackInstanceSummary)1 StackSetSummary (com.amazonaws.services.cloudformation.model.StackSetSummary)1 StaleRequestException (com.amazonaws.services.cloudformation.model.StaleRequestException)1 Tag (com.amazonaws.services.cloudformation.model.Tag)1 AWSClientFactory (de.taimos.pipeline.aws.AWSClientFactory)1 EnvVars (hudson.EnvVars)1