Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Sep 4, 2011
1 parent 5dedeaa commit 5af26b8
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 152 deletions.
3 changes: 2 additions & 1 deletion pom.xml
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down
Expand Up @@ -11,6 +11,7 @@
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.service.EnvInjectActionSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectScriptExecutorService;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -47,16 +48,6 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui

final FilePath ws = build.getWorkspace();

//Fix JENKINS-10847 postponed
// //Add the current system env vars
// ws.act(new Callable<Void, Throwable>() {
//
// public Void call() throws Throwable {
// resultVariables.putAll(EnvVars.masterEnvVars);
// return null;
// }
// });

//Always keep build variables (such as parameter variables).
resultVariables.putAll(getAndAddBuildVariables(build));

Expand All @@ -72,12 +63,8 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
//Resolve vars each other
EnvVars.resolve(resultVariables);

//Fix JENKINS-10847 postponed
//Set the new build variables map
//build.getWorkspace().act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));

//Add or get the existing action to add new env vars
addEnvVarsToEnvInjectBuildAction(build, resultVariables);
new EnvInjectActionSetter(ws).addEnvVarsToEnvInjectBuildAction(build, resultVariables);

} catch (Throwable throwable) {
build.setResult(Result.FAILURE);
Expand All @@ -87,7 +74,6 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(resultVariables);

}
};
}
Expand All @@ -104,15 +90,6 @@ private Map<String, String> getAndAddBuildVariables(AbstractBuild build) {
return result;
}

private void addEnvVarsToEnvInjectBuildAction(AbstractBuild<?, ?> build, Map<String, String> envMap) {
EnvInjectAction envInjectAction = build.getAction(EnvInjectAction.class);
if (envInjectAction != null) {
envInjectAction.overrideAll(envMap);
} else {
build.addAction(new EnvInjectAction(envMap));
}
}

@Extension
@SuppressWarnings("unused")
public static final class DescriptorImpl extends BuildWrapperDescriptor {
Expand Down
Expand Up @@ -8,6 +8,7 @@
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.service.EnvInjectActionSetter;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -39,31 +40,21 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

try {

FilePath ws = build.getWorkspace();
FilePath workspace = build.getWorkspace();


//Fix JENKINS-10847 postponed
/*
//Add the current system env vars
resultVariables.putAll(ws.act(new Callable<Map<String, String>, Throwable>() {
public Map<String, String> call() throws Throwable {
return EnvVars.masterEnvVars;
}
}));
*/
//Always keep build variables (such as parameter variables).
resultVariables.putAll(getAndAddBuildVariables(build));

//Get env vars from properties info.
//File information path can be relative to the workspace
Map<String, String> envMap = build.getWorkspace().act(new PropertiesVariablesRetriever(info, resultVariables, new EnvInjectLogger(listener)));
Map<String, String> envMap = workspace.act(new PropertiesVariablesRetriever(info, resultVariables, new EnvInjectLogger(listener)));
resultVariables.putAll(envMap);

//Resolve vars each other
EnvVars.resolve(resultVariables);

//Fix JENKINS-10847 postponed
//Set the new build variables map
//ws.act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));
build.addAction(new EnvironmentContributingAction() {
public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
env.putAll(resultVariables);
Expand All @@ -83,7 +74,7 @@ public String getUrlName() {
});

//Add or get the existing action to add new env vars
addEnvVarsToEnvInjectBuildAction(build, resultVariables);
new EnvInjectActionSetter(workspace).addEnvVarsToEnvInjectBuildAction(build, resultVariables);

} catch (Throwable throwable) {
build.setResult(Result.FAILURE);
Expand All @@ -105,15 +96,6 @@ private Map<String, String> getAndAddBuildVariables(AbstractBuild build) {
return result;
}

private void addEnvVarsToEnvInjectBuildAction(AbstractBuild<?, ?> build, Map<String, String> envMap) {
EnvInjectAction envInjectAction = build.getAction(EnvInjectAction.class);
if (envInjectAction != null) {
envInjectAction.overrideAll(envMap);
} else {
build.addAction(new EnvInjectAction(envMap));
}
}

@Extension
@SuppressWarnings("unused")
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
Expand Down
@@ -0,0 +1,63 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.slaves.ComputerListener;
import hudson.slaves.NodeProperty;
import org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter;

import java.io.IOException;
import java.io.Serializable;
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 {

//Default value to false (even if no checked)
boolean unsetSystemVariables = false;

//Global Properties
for (NodeProperty<?> nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
}
}

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

//Remove System vars is necessary
if (unsetSystemVariables) {
try {
c.getNode().getRootPath().act(new EnvInjectMasterEnvVarsSetter(new EnvVars()));
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (EnvInjectException e) {
e.printStackTrace();
}
}
}
}
Expand Up @@ -18,6 +18,8 @@ public class EnvInjectJobProperty<T extends Job<?, ?>> extends JobProperty<T> {

private transient boolean keepSystemVariables;

private boolean keepJenkinsSystemVariables;

private boolean keepBuildVariables;

@SuppressWarnings("unused")
Expand All @@ -35,6 +37,11 @@ public boolean isKeepSystemVariables() {
return keepSystemVariables;
}

@SuppressWarnings("unused")
public boolean isKeepJenkinsSystemVariables() {
return keepJenkinsSystemVariables;
}

@SuppressWarnings("unused")
public boolean isKeepBuildVariables() {
return keepBuildVariables;
Expand All @@ -48,8 +55,8 @@ public void setOn(boolean on) {
this.on = on;
}

public void setKeepSystemVariables(boolean keepSystemVariables) {
this.keepSystemVariables = keepSystemVariables;
public void setKeepJenkinsSystemVariables(boolean keepJenkinsSystemVariables) {
this.keepJenkinsSystemVariables = keepJenkinsSystemVariables;
}

public void setKeepBuildVariables(boolean keepBuildVariables) {
Expand Down Expand Up @@ -85,7 +92,7 @@ public EnvInjectJobProperty newInstance(StaplerRequest req, JSONObject formData)
envInjectJobProperty.setInfo(info);
envInjectJobProperty.setOn(true);
if (onObject instanceof JSONObject) {
//envInjectJobProperty.setKeepSystemVariables(((JSONObject) onObject).getBoolean("keepSystemVariables"));
envInjectJobProperty.setKeepJenkinsSystemVariables(((JSONObject) onObject).getBoolean("keepJenkinsSystemVariables"));
envInjectJobProperty.setKeepBuildVariables(((JSONObject) onObject).getBoolean("keepBuildVariables"));
return envInjectJobProperty;
}
Expand Down

0 comments on commit 5af26b8

Please sign in to comment.