Search in sources :

Example 1 with AmazonCloudFormationException

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

the class CloudFormationStack method update.

public void update(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, long pollIntervallMillis, String roleArn) throws ExecutionException {
    try {
        UpdateStackRequest req = new UpdateStackRequest();
        req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM);
        if (templateBody != null && !templateBody.isEmpty()) {
            req.setTemplateBody(templateBody);
        } else if (templateUrl != null && !templateUrl.isEmpty()) {
            req.setTemplateURL(templateUrl);
        } else {
            req.setUsePreviousTemplate(true);
        }
        req.withParameters(params).withTags(tags).withRoleARN(roleArn);
        this.client.updateStack(req);
        new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, this.client.waiters().stackUpdateComplete(), pollIntervallMillis);
        this.listener.getLogger().format("Updated CloudFormation stack %s %n", this.stack);
    } catch (AmazonCloudFormationException e) {
        if (e.getMessage().contains("No updates are to be performed")) {
            this.listener.getLogger().format("No updates were needed for CloudFormation stack %s %n", this.stack);
            return;
        }
        this.listener.getLogger().format("Failed to update CloudFormation stack %s %n", this.stack);
        throw e;
    }
}
Also used : AmazonCloudFormationException(com.amazonaws.services.cloudformation.model.AmazonCloudFormationException) UpdateStackRequest(com.amazonaws.services.cloudformation.model.UpdateStackRequest)

Example 2 with AmazonCloudFormationException

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

the class CFNValidateStepTests method validateWithUrlFailure.

@Test
public void validateWithUrlFailure() throws Exception {
    WorkflowJob job = this.jenkinsRule.jenkins.createProject(WorkflowJob.class, "cfnTest");
    AmazonCloudFormationException ex = new AmazonCloudFormationException("invalid template");
    Mockito.when(this.cloudFormation.validateTemplate(Mockito.any(ValidateTemplateRequest.class))).thenThrow(ex);
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  cfnValidate(url: 'foo')" + "}\n", true));
    this.jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0));
    ArgumentCaptor<ValidateTemplateRequest> captor = ArgumentCaptor.forClass(ValidateTemplateRequest.class);
    Mockito.verify(this.cloudFormation).validateTemplate(captor.capture());
    Assertions.assertThat(captor.getValue()).isEqualTo(new ValidateTemplateRequest().withTemplateURL("foo"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ValidateTemplateRequest(com.amazonaws.services.cloudformation.model.ValidateTemplateRequest) AmazonCloudFormationException(com.amazonaws.services.cloudformation.model.AmazonCloudFormationException) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with AmazonCloudFormationException

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

the class EventPrinter method waitAndPrintEvents.

private void waitAndPrintEvents(String stack, long pollIntervalMillis, BasicFuture<AmazonWebServiceRequest> waitResult) throws ExecutionException {
    Date startDate = new Date();
    String lastEventId = null;
    this.printLine();
    this.printStackName(stack);
    this.printLine();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    if (pollIntervalMillis > 0) {
        while (!waitResult.isDone()) {
            try {
                DescribeStackEventsResult result = this.client.describeStackEvents(new DescribeStackEventsRequest().withStackName(stack));
                List<StackEvent> stackEvents = new ArrayList<>();
                for (StackEvent event : result.getStackEvents()) {
                    if (event.getEventId().equals(lastEventId) || event.getTimestamp().before(startDate)) {
                        break;
                    }
                    stackEvents.add(event);
                }
                if (!stackEvents.isEmpty()) {
                    Collections.reverse(stackEvents);
                    for (StackEvent event : stackEvents) {
                        this.printEvent(sdf, event);
                        this.printLine();
                    }
                    lastEventId = stackEvents.get(stackEvents.size() - 1).getEventId();
                }
            } catch (AmazonCloudFormationException e) {
            // suppress and continue
            }
            try {
                Thread.sleep(pollIntervalMillis);
            } catch (InterruptedException e) {
            // suppress and continue
            }
        }
    }
    try {
        waitResult.get();
    } catch (InterruptedException e) {
        this.listener.getLogger().format("Failed to wait for CFN action to complete: %s", e.getMessage());
    }
}
Also used : DescribeStackEventsResult(com.amazonaws.services.cloudformation.model.DescribeStackEventsResult) StackEvent(com.amazonaws.services.cloudformation.model.StackEvent) ArrayList(java.util.ArrayList) AmazonCloudFormationException(com.amazonaws.services.cloudformation.model.AmazonCloudFormationException) SimpleDateFormat(java.text.SimpleDateFormat) DescribeStackEventsRequest(com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest) Date(java.util.Date)

Aggregations

AmazonCloudFormationException (com.amazonaws.services.cloudformation.model.AmazonCloudFormationException)3 DescribeStackEventsRequest (com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest)1 DescribeStackEventsResult (com.amazonaws.services.cloudformation.model.DescribeStackEventsResult)1 StackEvent (com.amazonaws.services.cloudformation.model.StackEvent)1 UpdateStackRequest (com.amazonaws.services.cloudformation.model.UpdateStackRequest)1 ValidateTemplateRequest (com.amazonaws.services.cloudformation.model.ValidateTemplateRequest)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1