aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-21 15:51:30 +0200
committerChocobozzz <me@florianbigard.com>2021-07-21 15:51:30 +0200
commita24bd1ed41b43790bab6ba789580bb4e85f07d85 (patch)
treea54b0f6c921ba83a6e909cd0ced325b2d4b8863c /server/tools
parent5f26f13b3c16ac5ae0a3b0a7142d84a9528cf565 (diff)
parentc63830f15403ac4e750829f27d8bbbdc9a59282c (diff)
downloadPeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.gz
PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.zst
PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.zip
Merge branch 'next' into develop
Diffstat (limited to 'server/tools')
-rw-r--r--server/tools/cli.ts44
-rw-r--r--server/tools/peertube-auth.ts6
-rw-r--r--server/tools/peertube-get-access-token.ts23
-rw-r--r--server/tools/peertube-import-videos.ts110
-rw-r--r--server/tools/peertube-plugins.ts49
-rw-r--r--server/tools/peertube-redundancy.ts60
-rw-r--r--server/tools/peertube-upload.ts17
-rw-r--r--server/tools/test-live.ts (renamed from server/tools/test.ts)61
8 files changed, 160 insertions, 210 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 7b94306cd..52e6ea593 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -1,14 +1,11 @@
1import { Command } from 'commander'
1import { Netrc } from 'netrc-parser' 2import { Netrc } from 'netrc-parser'
2import { getAppNumber, isTestInstance } from '../helpers/core-utils'
3import { join } from 'path' 3import { join } from 'path'
4import { root } from '../../shared/extra-utils/miscs/miscs'
5import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
6import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'
7import { createLogger, format, transports } from 'winston' 4import { createLogger, format, transports } from 'winston'
8import { getMyUserInformation } from '@shared/extra-utils/users/users' 5import { PeerTubeServer } from '@shared/extra-utils'
9import { User, UserRole } from '@shared/models' 6import { UserRole } from '@shared/models'
10import { getAccessToken } from '@shared/extra-utils/users/login' 7import { VideoPrivacy } from '../../shared/models/videos'
11import { Command } from 'commander' 8import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
12 9
13let configName = 'PeerTube/CLI' 10let configName = 'PeerTube/CLI'
14if (isTestInstance()) configName += `-${getAppNumber()}` 11if (isTestInstance()) configName += `-${getAppNumber()}`
@@ -17,17 +14,16 @@ const config = require('application-config')(configName)
17 14
18const version = require('../../../package.json').version 15const version = require('../../../package.json').version
19 16
20async function getAdminTokenOrDie (url: string, username: string, password: string) { 17async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) {
21 const accessToken = await getAccessToken(url, username, password) 18 const token = await server.login.getAccessToken(username, password)
22 const resMe = await getMyUserInformation(url, accessToken) 19 const me = await server.users.getMyInfo({ token })
23 const me: User = resMe.body
24 20
25 if (me.role !== UserRole.ADMINISTRATOR) { 21 if (me.role !== UserRole.ADMINISTRATOR) {
26 console.error('You must be an administrator.') 22 console.error('You must be an administrator.')
27 process.exit(-1) 23 process.exit(-1)
28 } 24 }
29 25
30 return accessToken 26 return token
31} 27}
32 28
33interface Settings { 29interface Settings {
@@ -128,7 +124,7 @@ function buildCommonVideoOptions (command: Command) {
128 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') 124 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
129} 125}
130 126
131async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { 127async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) {
132 const options = command.opts() 128 const options = command.opts()
133 129
134 const defaultBooleanAttributes = { 130 const defaultBooleanAttributes = {
@@ -164,8 +160,7 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
164 Object.assign(videoAttributes, booleanAttributes) 160 Object.assign(videoAttributes, booleanAttributes)
165 161
166 if (options.channelName) { 162 if (options.channelName) {
167 const res = await getVideoChannel(url, options.channelName) 163 const videoChannel = await server.channels.get({ channelName: options.channelName })
168 const videoChannel: VideoChannel = res.body
169 164
170 Object.assign(videoAttributes, { channelId: videoChannel.id }) 165 Object.assign(videoAttributes, { channelId: videoChannel.id })
171 166
@@ -184,6 +179,19 @@ function getServerCredentials (program: Command) {
184 }) 179 })
185} 180}
186 181
182function buildServer (url: string) {
183 return new PeerTubeServer({ url })
184}
185
186async function assignToken (server: PeerTubeServer, username: string, password: string) {
187 const bodyClient = await server.login.getClient()
188 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
189
190 const body = await server.login.login({ client, user: { username, password } })
191
192 server.accessToken = body.access_token
193}
194
187function getLogger (logLevel = 'info') { 195function getLogger (logLevel = 'info') {
188 const logLevels = { 196 const logLevels = {
189 0: 0, 197 0: 0,
@@ -230,5 +238,7 @@ export {
230 buildCommonVideoOptions, 238 buildCommonVideoOptions,
231 buildVideoAttributesFromCommander, 239 buildVideoAttributesFromCommander,
232 240
233 getAdminTokenOrDie 241 getAdminTokenOrDie,
242 buildServer,
243 assignToken
234} 244}
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
index 1934e7986..b9f4ef4f8 100644
--- a/server/tools/peertube-auth.ts
+++ b/server/tools/peertube-auth.ts
@@ -5,9 +5,8 @@ registerTSPaths()
5 5
6import { OptionValues, program } from 'commander' 6import { OptionValues, program } from 'commander'
7import * as prompt from 'prompt' 7import * as prompt from 'prompt'
8import { getNetrc, getSettings, writeSettings } from './cli' 8import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
9import { isUserUsernameValid } from '../helpers/custom-validators/users' 9import { isUserUsernameValid } from '../helpers/custom-validators/users'
10import { getAccessToken } from '../../shared/extra-utils'
11import * as CliTable3 from 'cli-table3' 10import * as CliTable3 from 'cli-table3'
12 11
13async function delInstance (url: string) { 12async function delInstance (url: string) {
@@ -97,7 +96,8 @@ program
97 // @see https://github.com/Chocobozzz/PeerTube/issues/3520 96 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
98 result.url = stripExtraneousFromPeerTubeUrl(result.url) 97 result.url = stripExtraneousFromPeerTubeUrl(result.url)
99 98
100 await getAccessToken(result.url, result.username, result.password) 99 const server = buildServer(result.url)
100 await assignToken(server, result.username, result.password)
101 } catch (err) { 101 } catch (err) {
102 console.error(err.message) 102 console.error(err.message)
103 process.exit(-1) 103 process.exit(-1)
diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts
index 9488eba0e..a67de9180 100644
--- a/server/tools/peertube-get-access-token.ts
+++ b/server/tools/peertube-get-access-token.ts
@@ -2,7 +2,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
2registerTSPaths() 2registerTSPaths()
3 3
4import { program } from 'commander' 4import { program } from 'commander'
5import { getClient, Server, serverLogin } from '../../shared/extra-utils' 5import { assignToken, buildServer } from './cli'
6 6
7program 7program
8 .option('-u, --url <url>', 'Server url') 8 .option('-u, --url <url>', 'Server url')
@@ -24,24 +24,11 @@ if (
24 process.exit(-1) 24 process.exit(-1)
25} 25}
26 26
27getClient(options.url) 27const server = buildServer(options.url)
28 .then(res => {
29 const server = {
30 url: options.url,
31 user: {
32 username: options.username,
33 password: options.password
34 },
35 client: {
36 id: res.body.client_id,
37 secret: res.body.client_secret
38 }
39 } as Server
40 28
41 return serverLogin(server) 29assignToken(server, options.username, options.password)
42 }) 30 .then(() => {
43 .then(accessToken => { 31 console.log(server.accessToken)
44 console.log(accessToken)
45 process.exit(0) 32 process.exit(0)
46 }) 33 })
47 .catch(err => { 34 .catch(err => {
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index 101a95b2a..52aae3d2c 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -8,17 +8,19 @@ import { truncate } from 'lodash'
8import { join } from 'path' 8import { join } from 'path'
9import * as prompt from 'prompt' 9import * as prompt from 'prompt'
10import { promisify } from 'util' 10import { promisify } from 'util'
11import { advancedVideosSearch, getClient, getVideoCategories, login, uploadVideo } from '../../shared/extra-utils/index' 11import { YoutubeDL } from '@server/helpers/youtube-dl'
12import { sha256 } from '../helpers/core-utils' 12import { sha256 } from '../helpers/core-utils'
13import { doRequestAndSaveToFile } from '../helpers/requests' 13import { doRequestAndSaveToFile } from '../helpers/requests'
14import { CONSTRAINTS_FIELDS } from '../initializers/constants' 14import { CONSTRAINTS_FIELDS } from '../initializers/constants'
15import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getLogger, getServerCredentials } from './cli' 15import {
16import { YoutubeDL } from '@server/helpers/youtube-dl' 16 assignToken,
17 17 buildCommonVideoOptions,
18type UserInfo = { 18 buildServer,
19 username: string 19 buildVideoAttributesFromCommander,
20 password: string 20 getLogger,
21} 21 getServerCredentials
22} from './cli'
23import { PeerTubeServer } from '@shared/extra-utils'
22 24
23const processOptions = { 25const processOptions = {
24 maxBuffer: Infinity 26 maxBuffer: Infinity
@@ -62,17 +64,13 @@ getServerCredentials(command)
62 url = normalizeTargetUrl(url) 64 url = normalizeTargetUrl(url)
63 options.targetUrl = normalizeTargetUrl(options.targetUrl) 65 options.targetUrl = normalizeTargetUrl(options.targetUrl)
64 66
65 const user = { username, password } 67 run(url, username, password)
66
67 run(url, user)
68 .catch(err => exitError(err)) 68 .catch(err => exitError(err))
69 }) 69 })
70 .catch(err => console.error(err)) 70 .catch(err => console.error(err))
71 71
72async function run (url: string, user: UserInfo) { 72async function run (url: string, username: string, password: string) {
73 if (!user.password) { 73 if (!password) password = await promptPassword()
74 user.password = await promptPassword()
75 }
76 74
77 const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL() 75 const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL()
78 76
@@ -111,7 +109,8 @@ async function run (url: string, user: UserInfo) {
111 await processVideo({ 109 await processVideo({
112 cwd: options.tmpdir, 110 cwd: options.tmpdir,
113 url, 111 url,
114 user, 112 username,
113 password,
115 youtubeInfo: info 114 youtubeInfo: info
116 }) 115 })
117 } catch (err) { 116 } catch (err) {
@@ -119,17 +118,18 @@ async function run (url: string, user: UserInfo) {
119 } 118 }
120 } 119 }
121 120
122 log.info('Video/s for user %s imported: %s', user.username, options.targetUrl) 121 log.info('Video/s for user %s imported: %s', username, options.targetUrl)
123 process.exit(0) 122 process.exit(0)
124} 123}
125 124
126async function processVideo (parameters: { 125async function processVideo (parameters: {
127 cwd: string 126 cwd: string
128 url: string 127 url: string
129 user: { username: string, password: string } 128 username: string
129 password: string
130 youtubeInfo: any 130 youtubeInfo: any
131}) { 131}) {
132 const { youtubeInfo, cwd, url, user } = parameters 132 const { youtubeInfo, cwd, url, username, password } = parameters
133 const youtubeDL = new YoutubeDL('', []) 133 const youtubeDL = new YoutubeDL('', [])
134 134
135 log.debug('Fetching object.', youtubeInfo) 135 log.debug('Fetching object.', youtubeInfo)
@@ -138,22 +138,29 @@ async function processVideo (parameters: {
138 log.debug('Fetched object.', videoInfo) 138 log.debug('Fetched object.', videoInfo)
139 139
140 const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo) 140 const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo)
141
141 if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) { 142 if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) {
142 log.info('Video "%s" has been published before "%s", don\'t upload it.\n', 143 log.info('Video "%s" has been published before "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.since))
143 videoInfo.title, formatDate(options.since))
144 return 144 return
145 } 145 }
146
146 if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) { 147 if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) {
147 log.info('Video "%s" has been published after "%s", don\'t upload it.\n', 148 log.info('Video "%s" has been published after "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.until))
148 videoInfo.title, formatDate(options.until))
149 return 149 return
150 } 150 }
151 151
152 const result = await advancedVideosSearch(url, { search: videoInfo.title, sort: '-match', searchTarget: 'local' }) 152 const server = buildServer(url)
153 const { data } = await server.search.advancedVideoSearch({
154 search: {
155 search: videoInfo.title,
156 sort: '-match',
157 searchTarget: 'local'
158 }
159 })
153 160
154 log.info('############################################################\n') 161 log.info('############################################################\n')
155 162
156 if (result.body.data.find(v => v.name === videoInfo.title)) { 163 if (data.find(v => v.name === videoInfo.title)) {
157 log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) 164 log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title)
158 return 165 return
159 } 166 }
@@ -172,7 +179,8 @@ async function processVideo (parameters: {
172 youtubeDL, 179 youtubeDL,
173 cwd, 180 cwd,
174 url, 181 url,
175 user, 182 username,
183 password,
176 videoInfo: normalizeObject(videoInfo), 184 videoInfo: normalizeObject(videoInfo),
177 videoPath: path 185 videoPath: path
178 }) 186 })
@@ -187,11 +195,15 @@ async function uploadVideoOnPeerTube (parameters: {
187 videoPath: string 195 videoPath: string
188 cwd: string 196 cwd: string
189 url: string 197 url: string
190 user: { username: string, password: string } 198 username: string
199 password: string
191}) { 200}) {
192 const { youtubeDL, videoInfo, videoPath, cwd, url, user } = parameters 201 const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters
193 202
194 const category = await getCategory(videoInfo.categories, url) 203 const server = buildServer(url)
204 await assignToken(server, username, password)
205
206 const category = await getCategory(server, videoInfo.categories)
195 const licence = getLicence(videoInfo.license) 207 const licence = getLicence(videoInfo.license)
196 let tags = [] 208 let tags = []
197 if (Array.isArray(videoInfo.tags)) { 209 if (Array.isArray(videoInfo.tags)) {
@@ -223,28 +235,28 @@ async function uploadVideoOnPeerTube (parameters: {
223 tags 235 tags
224 } 236 }
225 237
226 const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) 238 const baseAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes)
239
240 const attributes = {
241 ...baseAttributes,
227 242
228 Object.assign(videoAttributes, {
229 originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, 243 originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null,
230 thumbnailfile, 244 thumbnailfile,
231 previewfile: thumbnailfile, 245 previewfile: thumbnailfile,
232 fixture: videoPath 246 fixture: videoPath
233 }) 247 }
234
235 log.info('\nUploading on PeerTube video "%s".', videoAttributes.name)
236 248
237 let accessToken = await getAccessTokenOrDie(url, user) 249 log.info('\nUploading on PeerTube video "%s".', attributes.name)
238 250
239 try { 251 try {
240 await uploadVideo(url, accessToken, videoAttributes) 252 await server.videos.upload({ attributes })
241 } catch (err) { 253 } catch (err) {
242 if (err.message.indexOf('401') !== -1) { 254 if (err.message.indexOf('401') !== -1) {
243 log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') 255 log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.')
244 256
245 accessToken = await getAccessTokenOrDie(url, user) 257 server.accessToken = await server.login.getAccessToken(username, password)
246 258
247 await uploadVideo(url, accessToken, videoAttributes) 259 await server.videos.upload({ attributes })
248 } else { 260 } else {
249 exitError(err.message) 261 exitError(err.message)
250 } 262 }
@@ -253,20 +265,19 @@ async function uploadVideoOnPeerTube (parameters: {
253 await remove(videoPath) 265 await remove(videoPath)
254 if (thumbnailfile) await remove(thumbnailfile) 266 if (thumbnailfile) await remove(thumbnailfile)
255 267
256 log.warn('Uploaded video "%s"!\n', videoAttributes.name) 268 log.warn('Uploaded video "%s"!\n', attributes.name)
257} 269}
258 270
259/* ---------------------------------------------------------- */ 271/* ---------------------------------------------------------- */
260 272
261async function getCategory (categories: string[], url: string) { 273async function getCategory (server: PeerTubeServer, categories: string[]) {
262 if (!categories) return undefined 274 if (!categories) return undefined
263 275
264 const categoryString = categories[0] 276 const categoryString = categories[0]
265 277
266 if (categoryString === 'News & Politics') return 11 278 if (categoryString === 'News & Politics') return 11
267 279
268 const res = await getVideoCategories(url) 280 const categoriesServer = await server.videos.getCategories()
269 const categoriesServer = res.body
270 281
271 for (const key of Object.keys(categoriesServer)) { 282 for (const key of Object.keys(categoriesServer)) {
272 const categoryServer = categoriesServer[key] 283 const categoryServer = categoriesServer[key]
@@ -362,21 +373,6 @@ async function promptPassword () {
362 }) 373 })
363} 374}
364 375
365async function getAccessTokenOrDie (url: string, user: UserInfo) {
366 const resClient = await getClient(url)
367 const client = {
368 id: resClient.body.client_id,
369 secret: resClient.body.client_secret
370 }
371
372 try {
373 const res = await login(url, client, user)
374 return res.body.access_token
375 } catch (err) {
376 exitError('Cannot authenticate. Please check your username/password.')
377 }
378}
379
380function parseDate (dateAsStr: string): Date { 376function parseDate (dateAsStr: string): Date {
381 if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) { 377 if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) {
382 exitError(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`) 378 exitError(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`)
diff --git a/server/tools/peertube-plugins.ts b/server/tools/peertube-plugins.ts
index 54ea1264d..d9c285115 100644
--- a/server/tools/peertube-plugins.ts
+++ b/server/tools/peertube-plugins.ts
@@ -4,9 +4,8 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
4registerTSPaths() 4registerTSPaths()
5 5
6import { program, Command, OptionValues } from 'commander' 6import { program, Command, OptionValues } from 'commander'
7import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins' 7import { assignToken, buildServer, getServerCredentials } from './cli'
8import { getAdminTokenOrDie, getServerCredentials } from './cli' 8import { PluginType } from '../../shared/models'
9import { PeerTubePlugin, PluginType } from '../../shared/models'
10import { isAbsolute } from 'path' 9import { isAbsolute } from 'path'
11import * as CliTable3 from 'cli-table3' 10import * as CliTable3 from 'cli-table3'
12 11
@@ -63,28 +62,21 @@ program.parse(process.argv)
63 62
64async function pluginsListCLI (command: Command, options: OptionValues) { 63async function pluginsListCLI (command: Command, options: OptionValues) {
65 const { url, username, password } = await getServerCredentials(command) 64 const { url, username, password } = await getServerCredentials(command)
66 const accessToken = await getAdminTokenOrDie(url, username, password) 65 const server = buildServer(url)
66 await assignToken(server, username, password)
67 67
68 let pluginType: PluginType 68 let pluginType: PluginType
69 if (options.onlyThemes) pluginType = PluginType.THEME 69 if (options.onlyThemes) pluginType = PluginType.THEME
70 if (options.onlyPlugins) pluginType = PluginType.PLUGIN 70 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
71 71
72 const res = await listPlugins({ 72 const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType })
73 url,
74 accessToken,
75 start: 0,
76 count: 100,
77 sort: 'name',
78 pluginType
79 })
80 const plugins: PeerTubePlugin[] = res.body.data
81 73
82 const table = new CliTable3({ 74 const table = new CliTable3({
83 head: [ 'name', 'version', 'homepage' ], 75 head: [ 'name', 'version', 'homepage' ],
84 colWidths: [ 50, 10, 50 ] 76 colWidths: [ 50, 10, 50 ]
85 }) as any 77 }) as any
86 78
87 for (const plugin of plugins) { 79 for (const plugin of data) {
88 const npmName = plugin.type === PluginType.PLUGIN 80 const npmName = plugin.type === PluginType.PLUGIN
89 ? 'peertube-plugin-' + plugin.name 81 ? 'peertube-plugin-' + plugin.name
90 : 'peertube-theme-' + plugin.name 82 : 'peertube-theme-' + plugin.name
@@ -113,15 +105,11 @@ async function installPluginCLI (command: Command, options: OptionValues) {
113 } 105 }
114 106
115 const { url, username, password } = await getServerCredentials(command) 107 const { url, username, password } = await getServerCredentials(command)
116 const accessToken = await getAdminTokenOrDie(url, username, password) 108 const server = buildServer(url)
109 await assignToken(server, username, password)
117 110
118 try { 111 try {
119 await installPlugin({ 112 await server.plugins.install({ npmName: options.npmName, path: options.path })
120 url,
121 accessToken,
122 npmName: options.npmName,
123 path: options.path
124 })
125 } catch (err) { 113 } catch (err) {
126 console.error('Cannot install plugin.', err) 114 console.error('Cannot install plugin.', err)
127 process.exit(-1) 115 process.exit(-1)
@@ -144,15 +132,11 @@ async function updatePluginCLI (command: Command, options: OptionValues) {
144 } 132 }
145 133
146 const { url, username, password } = await getServerCredentials(command) 134 const { url, username, password } = await getServerCredentials(command)
147 const accessToken = await getAdminTokenOrDie(url, username, password) 135 const server = buildServer(url)
136 await assignToken(server, username, password)
148 137
149 try { 138 try {
150 await updatePlugin({ 139 await server.plugins.update({ npmName: options.npmName, path: options.path })
151 url,
152 accessToken,
153 npmName: options.npmName,
154 path: options.path
155 })
156 } catch (err) { 140 } catch (err) {
157 console.error('Cannot update plugin.', err) 141 console.error('Cannot update plugin.', err)
158 process.exit(-1) 142 process.exit(-1)
@@ -170,14 +154,11 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) {
170 } 154 }
171 155
172 const { url, username, password } = await getServerCredentials(command) 156 const { url, username, password } = await getServerCredentials(command)
173 const accessToken = await getAdminTokenOrDie(url, username, password) 157 const server = buildServer(url)
158 await assignToken(server, username, password)
174 159
175 try { 160 try {
176 await uninstallPlugin({ 161 await server.plugins.uninstall({ npmName: options.npmName })
177 url,
178 accessToken,
179 npmName: options.npmName
180 })
181 } catch (err) { 162 } catch (err) {
182 console.error('Cannot uninstall plugin.', err) 163 console.error('Cannot uninstall plugin.', err)
183 process.exit(-1) 164 process.exit(-1)
diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts
index 4810deee0..73b026ac8 100644
--- a/server/tools/peertube-redundancy.ts
+++ b/server/tools/peertube-redundancy.ts
@@ -1,17 +1,13 @@
1// eslint-disable @typescript-eslint/no-unnecessary-type-assertion
2
3import { registerTSPaths } from '../helpers/register-ts-paths' 1import { registerTSPaths } from '../helpers/register-ts-paths'
4registerTSPaths() 2registerTSPaths()
5 3
6import { program, Command } from 'commander'
7import { getAdminTokenOrDie, getServerCredentials } from './cli'
8import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
9import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy'
10import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
11import validator from 'validator'
12import * as CliTable3 from 'cli-table3' 4import * as CliTable3 from 'cli-table3'
13import { URL } from 'url' 5import { Command, program } from 'commander'
14import { uniq } from 'lodash' 6import { uniq } from 'lodash'
7import { URL } from 'url'
8import validator from 'validator'
9import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models'
10import { assignToken, buildServer, getServerCredentials } from './cli'
15 11
16import bytes = require('bytes') 12import bytes = require('bytes')
17 13
@@ -63,15 +59,16 @@ program.parse(process.argv)
63 59
64async function listRedundanciesCLI (target: VideoRedundanciesTarget) { 60async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
65 const { url, username, password } = await getServerCredentials(program) 61 const { url, username, password } = await getServerCredentials(program)
66 const accessToken = await getAdminTokenOrDie(url, username, password) 62 const server = buildServer(url)
63 await assignToken(server, username, password)
67 64
68 const redundancies = await listVideoRedundanciesData(url, accessToken, target) 65 const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })
69 66
70 const table = new CliTable3({ 67 const table = new CliTable3({
71 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] 68 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
72 }) as any 69 }) as any
73 70
74 for (const redundancy of redundancies) { 71 for (const redundancy of data) {
75 const webtorrentFiles = redundancy.redundancies.files 72 const webtorrentFiles = redundancy.redundancies.files
76 const streamingPlaylists = redundancy.redundancies.streamingPlaylists 73 const streamingPlaylists = redundancy.redundancies.streamingPlaylists
77 74
@@ -106,7 +103,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
106 103
107async function addRedundancyCLI (options: { video: number }, command: Command) { 104async function addRedundancyCLI (options: { video: number }, command: Command) {
108 const { url, username, password } = await getServerCredentials(command) 105 const { url, username, password } = await getServerCredentials(command)
109 const accessToken = await getAdminTokenOrDie(url, username, password) 106 const server = buildServer(url)
107 await assignToken(server, username, password)
110 108
111 if (!options.video || validator.isInt('' + options.video) === false) { 109 if (!options.video || validator.isInt('' + options.video) === false) {
112 console.error('You need to specify the video id to duplicate and it should be a number.\n') 110 console.error('You need to specify the video id to duplicate and it should be a number.\n')
@@ -115,11 +113,7 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
115 } 113 }
116 114
117 try { 115 try {
118 await addVideoRedundancy({ 116 await server.redundancy.addVideo({ videoId: options.video })
119 url,
120 accessToken,
121 videoId: options.video
122 })
123 117
124 console.log('Video will be duplicated by your instance!') 118 console.log('Video will be duplicated by your instance!')
125 119
@@ -139,7 +133,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
139 133
140async function removeRedundancyCLI (options: { video: number }, command: Command) { 134async function removeRedundancyCLI (options: { video: number }, command: Command) {
141 const { url, username, password } = await getServerCredentials(command) 135 const { url, username, password } = await getServerCredentials(command)
142 const accessToken = await getAdminTokenOrDie(url, username, password) 136 const server = buildServer(url)
137 await assignToken(server, username, password)
143 138
144 if (!options.video || validator.isInt('' + options.video) === false) { 139 if (!options.video || validator.isInt('' + options.video) === false) {
145 console.error('You need to specify the video id to remove from your redundancies.\n') 140 console.error('You need to specify the video id to remove from your redundancies.\n')
@@ -149,12 +144,12 @@ async function removeRedundancyCLI (options: { video: number }, command: Command
149 144
150 const videoId = parseInt(options.video + '', 10) 145 const videoId = parseInt(options.video + '', 10)
151 146
152 let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') 147 const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
153 let videoRedundancy = redundancies.find(r => videoId === r.id) 148 let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
154 149
155 if (!videoRedundancy) { 150 if (!videoRedundancy) {
156 redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos') 151 const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
157 videoRedundancy = redundancies.find(r => videoId === r.id) 152 videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
158 } 153 }
159 154
160 if (!videoRedundancy) { 155 if (!videoRedundancy) {
@@ -168,11 +163,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command
168 .map(r => r.id) 163 .map(r => r.id)
169 164
170 for (const id of ids) { 165 for (const id of ids) {
171 await removeVideoRedundancy({ 166 await server.redundancy.removeVideo({ redundancyId: id })
172 url,
173 accessToken,
174 redundancyId: id
175 })
176 } 167 }
177 168
178 console.log('Video redundancy removed!') 169 console.log('Video redundancy removed!')
@@ -183,16 +174,3 @@ async function removeRedundancyCLI (options: { video: number }, command: Command
183 process.exit(-1) 174 process.exit(-1)
184 } 175 }
185} 176}
186
187async function listVideoRedundanciesData (url: string, accessToken: string, target: VideoRedundanciesTarget) {
188 const res = await listVideoRedundancies({
189 url,
190 accessToken,
191 start: 0,
192 count: 100,
193 sort: 'name',
194 target
195 })
196
197 return res.body.data as VideoRedundancy[]
198}
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts
index 02edbd809..01fb1fe8d 100644
--- a/server/tools/peertube-upload.ts
+++ b/server/tools/peertube-upload.ts
@@ -4,9 +4,7 @@ registerTSPaths()
4import { program } from 'commander' 4import { program } from 'commander'
5import { access, constants } from 'fs-extra' 5import { access, constants } from 'fs-extra'
6import { isAbsolute } from 'path' 6import { isAbsolute } from 'path'
7import { getAccessToken } from '../../shared/extra-utils' 7import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
8import { uploadVideo } from '../../shared/extra-utils/'
9import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
10 8
11let command = program 9let command = program
12 .name('upload') 10 .name('upload')
@@ -46,22 +44,25 @@ getServerCredentials(command)
46 .catch(err => console.error(err)) 44 .catch(err => console.error(err))
47 45
48async function run (url: string, username: string, password: string) { 46async function run (url: string, username: string, password: string) {
49 const accessToken = await getAccessToken(url, username, password) 47 const server = buildServer(url)
48 await assignToken(server, username, password)
50 49
51 await access(options.file, constants.F_OK) 50 await access(options.file, constants.F_OK)
52 51
53 console.log('Uploading %s video...', options.videoName) 52 console.log('Uploading %s video...', options.videoName)
54 53
55 const videoAttributes = await buildVideoAttributesFromCommander(url, program) 54 const baseAttributes = await buildVideoAttributesFromCommander(server, program)
55
56 const attributes = {
57 ...baseAttributes,
56 58
57 Object.assign(videoAttributes, {
58 fixture: options.file, 59 fixture: options.file,
59 thumbnailfile: options.thumbnail, 60 thumbnailfile: options.thumbnail,
60 previewfile: options.preview 61 previewfile: options.preview
61 }) 62 }
62 63
63 try { 64 try {
64 await uploadVideo(url, accessToken, videoAttributes) 65 await server.videos.upload({ attributes })
65 console.log(`Video ${options.videoName} uploaded.`) 66 console.log(`Video ${options.videoName} uploaded.`)
66 process.exit(0) 67 process.exit(0)
67 } catch (err) { 68 } catch (err) {
diff --git a/server/tools/test.ts b/server/tools/test-live.ts
index fbdbae0b0..0cb0c3668 100644
--- a/server/tools/test.ts
+++ b/server/tools/test-live.ts
@@ -1,26 +1,23 @@
1import { registerTSPaths } from '../helpers/register-ts-paths'
2registerTSPaths()
3
4import { LiveVideo, LiveVideoCreate, VideoPrivacy } from '@shared/models'
5import { program } from 'commander' 1import { program } from 'commander'
2import { LiveVideoCreate, VideoPrivacy } from '@shared/models'
6import { 3import {
7 createLive, 4 createSingleServer,
8 flushAndRunServer,
9 getLive,
10 killallServers, 5 killallServers,
11 sendRTMPStream, 6 sendRTMPStream,
12 ServerInfo, 7 PeerTubeServer,
13 setAccessTokensToServers, 8 setAccessTokensToServers,
14 setDefaultVideoChannel, 9 setDefaultVideoChannel
15 updateCustomSubConfig
16} from '../../shared/extra-utils' 10} from '../../shared/extra-utils'
11import { registerTSPaths } from '../helpers/register-ts-paths'
12
13registerTSPaths()
17 14
18type CommandType = 'live-mux' | 'live-transcoding' 15type CommandType = 'live-mux' | 'live-transcoding'
19 16
20registerTSPaths() 17registerTSPaths()
21 18
22const command = program 19const command = program
23 .name('test') 20 .name('test-live')
24 .option('-t, --type <type>', 'live-muxing|live-transcoding') 21 .option('-t, --type <type>', 'live-muxing|live-transcoding')
25 .parse(process.argv) 22 .parse(process.argv)
26 23
@@ -39,11 +36,11 @@ async function run () {
39 36
40 console.log('Starting server.') 37 console.log('Starting server.')
41 38
42 const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) 39 const server = await createSingleServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] })
43 40
44 const cleanup = () => { 41 const cleanup = async () => {
45 console.log('Killing server') 42 console.log('Killing server')
46 killallServers([ server ]) 43 await killallServers([ server ])
47 } 44 }
48 45
49 process.on('exit', cleanup) 46 process.on('exit', cleanup)
@@ -57,17 +54,15 @@ async function run () {
57 const attributes: LiveVideoCreate = { 54 const attributes: LiveVideoCreate = {
58 name: 'live', 55 name: 'live',
59 saveReplay: true, 56 saveReplay: true,
60 channelId: server.videoChannel.id, 57 channelId: server.store.channel.id,
61 privacy: VideoPrivacy.PUBLIC 58 privacy: VideoPrivacy.PUBLIC
62 } 59 }
63 60
64 console.log('Creating live.') 61 console.log('Creating live.')
65 62
66 const res = await createLive(server.url, server.accessToken, attributes) 63 const { uuid: liveVideoUUID } = await server.live.create({ fields: attributes })
67 const liveVideoUUID = res.body.video.uuid
68 64
69 const resLive = await getLive(server.url, server.accessToken, liveVideoUUID) 65 const live = await server.live.get({ videoId: liveVideoUUID })
70 const live: LiveVideo = resLive.body
71 66
72 console.log('Sending RTMP stream.') 67 console.log('Sending RTMP stream.')
73 68
@@ -86,19 +81,21 @@ async function run () {
86 81
87// ---------------------------------------------------------------------------- 82// ----------------------------------------------------------------------------
88 83
89async function buildConfig (server: ServerInfo, commandType: CommandType) { 84async function buildConfig (server: PeerTubeServer, commandType: CommandType) {
90 await updateCustomSubConfig(server.url, server.accessToken, { 85 await server.config.updateCustomSubConfig({
91 instance: { 86 newConfig: {
92 customizations: { 87 instance: {
93 javascript: '', 88 customizations: {
94 css: '' 89 javascript: '',
95 } 90 css: ''
96 }, 91 }
97 live: { 92 },
98 enabled: true, 93 live: {
99 allowReplay: true, 94 enabled: true,
100 transcoding: { 95 allowReplay: true,
101 enabled: commandType === 'live-transcoding' 96 transcoding: {
97 enabled: commandType === 'live-transcoding'
98 }
102 } 99 }
103 } 100 }
104 }) 101 })