Skip to content

Commit

Permalink
Merge branch 'master' into JENKINS-25223
Browse files Browse the repository at this point in the history
  • Loading branch information
arcivanov committed Aug 20, 2015
2 parents 1d8c769 + 3dcf5ae commit 0916c1c
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 20 deletions.
20 changes: 15 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>envinject</artifactId>
<version>1.92.0-beta-2-SNAPSHOT</version>
<version>1.93-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Environment Injector Plugin</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin</url>
Expand Down Expand Up @@ -41,6 +41,15 @@
<role>Maintainer</role>
</roles>
</developer>
<developer>
<id>recena</id>
<name>Manuel Recena</name>
<email>[email protected]</email>
<timezone>+1</timezone>
<roles>
<role>Contributor</role>
</roles>
</developer>
</developers>

<properties>
Expand All @@ -60,16 +69,17 @@
</scm>

<build>

<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>1.98</version>
<extensions>true</extensions>
</plugin>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
Expand Down Expand Up @@ -120,6 +130,6 @@
</pluginRepository>
</pluginRepositories>

</project>
</project>


Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -53,10 +55,13 @@ public Map<String, String> call() throws IOException {
globalPropertiesEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
}

if (nodeProperty instanceof EnvInjectNodeProperty) {
final Node node = c.getNode();
if (node != null && nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(
node.getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(),
null, nodeEnvVars));
}

}
Expand All @@ -80,6 +85,11 @@ private EnvVars getNewSlaveEnvironmentVariables(Computer c, FilePath nodePath, T
EnvInjectLogger logger = new EnvInjectLogger(listener);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

final Node node = c.getNode();
if (node == null) {
throw new EnvInjectException("Node is removed, but the computer has not gone yet");
}

//Get env vars for the current node
Map<String, String> nodeEnvVars = nodePath.act(
new Callable<Map<String, String>, IOException>() {
Expand All @@ -90,7 +100,7 @@ public Map<String, String> call() throws IOException {

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

if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
currentEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());
Expand All @@ -99,7 +109,7 @@ public Map<String, String> call() throws IOException {
if (nodeProperty instanceof EnvInjectNodeProperty) {
EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty);
unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables();
currentEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
currentEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(node.getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars));
}
}

Expand All @@ -121,7 +131,8 @@ public Map<String, String> call() throws IOException {
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {

//Get node path
FilePath nodePath = c.getNode().getRootPath();
final Node node = c.getNode();
final FilePath nodePath = node != null ? node.getRootPath() : null;
if (nodePath == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.kohsuke.stapler.StaplerRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.CheckForNull;

Expand Down Expand Up @@ -69,7 +70,7 @@ public EnvInjectJobPropertyContributor[] getContributors() {
contributors = contributorsComputed;
}

return contributors;
return Arrays.copyOf(contributors, contributors.length);
}

private EnvInjectJobPropertyContributor[] computeEnvInjectContributors() throws org.jenkinsci.lib.envinject.EnvInjectException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public void beforeUse(AbstractBuild build, FilePath ws, BuildListener listener)
Map<String, String> cleanVariables = envInjectEnvVars.removeUnsetVars(previousEnvVars);

//Set new env vars
new EnvInjectActionSetter(build.getBuiltOn().getRootPath()).addEnvVarsToEnvInjectBuildAction(build, cleanVariables);
final Node builtOn = build.getBuiltOn();
new EnvInjectActionSetter(builtOn != null ? builtOn.getRootPath() : null)
.addEnvVarsToEnvInjectBuildAction(build, cleanVariables);

} catch (EnvInjectException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hudson.model.Node;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import java.util.Arrays;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getHelpFile() {

@SuppressWarnings("unused")
public EnvInjectGlobalPasswordEntry[] getEnvInjectGlobalPasswordEntries() {
return envInjectGlobalPasswordEntries;
return Arrays.copyOf(envInjectGlobalPasswordEntries, envInjectGlobalPasswordEntries.length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class EnvInjectPasswordsOutputStream extends LineTransformationOutputStream {
nbMaskedPasswords++;
}
}
if (nbMaskedPasswords++ >= 1) { // is there at least one password to mask?
if (nbMaskedPasswords >= 1) { // is there at least one password to mask?
regex.deleteCharAt(regex.length() - 1); // removes the last unuseful pipe
regex.append(')');
passwordsAsPattern = Pattern.compile(regex.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void writeTextResponse(StaplerResponse response) throws IOException {
response.setContentType("plain/text");
StringWriter stringWriter = new StringWriter();
for (Map.Entry<String, String> entry : envVars.entrySet()) {
stringWriter.write(String.format("%s%s%s\n", entry.getKey(), "=", entry.getValue()));
stringWriter.write(String.format("%s%s%s%n", entry.getKey(), "=", entry.getValue()));
}
response.getOutputStream().write(stringWriter.toString().getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;

import static com.google.common.base.Joiner.on;
import java.util.Locale;
import static org.apache.commons.lang.StringUtils.isNotBlank;


Expand Down Expand Up @@ -87,7 +88,7 @@ private static String getTriggerName(Cause cause) {
} else if (Cause.UpstreamCause.class.isInstance(cause)) {
return "UPSTREAMTRIGGER";
} else if (cause != null) {
return cause.getClass().getSimpleName().toUpperCase();
return cause.getClass().getSimpleName().toUpperCase(Locale.ENGLISH);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.CheckForNull;


/**
* @author Gregory Boissinot
*/
public class EnvInjectActionSetter implements Serializable {

@CheckForNull
private FilePath rootPath;

public EnvInjectActionSetter(FilePath rootPath) {
public EnvInjectActionSetter(@CheckForNull FilePath rootPath) {
this.rootPath = rootPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Map<String, String> getEnvVarsPropertiesJobProperty(FilePath rootPath,
return resultMap;
}

@Nonnull
public Map<String, String> getEnvVarsFileProperty(@Nonnull FilePath rootPath,
EnvInjectLogger logger,
String propertiesFilePath,
Expand Down Expand Up @@ -120,7 +121,7 @@ public Map<String, String> executeAndGetMapGroovyScript(EnvInjectLogger logger,
return new HashMap<String, String>();
}

logger.info(String.format("Evaluation the following Groovy script content: \n%s\n", scriptContent));
logger.info(String.format("Evaluation the following Groovy script content: %n%s%n", scriptContent));

Binding binding = new Binding();
String jobName = envVars.get("JOB_NAME");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private int executeScriptContent(FilePath scriptExecutionRoot, String scriptCont
}

FilePath tmpFile = batchRunner.createScriptFile(scriptExecutionRoot);
logger.info(String.format("Executing and processing the following script content: \n%s\n", scriptContent));
logger.info(String.format("Executing and processing the following script content: %n%s%n", scriptContent));
int cmdCode = launcher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(launcher.getListener()).envs(scriptExecutionEnvVars).pwd(scriptExecutionRoot).join();
if (cmdCode != 0) {
logger.info(String.format("Script executed. The exit code is %s.", cmdCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Map<String, String> invoke(File base, VirtualChannel channel) throws IOEx
//Add the properties content
if (propertiesContent != null) {
PropertiesGetter propertiesGetter = new PropertiesGetter();
logger.info(String.format("Injecting as environment variables the properties content \n%s\n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent)));
logger.info(String.format("Injecting as environment variables the properties content %n%s%n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent)));
result.putAll(propertiesContent);
logger.info("Variables injected successfully.");
}
Expand Down

0 comments on commit 0916c1c

Please sign in to comment.