Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 20, 2012
1 parent 0dc9098 commit a15917a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 62 deletions.
Expand Up @@ -19,16 +19,13 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
* @author Gregory Boissinot
*/
@Extension
public class EnvInjectComputerListener extends ComputerListener implements Serializable {

private static final Logger LOGGER = Logger.getLogger(EnvInjectComputerListener.class.getName());

@Override
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
try {
Expand Down Expand Up @@ -97,7 +94,6 @@ public Map<String, String> call() throws Throwable {
}
}


EnvVars envVars2Set = new EnvVars();
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
Expand Down
@@ -1,6 +1,9 @@
package org.jenkinsci.plugins.envinject;

import hudson.*;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.*;
Expand Down Expand Up @@ -46,7 +49,10 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
//Load job envinject job property
if (isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentJobPropertyObject(build, launcher, listener);
} else {
return setUpEnvironmentWithoutJobPropertyObject(build, launcher, listener);
}

} catch (Run.RunnerAbortedException rre) {
logger.info("Fail the build.");
throw new Run.RunnerAbortedException();
Expand Down Expand Up @@ -213,8 +219,8 @@ private Environment setUpEnvironmentJobPropertyObject(AbstractBuild build, Launc
//Add Jenkins System variables
if (envInjectJobProperty.isKeepJenkinsSystemVariables()) {
logger.info("Keep Jenkins system variables.");
infraEnvVarsMaster.putAll(getJenkinsSystemVariables(true));
infraEnvVarsNode.putAll(getJenkinsSystemVariables(false));
infraEnvVarsMaster.putAll(variableGetter.getJenkinsSystemVariables(true));
infraEnvVarsNode.putAll(variableGetter.getJenkinsSystemVariables(false));
}

//Add build variables
Expand Down Expand Up @@ -277,6 +283,31 @@ public void buildEnvVars(Map<String, String> env) {
};
}

private Environment setUpEnvironmentWithoutJobPropertyObject(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, EnvInjectException {

final Map<String, String> resultVariables = new HashMap<String, String>();

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectLogger logger = new EnvInjectLogger(listener);
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, logger);
resultVariables.putAll(previousEnvVars);

resultVariables.putAll(variableGetter.getJenkinsSystemVariables(false));
resultVariables.putAll(variableGetter.getBuildVariables(build, logger));

final FilePath rootPath = getNodeRootPath();
if (rootPath != null) {
new EnvInjectActionSetter(rootPath).addEnvVarsToEnvInjectBuildAction(build, resultVariables);
}

return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(resultVariables);
}
};
}

private void injectPasswords(AbstractBuild build, EnvInjectJobProperty envInjectJobProperty, EnvInjectLogger logger) throws EnvInjectException {

//--Process global passwords
Expand All @@ -303,37 +334,6 @@ private void injectPasswords(AbstractBuild build, EnvInjectJobProperty envInject

}

private Map<String, String> getJenkinsSystemVariables(boolean onMaster) throws IOException, InterruptedException {

Map<String, String> result = new TreeMap<String, String>();

Computer computer;
if (onMaster) {
computer = Hudson.getInstance().toComputer();
} else {
computer = Computer.currentComputer();
}

//test if there is at least one executor
if (computer != null) {
result = computer.getEnvironment().overrideAll(result);
Node n = computer.getNode();
if (n != null)
result.put("NODE_NAME", computer.getName());
result.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " "));
}

String rootUrl = Hudson.getInstance().getRootUrl();
if (rootUrl != null) {
result.put("JENKINS_URL", rootUrl);
result.put("HUDSON_URL", rootUrl); // Legacy compatibility
}
result.put("JENKINS_HOME", Hudson.getInstance().getRootDir().getPath());
result.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath()); // legacy compatibility

return result;
}

private Node getNode() {
Computer computer = Computer.currentComputer();
if (computer == null) {
Expand Down
@@ -1,10 +1,9 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.Util;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.util.LogTaskListener;
import hudson.util.Secret;
import org.jenkinsci.lib.envinject.EnvInjectAction;
Expand All @@ -31,38 +30,38 @@ public class EnvInjectVariableGetter {

private static Logger LOG = Logger.getLogger(EnvInjectVariableGetter.class.getName());

public Map<String, String> getJenkinsSystemVariablesCurrentNode(AbstractBuild build) throws IOException, InterruptedException {
public Map<String, String> getJenkinsSystemVariables(boolean forceOnMaster) throws IOException, InterruptedException {

Map<String, String> result = new TreeMap<String, String>();

//Environment variables
result.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));

//Global properties
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty environmentVariablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
result.putAll(environmentVariablesNodeProperty.getEnvVars());
}
Computer computer;
if (forceOnMaster) {
computer = Hudson.getInstance().toComputer();
} else {
computer = Computer.currentComputer();
}

//Node properties
Computer computer = Computer.currentComputer();
//test if there is at least one executor
if (computer != null) {
Node node = computer.getNode();
if (node != null) {
for (NodeProperty<?> nodeProperty : node.getNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty environmentVariablesNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
result.putAll(environmentVariablesNodeProperty.getEnvVars());
}
}
}
result = computer.getEnvironment().overrideAll(result);
Node n = computer.getNode();
if (n != null)
result.put("NODE_NAME", computer.getName());
result.put("NODE_LABELS", Util.join(n.getAssignedLabels(), " "));
}

String rootUrl = Hudson.getInstance().getRootUrl();
if (rootUrl != null) {
result.put("JENKINS_URL", rootUrl);
result.put("HUDSON_URL", rootUrl); // Legacy compatibility
}
result.put("JENKINS_HOME", Hudson.getInstance().getRootDir().getPath());
result.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath()); // legacy compatibility

return result;
}


@SuppressWarnings("unchecked")
public Map<String, String> getBuildVariables(AbstractBuild build, EnvInjectLogger logger) throws EnvInjectException {
Map<String, String> result = new HashMap<String, String>();
Expand Down Expand Up @@ -171,7 +170,7 @@ public Map<String, String> getEnvVarsPreviousSteps(AbstractBuild build, EnvInjec
result.putAll(build.getBuildVariables());
}
} else {
result.putAll(getJenkinsSystemVariablesCurrentNode(build));
result.putAll(getJenkinsSystemVariables(false));
result.putAll(getBuildVariables(build, logger));
}
return result;
Expand Down

0 comments on commit a15917a

Please sign in to comment.