From 8d2be0ed7bb87283a1ec98609df6b82d83db706a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 11 Jul 2019 17:23:24 +0200 Subject: WIP plugins: move plugin CLI in peertube script Install/uninstall/list plugins remotely --- shared/extra-utils/index.ts | 1 + shared/extra-utils/miscs/miscs.ts | 5 +- shared/extra-utils/server/plugins.ts | 125 ++++++++++++++++++++++++++ shared/extra-utils/server/servers.ts | 14 +-- shared/extra-utils/users/login.ts | 19 ++++ shared/extra-utils/users/users.ts | 8 +- shared/extra-utils/videos/video-channels.ts | 8 +- shared/models/plugins/install-plugin.model.ts | 3 +- 8 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 shared/extra-utils/server/plugins.ts (limited to 'shared') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 9d0bbaa38..53ddaa681 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -11,6 +11,7 @@ export * from './server/follows' export * from './requests/requests' export * from './requests/check-api-params' export * from './server/servers' +export * from './server/plugins' export * from './videos/services' export * from './videos/video-playlists' export * from './users/users' diff --git a/shared/extra-utils/miscs/miscs.ts b/shared/extra-utils/miscs/miscs.ts index fb6430e4f..42250886c 100644 --- a/shared/extra-utils/miscs/miscs.ts +++ b/shared/extra-utils/miscs/miscs.ts @@ -8,7 +8,7 @@ import { pathExists, readFile } from 'fs-extra' import * as ffmpeg from 'fluent-ffmpeg' const expect = chai.expect -let webtorrent = new WebTorrent() +let webtorrent: WebTorrent.Instance function immutableAssign (target: T, source: U) { return Object.assign<{}, T, U>({}, target, source) @@ -27,6 +27,9 @@ function wait (milliseconds: number) { } function webtorrentAdd (torrent: string, refreshWebTorrent = false) { + const WebTorrent = require('webtorrent') + + if (!webtorrent) webtorrent = new WebTorrent() if (refreshWebTorrent === true) webtorrent = new WebTorrent() return new Promise(res => webtorrent.add(torrent, res)) diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts new file mode 100644 index 000000000..6cd7cd17a --- /dev/null +++ b/shared/extra-utils/server/plugins.ts @@ -0,0 +1,125 @@ +import { makeGetRequest, makePostBodyRequest } from '../requests/requests' +import { PluginType } from '../../models/plugins/plugin.type' + +function listPlugins (parameters: { + url: string, + accessToken: string, + start?: number, + count?: number, + sort?: string, + type?: PluginType, + expectedStatus?: number +}) { + const { url, accessToken, start, count, sort, type, expectedStatus = 200 } = parameters + const path = '/api/v1/plugins' + + return makeGetRequest({ + url, + path, + token: accessToken, + query: { + start, + count, + sort, + type + }, + statusCodeExpected: expectedStatus + }) +} + +function getPlugin (parameters: { + url: string, + accessToken: string, + npmName: string, + expectedStatus?: number +}) { + const { url, accessToken, npmName, expectedStatus = 200 } = parameters + const path = '/api/v1/plugins/' + npmName + + return makeGetRequest({ + url, + path, + token: accessToken, + statusCodeExpected: expectedStatus + }) +} + +function getPluginSettings (parameters: { + url: string, + accessToken: string, + npmName: string, + expectedStatus?: number +}) { + const { url, accessToken, npmName, expectedStatus = 200 } = parameters + const path = '/api/v1/plugins/' + npmName + '/settings' + + return makeGetRequest({ + url, + path, + token: accessToken, + statusCodeExpected: expectedStatus + }) +} + +function getPluginRegisteredSettings (parameters: { + url: string, + accessToken: string, + npmName: string, + expectedStatus?: number +}) { + const { url, accessToken, npmName, expectedStatus = 200 } = parameters + const path = '/api/v1/plugins/' + npmName + '/registered-settings' + + return makeGetRequest({ + url, + path, + token: accessToken, + statusCodeExpected: expectedStatus + }) +} + +function installPlugin (parameters: { + url: string, + accessToken: string, + path?: string, + npmName?: string + expectedStatus?: number +}) { + const { url, accessToken, npmName, path, expectedStatus = 204 } = parameters + const apiPath = '/api/v1/plugins/install' + + return makePostBodyRequest({ + url, + path: apiPath, + token: accessToken, + fields: { npmName, path }, + statusCodeExpected: expectedStatus + }) +} + +function uninstallPlugin (parameters: { + url: string, + accessToken: string, + npmName: string + expectedStatus?: number +}) { + const { url, accessToken, npmName, expectedStatus = 204 } = parameters + const apiPath = '/api/v1/plugins/uninstall' + + return makePostBodyRequest({ + url, + path: apiPath, + token: accessToken, + fields: { npmName }, + statusCodeExpected: expectedStatus + }) +} + +export { + listPlugins, + installPlugin, + getPlugin, + uninstallPlugin, + getPluginSettings, + getPluginRegisteredSettings +} diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 4c7d6862a..9167ebe5b 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -3,7 +3,7 @@ import { ChildProcess, exec, fork } from 'child_process' import { join } from 'path' import { root, wait } from '../miscs/miscs' -import { copy, readdir, readFile, remove } from 'fs-extra' +import { copy, pathExists, readdir, readFile, remove } from 'fs-extra' import { existsSync } from 'fs' import { expect } from 'chai' import { VideoChannel } from '../../models/videos' @@ -241,20 +241,22 @@ async function reRunServer (server: ServerInfo, configOverride?: any) { return server } -async function checkTmpIsEmpty (server: ServerInfo) { - return checkDirectoryIsEmpty(server, 'tmp') +function checkTmpIsEmpty (server: ServerInfo) { + return checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css' ]) } -async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) { +async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) { const testDirectory = 'test' + server.internalServerNumber const directoryPath = join(root(), testDirectory, directory) - const directoryExists = existsSync(directoryPath) + const directoryExists = await pathExists(directoryPath) expect(directoryExists).to.be.true const files = await readdir(directoryPath) - expect(files).to.have.lengthOf(0) + const filtered = files.filter(f => exceptions.includes(f) === false) + + expect(filtered).to.have.lengthOf(0) } function killallServers (servers: ServerInfo[]) { diff --git a/shared/extra-utils/users/login.ts b/shared/extra-utils/users/login.ts index ddeb9df2a..f9bfb3cb3 100644 --- a/shared/extra-utils/users/login.ts +++ b/shared/extra-utils/users/login.ts @@ -1,6 +1,7 @@ import * as request from 'supertest' import { ServerInfo } from '../server/servers' +import { getClient } from '../server/clients' type Client = { id: string, secret: string } type User = { username: string, password: string } @@ -38,6 +39,23 @@ async function userLogin (server: Server, user: User, expectedStatus = 200) { return res.body.access_token as string } +async function getAccessToken (url: string, username: string, password: string) { + const resClient = await getClient(url) + const client = { + id: resClient.body.client_id, + secret: resClient.body.client_secret + } + + const user = { username, password } + + try { + const res = await login(url, client, user) + return res.body.access_token + } catch (err) { + throw new Error('Cannot authenticate. Please check your username/password.') + } +} + function setAccessTokensToServers (servers: ServerInfo[]) { const tasks: Promise[] = [] @@ -55,6 +73,7 @@ export { login, serverLogin, userLogin, + getAccessToken, setAccessTokensToServers, Server, Client, diff --git a/shared/extra-utils/users/users.ts b/shared/extra-utils/users/users.ts index 1c39881d6..5fa8cde0c 100644 --- a/shared/extra-utils/users/users.ts +++ b/shared/extra-utils/users/users.ts @@ -1,11 +1,11 @@ import * as request from 'supertest' -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' - -import { UserCreate, UserRole } from '../../index' +import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' -import { ServerInfo, userLogin } from '..' import { UserAdminFlag } from '../../models/users/user-flag.model' import { UserRegister } from '../../models/users/user-register.model' +import { UserRole } from '../../models/users/user-role' +import { ServerInfo } from '../server/servers' +import { userLogin } from './login' type CreateUserArgs = { url: string, accessToken: string, diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts index 3e79cf15a..053842331 100644 --- a/shared/extra-utils/videos/video-channels.ts +++ b/shared/extra-utils/videos/video-channels.ts @@ -1,8 +1,10 @@ import * as request from 'supertest' -import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' +import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' +import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' import { makeGetRequest, updateAvatarRequest } from '../requests/requests' -import { getMyUserInformation, ServerInfo } from '..' -import { User } from '../..' +import { ServerInfo } from '../server/servers' +import { User } from '../../models/users/user.model' +import { getMyUserInformation } from '../users/users' function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { const path = '/api/v1/video-channels' diff --git a/shared/models/plugins/install-plugin.model.ts b/shared/models/plugins/install-plugin.model.ts index 03d87fe57..b1b46fa08 100644 --- a/shared/models/plugins/install-plugin.model.ts +++ b/shared/models/plugins/install-plugin.model.ts @@ -1,3 +1,4 @@ export interface InstallPlugin { - npmName: string + npmName?: string + path?: string } -- cgit v1.2.3