Skip to content

Commit

Permalink
use SynchronousNonBlockingStepExecution for some steps for better e…
Browse files Browse the repository at this point in the history
…rror handling
  • Loading branch information
hoegertn committed Mar 5, 2018
1 parent 0b3c4f5 commit ae52e4a
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 187 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -467,6 +467,14 @@ The step returns the ARN of the created identity provider.
def idp = updateIdP(name: 'nameToCreateOrUpdate', metadata: 'pathToMetadataFile')
```

## updateTrustPolicy

Update the assume role trust policy of the given role using the provided file.

```
updateTrustPolicy(roleName: 'SomeRole', policyFile: 'path/to/somefile.json')
```

## setAccountAlias

Create or update the AWS account alias.
Expand Down Expand Up @@ -528,6 +536,7 @@ ec2ShareAmi(
# Changelog

## current master
* use `SynchronousNonBlockingStepExecution` for some steps for better error handling

## 1.24
* Do not fail job on empty change set creation
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/AWSIdentityStep.java
Expand Up @@ -31,7 +31,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
Expand Down Expand Up @@ -74,7 +74,7 @@ public Set<? extends Class<?>> getRequiredContext() {
}
}

public static class Execution extends SynchronousStepExecution<Map<String, String>> {
public static class Execution extends SynchronousNonBlockingStepExecution<Map<String, String>> {

protected Execution(@Nonnull StepContext context) {
super(context);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/CFInvalidateStep.java
Expand Up @@ -28,7 +28,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import com.amazonaws.services.cloudfront.AmazonCloudFront;
Expand Down Expand Up @@ -84,7 +84,7 @@ public String getDisplayName() {
}
}

public static class Execution extends SynchronousStepExecution<Void> {
public static class Execution extends SynchronousNonBlockingStepExecution<Void> {

private final transient CFInvalidateStep step;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/DeployAPIStep.java
Expand Up @@ -29,7 +29,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -104,7 +104,7 @@ public String getDisplayName() {
}
}

public static class Execution extends SynchronousStepExecution<Void> {
public static class Execution extends SynchronousNonBlockingStepExecution<Void> {

private final transient DeployAPIStep step;

Expand Down
27 changes: 15 additions & 12 deletions src/main/java/de/taimos/pipeline/aws/EC2ShareAmiStep.java
Expand Up @@ -21,27 +21,30 @@

package de.taimos.pipeline.aws;

import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.LaunchPermission;
import com.amazonaws.services.ec2.model.LaunchPermissionModifications;
import com.amazonaws.services.ec2.model.ModifyImageAttributeRequest;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

import de.taimos.pipeline.aws.utils.StepUtils;
import hudson.Extension;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;

public class EC2ShareAmiStep extends Step {

Expand Down Expand Up @@ -95,7 +98,7 @@ public Set<? extends Class<?>> getRequiredContext() {
}
}

public static class Execution extends SynchronousStepExecution<String> {
public static class Execution extends SynchronousNonBlockingStepExecution<String> {

private final transient EC2ShareAmiStep step;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/ECRLoginStep.java
Expand Up @@ -28,7 +28,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -83,7 +83,7 @@ public Set<? extends Class<?>> getRequiredContext() {
}
}

public static class Execution extends SynchronousStepExecution<String> {
public static class Execution extends SynchronousNonBlockingStepExecution<String> {

private final transient ECRLoginStep step;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/InvokeLambdaStep.java
Expand Up @@ -30,7 +30,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -115,7 +115,7 @@ public String getDisplayName() {
}
}

public static class Execution extends SynchronousStepExecution<Object> {
public static class Execution extends SynchronousNonBlockingStepExecution<Object> {

private static final long serialVersionUID = 1L;

Expand Down
52 changes: 17 additions & 35 deletions src/main/java/de/taimos/pipeline/aws/ListAWSAccountsStep.java
Expand Up @@ -27,15 +27,13 @@
import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnull;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import com.amazonaws.services.cloudformation.model.AmazonCloudFormationException;
import com.amazonaws.services.organizations.AWSOrganizations;
import com.amazonaws.services.organizations.AWSOrganizationsClientBuilder;
import com.amazonaws.services.organizations.model.Account;
Expand Down Expand Up @@ -77,46 +75,30 @@ public String getDisplayName() {
}
}

public static class Execution extends StepExecution {
public static class Execution extends SynchronousNonBlockingStepExecution<List> {

public Execution(StepContext context) {
super(context);
}

@Override
public boolean start() throws Exception {
protected List run() throws Exception {
this.getContext().get(TaskListener.class).getLogger().format("Getting AWS accounts %n");

new Thread("listAWSAccounts") {
@Override
public void run() {
AWSOrganizations client = AWSClientFactory.create(AWSOrganizationsClientBuilder.standard(), Execution.this.getContext());
ListAccountsResult exports = client.listAccounts(new ListAccountsRequest());

List<Map<String, String>> accounts = new ArrayList<>();
for (Account account : exports.getAccounts()) {
Map<String, String> awsAccount = new HashMap<>();
awsAccount.put("id", account.getId());
awsAccount.put("arn", account.getArn());
awsAccount.put("name", account.getName());
awsAccount.put("safeName", SafeNameCreator.createSafeName(account.getName()));
awsAccount.put("status", account.getStatus());
accounts.add(awsAccount);
}

try {
Execution.this.getContext().onSuccess(accounts);
} catch (AmazonCloudFormationException e) {
Execution.this.getContext().onFailure(e);
}
}
}.start();
return false;
}

@Override
public void stop(@Nonnull Throwable cause) throws Exception {
//
AWSOrganizations client = AWSClientFactory.create(AWSOrganizationsClientBuilder.standard(), Execution.this.getContext());
ListAccountsResult exports = client.listAccounts(new ListAccountsRequest());

List<Map<String, String>> accounts = new ArrayList<>();
for (Account account : exports.getAccounts()) {
Map<String, String> awsAccount = new HashMap<>();
awsAccount.put("id", account.getId());
awsAccount.put("arn", account.getArn());
awsAccount.put("name", account.getName());
awsAccount.put("safeName", SafeNameCreator.createSafeName(account.getName()));
awsAccount.put("status", account.getStatus());
accounts.add(awsAccount);
}
return accounts;
}

private static final long serialVersionUID = 1L;
Expand Down
34 changes: 9 additions & 25 deletions src/main/java/de/taimos/pipeline/aws/SNSPublishStep.java
Expand Up @@ -23,12 +23,11 @@

import java.util.Set;

import javax.annotation.Nonnull;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import com.amazonaws.services.sns.AmazonSNS;
Expand Down Expand Up @@ -88,7 +87,7 @@ public String getDisplayName() {
}
}

public static class Execution extends StepExecution {
public static class Execution extends SynchronousNonBlockingStepExecution<Void> {

private final transient SNSPublishStep step;

Expand All @@ -98,33 +97,18 @@ public Execution(SNSPublishStep step, StepContext context) {
}

@Override
public boolean start() throws Exception {
protected Void run() throws Exception {
final String topicArn = this.step.getTopicArn();
final String subject = this.step.getSubject();
final String message = this.step.getMessage();

new Thread("snsPublish") {
@Override
public void run() {
try {
TaskListener listener = Execution.this.getContext().get(TaskListener.class);
AmazonSNS snsClient = AWSClientFactory.create(AmazonSNSClientBuilder.standard(), Execution.this.getContext());

listener.getLogger().format("Publishing notification %s to %s %n", subject, topicArn);
PublishResult result = snsClient.publish(topicArn, message, subject);
listener.getLogger().format("Message published as %s %n", result.getMessageId());
Execution.this.getContext().onSuccess(null);
} catch (Exception e) {
Execution.this.getContext().onFailure(e);
}
}
}.start();
return false;
}
TaskListener listener = this.getContext().get(TaskListener.class);
AmazonSNS snsClient = AWSClientFactory.create(AmazonSNSClientBuilder.standard(), this.getContext());

@Override
public void stop(@Nonnull Throwable cause) throws Exception {
//
listener.getLogger().format("Publishing notification %s to %s %n", subject, topicArn);
PublishResult result = snsClient.publish(topicArn, message, subject);
listener.getLogger().format("Message published as %s %n", result.getMessageId());
return null;
}

private static final long serialVersionUID = 1L;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/taimos/pipeline/aws/SetAccountAliasStep.java
Expand Up @@ -27,7 +27,7 @@
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import com.amazonaws.services.identitymanagement.AmazonIdentityManagement;
Expand Down Expand Up @@ -77,7 +77,7 @@ public String getDisplayName() {
}
}

public static class Execution extends SynchronousStepExecution<Void> {
public static class Execution extends SynchronousNonBlockingStepExecution<Void> {

@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.")
private final transient String name;
Expand Down

0 comments on commit ae52e4a

Please sign in to comment.