Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 17, 2012
1 parent b920e78 commit 33662e7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 35 deletions.
Expand Up @@ -47,6 +47,16 @@ public void onOnline(Computer c, TaskListener listener) throws IOException, Inte
//Default properties vars
Map<String, String> globalPropertiesEnvVars = new HashMap<String, String>();

//Get env vars for the current node
Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
public Map<String, String> call() throws IOException {
return EnvVars.masterEnvVars;
}
}
);


//Global Properties
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvInjectNodeProperty) {
Expand All @@ -73,29 +83,21 @@ public Map<String, String> call() throws Throwable {
}


Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
public Map<String, String> call() throws IOException {
return EnvVars.masterEnvVars;
}
}
);

Node slave = Hudson.getInstance().getNode(c.getName());
//Specific nodeProperties can overrides the value if this is a slave
if (slave != null) {
for (NodeProperty<?> nodeProperty : c.getNode().getNodeProperties()) {
if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();

//Add global properties
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsPropertiesProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));

}
}
}


EnvVars envVars2Set = new EnvVars();
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
Expand Down
Expand Up @@ -5,6 +5,7 @@
import hudson.matrix.MatrixProject;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.remoting.Callable;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.tasks.BuildWrapper;
Expand Down Expand Up @@ -34,6 +35,14 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
if (!(build instanceof MatrixBuild)) {
EnvInjectLogger logger = new EnvInjectLogger(listener);
try {

//Process environment variables at node level
Node buildNode = build.getBuiltOn();
if (buildNode != null) {
loadEnvironmentVariablesNode(build, buildNode, listener);
}

//Load job envinject job property
if (isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentRun(build, launcher, listener);
}
Expand All @@ -50,6 +59,62 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
};
}

private void loadEnvironmentVariablesNode(AbstractBuild build, Node buildNode, BuildListener listener) throws EnvInjectException {

if (buildNode == null) {
return;
}

FilePath nodePath = buildNode.getRootPath();
if (nodePath == null) {
return;
}

try {
EnvInjectLogger logger = new EnvInjectLogger(listener);
//Default node envVars
Map<String, String> configNodeEnvVars = new HashMap<String, String>();

//Get env vars for the current node
Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
public Map<String, String> call() throws IOException {
return EnvVars.masterEnvVars;
}
}
);


for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

for (NodeProperty<?> nodeProperty : buildNode.getNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty variablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
EnvVars envVars = variablesNodeProperty.getEnvVars();
EnvInjectEnvVars envInjectEnvVars = new EnvInjectEnvVars(logger);
configNodeEnvVars.putAll(envVars);
envInjectEnvVars.resolveVars(configNodeEnvVars, nodeEnvVars);
}
}

EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(nodePath);
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, configNodeEnvVars);

} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
}
}

@SuppressWarnings("unchecked")
private void addBuildWrapper(AbstractBuild build, BuildWrapper buildWrapper) throws EnvInjectException {
try {
Expand Down Expand Up @@ -140,8 +205,10 @@ private Environment setUpEnvironmentRun(AbstractBuild build, Launcher launcher,
EnvInjectLogger logger = new EnvInjectLogger(listener);
logger.info("Preparing an environment for the job.");

Map<String, String> infraEnvVarsNode = new LinkedHashMap<String, String>();
Map<String, String> infraEnvVarsMaster = new LinkedHashMap<String, String>();
//Init infra env vars
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, logger);
Map<String, String> infraEnvVarsNode = new LinkedHashMap<String, String>(previousEnvVars);
Map<String, String> infraEnvVarsMaster = new LinkedHashMap<String, String>(previousEnvVars);

//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
Expand Down Expand Up @@ -296,25 +363,6 @@ private FilePath getNodeRootPath() {
return null;
}

private boolean isParameterAction(EnvironmentContributingAction a) {

if (a instanceof ParametersAction) {
return true;
}

return false;
}

private boolean isEnvInjectAction(EnvironmentContributingAction a) {

if ((EnvInjectBuilder.ENVINJECT_BUILDER_ACTION_NAME).equals(a.getDisplayName())) {
return true;
}

return false;
}


private Map<String, String> getEnvVarsByContribution(AbstractBuild build, EnvInjectJobProperty envInjectJobProperty, BuildListener listener) throws EnvInjectException {

assert envInjectJobProperty != null;
Expand Down
Expand Up @@ -124,7 +124,7 @@ public Map<String, String> getMergedVariables(Map<String, String> infraEnvVars,
}


private void resolveVars(Map<String, String> variables, Map<String, String> env) {
public void resolveVars(Map<String, String> variables, Map<String, String> env) {

//Resolve variables against env
for (Map.Entry<String, String> entry : variables.entrySet()) {
Expand Down
Expand Up @@ -11,9 +11,9 @@
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.lib.envinject.service.EnvInjectActionRetriever;
import org.jenkinsci.lib.envinject.service.EnvInjectDetector;
import org.jenkinsci.plugins.envinject.EnvInjectJobProperty;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.jenkinsci.plugins.envinject.EnvInjectPluginAction;

import java.io.IOException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -162,10 +162,10 @@ public EnvInjectJobProperty getEnvInjectJobProperty(AbstractBuild build) {

public Map<String, String> getEnvVarsPreviousSteps(AbstractBuild build, EnvInjectLogger logger) throws IOException, InterruptedException, EnvInjectException {
Map<String, String> result = new HashMap<String, String>();
EnvInjectDetector envInjectDetector = new EnvInjectDetector();
if (envInjectDetector.isEnvInjectActivated(build)) {
result.putAll(getCurrentInjectedEnvVars(build));

EnvInjectPluginAction envInjectAction = build.getAction(EnvInjectPluginAction.class);
if (envInjectAction != null) {
result.putAll(getCurrentInjectedEnvVars(build));
//Add build variables with axis for a MatrixRun
if (build instanceof MatrixRun) {
result.putAll(build.getBuildVariables());
Expand Down

0 comments on commit 33662e7

Please sign in to comment.