diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-11 17:23:24 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 8d2be0ed7bb87283a1ec98609df6b82d83db706a (patch) | |
tree | 31a36b252df32be83ceb77658a53b57f9d15e8ac /shared | |
parent | dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8 (diff) | |
download | PeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.tar.gz PeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.tar.zst PeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.zip |
WIP plugins: move plugin CLI in peertube script
Install/uninstall/list plugins remotely
Diffstat (limited to 'shared')
-rw-r--r-- | shared/extra-utils/index.ts | 1 | ||||
-rw-r--r-- | shared/extra-utils/miscs/miscs.ts | 5 | ||||
-rw-r--r-- | shared/extra-utils/server/plugins.ts | 125 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 14 | ||||
-rw-r--r-- | shared/extra-utils/users/login.ts | 19 | ||||
-rw-r--r-- | shared/extra-utils/users/users.ts | 8 | ||||
-rw-r--r-- | shared/extra-utils/videos/video-channels.ts | 8 | ||||
-rw-r--r-- | shared/models/plugins/install-plugin.model.ts | 3 |
8 files changed, 168 insertions, 15 deletions
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' | |||
11 | export * from './requests/requests' | 11 | export * from './requests/requests' |
12 | export * from './requests/check-api-params' | 12 | export * from './requests/check-api-params' |
13 | export * from './server/servers' | 13 | export * from './server/servers' |
14 | export * from './server/plugins' | ||
14 | export * from './videos/services' | 15 | export * from './videos/services' |
15 | export * from './videos/video-playlists' | 16 | export * from './videos/video-playlists' |
16 | export * from './users/users' | 17 | 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' | |||
8 | import * as ffmpeg from 'fluent-ffmpeg' | 8 | import * as ffmpeg from 'fluent-ffmpeg' |
9 | 9 | ||
10 | const expect = chai.expect | 10 | const expect = chai.expect |
11 | let webtorrent = new WebTorrent() | 11 | let webtorrent: WebTorrent.Instance |
12 | 12 | ||
13 | function immutableAssign <T, U> (target: T, source: U) { | 13 | function immutableAssign <T, U> (target: T, source: U) { |
14 | return Object.assign<{}, T, U>({}, target, source) | 14 | return Object.assign<{}, T, U>({}, target, source) |
@@ -27,6 +27,9 @@ function wait (milliseconds: number) { | |||
27 | } | 27 | } |
28 | 28 | ||
29 | function webtorrentAdd (torrent: string, refreshWebTorrent = false) { | 29 | function webtorrentAdd (torrent: string, refreshWebTorrent = false) { |
30 | const WebTorrent = require('webtorrent') | ||
31 | |||
32 | if (!webtorrent) webtorrent = new WebTorrent() | ||
30 | if (refreshWebTorrent === true) webtorrent = new WebTorrent() | 33 | if (refreshWebTorrent === true) webtorrent = new WebTorrent() |
31 | 34 | ||
32 | return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res)) | 35 | return new Promise<WebTorrent.Torrent>(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 @@ | |||
1 | import { makeGetRequest, makePostBodyRequest } from '../requests/requests' | ||
2 | import { PluginType } from '../../models/plugins/plugin.type' | ||
3 | |||
4 | function listPlugins (parameters: { | ||
5 | url: string, | ||
6 | accessToken: string, | ||
7 | start?: number, | ||
8 | count?: number, | ||
9 | sort?: string, | ||
10 | type?: PluginType, | ||
11 | expectedStatus?: number | ||
12 | }) { | ||
13 | const { url, accessToken, start, count, sort, type, expectedStatus = 200 } = parameters | ||
14 | const path = '/api/v1/plugins' | ||
15 | |||
16 | return makeGetRequest({ | ||
17 | url, | ||
18 | path, | ||
19 | token: accessToken, | ||
20 | query: { | ||
21 | start, | ||
22 | count, | ||
23 | sort, | ||
24 | type | ||
25 | }, | ||
26 | statusCodeExpected: expectedStatus | ||
27 | }) | ||
28 | } | ||
29 | |||
30 | function getPlugin (parameters: { | ||
31 | url: string, | ||
32 | accessToken: string, | ||
33 | npmName: string, | ||
34 | expectedStatus?: number | ||
35 | }) { | ||
36 | const { url, accessToken, npmName, expectedStatus = 200 } = parameters | ||
37 | const path = '/api/v1/plugins/' + npmName | ||
38 | |||
39 | return makeGetRequest({ | ||
40 | url, | ||
41 | path, | ||
42 | token: accessToken, | ||
43 | statusCodeExpected: expectedStatus | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | function getPluginSettings (parameters: { | ||
48 | url: string, | ||
49 | accessToken: string, | ||
50 | npmName: string, | ||
51 | expectedStatus?: number | ||
52 | }) { | ||
53 | const { url, accessToken, npmName, expectedStatus = 200 } = parameters | ||
54 | const path = '/api/v1/plugins/' + npmName + '/settings' | ||
55 | |||
56 | return makeGetRequest({ | ||
57 | url, | ||
58 | path, | ||
59 | token: accessToken, | ||
60 | statusCodeExpected: expectedStatus | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | function getPluginRegisteredSettings (parameters: { | ||
65 | url: string, | ||
66 | accessToken: string, | ||
67 | npmName: string, | ||
68 | expectedStatus?: number | ||
69 | }) { | ||
70 | const { url, accessToken, npmName, expectedStatus = 200 } = parameters | ||
71 | const path = '/api/v1/plugins/' + npmName + '/registered-settings' | ||
72 | |||
73 | return makeGetRequest({ | ||
74 | url, | ||
75 | path, | ||
76 | token: accessToken, | ||
77 | statusCodeExpected: expectedStatus | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | function installPlugin (parameters: { | ||
82 | url: string, | ||
83 | accessToken: string, | ||
84 | path?: string, | ||
85 | npmName?: string | ||
86 | expectedStatus?: number | ||
87 | }) { | ||
88 | const { url, accessToken, npmName, path, expectedStatus = 204 } = parameters | ||
89 | const apiPath = '/api/v1/plugins/install' | ||
90 | |||
91 | return makePostBodyRequest({ | ||
92 | url, | ||
93 | path: apiPath, | ||
94 | token: accessToken, | ||
95 | fields: { npmName, path }, | ||
96 | statusCodeExpected: expectedStatus | ||
97 | }) | ||
98 | } | ||
99 | |||
100 | function uninstallPlugin (parameters: { | ||
101 | url: string, | ||
102 | accessToken: string, | ||
103 | npmName: string | ||
104 | expectedStatus?: number | ||
105 | }) { | ||
106 | const { url, accessToken, npmName, expectedStatus = 204 } = parameters | ||
107 | const apiPath = '/api/v1/plugins/uninstall' | ||
108 | |||
109 | return makePostBodyRequest({ | ||
110 | url, | ||
111 | path: apiPath, | ||
112 | token: accessToken, | ||
113 | fields: { npmName }, | ||
114 | statusCodeExpected: expectedStatus | ||
115 | }) | ||
116 | } | ||
117 | |||
118 | export { | ||
119 | listPlugins, | ||
120 | installPlugin, | ||
121 | getPlugin, | ||
122 | uninstallPlugin, | ||
123 | getPluginSettings, | ||
124 | getPluginRegisteredSettings | ||
125 | } | ||
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 @@ | |||
3 | import { ChildProcess, exec, fork } from 'child_process' | 3 | import { ChildProcess, exec, fork } from 'child_process' |
4 | import { join } from 'path' | 4 | import { join } from 'path' |
5 | import { root, wait } from '../miscs/miscs' | 5 | import { root, wait } from '../miscs/miscs' |
6 | import { copy, readdir, readFile, remove } from 'fs-extra' | 6 | import { copy, pathExists, readdir, readFile, remove } from 'fs-extra' |
7 | import { existsSync } from 'fs' | 7 | import { existsSync } from 'fs' |
8 | import { expect } from 'chai' | 8 | import { expect } from 'chai' |
9 | import { VideoChannel } from '../../models/videos' | 9 | import { VideoChannel } from '../../models/videos' |
@@ -241,20 +241,22 @@ async function reRunServer (server: ServerInfo, configOverride?: any) { | |||
241 | return server | 241 | return server |
242 | } | 242 | } |
243 | 243 | ||
244 | async function checkTmpIsEmpty (server: ServerInfo) { | 244 | function checkTmpIsEmpty (server: ServerInfo) { |
245 | return checkDirectoryIsEmpty(server, 'tmp') | 245 | return checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css' ]) |
246 | } | 246 | } |
247 | 247 | ||
248 | async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) { | 248 | async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) { |
249 | const testDirectory = 'test' + server.internalServerNumber | 249 | const testDirectory = 'test' + server.internalServerNumber |
250 | 250 | ||
251 | const directoryPath = join(root(), testDirectory, directory) | 251 | const directoryPath = join(root(), testDirectory, directory) |
252 | 252 | ||
253 | const directoryExists = existsSync(directoryPath) | 253 | const directoryExists = await pathExists(directoryPath) |
254 | expect(directoryExists).to.be.true | 254 | expect(directoryExists).to.be.true |
255 | 255 | ||
256 | const files = await readdir(directoryPath) | 256 | const files = await readdir(directoryPath) |
257 | expect(files).to.have.lengthOf(0) | 257 | const filtered = files.filter(f => exceptions.includes(f) === false) |
258 | |||
259 | expect(filtered).to.have.lengthOf(0) | ||
258 | } | 260 | } |
259 | 261 | ||
260 | function killallServers (servers: ServerInfo[]) { | 262 | 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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | 2 | ||
3 | import { ServerInfo } from '../server/servers' | 3 | import { ServerInfo } from '../server/servers' |
4 | import { getClient } from '../server/clients' | ||
4 | 5 | ||
5 | type Client = { id: string, secret: string } | 6 | type Client = { id: string, secret: string } |
6 | type User = { username: string, password: string } | 7 | type User = { username: string, password: string } |
@@ -38,6 +39,23 @@ async function userLogin (server: Server, user: User, expectedStatus = 200) { | |||
38 | return res.body.access_token as string | 39 | return res.body.access_token as string |
39 | } | 40 | } |
40 | 41 | ||
42 | async function getAccessToken (url: string, username: string, password: string) { | ||
43 | const resClient = await getClient(url) | ||
44 | const client = { | ||
45 | id: resClient.body.client_id, | ||
46 | secret: resClient.body.client_secret | ||
47 | } | ||
48 | |||
49 | const user = { username, password } | ||
50 | |||
51 | try { | ||
52 | const res = await login(url, client, user) | ||
53 | return res.body.access_token | ||
54 | } catch (err) { | ||
55 | throw new Error('Cannot authenticate. Please check your username/password.') | ||
56 | } | ||
57 | } | ||
58 | |||
41 | function setAccessTokensToServers (servers: ServerInfo[]) { | 59 | function setAccessTokensToServers (servers: ServerInfo[]) { |
42 | const tasks: Promise<any>[] = [] | 60 | const tasks: Promise<any>[] = [] |
43 | 61 | ||
@@ -55,6 +73,7 @@ export { | |||
55 | login, | 73 | login, |
56 | serverLogin, | 74 | serverLogin, |
57 | userLogin, | 75 | userLogin, |
76 | getAccessToken, | ||
58 | setAccessTokensToServers, | 77 | setAccessTokensToServers, |
59 | Server, | 78 | Server, |
60 | Client, | 79 | 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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' | 2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' |
3 | |||
4 | import { UserCreate, UserRole } from '../../index' | ||
5 | import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' | 3 | import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' |
6 | import { ServerInfo, userLogin } from '..' | ||
7 | import { UserAdminFlag } from '../../models/users/user-flag.model' | 4 | import { UserAdminFlag } from '../../models/users/user-flag.model' |
8 | import { UserRegister } from '../../models/users/user-register.model' | 5 | import { UserRegister } from '../../models/users/user-register.model' |
6 | import { UserRole } from '../../models/users/user-role' | ||
7 | import { ServerInfo } from '../server/servers' | ||
8 | import { userLogin } from './login' | ||
9 | 9 | ||
10 | type CreateUserArgs = { url: string, | 10 | type CreateUserArgs = { url: string, |
11 | accessToken: string, | 11 | 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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' | 2 | import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' |
3 | import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' | ||
3 | import { makeGetRequest, updateAvatarRequest } from '../requests/requests' | 4 | import { makeGetRequest, updateAvatarRequest } from '../requests/requests' |
4 | import { getMyUserInformation, ServerInfo } from '..' | 5 | import { ServerInfo } from '../server/servers' |
5 | import { User } from '../..' | 6 | import { User } from '../../models/users/user.model' |
7 | import { getMyUserInformation } from '../users/users' | ||
6 | 8 | ||
7 | function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { | 9 | function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { |
8 | const path = '/api/v1/video-channels' | 10 | 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 @@ | |||
1 | export interface InstallPlugin { | 1 | export interface InstallPlugin { |
2 | npmName: string | 2 | npmName?: string |
3 | path?: string | ||
3 | } | 4 | } |