Skip to content

Commit

Permalink
[JENKINS-42675] Permit to select a npm user config for each NodeJS in…
Browse files Browse the repository at this point in the history
…stallation, but when the install should be performed to generate the content of NPMConfig file an instance of build is needed because:

- provides a non accessible path where place the file (with write permissions)
- environment variables (of the build) to perform the replace token
- resolve the credentials
  • Loading branch information
nfalco79 committed Aug 5, 2017
1 parent 71a77fc commit b6e03b5
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/main/java/jenkins/plugins/nodejs/NodeJSBuildWrapper.java
Expand Up @@ -30,6 +30,8 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMConfig.NPMConfigProvider;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
Expand Down Expand Up @@ -163,6 +165,22 @@ public Collection<Config> getConfigs() {
return GlobalConfigFiles.get().getConfigs(NPMConfigProvider.class);
}

public ListBoxModel doFillConfigIdItems(@QueryParameter final String nodeJSInstallationName) {
NodeJSInstallation nodeJS = NodeJSUtils.getNodeJS(nodeJSInstallationName);
String defaultConfigId = null;
if (nodeJS != null && defaultConfigId == null) {
defaultConfigId = nodeJS.getDefaultConfigId();
}

ListBoxModel result = new ListBoxModel(new Option("- use system default -", "", defaultConfigId == null));
for (Config config : getConfigs()) {
Option option = new Option(config.name, config.id);
option.selected = (defaultConfigId != null && defaultConfigId.equals(config.id));
result.add(option);
}
return result;
}

public FormValidation doCheckConfigId(@CheckForNull @QueryParameter final String configId) {
NPMConfig config = (NPMConfig) GlobalConfigFiles.get().getById(configId);
if (config != null) {
Expand Down
Expand Up @@ -25,13 +25,19 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.jenkinsci.Symbol;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -48,8 +54,12 @@
import hudson.tools.ToolInstallation;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolProperty;
import hudson.util.FormValidation;
import jenkins.plugins.nodejs.Messages;
import jenkins.plugins.nodejs.NodeJSConstants;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
import jenkins.plugins.nodejs.configfiles.NPMConfig.NPMConfigProvider;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;

Expand All @@ -65,10 +75,16 @@ public class NodeJSInstallation extends ToolInstallation implements EnvironmentS

@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "calculate at runtime, its value depends on the OS where it run")
private transient Platform platform;
private String defaultConfigId;

@DataBoundConstructor
public NodeJSInstallation(@Nonnull String name, @Nonnull String home, List<? extends ToolProperty<?>> properties) {
this(name, home, properties, null);
this(name, home, properties, (String) null);
}

public NodeJSInstallation(@Nonnull String name, @Nonnull String home, List<? extends ToolProperty<?>> properties, String defaultConfigId) {
this(name, home, properties, (Platform) null);
this.defaultConfigId = Util.fixEmptyAndTrim(defaultConfigId);
}

protected NodeJSInstallation(@Nonnull String name, @Nonnull String home, List<? extends ToolProperty<?>> properties, Platform platform) {
Expand Down Expand Up @@ -192,6 +208,16 @@ private Platform getPlatform() throws DetectionFailedException {
}


public String getDefaultConfigId() {
return defaultConfigId;
}

@DataBoundSetter
public void setDefaultConfigId(String defaultConfigId) {
this.defaultConfigId = Util.fixEmptyAndTrim(defaultConfigId);
}


@Symbol("nodejs")
@Extension
public static class DescriptorImpl extends ToolDescriptor<NodeJSInstallation> {
Expand All @@ -211,6 +237,28 @@ public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new NodeJSInstaller(null, null, 72));
}

/**
* Gather all defined npmrc config files.
*
* @return a collection of user npmrc files or {@code empty} if no one
* defined.
*/
public Collection<Config> getConfigs() {
return GlobalConfigFiles.get().getConfigs(NPMConfigProvider.class);
}

public FormValidation doCheckDefaultConfigId(@CheckForNull @QueryParameter final String defaultConfigId) {
NPMConfig config = (NPMConfig) GlobalConfigFiles.get().getById(defaultConfigId);
if (config != null) {
try {
config.doVerify();
} catch (VerifyConfigProviderException e) {
return FormValidation.error(e.getMessage());
}
}
return FormValidation.ok();
}

/*
* (non-Javadoc)
* @see hudson.tools.Descriptor#configure(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject)
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/jenkins/plugins/nodejs/tools/NodeJSInstaller.java
Expand Up @@ -40,6 +40,13 @@
import javax.annotation.Nonnull;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.lf5.viewer.configure.ConfigurationManager;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigFile;
import org.jenkinsci.lib.configprovider.model.ConfigFileManager;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;
import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.base.Predicate;
Expand All @@ -61,6 +68,7 @@
import hudson.util.ArgumentListBuilder;
import hudson.util.Secret;
import jenkins.MasterToSlaveFileCallable;
import jenkins.model.Jenkins;
import jenkins.plugins.nodejs.Messages;
import jenkins.plugins.nodejs.NodeJSConstants;
import jenkins.plugins.tools.Installables;
Expand Down Expand Up @@ -119,13 +127,14 @@ public Installable getInstallable() throws IOException {
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
this.platform = getPlatform(node);
this.cpu = getCPU(node);
NodeJSInstallation nodeJSTool = (NodeJSInstallation) tool;

FilePath expected;
Installable installable = getInstallable();
if (installable == null || !installable.url.toLowerCase(Locale.ENGLISH).endsWith("msi")) {
expected = super.performInstallation(tool, node, log);
expected = super.performInstallation(nodeJSTool, node, log);
} else {
expected = preferredLocation(tool, node);
expected = preferredLocation(nodeJSTool, node);
if (!isUpToDate(expected, installable)) {
if (installIfNecessaryMSI(expected, new URL(installable.url), log, "Installing " + installable.url + " to " + expected + " on " + node.getDisplayName())) {
expected.child(".timestamp").delete(); // we don't use the timestamp
Expand All @@ -138,7 +147,7 @@ public FilePath performInstallation(ToolInstallation tool, Node node, TaskListen
}
}

refreshGlobalPackages(node, log, expected);
refreshGlobalPackages(nodeJSTool.getDefaultConfigId(), node, log, expected);

return expected;
}
Expand All @@ -154,7 +163,7 @@ private Platform getPlatform(Node node) throws DetectionFailedException {
/*
* Installing npm packages if needed
*/
protected void refreshGlobalPackages(Node node, TaskListener log, FilePath expected) throws IOException, InterruptedException {
protected void refreshGlobalPackages(String configId, Node node, TaskListener log, FilePath expected) throws IOException, InterruptedException {
String globalPackages = getNpmPackages();

if (StringUtils.isNotBlank(globalPackages)) { // JENKINS-41876
Expand All @@ -180,6 +189,15 @@ protected void refreshGlobalPackages(Node node, TaskListener log, FilePath expec

EnvVars env = new EnvVars();
env.put(NodeJSConstants.ENVVAR_NODEJS_PATH, binFolder.getRemote());
if (configId != null) {
FilePath npmrc = expected.child(".npmrc");
npmrc.delete();
ConfigFile cf = new ConfigFile(configId, npmrc.getRemote(), false);
Config config = GlobalConfigFiles.get().getById(configId);
String supplyContent = config.getProvider().supplyContent(config, null, expected, log, new ArrayList<String>());
// FilePath configFile = ConfigFileManager.provisionConfigFile(cf, env, Jenkins.getActiveInstance().getItemGroup(), expected, log, new ArrayList<String>());
env.put(NodeJSConstants.NPM_USERCONFIG, npmrc.getRemote());
}
try {
buildProxyEnvVars(env, log);
} catch (URISyntaxException e) {
Expand Down
Expand Up @@ -31,9 +31,13 @@ THE SOFTWARE.
</j:forEach>
</select>
</f:entry>

<!--
<f:entry title="${%configId.title}">
<cf:select field="configId" configs="${descriptor.configs}" allowsEmpty="true" emptyValue="" emptyLabel="${%configId.emptyValue}" />
</f:entry>

-->
<f:entry title="${%configId.title}">
<f:select field="configId" />
</f:entry>

</j:jelly>
@@ -0,0 +1,37 @@
<!--
The MIT License
Copyright (c) 2017, Nikolas Falco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?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" xmlns:cf="/lib/nodejs">
<f:entry title="${%Name}" field="name">
<f:textbox />
</f:entry>

<f:entry title="NODEJS_HOME" field="home">
<f:textbox />
</f:entry>

<f:entry title="${%defaultConfigId.title}">
<cf:select field="defaultConfigId" configs="${descriptor.configs}" allowsEmpty="true" emptyValue="" emptyLabel="${%defaultConfigId.emptyValue}" />
</f:entry>
</j:jelly>
@@ -1,6 +1,6 @@
# The MIT License
#
# Copyright (c) 2016, Nikolas Falco
# Copyright (c) 2017, Nikolas Falco
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,5 +20,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

NodeJS\ installation=Installazioni NodeJS
List\ of\ NodeJS\ installations\ on\ this\ system=Lista delle installazioni di NodeJS nel sistema
defaultConfigId.title=Default npmrc file
defaultConfigId.emptyValue=- use system default -

0 comments on commit b6e03b5

Please sign in to comment.