Improve plugins docker build and fixed plugin loading when dependencies are specified (#6164)

* Install pnpm only local - not global

* Install plugins during docker build with live-plugin-manager

* Migrated installer to ts.

* Added missing workspace script.

* Fixed docker build.

* Fix Dockerfile

* Fixed installer not being yet initialized.

* Ported installer to correct install path.

* Fixed pnpm installation.

* Fixed docker build.

* Fixed plugin loading.

* Fixed plugins not being able to be loaded.

* Fix plugin installation instructions in README

* Fixed startup.

* Fixed folder not present.

* Added unlinking dependencies.

* Added deleting dependencies.

* Fixed listing plugins.

---------

Co-authored-by: SamTV12345 <40429738+samtv12345@users.noreply.github.com>
This commit is contained in:
Stefan Müller 2024-03-14 16:06:32 +01:00 committed by GitHub
parent f9e3416d78
commit fe106f0afc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 413 additions and 66 deletions

View file

@ -0,0 +1,230 @@
import {IPluginInfo, PluginManager} from "live-plugin-manager-pnpm";
import path from "path";
import {node_modules, pluginInstallPath} from "./installer";
import {accessSync, constants, rmSync, symlinkSync, unlinkSync} from "node:fs";
import {dependencies, name} from '../../../package.json'
import {pathToFileURL} from 'node:url';
const settings = require('../../../node/utils/Settings');
import {readFileSync} from "fs";
export class LinkInstaller {
private livePluginManager: PluginManager;
private loadedPlugins: IPluginInfo[] = [];
/*
* A map of dependencies to their dependents
*
*/
private readonly dependenciesMap: Map<string, Set<string>>;
constructor() {
this.livePluginManager = new PluginManager({
pluginsPath: pluginInstallPath,
cwd: path.join(settings.root, 'src')
});
this.dependenciesMap = new Map();
}
public async init() {
// Insert Etherpad lite dependencies
for (let [dependency] of Object.entries(dependencies)) {
if (this.dependenciesMap.has(dependency)) {
this.dependenciesMap.get(dependency)?.add(name)
} else {
this.dependenciesMap.set(dependency, new Set([name]))
}
}
}
public async installFromPath(path: string) {
const installedPlugin = await this.livePluginManager.installFromPath(path)
await this.checkLinkedDependencies(installedPlugin)
}
public async installPlugin(pluginName: string, version?: string) {
if (version) {
const installedPlugin = await this.livePluginManager.install(pluginName, version);
this.linkDependency(pluginName)
await this.checkLinkedDependencies(installedPlugin)
} else {
const installedPlugin = await this.livePluginManager.install(pluginName);
this.linkDependency(pluginName)
await this.checkLinkedDependencies(installedPlugin)
}
}
public async listPlugins() {
const plugins = this.livePluginManager.list()
if (plugins && plugins.length > 0 && this.loadedPlugins.length == 0) {
this.loadedPlugins = plugins
// Check already installed plugins
for (let plugin of plugins) {
await this.checkLinkedDependencies(plugin)
}
}
return plugins
}
public async uninstallPlugin(pluginName: string) {
const installedPlugin = this.livePluginManager.getInfo(pluginName)
if (installedPlugin) {
console.debug(`Uninstalling plugin ${pluginName}`)
await this.removeSymlink(installedPlugin)
await this.livePluginManager.uninstall(pluginName)
await this.removeSubDependencies(installedPlugin)
}
}
private async removeSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
console.debug("Removing sub dependencies",pluginDependencies)
for (let dependency of pluginDependencies) {
await this.removeSubDependency(plugin.name, dependency)
}
}
private async removeSubDependency(_name: string, dependency:string) {
if (this.dependenciesMap.has(dependency)) {
console.debug(`Dependency ${dependency} is still being used by other plugins`)
return
}
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
for (let [subDependency] of Object.entries(json.dependencies)) {
await this.removeSubDependency(dependency, subDependency)
}
}
} catch (e){}
this.uninstallDependency(dependency)
}
private uninstallDependency(dependency: string) {
try {
console.debug(`Uninstalling dependency ${dependency}`)
// Check if the dependency is already installed
accessSync(path.join(pluginInstallPath, dependency), constants.F_OK)
rmSync(path.join(pluginInstallPath, dependency), {
force: true,
recursive: true
})
} catch (err) {
// Symlink does not exist
// So nothing to do
}
}
private async removeSymlink(plugin: IPluginInfo) {
try {
accessSync(path.join(node_modules, plugin.name), constants.F_OK)
await this.unlinkSubDependencies(plugin)
// Remove the plugin itself
this.unlinkDependency(plugin.name)
} catch (err) {
console.error(`Symlink for ${plugin.name} does not exist`)
// Symlink does not exist
// So nothing to do
}
}
private async unlinkSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
for (let dependency of pluginDependencies) {
this.dependenciesMap.get(dependency)?.delete(plugin.name)
await this.unlinkSubDependency(plugin.name, dependency)
}
}
private async unlinkSubDependency(plugin: string, dependency: string) {
if (this.dependenciesMap.has(dependency)) {
this.dependenciesMap.get(dependency)?.delete(plugin)
if (this.dependenciesMap.get(dependency)!.size > 0) {
// We have other dependants so do not uninstall
return
}
}
this.unlinkDependency(dependency)
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
for (let [subDependency] of Object.entries(json.dependencies)) {
await this.unlinkSubDependency(dependency, subDependency)
}
}
} catch (e){}
console.debug("Unlinking sub dependency",dependency)
this.dependenciesMap.delete(dependency)
}
private async addSubDependencies(plugin: IPluginInfo) {
const pluginDependencies = Object.keys(plugin.dependencies)
for (let dependency of pluginDependencies) {
await this.addSubDependency(plugin.name, dependency)
}
}
private async addSubDependency(plugin: string, dependency: string) {
if (this.dependenciesMap.has(dependency)) {
// We already added the sub dependency
this.dependenciesMap.get(dependency)?.add(plugin)
} else {
this.linkDependency(dependency)
// Read sub dependencies
try {
const json:IPluginInfo = JSON.parse(
readFileSync(pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json'))) as unknown as string);
if(json.dependencies){
Object.keys(json.dependencies).forEach((subDependency: string) => {
this.addSubDependency(dependency, subDependency)
})
}
} catch (err) {
console.error(`Error reading package.json ${err} for ${pathToFileURL(path.join(pluginInstallPath, dependency, 'package.json')).toString()}`)
}
this.dependenciesMap.set(dependency, new Set([plugin]))
}
}
private linkDependency(dependency: string) {
try {
// Check if the dependency is already installed
accessSync(path.join(node_modules, dependency), constants.F_OK)
} catch (err) {
symlinkSync(path.join(pluginInstallPath, dependency), path.join(node_modules, dependency), 'dir')
}
}
private unlinkDependency(dependency: string) {
try {
// Check if the dependency is already installed
accessSync(path.join(node_modules, dependency), constants.F_OK)
unlinkSync(path.join(node_modules, dependency))
} catch (err) {
// Symlink does not exist
// So nothing to do
}
}
private async checkLinkedDependencies(plugin: IPluginInfo) {
// Check if the plugin really exists at source
try {
accessSync(path.join(pluginInstallPath, plugin.name), constants.F_OK)
// Skip if the plugin is already linked
} catch (err) {
// The plugin is not installed
console.debug(`Plugin ${plugin.name} is not installed`)
}
await this.addSubDependencies(plugin)
this.dependenciesMap.set(plugin.name, new Set())
}
}

View file

@ -1,20 +1,28 @@
'use strict';
const log4js = require('log4js');
import log4js from "log4js";
import axios, {AxiosResponse} from "axios";
import {PackageData, PackageInfo} from "../../../node/types/PackageInfo";
import {MapArrayType} from "../../../node/types/MapType";
import path from "path";
import {promises as fs} from "fs";
const plugins = require('./plugins');
const hooks = require('./hooks');
const runCmd = require('../../../node/utils/run_cmd');
const settings = require('../../../node/utils/Settings');
const axios = require('axios');
const {PluginManager} = require('live-plugin-manager-pnpm');
const {promises: fs} = require('fs');
const path = require('path');
import {LinkInstaller} from "./LinkInstaller";
const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths');
const logger = log4js.getLogger('plugins');
exports.manager = new PluginManager();
export const pluginInstallPath = path.join(settings.root, 'src','plugin_packages');
export const node_modules = path.join(findEtherpadRoot(),'src', 'node_modules');
const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
export const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
const onAllTasksFinished = async () => {
await plugins.update();
@ -30,10 +38,12 @@ const headers = {
let tasks = 0;
const wrapTaskCb = (cb) => {
export const linkInstaller = new LinkInstaller();
const wrapTaskCb = (cb:Function|null) => {
tasks++;
return (...args) => {
return (...args: any) => {
cb && cb(...args);
tasks--;
if (tasks === 0) onAllTasksFinished();
@ -53,18 +63,23 @@ const migratePluginsFromNodeModules = async () => {
await Promise.all(Object.entries(dependencies)
.filter(([pkg, info]) => pkg.startsWith(plugins.prefix) && pkg !== 'ep_etherpad-lite')
.map(async ([pkg, info]) => {
if (!info.resolved) {
const _info = info as PackageInfo
if (!_info.resolved) {
// Install from node_modules directory
await exports.manager.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
await linkInstaller.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
} else {
await exports.manager.install(pkg);
await linkInstaller.installPlugin(pkg);
}
}));
await persistInstalledPlugins();
};
exports.checkForMigration = async () => {
export const checkForMigration = async () => {
logger.info('check installed plugins for migration');
// Initialize linkInstaller
await linkInstaller.init()
try {
await fs.access(installedPluginsPath, fs.constants.F_OK);
@ -72,19 +87,47 @@ exports.checkForMigration = async () => {
await migratePluginsFromNodeModules();
}
/*
* Check if the plugin is already installed in node_modules
* If not, create a symlink to node_modules
* This is necessary as
* 1. Live Plugin Manager does not support loading plugins from the directory so that node can access them normally
* 2. Plugins can't be directly installed to node_modules otherwise upgrading Etherpad will remove them
*/
fs.stat(pluginInstallPath).then(async (err) => {
const files = await fs.readdir(pluginInstallPath);
for (let file of files){
const moduleName = path.basename(file);
try {
await fs.access(path.join(node_modules, moduleName), fs.constants.F_OK);
logger.debug(`plugin ${moduleName} already exists in node_modules`);
} catch (err) {
// Create symlink to node_modules
logger.debug(`create symlink for ${file} to ${path.join(node_modules,moduleName)}`)
await fs.symlink(path.join(pluginInstallPath,file), path.join(node_modules,moduleName), 'dir')
}
}
}).catch(()=>{
logger.debug('plugin directory does not exist');
})
const fileContent = await fs.readFile(installedPluginsPath);
const installedPlugins = JSON.parse(fileContent.toString());
for (const plugin of installedPlugins.plugins) {
if (plugin.name.startsWith(plugins.prefix) && plugin.name !== 'ep_etherpad-lite') {
await exports.manager.install(plugin.name, plugin.version);
await linkInstaller.installPlugin(plugin.name, plugin.version);
}
}
};
const persistInstalledPlugins = async () => {
const installedPlugins = {plugins: []};
for (const pkg of Object.values(await plugins.getPackages())) {
const installedPlugins:{
plugins: PackageData[]
} = {plugins: []};
for (const pkg of Object.values(await plugins.getPackages()) as PackageData[]) {
installedPlugins.plugins.push({
name: pkg.name,
version: pkg.version,
@ -94,50 +137,51 @@ const persistInstalledPlugins = async () => {
await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins));
};
exports.uninstall = async (pluginName, cb = null) => {
export const uninstall = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb);
logger.info(`Uninstalling plugin ${pluginName}...`);
await exports.manager.uninstall(pluginName);
await linkInstaller.uninstallPlugin(pluginName);
logger.info(`Successfully uninstalled plugin ${pluginName}`);
await hooks.aCallAll('pluginUninstall', {pluginName});
cb(null);
};
exports.install = async (pluginName, cb = null) => {
export const install = async (pluginName: string, cb:Function|null = null) => {
cb = wrapTaskCb(cb);
logger.info(`Installing plugin ${pluginName}...`);
await exports.manager.install(pluginName);
await linkInstaller.installPlugin(pluginName);
logger.info(`Successfully installed plugin ${pluginName}`);
await hooks.aCallAll('pluginInstall', {pluginName});
cb(null);
};
exports.availablePlugins = null;
export let availablePlugins:MapArrayType<PackageInfo>|null = null;
let cacheTimestamp = 0;
exports.getAvailablePlugins = (maxCacheAge) => {
export const getAvailablePlugins = (maxCacheAge: number|false) => {
const nowTimestamp = Math.round(Date.now() / 1000);
return new Promise(async (resolve, reject) => {
return new Promise<MapArrayType<PackageInfo>>(async (resolve, reject) => {
// check cache age before making any request
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(exports.availablePlugins);
if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(availablePlugins);
}
await axios.get('https://static.etherpad.org/plugins.json', {headers})
.then((pluginsLoaded) => {
exports.availablePlugins = pluginsLoaded.data;
.then((pluginsLoaded:AxiosResponse<MapArrayType<PackageInfo>>) => {
availablePlugins = pluginsLoaded.data;
cacheTimestamp = nowTimestamp;
resolve(exports.availablePlugins);
resolve(availablePlugins);
})
.catch(async (err) => reject(err));
});
};
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then(
(results) => {
const res = {};
export const search = (searchTerm: string, maxCacheAge: number) => getAvailablePlugins(maxCacheAge).then(
(results: MapArrayType<PackageInfo>) => {
const res:MapArrayType<PackageData> = {};
if (searchTerm) {
searchTerm = searchTerm.toLowerCase();

View file

@ -8,7 +8,6 @@ const runCmd = require('../../../node/utils/run_cmd');
const tsort = require('./tsort');
const pluginUtils = require('./shared');
const defs = require('./plugin_defs');
const {manager} = require('./installer');
const settings = require('../../../node/utils/Settings');
const logger = log4js.getLogger('plugins');
@ -122,7 +121,8 @@ exports.update = async () => {
};
exports.getPackages = async () => {
const plugins = manager.list();
const {linkInstaller} = require("./installer");
const plugins = await linkInstaller.listPlugins();
const newDependencies = {};
for (const plugin of plugins) {