Skip to content

Commit

Permalink
Merge pull request #8 from synopsys-arc-oss/additional-exported-paths
Browse files Browse the repository at this point in the history
Added support of the label-specific exported paths (JENKINS-18774)
  • Loading branch information
oleg-nenashev committed Jul 28, 2013
2 parents e096681 + 35c4e7a commit 046c5ca
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 23 deletions.
Expand Up @@ -118,6 +118,22 @@ public void check() throws CustomToolException {
EnvStringParseHelper.checkStringForMacro("EXPORTED_PATHS", getExportedPaths());
EnvStringParseHelper.checkStringForMacro("HOME_DIR", getHome());
}

/**
* Get list of label specifics, which apply to the specified node.
* @param node Node to be checked
* @return List of the specifics to be applied
* @since 0.3
*/
public List<LabelSpecifics> getAppliedSpecifics(Node node) {
List<LabelSpecifics> out = new LinkedList<LabelSpecifics>();
for (LabelSpecifics spec : labelSpecifics) {
if (spec.appliesTo(node)) {
out.add(spec);
}
}
return out;
}

@Extension
public static class DescriptorImpl extends ToolDescriptor<CustomTool> {
Expand Down Expand Up @@ -166,21 +182,38 @@ public List<? extends ToolInstaller> getDefaultInstallers() {
protected PathsList getPaths(Node node) throws IOException, InterruptedException {

FilePath homePath = new FilePath(node.getChannel(), getHome());
//FIXME: Why?
if (exportedPaths == null) {
return PathsList.EMPTY;
}

PathsList pathsFound = homePath.act(new FileCallable<PathsList>() {

public PathsList invoke(File f, VirtualChannel channel)
throws IOException, InterruptedException {
String[] items = exportedPaths.split("\\s*,\\s*");
List<String> outList = new LinkedList<String>();
}
final List<LabelSpecifics> specs = getAppliedSpecifics(node);

PathsList pathsFound = homePath.act(new FileCallable<PathsList>() {

private void parseLists(String pathList, List<String> target) {
String[] items = pathList.split("\\s*,\\s*");
for (String item : items) {
if (item.isEmpty()) {
continue;
}

}
target.add(item);
}
}

@Override
public PathsList invoke(File f, VirtualChannel channel)
throws IOException, InterruptedException {

// Construct output paths
List<String> items = new LinkedList<String>();
parseLists(exportedPaths, items);
for (LabelSpecifics spec : specs) {
parseLists(spec.getExportedPaths(), items);
}

// Resolve exported paths
List<String> outList = new LinkedList<String>();
for (String item : items) {
File file = new File(item);
if (!file.isAbsolute()) {
file = new File (getHome(), item);
Expand All @@ -193,7 +226,7 @@ public PathsList invoke(File f, VirtualChannel channel)
outList.add(file.getAbsolutePath());
}

// resolve home dir
// Resolve home dir
File homeDir = new File(getHome());
return new PathsList(outList, homeDir.getAbsolutePath());
};
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2013 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc..
* Copyright 2013 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Expand Up @@ -108,7 +108,12 @@ public void Inject(EnvVars target) throws IOException {
//TODO: check overrides
//TODO: check lists
//TODO: substitute, check, etc.
target.put(envName, envValue);

//Substitute current envValue
String replacedValue = target.containsKey(envName) ? target.get(envName) : "";
String newEnvValue=envValue.replace("${"+envName+"}", replacedValue);

target.put(envName, newEnvValue);
}
}

Expand Down
Expand Up @@ -16,7 +16,11 @@
package com.synopsys.arc.jenkinsci.plugins.customtools;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Label;
import hudson.model.Node;
import java.io.Serializable;
Expand All @@ -28,14 +32,16 @@
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since 0.3
*/
public class LabelSpecifics implements Serializable {
public class LabelSpecifics extends AbstractDescribableImpl<LabelSpecifics> implements Serializable {
private String label;
private String additionalVars;

private String exportedPaths;

@DataBoundConstructor
public LabelSpecifics(String label, String additionalVars) {
public LabelSpecifics(String label, String additionalVars, String exportedPaths) {
this.label = Util.fixEmptyAndTrim(label);
this.additionalVars = additionalVars;
this.exportedPaths = exportedPaths;
}

public String getAdditionalVars() {
Expand All @@ -49,6 +55,10 @@ public boolean hasAdditionalVars() {
public String getLabel() {
return label;
}

public String getExportedPaths() {
return exportedPaths;
}

/**
* Check if specifics is applicable to node
Expand All @@ -66,11 +76,13 @@ public boolean appliesTo(Node node) {
}

public LabelSpecifics substitute(EnvVars vars) {
return new LabelSpecifics(label, vars.expand(additionalVars));
return new LabelSpecifics(label, vars.expand(additionalVars), vars.expand(exportedPaths));
}

public LabelSpecifics substitute(Node node) {
return new LabelSpecifics(label, EnvStringParseHelper.resolveExportedPath(additionalVars, node));
return new LabelSpecifics(label,
EnvStringParseHelper.resolveExportedPath(additionalVars, node),
EnvStringParseHelper.resolveExportedPath(exportedPaths, node));
}

public static LabelSpecifics[] substitute (LabelSpecifics[] specifics, EnvVars vars) {
Expand All @@ -88,5 +100,12 @@ public static LabelSpecifics[] substitute (LabelSpecifics[] specifics, Node node
}
return out;
}


@Extension
public static class DescriptorImpl extends Descriptor<LabelSpecifics> {
@Override
public String getDisplayName() {
return Messages.LabelSpecifics_DisplayName();
}
}
}
Expand Up @@ -40,11 +40,14 @@ THE SOFTWARE.
<f:description>Variables with macro support. *.prop file format</f:description>
</f:entry>
<f:entry title="${%Label-specific options}">
<f:repeatable name="labelSpecifics" header="${%Label specific}" add="${%Add label}" var="labelSpecific" field="labelSpecifics">
<table width="100%">
<f:repeatableProperty name="labelSpecifics" header="${%Label specific}" add="${%Add label}" var="labelSpecific" field="labelSpecifics">
<!-- <table width="100%">
<f:entry title="${%Label}" field="label">
<f:textbox/>
</f:entry>
<f:entry title="${%Exported paths}" field="exportedPaths">
<f:textbox/>
</f:entry>
<f:entry title="${%Additional variables}" field="additionalVars">
<f:textarea/>
</f:entry>
Expand All @@ -53,7 +56,7 @@ THE SOFTWARE.
<f:repeatableDeleteButton value="Remove..."/>
</div>
</f:entry>
</table>
</f:repeatable>
</table> -->
</f:repeatableProperty>
</f:entry>
</j:jelly>
@@ -0,0 +1,4 @@
<div>
Comma-separated list of the additional paths to be exported.
Plugin supports both absolute and relative paths (base directory - tool's HomeDir).
</div>
@@ -0,0 +1,20 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<!-- <table width="100%"> -->
<f:entry title="${%Label}" field="label">
<f:textbox/>
</f:entry>
<f:entry title="${%Exported paths}" field="exportedPaths">
<f:textbox/>
</f:entry>
<f:entry title="${%Additional variables}" field="additionalVars">
<f:textarea/>
</f:entry>
<f:entry title="">
<div align="right">
<f:repeatableDeleteButton value="Remove..."/>
</div>
</f:entry>
<!-- </table> -->

</j:jelly>
@@ -0,0 +1,5 @@
<div>
Additional variables to be exported into environment. Existing variables will be overridden.
<p>Field supports environment variables, non-existent variables will be replaced by the empty strings. In case of the list variables, you can reuse variable in parameter.</p>
<p>Example: <b>LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:myPath</b> </p>
</div>
@@ -0,0 +1,4 @@
<div>
Comma-separated list of the additional paths to be exported.
Plugin supports both absolute and relative paths (base directory - tool's HomeDir).
</div>
@@ -0,0 +1,4 @@
<div>
Label expression of the node, to which specifics should be applied.
Described specifics will be ignored for other nodes.
</div>
@@ -0,0 +1,4 @@
<div>
Adds label-specific options, which will be applied only if the node satisfy specified labels.
<p>Each field supports substitution of parameters</p>
</div>
@@ -0,0 +1 @@
LabelSpecifics.DisplayName=Label specifics

0 comments on commit 046c5ca

Please sign in to comment.