Skip to content

Commit

Permalink
Added support of additional variables, which can be specified individ…
Browse files Browse the repository at this point in the history
…ually for labels.

Resolves: https://issues.jenkins-ci.org/browse/JENKINS-18774

Signed-off-by: Oleg Nenashev <nenashev@synopsys.com>
  • Loading branch information
oleg-nenashev committed Jul 18, 2013
1 parent 3bada61 commit 8adaaaa
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 87 deletions.
Expand Up @@ -18,13 +18,13 @@

import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolException;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvStringParseHelper;
import com.synopsys.arc.jenkinsci.plugins.customtools.LabelSpecifics;
import com.synopsys.arc.jenkinsci.plugins.customtools.PathsList;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.Util;
import hudson.model.EnvironmentSpecific;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
Expand Down Expand Up @@ -58,43 +58,44 @@ public class CustomTool extends ToolInstallation implements
* File set includes string like **\/bin These will be added to the PATH
*/
private final String exportedPaths;
private final String additionalVariables;
private final LabelSpecifics[] labelSpecifics;

@DataBoundConstructor
public CustomTool(String name, String home, List properties,
String exportedPaths, String additionalVariables) {
String exportedPaths, LabelSpecifics[] labelSpecifics) {
super(name, home, properties);
this.exportedPaths = exportedPaths;
this.additionalVariables = Util.fixEmpty(additionalVariables);
this.labelSpecifics = (labelSpecifics != null) ? labelSpecifics : new LabelSpecifics[0];
}

public String getExportedPaths() {
return exportedPaths;
}

public String getAdditionalVariables() {
return additionalVariables;
public LabelSpecifics[] getLabelSpecifics() {
return labelSpecifics;
}

@Override
public CustomTool forEnvironment(EnvVars environment) {
return new CustomTool(getName(), environment.expand(getHome()),
getProperties().toList(), environment.expand(exportedPaths),
environment.expand(additionalVariables));
LabelSpecifics.substitute(labelSpecifics, environment));
}

@Override
public CustomTool forNode(Node node, TaskListener log) throws IOException,
InterruptedException {
String substitutedPath = EnvStringParseHelper.resolveExportedPath(exportedPaths, node);
String substitutedAddVars = EnvStringParseHelper.resolveExportedPath(additionalVariables, node);
String substitutedHomeDir = EnvStringParseHelper.resolveExportedPath(translateFor(node, log), node);

return new CustomTool(getName(), substitutedHomeDir, getProperties().toList(), substitutedPath, substitutedAddVars);
return new CustomTool(getName(), substitutedHomeDir, getProperties().toList(),
substitutedPath, LabelSpecifics.substitute(labelSpecifics, node));
}

//FIXME: just a stub
public CustomTool forBuildProperties(Map<JobPropertyDescriptor,JobProperty> properties) {
return new CustomTool(getName(), getHome(), getProperties().toList(), getExportedPaths(), getAdditionalVariables());
return new CustomTool(getName(), getHome(), getProperties().toList(), getExportedPaths(), labelSpecifics);
}

/**
Expand Down
Expand Up @@ -17,6 +17,8 @@
package com.cloudbees.jenkins.plugins.customtools;

import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolException;
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 hudson.AbortException;
Expand All @@ -32,12 +34,14 @@
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Run.RunnerAbortedException;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONObject;
Expand Down Expand Up @@ -120,6 +124,7 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
EnvVars buildEnv = build.getEnvironment(listener);
final EnvVars homes = new EnvVars();
final PathsList paths = new PathsList();
final List<EnvVariablesInjector> additionalVarInjectors = new LinkedList<EnvVariablesInjector>();

// Handle multi-configuration build
if (MatrixBuild.class.isAssignableFrom(build.getClass())) {
Expand All @@ -143,6 +148,18 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
throw new AbortException(ex.getMessage());
}

// Prepare additional variables
Node node = Computer.currentComputer().getNode();
for (LabelSpecifics spec : tool.getLabelSpecifics()) {
if (!spec.appliesTo(node)) {
continue;
}

if (spec.hasAdditionalVars()) {
additionalVarInjectors.add(EnvVariablesInjector.Create(spec.getAdditionalVars()));
}
}

listener.getLogger().println("[CustomTools] - "+tool.getName()+" is installed at "+ installed.getHome());

homes.put(tool.getName()+"_HOME", installed.getHome());
Expand All @@ -162,7 +179,9 @@ public Proc launch(ProcStarter starter) throws IOException {
vars.putAll(homes);

// Inject additional variables
vars.
for (EnvVariablesInjector injector : additionalVarInjectors) {
injector.Inject(vars);
}

return super.launch(starter.envs(Util.mapToEnv(vars)));
}
Expand Down
Expand Up @@ -72,7 +72,7 @@ public static String resolveExportedPath(String exportedPaths, Node node) {

return substitutedString;
}

/**
* Substitutes string according to node property.
* @param macroString String to be substituted
Expand Down

This file was deleted.

@@ -0,0 +1,119 @@
/*
* 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;

import hudson.EnvVars;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;

/**
* Tool-specific environment variables injector.
* Implements additionalVariables for CutomTool.
* @author Oleg Nenashev <nenashev@synopsys.com>
* @since 0.3
*/
public class EnvVariablesInjector extends TreeMap<String, EnvVariablesInjector.Entity>
{
private EnvVariablesInjector(){}

public static EnvVariablesInjector Create(String props) throws IOException {
Properties prop = new Properties();
StringReader rdr = new StringReader(props);
prop.load(rdr);

EnvVariablesInjector vars = new EnvVariablesInjector();
for (Entry<Object,Object> entry: prop.entrySet())
{
String varName = (String)entry.getKey();
Entity ent = new Entity(varName, (String)entry.getValue());
vars.put(varName, ent);
}
return vars;
}

/**
* Inject variables into EnvVars
* @param target Target variables
* @throws IOException Exception during modification of EnvVars
*/
public void Inject(EnvVars target) throws IOException {
for (Entry<String, EnvVariablesInjector.Entity> entry: entrySet()) {
entry.getValue().Inject(target);
}
}

/**
* Internal entry, which describes modification of Environment Variables
*/
//TODO: Handle delimiters
//TODO: Handle modification conflicts
// etc.
public static class Entity {
public String envName;
public String envValue;
public final static String DEFAULT_LIST_DELIMITER=",";

@Deprecated
public String listDelimiter;
@Deprecated
public boolean isList;
@Deprecated
public boolean isOverrides;

public Entity(String envName, String envValue) {
this(envName, envValue, DEFAULT_LIST_DELIMITER, false, true);
}

/**
* @deprecated Not implemented in 0.3
* @param envName
* @param envValue
* @param listDelimiter
* @param isList
* @param isOverrides
*/
public Entity(String envName, String envValue, String listDelimiter,
boolean isList, boolean isOverrides)
{
this.envName = envName;
this.envValue = envValue;
this.listDelimiter = listDelimiter;
this.isList = isList;
this.isOverrides = isOverrides;
}

/**
* Inject variables into EnvVars
* @param target Target environment
* @throws IOException Exception during modification of EnvVars
*/
public void Inject(EnvVars target) throws IOException {
//TODO: check overrides
//TODO: check lists
String val = target.get(envName);

//TODO: substitute, check, etc.
val = envValue;
target.put(envName, val);
}
}


}

0 comments on commit 8adaaaa

Please sign in to comment.