Skip to content

Commit

Permalink
Merge pull request #4 from gfleury/fixing-concurrency
Browse files Browse the repository at this point in the history
Workaround for race condition in multibranch configuration. Contexts …
  • Loading branch information
gfleury authored Sep 1, 2018
2 parents b95e87c + 96d636d commit 394ac52
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
44 changes: 22 additions & 22 deletions jenkinstsuruclient.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-test-sources/injected" isTestSource="true" generated="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:credentials:2.1.9" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:credentials:2.1.16" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr4-runtime:4.5" level="project" />
<orderEntry type="library" name="Maven: org.abego.treelayout:org.abego.treelayout.core:1.0.1" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-cps:2.17" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-step-api:2.3" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-api:2.3" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-support:2.1" level="project" />
<orderEntry type="library" name="Maven: org.jboss.marshalling:jboss-marshalling-river:1.4.9.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.marshalling:jboss-marshalling:1.4.9.Final" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-scm-step:1.15" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:script-security:1.21" level="project" />
<orderEntry type="library" name="Maven: org.kohsuke:groovy-sandbox:1.10" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:scm-api:1.1" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:structs:1.5" level="project" />
<orderEntry type="library" name="Maven: com.cloudbees:groovy-cps:1.10" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-cps:2.45" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-step-api:2.13" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-api:2.25" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-support:2.17" level="project" />
<orderEntry type="library" name="Maven: org.jboss.marshalling:jboss-marshalling-river:1.4.12.jenkins-3" level="project" />
<orderEntry type="library" name="Maven: org.jboss.marshalling:jboss-marshalling:1.4.12.jenkins-3" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins.workflow:workflow-scm-step:2.4" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:script-security:1.39" level="project" />
<orderEntry type="library" name="Maven: org.kohsuke:groovy-sandbox:1.16" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:scm-api:2.0.8" level="project" />
<orderEntry type="library" name="Maven: com.cloudbees:groovy-cps:1.22" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.ui:jquery-detached:1.2.1" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.ui:ace-editor:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.cloudbees:diff4j:1.2" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:durable-task:1.12" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:durable-task:1.22" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:structs:1.14" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci:symbol-annotation:1.14" level="project" />
<orderEntry type="library" name="Maven: io.tsuru:tsuru-client-java:1.0.0" level="project" />
<orderEntry type="library" name="Maven: io.swagger:swagger-annotations:1.5.15" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp:okhttp:2.7.5" level="project" />
Expand All @@ -40,8 +42,7 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:annotations:3.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.jcip:jcip-annotations:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci.main:jenkins-core:2.60.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci.plugins.icon-shim:icon-set:1.0.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci.main:remoting:3.7" level="project" />
Expand Down Expand Up @@ -88,11 +89,11 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.sf.ezmorph:ezmorph:1.0.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: args4j:args4j:2.0.31" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci:annotation-indexer:1.11" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci:annotation-indexer:1.11" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci:bytecode-compatibility-transformer:1.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.kohsuke:asm5:5.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci:task-reactor:1.4" level="project" />
<orderEntry type="library" name="Maven: org.jvnet.localizer:localizer:1.24" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jvnet.localizer:localizer:1.24" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: antlr:antlr:2.7.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jvnet.hudson:xstream:1.4.7-jenkins-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: jfree:jfreechart:1.0.9" level="project" />
Expand Down Expand Up @@ -147,12 +148,11 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: org.kohsuke.jinterop:j-interopdeps:2.0.6-kohsuke-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.samba.jcifs:jcifs:1.2.19" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jvnet.robust-http-client:robust-http-client:1.2" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci:symbol-annotation:1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-codec:commons-codec:1.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.kohsuke:access-modifier-annotation:1.12" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-fileupload:commons-fileupload:1.3.1-jenkins-1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:11.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:11.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.jcraft:jzlib:1.1.3-kohsuke-1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.modules:instance-identity:2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: io.github.stephenc.crypto:self-signed-cert-generator:1.0.0" level="project" />
Expand All @@ -167,7 +167,7 @@
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.ui:jquery-detached:core-assets:1.2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.ui:bootstrap:core-assets:1.3.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.ui:handlebars:core-assets:1.1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.main:jenkins-test-harness:2.28" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.main:jenkins-test-harness:2.34" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-webapp:9.4.5.v20170502" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-xml:9.4.5.v20170502" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-util:9.4.5.v20170502" level="project" />
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.6</version>
<version>3.4</version>
<relativePath />
</parent>
<artifactId>jenkins-tsuru-client</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.3-SNAPSHOT</version>
<packaging>hpi</packaging>
<properties>
<jenkins.version>2.107.3</jenkins.version>
<java.level>8</java.level>
<jenkins.version>2.60.3</jenkins.version>
</properties>
<name>Tsuru Plugin</name>
<name>Tsuru Client Jenkins Plugin</name>
<description>Tsuru.io integration plugin</description>
<url>https://tsuru.io/</url>
<!-- The default licence for Jenkins OSS Plugins is MIT. Substitute for the applicable one if needed. -->
Expand Down
60 changes: 47 additions & 13 deletions src/main/resources/org/jenkinsci/plugins/tsuru/TsuruDSL.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import hudson.AbortException
import hudson.EnvVars
import hudson.FilePath
import hudson.Util
import hudson.model.Run
import io.tsuru.client.api.TsuruApi
import io.tsuru.client.model.LoginToken
import org.jenkinsci.plugins.tsuru.pipeline.TsuruAction
import org.jenkinsci.plugins.tsuru.pipeline.TsuruContextInit
import org.jenkinsci.plugins.workflow.cps.EnvActionImpl

import hudson.model.Job;

import java.util.logging.Logger

class TsuruDSL implements Serializable {
Expand All @@ -25,28 +28,52 @@ class TsuruDSL implements Serializable {

private transient Tsuru.DescriptorImpl config = new Tsuru.DescriptorImpl();

private transient TsuruApi apiInstance = null;
private transient static HashMap<String, TsuruApi> apiInstance = null;

private transient Job job;

Boolean authenticated = false;

public TsuruDSL(org.jenkinsci.plugins.workflow.cps.CpsScript script) {
this.script = script;

if (this.apiInstance == null) {
this.apiInstance = new HashMap<String, TsuruApi>();
}

if (this.contexts == null) {
this.contexts = new HashMap<String, Context>();
}

Run<?, ?> build = script.$build();
if (build == null) {
throw new IllegalStateException("No associated build");
}
this.job = build.getParent();
}

public TsuruApi connect() {
apiInstance = new TsuruApi();
String apiUrl = currentContext.getServerUrl();
Context localCurrentContext = contexts.get(this.job.getFullName());
LOGGER.println("Running connect on -> " + localCurrentContext.getServerUrl());
LOGGER.println("Job Name -> " + this.job.getFullName());
TsuruApi localApiInstance = this.apiInstance.get(localCurrentContext.getServerUrl());
if (localApiInstance == null) {
localApiInstance = new TsuruApi();
this.apiInstance.put(localCurrentContext.getServerUrl(), localApiInstance);
}

String apiUrl = localCurrentContext.getServerUrl();
if (apiUrl.endsWith("/")) {
apiUrl = apiUrl.substring(0, apiUrl.length() - 1);
}
apiInstance.getApiClient().setBasePath(apiUrl);
if (currentContext.getEmail() != null) {
LoginToken token = apiInstance.login(currentContext.getEmail(), currentContext.getPassword());
localApiInstance.getApiClient().setBasePath(apiUrl);
if (localCurrentContext.getEmail() != null) {
LoginToken token = localApiInstance.login(localCurrentContext.getEmail(), localCurrentContext.getPassword());
} else {
apiInstance.getApiClient().addDefaultHeader("Authorization", "bearer " + currentContext.getToken());
localApiInstance.getApiClient().addDefaultHeader("Authorization", "bearer " + localCurrentContext.getToken());
}
authenticated = true;
return apiInstance;
return localApiInstance;
}

public Boolean deploy(String appName) {
Expand Down Expand Up @@ -138,8 +165,10 @@ class TsuruDSL implements Serializable {
}

private Boolean executeTsuruAction (TsuruAction.Action action, HashMap<String, String> Param) {
Context localCurrentContext = contexts.get(this.job.getFullName());
TsuruApi localApiInstance = this.apiInstance.get(localCurrentContext.getServerUrl());
Map Args = [
apiInstance: this.apiInstance,
apiInstance: localApiInstance,
action: action,
Args: Param
]
Expand All @@ -148,6 +177,7 @@ class TsuruDSL implements Serializable {
}

private Context currentContext = null;
private HashMap<String, Context> contexts = null;

enum ContextId implements Serializable{
WITH_API("tsuru.withAPI"), WITH_APPLICATION("tsuru.withApplication")
Expand Down Expand Up @@ -308,11 +338,13 @@ class TsuruDSL implements Serializable {
}

public String application() {
return currentContext.getApplication();
Context localCurrentContext = contexts.get(this.job.getFullName());
return localCurrentContext.getApplication();
}

public String tsuruApi() {
return currentContext.getServerUrl();
Context localCurrentContext = contexts.get(this.job.getFullName());
return localCurrentContext.getServerUrl();
}

public <V> V withAPI(Object tname =null, Object tusername=null, Object tpassword=null, Closure<V> body) {
Expand All @@ -322,8 +354,8 @@ class TsuruDSL implements Serializable {
String password = toSingleString(tpassword);

node {

dieIfWithin(ContextId.WITH_API, currentContext, ContextId.WITH_APPLICATION)
// TODO: Check why it's failing hard.
//dieIfWithin(ContextId.WITH_API, currentContext, ContextId.WITH_APPLICATION)

Context context = new Context(null, ContextId.WITH_API);

Expand All @@ -346,6 +378,8 @@ class TsuruDSL implements Serializable {
context.setPassword(password);
}

contexts.put(this.job.getFullName(), context);

context.run {
body()
}
Expand Down

0 comments on commit 394ac52

Please sign in to comment.