Skip to content

Commit

Permalink
Merge pull request #9 from synopsys-arc-oss/tool-versions
Browse files Browse the repository at this point in the history
[FIXED JENKINS-18772] - Versions support (no proper propagation to Environment)
  • Loading branch information
oleg-nenashev committed Aug 9, 2013
2 parents 03892c9 + 520a418 commit c452b78
Show file tree
Hide file tree
Showing 15 changed files with 320 additions and 49 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -56,4 +56,13 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>extended-choice-parameter</artifactId>
<version>0.28</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
Expand Up @@ -20,6 +20,7 @@
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvStringParseHelper;
import com.synopsys.arc.jenkinsci.plugins.customtools.LabelSpecifics;
import com.synopsys.arc.jenkinsci.plugins.customtools.PathsList;
import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersionConfig;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
Expand Down Expand Up @@ -62,19 +63,29 @@ public class CustomTool extends ToolInstallation implements
private final LabelSpecifics[] labelSpecifics;
private static final LabelSpecifics[] EMPTY_LABELS = new LabelSpecifics[0];
private transient String correctedHome;
private final ToolVersionConfig toolVersion;

@DataBoundConstructor
public CustomTool(String name, String home, List properties,
String exportedPaths, LabelSpecifics[] labelSpecifics) {
String exportedPaths, LabelSpecifics[] labelSpecifics, ToolVersionConfig toolVersion) {
super(name, home, properties);
this.exportedPaths = exportedPaths;
this.labelSpecifics = labelSpecifics;
this.toolVersion = toolVersion;
}

public String getExportedPaths() {
return exportedPaths;
}

public ToolVersionConfig getToolVersion() {
return toolVersion;
}

public boolean hasVersions() {
return toolVersion != null;
}

@Override
public String getHome() {
return (correctedHome != null) ? correctedHome : super.getHome();
Expand All @@ -92,7 +103,7 @@ public LabelSpecifics[] getLabelSpecifics() {
public CustomTool forEnvironment(EnvVars environment) {
return new CustomTool(getName(), environment.expand(getHome()),
getProperties().toList(), environment.expand(exportedPaths),
LabelSpecifics.substitute(getLabelSpecifics(), environment));
LabelSpecifics.substitute(getLabelSpecifics(), environment), toolVersion);
}

@Override
Expand All @@ -102,12 +113,12 @@ public CustomTool forNode(Node node, TaskListener log) throws IOException,
String substitutedHomeDir = EnvStringParseHelper.resolveExportedPath(translateFor(node, log), node);

return new CustomTool(getName(), substitutedHomeDir, getProperties().toList(),
substitutedPath, LabelSpecifics.substitute(getLabelSpecifics(), node));
substitutedPath, LabelSpecifics.substitute(getLabelSpecifics(), node), toolVersion);
}

//FIXME: just a stub
public CustomTool forBuildProperties(Map<JobPropertyDescriptor,JobProperty> properties) {
return new CustomTool(getName(), getHome(), getProperties().toList(), getExportedPaths(), getLabelSpecifics());
return new CustomTool(getName(), getHome(), getProperties().toList(), getExportedPaths(), getLabelSpecifics(), toolVersion);
}

/**
Expand Down
Expand Up @@ -16,8 +16,10 @@

package com.cloudbees.jenkins.plugins.customtools;

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolsLogger;
import com.synopsys.arc.jenkinsci.plugins.customtools.CustomToolException;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvStringParseHelper;
import com.synopsys.arc.jenkinsci.plugins.customtools.EnvVariablesInjector;
import com.synopsys.arc.jenkinsci.plugins.customtools.LabelSpecifics;
import com.synopsys.arc.jenkinsci.plugins.customtools.PathsList;
Expand Down Expand Up @@ -129,7 +131,7 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
RunnerAbortedException {

EnvVars buildEnv = build.getEnvironment(listener);
final EnvVars homes = new EnvVars();
final EnvVars homesAndVersions = new EnvVars();
final PathsList paths = new PathsList();
final List<EnvVariablesInjector> additionalVarInjectors = new LinkedList<EnvVariablesInjector>();

Expand All @@ -141,12 +143,17 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
}
}

//each tool can export zero or many directories to the PATH
// Each tool can export zero or many directories to the PATH
Node node = Computer.currentComputer().getNode();
for (CustomTool tool : customTools()) {
CustomToolsLogger.LogMessage(listener, tool.getName(), "Starting installation");
//this installs the tool if necessary

// Check versioning
CheckVersions(tool, listener, buildEnv, node, homesAndVersions);

// This installs the tool if necessary
CustomTool installed = tool
.forNode(Computer.currentComputer().getNode(), listener)
.forNode(node, listener)
.forEnvironment(buildEnv)
.forBuildProperties(build.getProject().getProperties());

Expand All @@ -156,8 +163,7 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
throw new AbortException(ex.getMessage());
}

// Prepare additional variables
Node node = Computer.currentComputer().getNode();
// Prepare additional variables
PathsList installedPaths = installed.getPaths(node);
installed.correctHome(installedPaths);
paths.add(installedPaths);
Expand All @@ -176,10 +182,9 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
CustomToolsLogger.LogMessage(listener, installed.getName(), "Tool is installed at "+ installed.getHome());
String homeDirVarName = (convertHomesToUppercase ? installed.getName().toUpperCase() : installed.getName()) +"_HOME";
CustomToolsLogger.LogMessage(listener, installed.getName(), "Setting "+ homeDirVarName+"="+installed.getHome());
homes.put(homeDirVarName, installed.getHome());
homesAndVersions.put(homeDirVarName, installed.getHome());
}


return new DecoratedLauncher(launcher) {
@Override
public Proc launch(ProcStarter starter) throws IOException {
Expand All @@ -189,7 +194,7 @@ public Proc launch(ProcStarter starter) throws IOException {
String overridenPaths = vars.get("PATH");
overridenPaths += paths.toListString();
vars.override("PATH", overridenPaths);
vars.putAll(homes);
vars.putAll(homesAndVersions);

// Inject additional variables
for (EnvVariablesInjector injector : additionalVarInjectors) {
Expand All @@ -209,6 +214,42 @@ private EnvVars toEnvVars(String[] envs) {
};
}

/**
* Check versions and modify build environment if required.
* @param tool Custom Tool
* @param listener Build Listener
* @param buildEnv Build Environment (can be modified)
* @param node Target Node
* @param target Build Internal Environment (can be modified)
* @throws CustomToolException
* @since 0.4
*/
public void CheckVersions (CustomTool tool, BuildListener listener, EnvVars buildEnv, Node node, EnvVars target) throws CustomToolException {
// Check version
if (tool.hasVersions()) {
ExtendedChoiceParameterDefinition def = tool.getToolVersion().getVersionsListSource();
String defaultVersion = hudson.Util.fixEmptyAndTrim(def.getEffectiveDefaultValue());
CustomToolsLogger.LogMessage(listener, tool.getName(), "Tool has versions. Default version is "+def.getName()+"="+defaultVersion);

// Check if node has version specified
String subst = "${"+def.getName()+"}";
String res = EnvStringParseHelper.resolveExportedPath(subst, node);
if (!subst.equals(res)) {
CustomToolsLogger.LogMessage(listener, tool.getName(), "Version "+res+" has been specified by node or global variables");
} else if (buildEnv.containsKey(def.getName())) {
String envVersion = buildEnv.get(def.getName());
CustomToolsLogger.LogMessage(listener, tool.getName(), "Version "+envVersion+" has been specified by the build environment");
} else if (defaultVersion != null){
CustomToolsLogger.LogMessage(listener, tool.getName(), "No version has been specified. Using default version");
target.addLine(def.getName()+"="+defaultVersion);
buildEnv.addLine(def.getName()+"="+defaultVersion);
} else {
CustomToolsLogger.LogMessage(listener, tool.getName(), "Error: No version has been specified, no default version. Failing the build...");
throw new CustomToolException("Version has not been specified for the "+tool.getName());
}
}
}

@Override
public Descriptor<BuildWrapper> getDescriptor() {
return DESCRIPTOR;
Expand Down
Expand Up @@ -15,12 +15,14 @@
*/
package com.synopsys.arc.jenkinsci.plugins.customtools;

import java.io.IOException;

/**
* Custom tool exception. Should be thrown if error occurs in plugin functions.
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since 0.3
*/
public class CustomToolException extends Exception {
public class CustomToolException extends IOException {
public CustomToolException(String message) {
super(message);
}
Expand Down
@@ -0,0 +1,53 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.synopsys.arc.jenkinsci.plugins.customtools.versions;

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import com.synopsys.arc.jenkinsci.plugins.customtools.Messages;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import java.io.Serializable;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Class implements support of versions for custom tools
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since 0.4
*/
public class ToolVersionConfig extends AbstractDescribableImpl<ToolVersionConfig>
implements Serializable
{
public static final ToolVersionConfig DEFAULT = null;
ExtendedChoiceParameterDefinition versionsListSource;

@DataBoundConstructor
public ToolVersionConfig(ExtendedChoiceParameterDefinition versionsListSource) {
this.versionsListSource = versionsListSource;
}

public ExtendedChoiceParameterDefinition getVersionsListSource() {
return versionsListSource;
}

@Extension
public static class DescriptorImpl extends Descriptor<ToolVersionConfig> {
@Override
public String getDisplayName() {
return Messages.Versions_ToolVersionConfig_DisplayName();
}
}
}
@@ -0,0 +1,64 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.synopsys.arc.jenkinsci.plugins.customtools.versions;

import com.cloudbees.jenkins.plugins.customtools.CustomTool;
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import hudson.tools.ToolInstallation;
import java.util.LinkedList;
import java.util.List;

/**
* Provides helper for toll versions handling.
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since 0.4
*/
public class ToolVersionHelper {
public static ExtendedChoiceParameterDefinition getDefaultVersionDescr(String toolName) {
return new ExtendedChoiceParameterDefinition(
getDefaultVersionVariableName(toolName),
ExtendedChoiceParameterDefinition.PARAMETER_TYPE_TEXT_BOX,
"default", null, null,
"default", null, null,
false, 1, "Default version stub"
);
}

public static String getDefaultVersionVariableName(String toolName) {
return toolName.toUpperCase().replaceAll("\\s+", "_");
}

public static ExtendedChoiceParameterDefinition getVersionDescr(String toolName) {
CustomTool.DescriptorImpl tools = ToolInstallation.all().get(CustomTool.DescriptorImpl.class);
CustomTool tool = tools.byName(toolName);
return tool.hasVersions() ? tool.getToolVersion().getVersionsListSource() : getDefaultVersionDescr(toolName);
}

/**
* Gets list of versioned tools.
* @return List of tools, which have versions specified
*/
public static List<CustomTool> getAllVersionedTools() {
CustomTool.DescriptorImpl tools = ToolInstallation.all().get(CustomTool.DescriptorImpl.class);
List<CustomTool> res = new LinkedList<CustomTool>();
for (CustomTool tool : tools.getInstallations()) {
if (tool.hasVersions()) {
res.add(tool);
}
}
return res;
}
}

0 comments on commit c452b78

Please sign in to comment.