Skip to content

Commit

Permalink
Merge pull request #2 from wolfs/env-vars
Browse files Browse the repository at this point in the history
[FIXED JENKINS-24107] Expose NODE_TO_SETUP_NAME and NODE_TO_SETUP_LABELS
  • Loading branch information
wolfs committed Sep 16, 2014
2 parents 8b0dc72 + 8a70760 commit dc690ae
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/main/java/org/jenkinsci/plugins/slave_setup/SetupDeployer.java
@@ -1,9 +1,6 @@
package org.jenkinsci.plugins.slave_setup;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.*;
import hudson.model.Computer;
import hudson.model.Label;
import hudson.model.Node;
Expand Down Expand Up @@ -95,7 +92,7 @@ public void deployToComputer(Computer c, FilePath root, TaskListener listener, S
// execute command line
String cmdLine = setupConfigItem.getCommandLine();

executeScript(c, root, listener, cmdLine);
executeScript(c, root, listener, cmdLine, createEnvVarsForComputer(c));
} else {
listener.getLogger().println("Slave " + c.getName() + " NOT set up as assigned label expression '" + setupConfigItem.getAssignedLabelString() + "' does not match with node label '" + c.getNode().getLabelString() + "'");
}
Expand All @@ -119,14 +116,14 @@ public boolean checkLabels(Computer c, SetupConfigItem setupConfigItem) {
return label.contains(c.getNode());
}

private void executeScript(Computer c, FilePath root, TaskListener listener, String cmdLine) throws IOException, InterruptedException {
private void executeScript(Computer c, FilePath root, TaskListener listener, String cmdLine, EnvVars additionalEnvironment) throws IOException, InterruptedException {
if (StringUtils.isNotBlank(cmdLine)) {
listener.getLogger().println("Executing script '" + cmdLine + "' on " + c.getName());
Node node = c.getNode();
Launcher launcher = root.createLauncher(listener);
Shell s = new Shell(cmdLine);
FilePath script = s.createScriptFile(root);
int r = launcher.launch().cmds(s.buildCommandLine(script)).envs(getEnvironment(node)).stdout(listener).pwd(root).join();
int r = launcher.launch().cmds(s.buildCommandLine(script)).envs(getEnvironment(node, additionalEnvironment)).stdout(listener).pwd(root).join();

if (r != 0) {
listener.getLogger().println("script failed!");
Expand Down Expand Up @@ -192,7 +189,7 @@ public void executePrepareScripts(Computer c, SetupConfig config, TaskListener l
// computer.
if (c == null || this.checkLabels(c, setupConfigItem)) {
try {
this.executeScript(computer, filePath, listener, setupConfigItem.getPrepareScript());
this.executeScript(computer, filePath, listener, setupConfigItem.getPrepareScript(), createEnvVarsForComputer(c));
setupConfigItem.setPrepareScriptExecuted(true);
} catch (Exception e) {
listener.getLogger().println("prepare script failed with exception: " + e.getMessage());
Expand All @@ -203,15 +200,33 @@ public void executePrepareScripts(Computer c, SetupConfig config, TaskListener l
}
}

private EnvVars createEnvVarsForComputer(Computer c) {
EnvVars additionalEnvironment = new EnvVars();
if (c != null) {
additionalEnvironment.put("NODE_TO_SETUP_NAME", c.getName());
Node node = c.getNode();
if (node != null) {
additionalEnvironment.put("NODE_TO_SETUP_LABELS", Util.join(node.getAssignedLabels(), " "));
}
}
return additionalEnvironment;
}

/**
* Returns the environment variables for the given node.
*
* @param node node to get the environment variables from
* @param additionalEnvironment environment added to the environment from the node. Take precedence over environment from the node.
* @return the environment variables for the given node
*/
private EnvVars getEnvironment(Node node) {
private EnvVars getEnvironment(Node node, EnvVars additionalEnvironment) {
EnvVars envVars = new EnvVars();
EnvironmentVariablesNodeProperty env = node.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
return env != null ? env.getEnvVars() : new EnvVars();
if (env != null) {
envVars.putAll(env.getEnvVars());
}
envVars.putAll(additionalEnvironment);
return envVars;
}


Expand Down

0 comments on commit dc690ae

Please sign in to comment.