Skip to content
Permalink
Browse files
Merge pull request #10 from synopsys-arc-oss/versions_envinject
[JENKINS-19177] - Inject default versions into environment
  • Loading branch information
oleg-nenashev committed Sep 5, 2013
2 parents c452b78 + 483d18a commit 468a968cb4197acccbb843612b963cb451af04d7
@@ -1,5 +1,5 @@
/*
* Copyright 2012, CloudBees Inc.
* Copyright 2012, CloudBees Inc., Synopsys Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/*
* Copyright 2012, CloudBees Inc.
* Copyright 2012, CloudBees Inc., Synopsys Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,13 @@

package com.cloudbees.jenkins.plugins.customtools;

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolsLogger;
import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolException;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvStringParseHelper;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvVariablesInjector;
import com.synopsys.arc.jenkinsci.plugins.customtools.LabelSpecifics;
import com.synopsys.arc.jenkinsci.plugins.customtools.PathsList;
import com.synopsys.arc.jenkinsci.plugins.customtools.multiconfig.MulticonfigWrapperOptions;
import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersion;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
@@ -46,6 +45,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

@@ -56,6 +56,7 @@
* Installs tools selected by the user. Exports configured paths and a home variable for each tool.
*
* @author rcampbell
* @author Oleg Nenashev
*
*/
public class CustomToolInstallWrapper extends BuildWrapper {
@@ -95,16 +96,27 @@ public CustomToolInstallWrapper(SelectedTool[] selectedTools, MulticonfigWrapper

public boolean isConvertHomesToUppercase() {
return convertHomesToUppercase;
}
}

@Override
public Environment setUp(AbstractBuild build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
return new Environment(){

final EnvVars buildEnv = build.getEnvironment(listener);
final Node node = build.getBuiltOn();

return new Environment(){
@Override
public boolean tearDown(AbstractBuild build, BuildListener listener)
throws IOException, InterruptedException {
return true;
public void buildEnvVars(Map<String, String> env) {
// TODO: Inject Home dirs as well
for (CustomTool tool : customTools()) {
if (tool.hasVersions()) {
ToolVersion version = ToolVersion.getEffectiveToolVersion(tool, buildEnv, node);
if (version != null && !env.containsKey(version.getVariableName())) {
env.put(version.getVariableName(), version.getDefaultVersion());
}
}
}
}
};
}
@@ -120,18 +132,19 @@ public boolean tearDown(AbstractBuild build, BuildListener listener)
}
return tools;
}

/**
* The heart of the beast. Installs selected tools and exports their paths to the
* PATH and their HOMEs as environment variables.
*/
@Override
public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
BuildListener listener) throws IOException, InterruptedException,
RunnerAbortedException {

RunnerAbortedException {
EnvVars buildEnv = build.getEnvironment(listener);
final EnvVars homesAndVersions = new EnvVars();
final EnvVars homes = new EnvVars();
final EnvVars versions = new EnvVars();

final PathsList paths = new PathsList();
final List<EnvVariablesInjector> additionalVarInjectors = new LinkedList<EnvVariablesInjector>();

@@ -149,7 +162,7 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
CustomToolsLogger.LogMessage(listener, tool.getName(), "Starting installation");

// Check versioning
CheckVersions(tool, listener, buildEnv, node, homesAndVersions);
CheckVersions(tool, listener, buildEnv, node, versions);

// This installs the tool if necessary
CustomTool installed = tool
@@ -182,10 +195,10 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
CustomToolsLogger.LogMessage(listener, installed.getName(), "Tool is installed at "+ installed.getHome());
String homeDirVarName = (convertHomesToUppercase ? installed.getName().toUpperCase() : installed.getName()) +"_HOME";
CustomToolsLogger.LogMessage(listener, installed.getName(), "Setting "+ homeDirVarName+"="+installed.getHome());
homesAndVersions.put(homeDirVarName, installed.getHome());
homes.put(homeDirVarName, installed.getHome());
}

return new DecoratedLauncher(launcher) {
return new DecoratedLauncher(launcher) {
@Override
public Proc launch(ProcStarter starter) throws IOException {
EnvVars vars = toEnvVars(starter.envs());
@@ -194,7 +207,8 @@ public Proc launch(ProcStarter starter) throws IOException {
String overridenPaths = vars.get("PATH");
overridenPaths += paths.toListString();
vars.override("PATH", overridenPaths);
vars.putAll(homesAndVersions);
vars.putAll(homes);
vars.putAll(versions);

// Inject additional variables
for (EnvVariablesInjector injector : additionalVarInjectors) {
@@ -203,7 +217,7 @@ public Proc launch(ProcStarter starter) throws IOException {

return super.launch(starter.envs(Util.mapToEnv(vars)));
}

private EnvVars toEnvVars(String[] envs) {
EnvVars vars = new EnvVars();
for (String line : envs) {
@@ -227,29 +241,25 @@ private EnvVars toEnvVars(String[] envs) {
public void CheckVersions (CustomTool tool, BuildListener listener, EnvVars buildEnv, Node node, EnvVars target) throws CustomToolException {
// Check version
if (tool.hasVersions()) {
ExtendedChoiceParameterDefinition def = tool.getToolVersion().getVersionsListSource();
String defaultVersion = hudson.Util.fixEmptyAndTrim(def.getEffectiveDefaultValue());
CustomToolsLogger.LogMessage(listener, tool.getName(), "Tool has versions. Default version is "+def.getName()+"="+defaultVersion);

// Check if node has version specified
String subst = "${"+def.getName()+"}";
String res = EnvStringParseHelper.resolveExportedPath(subst, node);
if (!subst.equals(res)) {
CustomToolsLogger.LogMessage(listener, tool.getName(), "Version "+res+" has been specified by node or global variables");
} else if (buildEnv.containsKey(def.getName())) {
String envVersion = buildEnv.get(def.getName());
CustomToolsLogger.LogMessage(listener, tool.getName(), "Version "+envVersion+" has been specified by the build environment");
} else if (defaultVersion != null){
CustomToolsLogger.LogMessage(listener, tool.getName(), "No version has been specified. Using default version");
target.addLine(def.getName()+"="+defaultVersion);
buildEnv.addLine(def.getName()+"="+defaultVersion);
} else {
ToolVersion version = ToolVersion.getEffectiveToolVersion(tool, buildEnv, node);
if (version == null) {
CustomToolsLogger.LogMessage(listener, tool.getName(), "Error: No version has been specified, no default version. Failing the build...");
throw new CustomToolException("Version has not been specified for the "+tool.getName());
}
}

CustomToolsLogger.LogMessage(listener, tool.getName(), "Version "+version.getActualVersion()+" has been specified by "+version.getVersionSource());

// Override default versions
if (version.getVersionSource().equals(ToolVersion.DEFAULTS_SOURCE)) {
String envStr = version.getVariableName()+"="+version.getDefaultVersion();
target.addLine(envStr);
buildEnv.addLine(envStr);
}
}
}



@Override
public Descriptor<BuildWrapper> getDescriptor() {
return DESCRIPTOR;
@@ -1,5 +1,5 @@
/*
* Copyright 2012, CloudBees Inc.
* Copyright 2012, CloudBees Inc., Synopsys Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@
* override.
*
* @author rcampbell
*
* @author Oleg Nenashev
*/
public class DecoratedLauncher extends Launcher {
private Launcher inner = null;
@@ -0,0 +1,120 @@
/*
* Copyright 2013 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc..
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.synopsys.arc.jenkinsci.plugins.customtools.versions;

import com.cloudbees.jenkins.plugins.customtools.CustomTool;
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvStringParseHelper;
import hudson.EnvVars;
import hudson.model.Node;
import java.io.Serializable;

/**
* A stub for a tool versions.
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since 0.4
*/
public class ToolVersion implements Serializable {
private String variableName;
private String defaultVersion;
private String actualVersion;
private String versionSource;
public static final String DEFAULTS_SOURCE = "defaults";

/**
* Constructs a default version.
* @param variableName
* @param defaultVersion
*/
private ToolVersion(String variableName, String defaultVersion) {
this(variableName, defaultVersion, null, null);
}

private ToolVersion (ToolVersion defaultVersion, String actualVersion, String versionSource) {
this(defaultVersion.getVariableName(), defaultVersion.getDefaultVersion(), actualVersion, versionSource);
}

public ToolVersion(String variableName, String defaultVersion, String actualVersion, String versionSource) {
this.variableName = variableName;
this.defaultVersion = defaultVersion;
this.actualVersion = actualVersion;
this.versionSource = versionSource;
}

public boolean hasDefaultVersion() {
return defaultVersion != null;
}

public String getDefaultVersion() {
return defaultVersion;
}

public void setDefaultVersion(String defaultVersion) {
this.defaultVersion = defaultVersion;
}

public String getVariableName() {
return variableName;
}

public String getActualVersion() {
return actualVersion;
}

public String getVersionSource() {
return versionSource;
}

public static ToolVersion getDefaultToolVersion(CustomTool tool) {
ExtendedChoiceParameterDefinition def = tool.getToolVersion().getVersionsListSource();
String defaultVersion = hudson.Util.fixEmptyAndTrim(def.getEffectiveDefaultValue());
return new ToolVersion(def.getName(), defaultVersion);
}

/**
* Method gets effective tool version for the build
* @param tool Custom tool
* @param buildEnv Current build environment
* @param node Node, where the build runs
* @return Effective tool version. null in case of unavailable version
*/
public static ToolVersion getEffectiveToolVersion(CustomTool tool, EnvVars buildEnv, Node node) {
ToolVersion defaultVersion = getDefaultToolVersion(tool);

// Check if node has version specified
String subst = "${"+defaultVersion.getVariableName()+"}";

// Try to find variable in environment
String res = EnvStringParseHelper.resolveExportedPath(subst, node);
if (!subst.equals(res)) {
return new ToolVersion(defaultVersion, res, "node or global variables");
} else if (buildEnv.containsKey(defaultVersion.getVariableName())) {
String envVersion = buildEnv.get(defaultVersion.getVariableName());
return new ToolVersion(defaultVersion, envVersion, "build environment");
} else if (defaultVersion.hasDefaultVersion()){
return new ToolVersion(defaultVersion, defaultVersion.getDefaultVersion(), DEFAULTS_SOURCE);
} else {
return null;
}
}

@Override
public String toString() {
return defaultVersion;
}


}

0 comments on commit 468a968

Please sign in to comment.