implemented plugin installation

This commit is contained in:
Sebastian Sdorra
2019-08-20 14:43:48 +02:00
parent 65b59d1aec
commit e24673be0a
12 changed files with 276 additions and 15 deletions

View File

@@ -37,14 +37,20 @@ package sonia.scm.plugin;
import com.google.common.collect.ImmutableList;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
//~--- JDK imports ------------------------------------------------------------
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
/**
*
* @author Sebastian Sdorra
@@ -52,13 +58,17 @@ import java.util.stream.Collectors;
@Singleton
public class DefaultPluginManager implements PluginManager {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPluginManager.class);
private final PluginLoader loader;
private final PluginCenter center;
private final PluginInstaller installer;
@Inject
public DefaultPluginManager(PluginLoader loader, PluginCenter center) {
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer) {
this.loader = loader;
this.center = center;
this.installer = installer;
}
@Override
@@ -98,6 +108,18 @@ public class DefaultPluginManager implements PluginManager {
@Override
public void install(String name) {
if (getInstalled(name).isPresent()){
LOG.info("plugin {} is already installed, skipping installation", name);
return;
}
AvailablePlugin plugin = getAvailable(name).orElseThrow(() -> NotFoundException.notFound(entity(AvailablePlugin.class, name)));
Set<String> dependencies = plugin.getDescriptor().getDependencies();
if (dependencies != null) {
for (String dependency: dependencies){
install(dependency);
}
}
installer.install(plugin);
}
}

View File

@@ -3,6 +3,7 @@ package sonia.scm.plugin;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -77,6 +78,8 @@ public final class PluginCenterDto implements Serializable {
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@NoArgsConstructor
@AllArgsConstructor
static class Link {
private String href;
}

View File

@@ -17,8 +17,9 @@ public abstract class PluginCenterDtoMapper {
Set<AvailablePlugin> map(PluginCenterDto pluginCenterDto) {
Set<AvailablePlugin> plugins = new HashSet<>();
for (PluginCenterDto.Plugin plugin : pluginCenterDto.getEmbedded().getPlugins()) {
String url = plugin.getLinks().get("download").getHref();
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
map(plugin), map(plugin.getConditions()), plugin.getDependencies()
map(plugin), map(plugin.getConditions()), plugin.getDependencies(), url, plugin.getSha256()
);
plugins.add(new AvailablePlugin(descriptor));
}

View File

@@ -0,0 +1,7 @@
package sonia.scm.plugin;
public class PluginChecksumMismatchException extends PluginInstallException {
public PluginChecksumMismatchException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
package sonia.scm.plugin;
public class PluginDownloadException extends PluginInstallException {
public PluginDownloadException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,12 @@
package sonia.scm.plugin;
public class PluginInstallException extends RuntimeException {
public PluginInstallException(String message) {
super(message);
}
public PluginInstallException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,63 @@
package sonia.scm.plugin;
import com.google.common.base.Throwables;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import sonia.scm.SCMContextProvider;
import sonia.scm.net.ahc.AdvancedHttpClient;
import sonia.scm.util.IOUtil;
import javax.inject.Inject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
class PluginInstaller {
private final SCMContextProvider context;
private final AdvancedHttpClient client;
@Inject
public PluginInstaller(SCMContextProvider context, AdvancedHttpClient client) {
this.context = context;
this.client = client;
}
public void install(AvailablePlugin plugin) {
File file = createFile(plugin);
try (InputStream input = download(plugin); OutputStream output = new FileOutputStream(file)) {
ByteStreams.copy(input, output);
verifyChecksum(plugin, file);
} catch (IOException ex) {
throw new PluginDownloadException("failed to install plugin", ex);
}
}
private void verifyChecksum(AvailablePlugin plugin, File file) throws IOException {
Optional<String> checksum = plugin.getDescriptor().getChecksum();
if (checksum.isPresent()) {
String calculatedChecksum = Files.hash(file, Hashing.sha256()).toString();
if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) {
throw new PluginChecksumMismatchException(
String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, checksum.get())
);
}
}
}
private InputStream download(AvailablePlugin plugin) throws IOException {
return client.get(plugin.getDescriptor().getUrl()).request().contentAsStream();
}
private File createFile(AvailablePlugin plugin) {
File pluginDirectory = new File(context.getBaseDirectory(), "plugins");
IOUtil.mkdirs(pluginDirectory);
return new File(pluginDirectory, plugin.getDescriptor().getInformation().getName() + ".smp");
}
}