io.provis.nexus.NexusProvisioner Maven / Gradle / Ivy
/**
* Copyright (c) 2016 Takari, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package io.provis.nexus;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Properties;
import javax.inject.Named;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;
import de.pdark.decentxml.Document;
import de.pdark.decentxml.Element;
import de.pdark.decentxml.XMLParser;
import de.pdark.decentxml.XMLWriter;
import io.provis.SimpleProvisioner;
@Named(NexusProvisioner.ID)
public class NexusProvisioner extends SimpleProvisioner {
static final String ID = "nexus";
private Hasher sha1Hasher = Hashing.sha1().newHasher();
public File provision(NexusProvisioningContext context) throws IOException {
String version = context.getVersion();
File installationDirectory = context.getInstallationDirectory();
File workDirectory = context.getWorkDirectory();
if (version.length() <= 0) {
throw new IllegalArgumentException("Nexus version not specified");
}
// http://central.maven.org/maven2/org/sonatype/nexus/assemblies/nexus-bundle-template/
File nexusDistribution = resolveFromServer(
String.format("http://www.sonatype.org/downloads/nexus-%s-bundle.zip", version),
"org.sonatype.nexus:nexus-bundle:zip:bin:" + context.getVersion());
// Create the installation and work directories
FileUtils.mkdir(installationDirectory.getAbsolutePath());
FileUtils.mkdir(workDirectory.getAbsolutePath());
// Unpack Nexus into the installation directory
unarchiver.unarchive(nexusDistribution, installationDirectory);
// Provision any Nexus plugin required
for (String plugin : context.getPlugins()) {
// null here means it will go to Maven Central
Artifact pluginArtifact = new DefaultArtifact(plugin);
String pluginDirectoryName = pluginArtifact.getArtifactId() + "-" + pluginArtifact.getVersion();
File pluginDirectory = new File(new File(installationDirectory, "nexus/WEB-INF/plugin-repository"), pluginDirectoryName);
File pluginZip = resolveFromRepository(null, plugin);
unarchiver.unarchive(pluginZip, pluginDirectory);
}
// Remove any requested plugins
for (String existingPluginToRemove : context.getExistingPluginsToRemove()) {
// Existing plugin directory is the artifactId-version
File pluginDirectory = new File(new File(installationDirectory, "nexus/WEB-INF/plugin-repository"), String.format("%s-%s", existingPluginToRemove, context.getVersion()));
System.out.println("!!!! REMOVING " + pluginDirectory);
FileUtils.deleteDirectory(pluginDirectory);
}
File securityConfigurationXml = setUpNexusConfigFile(workDirectory, "security-configuration.xml");
File securityXml = setUpNexusConfigFile(workDirectory, "security.xml");
addRealms(securityConfigurationXml, context.getRealms());
addUsers(securityXml, context.getUsers());
File nexusProperties = new File(installationDirectory, "conf/nexus.properties");
Properties p = new Properties();
try (InputStream is = new FileInputStream(nexusProperties)) {
p.load(is);
}
p.setProperty("application-port", Integer.toString(context.getPort()));
try (OutputStream os = new FileOutputStream(nexusProperties)) {
p.store(os, "Generated by Nexus Provisioner");
}
return installationDirectory;
}
private File setUpNexusConfigFile(File workDirectory, String configFileName) throws IOException {
InputStream is = getClass().getClassLoader().getResourceAsStream("nexus/conf/" + configFileName);
File configXml = new File(workDirectory, "conf/" + configFileName);
if (is != null) {
if (!configXml.getParentFile().exists()) {
configXml.getParentFile().mkdirs();
}
OutputStream os = new FileOutputStream(configXml);
ByteStreams.copy(is, os);
}
return configXml;
}
//
//
//
// 2.0.3
// true
// true
// anonymous
// {6xW8vwUKYaMHZa5LDPwrNa9+sqlDtm90JwhaeApXN10=}
//
// XmlAuthenticatingRealm
// XmlAuthorizingRealm
// GithubRealm
//
// default
//
//
private void addRealms(File securityConfigurationXml, List realms) throws IOException {
addRealms(securityConfigurationXml, realms.toArray(new String[realms.size()]));
}
private void addRealms(File securityConfigurationXml, String... realms) throws IOException {
Document document = XMLParser.parse(securityConfigurationXml);
Element e = document.getChild("/security-configuration/realms");
for (String realm : realms) {
e.addNode(new Element("realm").setText(realm));
}
writeResource(securityConfigurationXml, document);
}
private void addUsers(File securityXml, List users) throws IOException {
Document document = XMLParser.parse(securityXml);
Element e = document.getChild("/security/users");
for (NexusProvisioningContext.User user : users) {
Element userElement = new Element("user");
userElement.addNode(new Element("id").setText(user.getUsername()));
userElement.addNode(new Element("firstName").setText(user.getUsername()));
userElement.addNode(new Element("password").setText(sha1(user.getPassword())));
userElement.addNode(new Element("status").setText("active"));
userElement.addNode(new Element("email").setText(user.getUsername() + "@yourcompany.com"));
e.addNode(userElement);
}
writeResource(securityXml, document);
}
private void writeResource(File pom, Document document) throws IOException {
String encoding = document.getEncoding();
try (XMLWriter writer = new XMLWriter(encoding != null ? new OutputStreamWriter(new FileOutputStream(pom), encoding) : new OutputStreamWriter(new FileOutputStream(pom)))) {
document.toXML(writer);
}
}
private String sha1(String input) {
sha1Hasher.putBytes(input.getBytes());
return BaseEncoding.base16().encode(sha1Hasher.hash().asBytes()).toLowerCase();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy