From 74f3d92cca058f5c297e5b6032b5e41c935a5504 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sun, 13 Apr 2014 11:57:55 -0400 Subject: [PATCH] Upgrade plugin command --- .../com/gitblit/manager/GitblitManager.java | 5 +++ .../com/gitblit/manager/IPluginManager.java | 11 +++++ .../com/gitblit/manager/PluginManager.java | 24 ++++++++++ .../com/gitblit/models/PluginRegistry.java | 4 +- .../ssh/commands/PluginDispatcher.java | 44 ++++++++++++++++++- 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 9510de80..56a71d41 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -1259,6 +1259,11 @@ public class GitblitManager implements IGitblit { return pluginManager.installPlugin(url, verifyChecksum); } + @Override + public boolean upgradePlugin(String pluginId, String url, boolean verifyChecksum) throws IOException { + return pluginManager.upgradePlugin(pluginId, url, verifyChecksum); + } + @Override public List getRegisteredPlugins() { return pluginManager.getRegisteredPlugins(); diff --git a/src/main/java/com/gitblit/manager/IPluginManager.java b/src/main/java/com/gitblit/manager/IPluginManager.java index fd4247ed..317cff73 100644 --- a/src/main/java/com/gitblit/manager/IPluginManager.java +++ b/src/main/java/com/gitblit/manager/IPluginManager.java @@ -131,6 +131,17 @@ public interface IPluginManager extends IManager { */ boolean installPlugin(String url, boolean verifyChecksum) throws IOException; + /** + * Upgrade the install plugin from the specified url. + * + * @param pluginId + * @param url + * @param verifyChecksum + * @return true if the upgrade has been successful + * @throws IOException + */ + boolean upgradePlugin(String pluginId, String url, boolean verifyChecksum) throws IOException; + /** * The list of all registered plugins. * diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index 5bd97ea2..b2a87ab3 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -130,6 +130,29 @@ public class PluginManager implements IPluginManager, PluginStateListener { return PluginState.STARTED.equals(state); } + public synchronized boolean upgradePlugin(String pluginId, String url, boolean verifyChecksum) throws IOException { + // ensure we can download the update BEFORE we remove the existing one + File file = download(url, verifyChecksum); + if (file == null || !file.exists()) { + logger.error("Failed to download plugin {}", url); + return false; + } + + if (deletePlugin(pluginId)) { + String newPluginId = pf4j.loadPlugin(file); + if (StringUtils.isEmpty(newPluginId)) { + logger.error("Failed to load plugin {}", file); + return false; + } + + PluginState state = pf4j.startPlugin(newPluginId); + return PluginState.STARTED.equals(state); + } else { + logger.error("Failed to delete plugin {}", pluginId); + } + return false; + } + @Override public synchronized boolean disablePlugin(String pluginId) { return pf4j.disablePlugin(pluginId); @@ -296,6 +319,7 @@ public class PluginManager implements IPluginManager, PluginStateListener { map.put(reg.id, reg); } } + for (PluginWrapper pw : pf4j.getPlugins()) { String id = pw.getDescriptor().getPluginId(); PluginVersion pv = pw.getDescriptor().getVersion(); diff --git a/src/main/java/com/gitblit/models/PluginRegistry.java b/src/main/java/com/gitblit/models/PluginRegistry.java index 6d82b97e..7c1927f5 100644 --- a/src/main/java/com/gitblit/models/PluginRegistry.java +++ b/src/main/java/com/gitblit/models/PluginRegistry.java @@ -62,7 +62,7 @@ public class PluginRegistry implements Serializable { } public static enum InstallState { - NOT_INSTALLED, INSTALLED, CAN_UPDATE, UNKNOWN + NOT_INSTALLED, INSTALLED, UPDATE_AVAILABLE, UNKNOWN } /** @@ -131,7 +131,7 @@ public class PluginRegistry implements Serializable { case -1: return InstallState.UNKNOWN; case 1: - return InstallState.CAN_UPDATE; + return InstallState.UPDATE_AVAILABLE; default: return InstallState.INSTALLED; } diff --git a/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatcher.java b/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatcher.java index e33a2c91..313d731f 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatcher.java @@ -15,6 +15,7 @@ */ package com.gitblit.transport.ssh.commands; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -56,6 +57,7 @@ public class PluginDispatcher extends DispatchCommand { register(user, RefreshPlugins.class); register(user, AvailablePlugins.class); register(user, InstallPlugin.class); + register(user, UpgradePlugin.class); register(user, UninstallPlugin.class); } @@ -444,7 +446,7 @@ public class PluginDispatcher extends DispatchCommand { List list; if (updates) { - list = gitblit.getRegisteredPlugins(InstallState.CAN_UPDATE); + list = gitblit.getRegisteredPlugins(InstallState.UPDATE_AVAILABLE); } else { list = gitblit.getRegisteredPlugins(); } @@ -527,13 +529,51 @@ public class PluginDispatcher extends DispatchCommand { throw new Failure(1, String.format("Failed to install %s", urlOrIdOrName)); } } - } catch (Exception e) { + } catch (IOException e) { log.error("Failed to install " + urlOrIdOrName, e); throw new Failure(1, String.format("Failed to install %s", urlOrIdOrName), e); } } } + @CommandMetaData(name = "upgrade", description = "Upgrade a plugin") + public static class UpgradePlugin extends PluginCommand { + + @Argument(index = 0, required = true, metaVar = "|", usage = "the plugin to upgrade") + protected String id; + + @Option(name = "--version", usage = "The specific version to install") + private String version; + + @Option(name = "--noverify", usage = "Disable checksum verification") + private boolean disableChecksum; + + @Override + public void run() throws Failure { + IGitblit gitblit = getContext().getGitblit(); + PluginWrapper pluginWrapper = getPlugin(id); + if (pluginWrapper == null) { + throw new UnloggedFailure("Invalid plugin specified!"); + } + + PluginRelease pv = gitblit.lookupRelease(pluginWrapper.getPluginId(), version); + if (pv == null) { + throw new Failure(1, String.format("Plugin \"%s\" is not in the registry!", pluginWrapper.getPluginId())); + } + + try { + if (gitblit.upgradePlugin(pluginWrapper.getPluginId(), pv.url, !disableChecksum)) { + stdout.println(String.format("Upgraded %s", pluginWrapper.getPluginId())); + } else { + throw new Failure(1, String.format("Failed to upgrade %s", pluginWrapper.getPluginId())); + } + } catch (IOException e) { + log.error("Failed to upgrade " + pluginWrapper.getPluginId(), e); + throw new Failure(1, String.format("Failed to upgrade %s", pluginWrapper.getPluginId()), e); + } + } + } + @CommandMetaData(name = "uninstall", aliases = { "rm", "del" }, description = "Uninstall a plugin") public static class UninstallPlugin extends PluginCommand { -- 2.39.5