mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-12 08:25:50 +01:00
Make possible to install and uninstall bundled plugins
This commit is contained in:
BIN
src/main/resources/plugins/gitbucket-gist-plugin_2.12-4.9.0.jar
Normal file
BIN
src/main/resources/plugins/gitbucket-gist-plugin_2.12-4.9.0.jar
Normal file
Binary file not shown.
@@ -1 +1 @@
|
|||||||
#gitbucket-gist-plugin_2.12-4.9.0.jar
|
gitbucket-gist-plugin_2.12-4.9.0.jar
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
|||||||
|
|
||||||
post("/admin/plugins/_reload")(adminOnly {
|
post("/admin/plugins/_reload")(adminOnly {
|
||||||
PluginRegistry.reload(request.getServletContext(), loadSystemSettings(), request2Session(request).conn)
|
PluginRegistry.reload(request.getServletContext(), loadSystemSettings(), request2Session(request).conn)
|
||||||
flash += "info" -> "All plugins are reloaded."
|
flash += "info" -> "All plugins were reloaded."
|
||||||
redirect("/admin/plugins")
|
redirect("/admin/plugins")
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -194,13 +194,25 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
|||||||
val pluginId = params("pluginId")
|
val pluginId = params("pluginId")
|
||||||
PluginRegistry().getPlugins()
|
PluginRegistry().getPlugins()
|
||||||
.collect { case (plugin, true) if plugin.pluginId == pluginId => plugin }
|
.collect { case (plugin, true) if plugin.pluginId == pluginId => plugin }
|
||||||
.foreach { plugin =>
|
.foreach { _ =>
|
||||||
PluginRegistry.uninstall(pluginId, request.getServletContext, loadSystemSettings(), request2Session(request).conn)
|
PluginRegistry.uninstall(pluginId, request.getServletContext, loadSystemSettings(), request2Session(request).conn)
|
||||||
flash += "info" -> s"${pluginId} was uninstalled."
|
flash += "info" -> s"${pluginId} was uninstalled."
|
||||||
}
|
}
|
||||||
redirect("/admin/plugins")
|
redirect("/admin/plugins")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
post("/admin/plugins/:pluginId/_install")(adminOnly {
|
||||||
|
val pluginId = params("pluginId")
|
||||||
|
PluginRegistry().getPlugins()
|
||||||
|
.collect { case (plugin, false) if plugin.pluginId == pluginId => plugin }
|
||||||
|
.foreach { _ =>
|
||||||
|
PluginRegistry.install(pluginId, request.getServletContext, loadSystemSettings(), request2Session(request).conn)
|
||||||
|
flash += "info" -> s"${pluginId} was installed."
|
||||||
|
}
|
||||||
|
redirect("/admin/plugins")
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
get("/admin/users")(adminOnly {
|
get("/admin/users")(adminOnly {
|
||||||
val includeRemoved = params.get("includeRemoved").map(_.toBoolean).getOrElse(false)
|
val includeRemoved = params.get("includeRemoved").map(_.toBoolean).getOrElse(false)
|
||||||
val users = getAllUsers(includeRemoved)
|
val users = getAllUsers(includeRemoved)
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ object PluginRegistry {
|
|||||||
*/
|
*/
|
||||||
def uninstall(pluginId: String, context: ServletContext, settings: SystemSettings, conn: java.sql.Connection): Unit = synchronized {
|
def uninstall(pluginId: String, context: ServletContext, settings: SystemSettings, conn: java.sql.Connection): Unit = synchronized {
|
||||||
instance.getPlugins()
|
instance.getPlugins()
|
||||||
.collect { case (plugin, true) if plugin.pluginId == plugin => plugin }
|
.collect { case (plugin, true) if plugin.pluginId == pluginId => plugin }
|
||||||
.foreach { plugin =>
|
.foreach { plugin =>
|
||||||
// try {
|
// try {
|
||||||
// plugin.pluginClass.uninstall(instance, context, settings)
|
// plugin.pluginClass.uninstall(instance, context, settings)
|
||||||
@@ -216,30 +216,61 @@ object PluginRegistry {
|
|||||||
// case e: Exception =>
|
// case e: Exception =>
|
||||||
// logger.error(s"Error during uninstalling plugin: ${plugin.pluginJar.getName}", e)
|
// logger.error(s"Error during uninstalling plugin: ${plugin.pluginJar.getName}", e)
|
||||||
// }
|
// }
|
||||||
shutdown(context, settings)
|
shutdown(context, settings)
|
||||||
plugin.pluginJar.delete()
|
plugin.pluginJar.delete()
|
||||||
instance = new PluginRegistry()
|
instance = new PluginRegistry()
|
||||||
initialize(context, settings, conn)
|
initialize(context, settings, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def copyFile(from: File, to: File, retry: Int = 0): Unit = {
|
/**
|
||||||
using(FileChannel.open(from.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE)){ fc =>
|
* Install a specified plugin from local repository.
|
||||||
using(fc.tryLock()){ lock =>
|
*/
|
||||||
if(lock == null){
|
def install(pluginId: String, context: ServletContext, settings: SystemSettings, conn: java.sql.Connection): Unit = synchronized {
|
||||||
if(retry >= 3){ // Retry max 3 times
|
instance.getPlugins()
|
||||||
logger.info(s"Retire to install plugin: ${from.getAbsolutePath}")
|
.collect { case (plugin, false) if plugin.pluginId == pluginId => plugin }
|
||||||
} else {
|
.foreach { plugin =>
|
||||||
logger.info(s"Retry ${retry + 1} to install plugin: ${from.getAbsolutePath}")
|
FileUtils.copyFile(plugin.pluginJar, new File(PluginHome, plugin.pluginJar.getName))
|
||||||
Thread.sleep(500)
|
|
||||||
copyFile(from, to, retry + 1)
|
shutdown(context, settings)
|
||||||
}
|
instance = new PluginRegistry()
|
||||||
} else {
|
initialize(context, settings, conn)
|
||||||
logger.info(s"Install plugin: ${from.getAbsolutePath}")
|
|
||||||
FileUtils.copyFile(from, to)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// private def copyFile(from: File, to: File, retry: Int = 0): Unit = {
|
||||||
|
// using(FileChannel.open(from.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE)){ fc =>
|
||||||
|
// using(fc.tryLock()){ lock =>
|
||||||
|
// if(lock == null){
|
||||||
|
// if(retry >= 3){ // Retry max 3 times
|
||||||
|
// logger.info(s"Retire to install plugin: ${from.getAbsolutePath}")
|
||||||
|
// } else {
|
||||||
|
// logger.info(s"Retry ${retry + 1} to install plugin: ${from.getAbsolutePath}")
|
||||||
|
// Thread.sleep(500)
|
||||||
|
// copyFile(from, to, retry + 1)
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// logger.info(s"Install plugin: ${from.getAbsolutePath}")
|
||||||
|
// FileUtils.copyFile(from, to)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
private class PluginJarFileFilter extends FilenameFilter {
|
||||||
|
override def accept(dir: File, name: String): Boolean = name.endsWith(".jar")
|
||||||
|
}
|
||||||
|
|
||||||
|
private def listPluginJars(dir: File): Seq[File] = {
|
||||||
|
dir.listFiles(new PluginJarFileFilter()).map { file =>
|
||||||
|
val Array(name, version) = file.getName.split("_2.12-")
|
||||||
|
(name, Version.valueOf(version.replaceFirst("\\.jar$", "")), file)
|
||||||
|
}.groupBy { case (name, _, _) =>
|
||||||
|
name
|
||||||
|
}.map { case (name, versions) =>
|
||||||
|
// Adopt the latest version
|
||||||
|
versions.sortBy { case (name, version, file) => version }.reverse.head._3
|
||||||
|
}.toSeq.sortBy(_.getName)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -256,28 +287,20 @@ object PluginRegistry {
|
|||||||
}
|
}
|
||||||
installedDir.mkdir()
|
installedDir.mkdir()
|
||||||
|
|
||||||
if(pluginDir.exists && pluginDir.isDirectory){
|
if(pluginDir.exists && pluginDir.isDirectory) {
|
||||||
val files = pluginDir.listFiles(new FilenameFilter {
|
listPluginJars(pluginDir).foreach { pluginJar =>
|
||||||
override def accept(dir: File, name: String): Boolean = name.endsWith(".jar")
|
val installedJar = new File(installedDir, pluginJar.getName)
|
||||||
}).map { file =>
|
FileUtils.copyFile(pluginJar, installedJar)
|
||||||
val Array(name, version) = file.getName.split("_2.12-")
|
|
||||||
(name, Version.valueOf(version.replaceFirst("\\.jar$", "")), file)
|
|
||||||
}.groupBy { case (name, _, _) =>
|
|
||||||
name
|
|
||||||
}.map { case (name, versions) =>
|
|
||||||
// Adopt the latest version
|
|
||||||
versions.sortBy { case (name, version, file) => version }.reverse.head._3
|
|
||||||
}.toSeq.sortBy(_.getName).foreach { pluginJar =>
|
|
||||||
logger.info(s"Initialize ${pluginJar.getName}")
|
logger.info(s"Initialize ${pluginJar.getName}")
|
||||||
val classLoader = new URLClassLoader(Array(pluginJar.toURI.toURL), Thread.currentThread.getContextClassLoader)
|
val classLoader = new URLClassLoader(Array(installedJar.toURI.toURL), Thread.currentThread.getContextClassLoader)
|
||||||
try {
|
try {
|
||||||
val plugin = classLoader.loadClass("Plugin").getDeclaredConstructor().newInstance().asInstanceOf[Plugin]
|
val plugin = classLoader.loadClass("Plugin").getDeclaredConstructor().newInstance().asInstanceOf[Plugin]
|
||||||
val pluginId = plugin.pluginId
|
// val pluginId = plugin.pluginId
|
||||||
|
// // Check duplication
|
||||||
// // Check duplication
|
// instance.getPlugins().find(_.pluginId == pluginId).foreach { x =>
|
||||||
// instance.getPlugins().find(_.pluginId == pluginId).foreach { x =>
|
// throw new IllegalStateException(s"Plugin ${pluginId} is duplicated. ${x.pluginJar.getName} is available.")
|
||||||
// throw new IllegalStateException(s"Plugin ${pluginId} is duplicated. ${x.pluginJar.getName} is available.")
|
// }
|
||||||
// }
|
|
||||||
|
|
||||||
// Migration
|
// Migration
|
||||||
val solidbase = new Solidbase()
|
val solidbase = new Solidbase()
|
||||||
@@ -286,7 +309,7 @@ object PluginRegistry {
|
|||||||
// Check database version
|
// Check database version
|
||||||
val databaseVersion = manager.getCurrentVersion(plugin.pluginId)
|
val databaseVersion = manager.getCurrentVersion(plugin.pluginId)
|
||||||
val pluginVersion = plugin.versions.last.getVersion
|
val pluginVersion = plugin.versions.last.getVersion
|
||||||
if(databaseVersion != pluginVersion){
|
if (databaseVersion != pluginVersion) {
|
||||||
throw new IllegalStateException(s"Plugin version is ${pluginVersion}, but database version is ${databaseVersion}")
|
throw new IllegalStateException(s"Plugin version is ${pluginVersion}, but database version is ${databaseVersion}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,6 +331,38 @@ object PluginRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scan repository
|
||||||
|
val repositoryDir = new File(PluginHome, ".repository")
|
||||||
|
if (repositoryDir.exists) {
|
||||||
|
listPluginJars(repositoryDir).foreach { pluginJar =>
|
||||||
|
val classLoader = new URLClassLoader(Array(pluginJar.toURI.toURL), Thread.currentThread.getContextClassLoader)
|
||||||
|
try {
|
||||||
|
val plugin = classLoader.loadClass("Plugin").getDeclaredConstructor().newInstance().asInstanceOf[Plugin]
|
||||||
|
|
||||||
|
val enableSameOrNewer = instance.plugins.exists { case (installedPlugin, true) =>
|
||||||
|
installedPlugin.pluginId == plugin.pluginId &&
|
||||||
|
Version.valueOf(installedPlugin.pluginVersion).greaterThanOrEqualTo(Version.valueOf(plugin.versions.last.getVersion))
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!enableSameOrNewer){
|
||||||
|
instance.addPlugin(PluginInfo(
|
||||||
|
pluginId = plugin.pluginId,
|
||||||
|
pluginName = plugin.pluginName,
|
||||||
|
pluginVersion = plugin.versions.last.getVersion,
|
||||||
|
description = plugin.description,
|
||||||
|
pluginClass = plugin,
|
||||||
|
pluginJar = pluginJar,
|
||||||
|
classLoader = classLoader
|
||||||
|
), false)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
case e: Throwable => {
|
||||||
|
logger.error(s"Error during plugin initialization: ${pluginJar.getName}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(watcher == null){
|
if(watcher == null){
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
@gitbucket.core.admin.html.menu("plugins") {
|
@gitbucket.core.admin.html.menu("plugins") {
|
||||||
@gitbucket.core.helper.html.information(info)
|
@gitbucket.core.helper.html.information(info)
|
||||||
<form action="@context.path/admin/plugins/_reload" method="POST" class="pull-right">
|
<form action="@context.path/admin/plugins/_reload" method="POST" class="pull-right">
|
||||||
<input type="submit" value="Reload plugins" class="btn btn-success">
|
<input type="submit" value="Reload plugins" class="btn btn-default">
|
||||||
</form>
|
</form>
|
||||||
<h1>Installed plugins</h1>
|
<h1>Plugins</h1>
|
||||||
@if(plugins.size > 0) {
|
@if(plugins.size > 0) {
|
||||||
<ul>
|
<ul>
|
||||||
@plugins.map { case (plugin, enabled) =>
|
@plugins.map { case (plugin, enabled) =>
|
||||||
@@ -16,9 +16,15 @@
|
|||||||
@plugins.map { case (plugin, enabled) =>
|
@plugins.map { case (plugin, enabled) =>
|
||||||
<div class="panel panel-default">
|
<div class="panel panel-default">
|
||||||
<div class="panel-heading strong" id="@plugin.pluginId">
|
<div class="panel-heading strong" id="@plugin.pluginId">
|
||||||
<form action="@{context.path}/admin/plugins/@{plugin.pluginId}/_uninstall" method="POST" class="pull-right uninstall-form">
|
@if(enabled){
|
||||||
<input type="submit" value="Uninstall" class="btn btn-danger btn-sm" style="position: relative; top: -5px; left: 10px;" data-name="@plugin.pluginName">
|
<form action="@{context.path}/admin/plugins/@{plugin.pluginId}/_uninstall" method="POST" class="pull-right uninstall-form">
|
||||||
</form>
|
<input type="submit" value="Uninstall" class="btn btn-danger btn-sm" style="position: relative; top: -5px; left: 10px;" data-name="@plugin.pluginName">
|
||||||
|
</form>
|
||||||
|
} else {
|
||||||
|
<form action="@{context.path}/admin/plugins/@{plugin.pluginId}/_install" method="POST" class="pull-right install-form">
|
||||||
|
<input type="submit" value="Install" class="btn btn-success btn-sm" style="position: relative; top: -5px; left: 10px;" data-name="@plugin.pluginName">
|
||||||
|
</form>
|
||||||
|
}
|
||||||
@plugin.pluginName
|
@plugin.pluginName
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
@@ -52,5 +58,10 @@
|
|||||||
var name = $(e.target).data('name');
|
var name = $(e.target).data('name');
|
||||||
return confirm('Uninstall ' + name + '. Are you sure?');
|
return confirm('Uninstall ' + name + '. Are you sure?');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('.install-form').click(function(e){
|
||||||
|
var name = $(e.target).data('name');
|
||||||
|
return confirm('Install ' + name + '. Are you sure?');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user