Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix reopen JENKINS-16316
  • Loading branch information
gboissinot committed Feb 12, 2013
1 parent 2c02621 commit 9e27cf4
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 80 deletions.
Expand Up @@ -11,6 +11,8 @@
import hudson.slaves.ComputerListener;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.plugins.envinject.service.EnvInjectEnvVars;
Expand All @@ -27,100 +29,152 @@
@Extension
public class EnvInjectComputerListener extends ComputerListener implements Serializable {

@Override
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
try {
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

//Get node path
FilePath nodePath = c.getNode().getRootPath();
if (nodePath == null) {
return;

private EnvVars getNewMasterEnvironmentVariables(Computer c, FilePath nodePath, TaskListener listener) throws EnvInjectException, IOException, InterruptedException {

//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;
}
});


// -- Retrieve Environment variables from master
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

boolean unsetSystemVariables = false;
Map<String, String> globalPropertiesEnvVars = new HashMap<String, String>();
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {

if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
globalPropertiesEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}

//Default value to false (even if no checked)
boolean unsetSystemVariables = false;
if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsPropertiesFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
}

//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 EnvironmentVariablesNodeProperty) {
globalPropertiesEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}

if (nodeProperty instanceof EnvInjectNodeProperty) {

Map<String, String> masterEnvVars = new HashMap<String, String>();
try {
masterEnvVars = Hudson.getInstance().getRootPath().act(
new Callable<Map<String, String>, Throwable>() {
public Map<String, String> call() throws Throwable {
return EnvVars.masterEnvVars;
}
}
);
} catch (Throwable e) {
e.printStackTrace();
//Resolve against node env vars
envInjectEnvVarsService.resolveVars(globalPropertiesEnvVars, nodeEnvVars);

EnvVars envVars2Set = new EnvVars();
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
}
envVars2Set.putAll(globalPropertiesEnvVars);

return envVars2Set;
}

private EnvVars getNewSlaveEnvironmentVariables(Computer c, FilePath nodePath, TaskListener listener) throws EnvInjectException, IOException, InterruptedException {

Map<String, String> currentEnvVars = new HashMap<String, String>();

// -- Retrieve Environment variables from master
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
currentEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}
}

EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

//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;
}
});

EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
// -- Process slave properties
boolean unsetSystemVariables = false;
for (NodeProperty<?> nodeProperty : c.getNode().getNodeProperties()) {

//Add global properties
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsPropertiesProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, masterEnvVars));
}
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
currentEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}

if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
currentEnvVars.putAll(envInjectEnvVarsService.getEnvVarsPropertiesFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
}
}

Node slave = Hudson.getInstance().getNode(c.getName());
//Resolve against node env vars
envInjectEnvVarsService.resolveVars(currentEnvVars, nodeEnvVars);

//Specific nodeProperties can overrides the value if this is a slave
if (slave != null) {
for (NodeProperty<?> nodeProperty : c.getNode().getNodeProperties()) {
EnvVars envVars2Set = new EnvVars();
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
}
envVars2Set.putAll(currentEnvVars);

if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
globalPropertiesEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}
return envVars2Set;

if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsPropertiesProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
}
}
}


@Override
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {

//Get node path
FilePath nodePath = c.getNode().getRootPath();
if (nodePath == null) {
return;
}


//use case : it is a slave
if (isActiveSlave(c)) {

try {
EnvVars envVars2Set = getNewSlaveEnvironmentVariables(c, nodePath, listener);
nodePath.act(new EnvInjectMasterEnvVarsSetter(envVars2Set));
} catch (EnvInjectException e) {
throw new IOException(e);
}

//Resolve against node env vars
envInjectEnvVarsService.resolveVars(globalPropertiesEnvVars, nodeEnvVars);
}

EnvVars envVars2Set = new EnvVars();
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
//use case : it is only on master
else if (isGlobalEnvInjectActivatedOnMaster()) {
try {
EnvVars envVars2Set = getNewMasterEnvironmentVariables(c, nodePath, listener);
nodePath.act(new EnvInjectMasterEnvVarsSetter(envVars2Set));
} catch (EnvInjectException e) {
throw new IOException(e);
}
envVars2Set.putAll(globalPropertiesEnvVars);
}
}

//Set new env vars
nodePath.act(new EnvInjectMasterEnvVarsSetter(envVars2Set));
private boolean isActiveSlave(Computer c) {
if (c == null) {
return false;
}

} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (EnvInjectException e) {
e.printStackTrace();
Node slave = Hudson.getInstance().getNode(c.getName());
return slave != null;
}

private boolean isGlobalEnvInjectActivatedOnMaster() {
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = Hudson.getInstance().getGlobalNodeProperties();
for (NodeProperty<?> nodeProperty : globalNodeProperties) {
if (nodeProperty instanceof EnvInjectNodeProperty) {
return true;
}
}
return false;

}

}
Expand Up @@ -51,11 +51,11 @@ public Map<String, String> getEnvVarsPropertiesJobProperty(FilePath rootPath,
return resultMap;
}

public Map<String, String> getEnvVarsPropertiesProperty(FilePath rootPath,
EnvInjectLogger logger,
String propertiesFilePath,
Map<String, String> propertiesContent,
Map<String, String> currentEnvVars) throws EnvInjectException {
public Map<String, String> getEnvVarsPropertiesFileProperty(FilePath rootPath,
EnvInjectLogger logger,
String propertiesFilePath,
Map<String, String> propertiesContent,
Map<String, String> currentEnvVars) throws EnvInjectException {
Map<String, String> resultMap = new LinkedHashMap<String, String>();
try {
resultMap.putAll(rootPath.act(new PropertiesVariablesRetriever(propertiesFilePath, propertiesContent, currentEnvVars, logger)));
Expand Down Expand Up @@ -91,6 +91,7 @@ public int executeScript(boolean loadFromMaster,
}
}

@SuppressWarnings("unchecked")
public Map<String, String> executeAndGetMapGroovyScript(String scriptContent, Map<String, String> envVars) throws EnvInjectException {

if (scriptContent == null) {
Expand Down

1 comment on commit 9e27cf4

@buildhive
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jenkins » envinject-plugin #82 FAILURE
Looks like this commit caused a build failure
(what's this?)

Please sign in to comment.