From f023a19c3eeeea2b014b47fae522a62eab320048 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Jul 2019 15:28:49 +0200 Subject: WIP plugins: install/uninstall --- server/lib/plugins/plugin-manager.ts | 76 ++++++++++++++++++++++++++++++++++-- server/lib/plugins/yarn.ts | 61 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 server/lib/plugins/yarn.ts (limited to 'server/lib/plugins') diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index b48ecc991..533ed4391 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -1,7 +1,7 @@ import { PluginModel } from '../../models/server/plugin' import { logger } from '../../helpers/logger' import { RegisterHookOptions } from '../../../shared/models/plugins/register.model' -import { join } from 'path' +import { basename, join } from 'path' import { CONFIG } from '../../initializers/config' import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' @@ -9,6 +9,7 @@ import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.mod import { createReadStream, createWriteStream } from 'fs' import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' import { PluginType } from '../../../shared/models/plugins/plugin.type' +import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' export interface RegisteredPlugin { name: string @@ -84,11 +85,63 @@ export class PluginManager { await plugin.unregister() } + async install (toInstall: string, version: string, fromDisk = false) { + let plugin: PluginModel + let name: string + + logger.info('Installing plugin %s.', toInstall) + + try { + fromDisk + ? await installNpmPluginFromDisk(toInstall) + : await installNpmPlugin(toInstall, version) + + name = fromDisk ? basename(toInstall) : toInstall + const pluginType = name.startsWith('peertube-theme-') ? PluginType.THEME : PluginType.PLUGIN + const pluginName = this.normalizePluginName(name) + + const packageJSON = this.getPackageJSON(pluginName, pluginType) + if (!isPackageJSONValid(packageJSON, pluginType)) { + throw new Error('PackageJSON is invalid.') + } + + [ plugin ] = await PluginModel.upsert({ + name: pluginName, + description: packageJSON.description, + type: pluginType, + version: packageJSON.version, + enabled: true, + uninstalled: false, + peertubeEngine: packageJSON.engine.peertube + }, { returning: true }) + } catch (err) { + logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) + + try { + await removeNpmPlugin(name) + } catch (err) { + logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) + } + + throw err + } + + logger.info('Successful installation of plugin %s.', toInstall) + + await this.registerPluginOrTheme(plugin) + } + + async uninstall (packageName: string) { + await PluginModel.uninstall(this.normalizePluginName(packageName)) + + await removeNpmPlugin(packageName) + } + private async registerPluginOrTheme (plugin: PluginModel) { logger.info('Registering plugin or theme %s.', plugin.name) - const pluginPath = join(CONFIG.STORAGE.PLUGINS_DIR, plugin.name, plugin.version) - const packageJSON: PluginPackageJson = require(join(pluginPath, 'package.json')) + const packageJSON = this.getPackageJSON(plugin.name, plugin.type) + const pluginPath = this.getPluginPath(plugin.name, plugin.type) if (!isPackageJSONValid(packageJSON, plugin.type)) { throw new Error('Package.JSON is invalid.') @@ -124,6 +177,7 @@ export class PluginManager { } const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) + if (!isLibraryCodeValid(library)) { throw new Error('Library code is not valid (miss register or unregister function)') } @@ -163,6 +217,22 @@ export class PluginManager { }) } + private getPackageJSON (pluginName: string, pluginType: PluginType) { + const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') + + return require(pluginPath) as PluginPackageJson + } + + private getPluginPath (pluginName: string, pluginType: PluginType) { + const prefix = pluginType === PluginType.PLUGIN ? 'peertube-plugin-' : 'peertube-theme-' + + return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', prefix + pluginName) + } + + private normalizePluginName (name: string) { + return name.replace(/^peertube-((theme)|(plugin))-/, '') + } + static get Instance () { return this.instance || (this.instance = new this()) } diff --git a/server/lib/plugins/yarn.ts b/server/lib/plugins/yarn.ts new file mode 100644 index 000000000..35fe1625f --- /dev/null +++ b/server/lib/plugins/yarn.ts @@ -0,0 +1,61 @@ +import { execShell } from '../../helpers/core-utils' +import { logger } from '../../helpers/logger' +import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' +import { CONFIG } from '../../initializers/config' +import { outputJSON, pathExists } from 'fs-extra' +import { join } from 'path' + +async function installNpmPlugin (name: string, version: string) { + // Security check + checkNpmPluginNameOrThrow(name) + checkPluginVersionOrThrow(version) + + const toInstall = `${name}@${version}` + await execYarn('add ' + toInstall) +} + +async function installNpmPluginFromDisk (path: string) { + await execYarn('add file:' + path) +} + +async function removeNpmPlugin (name: string) { + checkNpmPluginNameOrThrow(name) + + await execYarn('remove ' + name) +} + +// ############################################################################ + +export { + installNpmPlugin, + installNpmPluginFromDisk, + removeNpmPlugin +} + +// ############################################################################ + +async function execYarn (command: string) { + try { + const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR + const pluginPackageJSON = join(pluginDirectory, 'package.json') + + // Create empty package.json file if needed + if (!await pathExists(pluginPackageJSON)) { + await outputJSON(pluginPackageJSON, {}) + } + + await execShell(`yarn ${command}`, { cwd: pluginDirectory }) + } catch (result) { + logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr }) + + throw result.err + } +} + +function checkNpmPluginNameOrThrow (name: string) { + if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install') +} + +function checkPluginVersionOrThrow (name: string) { + if (!isPluginVersionValid(name)) throw new Error('Invalid NPM plugin version to install') +} -- cgit v1.2.3