From 96d636d02c2a223bb4df8055af2cd42a0a65a82b Mon Sep 17 00:00:00 2001 From: gfleury Date: Sat, 1 Sep 2018 15:17:47 +0200 Subject: [PATCH] Workaround for race condition in multibranch configuration. Contexts per job/branch. --- jenkinstsuruclient.iml | 44 +++++++------- pom.xml | 8 +-- .../jenkinsci/plugins/tsuru/TsuruDSL.groovy | 60 +++++++++++++++---- 3 files changed, 73 insertions(+), 39 deletions(-) diff --git a/jenkinstsuruclient.iml b/jenkinstsuruclient.iml index 0dd3ba5..adf8da7 100644 --- a/jenkinstsuruclient.iml +++ b/jenkinstsuruclient.iml @@ -7,29 +7,31 @@ + - + - - - - - - - - - - - - + + + + + + + + + + + - + + + @@ -40,8 +42,7 @@ - - + @@ -88,11 +89,11 @@ - + - + @@ -147,12 +148,11 @@ - - - + + @@ -167,7 +167,7 @@ - + diff --git a/pom.xml b/pom.xml index 2e79c7c..6c0145c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,17 +4,17 @@ org.jenkins-ci.plugins plugin - 3.6 + 3.4 jenkins-tsuru-client - 1.0-SNAPSHOT + 1.3-SNAPSHOT hpi - 2.107.3 8 + 2.60.3 - Tsuru Plugin + Tsuru Client Jenkins Plugin Tsuru.io integration plugin https://tsuru.io/ diff --git a/src/main/resources/org/jenkinsci/plugins/tsuru/TsuruDSL.groovy b/src/main/resources/org/jenkinsci/plugins/tsuru/TsuruDSL.groovy index 8532421..77a3df0 100644 --- a/src/main/resources/org/jenkinsci/plugins/tsuru/TsuruDSL.groovy +++ b/src/main/resources/org/jenkinsci/plugins/tsuru/TsuruDSL.groovy @@ -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 { @@ -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 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(); + } + + if (this.contexts == null) { + this.contexts = new HashMap(); + } + + 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) { @@ -138,8 +165,10 @@ class TsuruDSL implements Serializable { } private Boolean executeTsuruAction (TsuruAction.Action action, HashMap 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 ] @@ -148,6 +177,7 @@ class TsuruDSL implements Serializable { } private Context currentContext = null; + private HashMap contexts = null; enum ContextId implements Serializable{ WITH_API("tsuru.withAPI"), WITH_APPLICATION("tsuru.withApplication") @@ -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 withAPI(Object tname =null, Object tusername=null, Object tpassword=null, Closure body) { @@ -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); @@ -346,6 +378,8 @@ class TsuruDSL implements Serializable { context.setPassword(password); } + contexts.put(this.job.getFullName(), context); + context.run { body() }