From 078f17e6d90376050f43ce639e88e11869b49ee7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 15:03:44 +0200 Subject: Fix CLI tools --- server/tools/cli.ts | 32 ++++++--- server/tools/peertube-get-access-token.ts | 19 +----- server/tools/peertube-import-videos.ts | 89 ++++++++++++------------- server/tools/peertube-plugins.ts | 49 +++++--------- server/tools/peertube-redundancy.ts | 61 ++++++----------- server/tools/peertube-upload.ts | 9 +-- server/tools/test-live.ts | 102 +++++++++++++++++++++++++++++ server/tools/test.ts | 105 ------------------------------ 8 files changed, 208 insertions(+), 258 deletions(-) create mode 100644 server/tools/test-live.ts delete mode 100644 server/tools/test.ts (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 7b94306cd..0528859a4 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -1,14 +1,14 @@ +import { Command } from 'commander' import { Netrc } from 'netrc-parser' -import { getAppNumber, isTestInstance } from '../helpers/core-utils' import { join } from 'path' -import { root } from '../../shared/extra-utils/miscs/miscs' -import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels' -import { VideoChannel, VideoPrivacy } from '../../shared/models/videos' import { createLogger, format, transports } from 'winston' +import { assignCommands, ServerInfo } from '@shared/extra-utils' +import { getAccessToken } from '@shared/extra-utils/users/login' import { getMyUserInformation } from '@shared/extra-utils/users/users' import { User, UserRole } from '@shared/models' -import { getAccessToken } from '@shared/extra-utils/users/login' -import { Command } from 'commander' +import { root } from '../../shared/extra-utils/miscs/miscs' +import { VideoPrivacy } from '../../shared/models/videos' +import { getAppNumber, isTestInstance } from '../helpers/core-utils' let configName = 'PeerTube/CLI' if (isTestInstance()) configName += `-${getAppNumber()}` @@ -30,6 +30,10 @@ async function getAdminTokenOrDie (url: string, username: string, password: stri return accessToken } +async function getAccessTokenOrDie (url: string, username: string, password: string) { + return getAccessToken(url, username, password) +} + interface Settings { remotes: any[] default: number @@ -128,7 +132,7 @@ function buildCommonVideoOptions (command: Command) { .option('-v, --verbose ', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') } -async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any = {}) { +async function buildVideoAttributesFromCommander (server: ServerInfo, command: Command, defaultAttributes: any = {}) { const options = command.opts() const defaultBooleanAttributes = { @@ -164,8 +168,7 @@ async function buildVideoAttributesFromCommander (url: string, command: Command, Object.assign(videoAttributes, booleanAttributes) if (options.channelName) { - const res = await getVideoChannel(url, options.channelName) - const videoChannel: VideoChannel = res.body + const videoChannel = await server.channelsCommand.get({ channelName: options.channelName }) Object.assign(videoAttributes, { channelId: videoChannel.id }) @@ -184,6 +187,13 @@ function getServerCredentials (program: Command) { }) } +function buildServer (url: string, accessToken?: string): ServerInfo { + const server = { url, accessToken, internalServerNumber: undefined } + assignCommands(server) + + return server +} + function getLogger (logLevel = 'info') { const logLevels = { 0: 0, @@ -230,5 +240,7 @@ export { buildCommonVideoOptions, buildVideoAttributesFromCommander, - getAdminTokenOrDie + getAdminTokenOrDie, + getAccessTokenOrDie, + buildServer } diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts index 9488eba0e..5868d0548 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' registerTSPaths() import { program } from 'commander' -import { getClient, Server, serverLogin } from '../../shared/extra-utils' +import { getAccessToken } from '../../shared/extra-utils' program .option('-u, --url ', 'Server url') @@ -24,22 +24,7 @@ if ( process.exit(-1) } -getClient(options.url) - .then(res => { - const server = { - url: options.url, - user: { - username: options.username, - password: options.password - }, - client: { - id: res.body.client_id, - secret: res.body.client_secret - } - } as Server - - return serverLogin(server) - }) +getAccessToken(options.url, options.username, options.password) .then(accessToken => { console.log(accessToken) process.exit(0) diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 101a95b2a..a546a8dbe 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -8,17 +8,19 @@ import { truncate } from 'lodash' import { join } from 'path' import * as prompt from 'prompt' import { promisify } from 'util' -import { advancedVideosSearch, getClient, getVideoCategories, login, uploadVideo } from '../../shared/extra-utils/index' +import { YoutubeDL } from '@server/helpers/youtube-dl' +import { getVideoCategories, uploadVideo } from '../../shared/extra-utils/index' import { sha256 } from '../helpers/core-utils' import { doRequestAndSaveToFile } from '../helpers/requests' import { CONSTRAINTS_FIELDS } from '../initializers/constants' -import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getLogger, getServerCredentials } from './cli' -import { YoutubeDL } from '@server/helpers/youtube-dl' - -type UserInfo = { - username: string - password: string -} +import { + buildCommonVideoOptions, + buildServer, + buildVideoAttributesFromCommander, + getAccessTokenOrDie, + getLogger, + getServerCredentials +} from './cli' const processOptions = { maxBuffer: Infinity @@ -62,17 +64,13 @@ getServerCredentials(command) url = normalizeTargetUrl(url) options.targetUrl = normalizeTargetUrl(options.targetUrl) - const user = { username, password } - - run(url, user) + run(url, username, password) .catch(err => exitError(err)) }) .catch(err => console.error(err)) -async function run (url: string, user: UserInfo) { - if (!user.password) { - user.password = await promptPassword() - } +async function run (url: string, username: string, password: string) { + if (!password) password = await promptPassword() const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL() @@ -111,7 +109,8 @@ async function run (url: string, user: UserInfo) { await processVideo({ cwd: options.tmpdir, url, - user, + username, + password, youtubeInfo: info }) } catch (err) { @@ -119,17 +118,18 @@ async function run (url: string, user: UserInfo) { } } - log.info('Video/s for user %s imported: %s', user.username, options.targetUrl) + log.info('Video/s for user %s imported: %s', username, options.targetUrl) process.exit(0) } async function processVideo (parameters: { cwd: string url: string - user: { username: string, password: string } + username: string + password: string youtubeInfo: any }) { - const { youtubeInfo, cwd, url, user } = parameters + const { youtubeInfo, cwd, url, username, password } = parameters const youtubeDL = new YoutubeDL('', []) log.debug('Fetching object.', youtubeInfo) @@ -138,22 +138,29 @@ async function processVideo (parameters: { log.debug('Fetched object.', videoInfo) const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo) + if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) { - log.info('Video "%s" has been published before "%s", don\'t upload it.\n', - videoInfo.title, formatDate(options.since)) + log.info('Video "%s" has been published before "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.since)) return } + if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) { - log.info('Video "%s" has been published after "%s", don\'t upload it.\n', - videoInfo.title, formatDate(options.until)) + log.info('Video "%s" has been published after "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.until)) return } - const result = await advancedVideosSearch(url, { search: videoInfo.title, sort: '-match', searchTarget: 'local' }) + const server = buildServer(url) + const { data } = await server.searchCommand.advancedVideoSearch({ + search: { + search: videoInfo.title, + sort: '-match', + searchTarget: 'local' + } + }) log.info('############################################################\n') - if (result.body.data.find(v => v.name === videoInfo.title)) { + if (data.find(v => v.name === videoInfo.title)) { log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) return } @@ -172,7 +179,8 @@ async function processVideo (parameters: { youtubeDL, cwd, url, - user, + username, + password, videoInfo: normalizeObject(videoInfo), videoPath: path }) @@ -187,9 +195,10 @@ async function uploadVideoOnPeerTube (parameters: { videoPath: string cwd: string url: string - user: { username: string, password: string } + username: string + password: string }) { - const { youtubeDL, videoInfo, videoPath, cwd, url, user } = parameters + const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters const category = await getCategory(videoInfo.categories, url) const licence = getLicence(videoInfo.license) @@ -223,7 +232,10 @@ async function uploadVideoOnPeerTube (parameters: { tags } - const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) + let accessToken = await getAccessTokenOrDie(url, username, password) + const server = buildServer(url, accessToken) + + const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) Object.assign(videoAttributes, { originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, @@ -234,15 +246,13 @@ async function uploadVideoOnPeerTube (parameters: { log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) - let accessToken = await getAccessTokenOrDie(url, user) - try { await uploadVideo(url, accessToken, videoAttributes) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') - accessToken = await getAccessTokenOrDie(url, user) + accessToken = await getAccessTokenOrDie(url, username, password) await uploadVideo(url, accessToken, videoAttributes) } else { @@ -362,21 +372,6 @@ async function promptPassword () { }) } -async function getAccessTokenOrDie (url: string, user: UserInfo) { - const resClient = await getClient(url) - const client = { - id: resClient.body.client_id, - secret: resClient.body.client_secret - } - - try { - const res = await login(url, client, user) - return res.body.access_token - } catch (err) { - exitError('Cannot authenticate. Please check your username/password.') - } -} - function parseDate (dateAsStr: string): Date { if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) { 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..63541bf2c 100644 --- a/server/tools/peertube-plugins.ts +++ b/server/tools/peertube-plugins.ts @@ -4,9 +4,8 @@ import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() import { program, Command, OptionValues } from 'commander' -import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins' -import { getAdminTokenOrDie, getServerCredentials } from './cli' -import { PeerTubePlugin, PluginType } from '../../shared/models' +import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' +import { PluginType } from '../../shared/models' import { isAbsolute } from 'path' import * as CliTable3 from 'cli-table3' @@ -63,28 +62,21 @@ program.parse(process.argv) async function pluginsListCLI (command: Command, options: OptionValues) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) let pluginType: PluginType if (options.onlyThemes) pluginType = PluginType.THEME if (options.onlyPlugins) pluginType = PluginType.PLUGIN - const res = await listPlugins({ - url, - accessToken, - start: 0, - count: 100, - sort: 'name', - pluginType - }) - const plugins: PeerTubePlugin[] = res.body.data + const { data } = await server.pluginsCommand.list({ start: 0, count: 100, sort: 'name', pluginType }) const table = new CliTable3({ head: [ 'name', 'version', 'homepage' ], colWidths: [ 50, 10, 50 ] }) as any - for (const plugin of plugins) { + for (const plugin of data) { const npmName = plugin.type === PluginType.PLUGIN ? 'peertube-plugin-' + plugin.name : 'peertube-theme-' + plugin.name @@ -113,15 +105,11 @@ async function installPluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) try { - await installPlugin({ - url, - accessToken, - npmName: options.npmName, - path: options.path - }) + await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) } catch (err) { console.error('Cannot install plugin.', err) process.exit(-1) @@ -144,15 +132,11 @@ async function updatePluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) try { - await updatePlugin({ - url, - accessToken, - npmName: options.npmName, - path: options.path - }) + await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) } catch (err) { console.error('Cannot update plugin.', err) process.exit(-1) @@ -170,14 +154,11 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) try { - await uninstallPlugin({ - url, - accessToken, - npmName: options.npmName - }) + await server.pluginsCommand.uninstall({ npmName: options.npmName }) } catch (err) { console.error('Cannot uninstall plugin.', err) process.exit(-1) diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 4810deee0..76d633f9e 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -1,17 +1,14 @@ -// eslint-disable @typescript-eslint/no-unnecessary-type-assertion - import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() -import { program, Command } from 'commander' -import { getAdminTokenOrDie, getServerCredentials } from './cli' -import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' -import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy' -import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' -import validator from 'validator' import * as CliTable3 from 'cli-table3' -import { URL } from 'url' +import { Command, program } from 'commander' import { uniq } from 'lodash' +import { URL } from 'url' +import validator from 'validator' +import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { VideoRedundanciesTarget } from '@shared/models' +import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' import bytes = require('bytes') @@ -63,15 +60,16 @@ program.parse(process.argv) async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const { url, username, password } = await getServerCredentials(program) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) - const redundancies = await listVideoRedundanciesData(url, accessToken, target) + const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) const table = new CliTable3({ head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] }) as any - for (const redundancy of redundancies) { + for (const redundancy of data) { const webtorrentFiles = redundancy.redundancies.files const streamingPlaylists = redundancy.redundancies.streamingPlaylists @@ -106,7 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { async function addRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to duplicate and it should be a number.\n') @@ -115,11 +114,7 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { } try { - await addVideoRedundancy({ - url, - accessToken, - videoId: options.video - }) + await server.redundancyCommand.addVideo({ videoId: options.video }) console.log('Video will be duplicated by your instance!') @@ -139,7 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { async function removeRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + const token = await getAdminTokenOrDie(url, username, password) + const server = buildServer(url, token) if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to remove from your redundancies.\n') @@ -149,12 +145,12 @@ async function removeRedundancyCLI (options: { video: number }, command: Command const videoId = parseInt(options.video + '', 10) - let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') - let videoRedundancy = redundancies.find(r => videoId === r.id) + const myVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'my-videos' }) + let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) if (!videoRedundancy) { - redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos') - videoRedundancy = redundancies.find(r => videoId === r.id) + const remoteVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'remote-videos' }) + videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) } if (!videoRedundancy) { @@ -168,11 +164,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command .map(r => r.id) for (const id of ids) { - await removeVideoRedundancy({ - url, - accessToken, - redundancyId: id - }) + await server.redundancyCommand.removeVideo({ redundancyId: id }) } console.log('Video redundancy removed!') @@ -183,16 +175,3 @@ async function removeRedundancyCLI (options: { video: number }, command: Command process.exit(-1) } } - -async function listVideoRedundanciesData (url: string, accessToken: string, target: VideoRedundanciesTarget) { - const res = await listVideoRedundancies({ - url, - accessToken, - start: 0, - count: 100, - sort: 'name', - target - }) - - return res.body.data as VideoRedundancy[] -} diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index 02edbd809..d1c5348d1 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts @@ -6,7 +6,7 @@ import { access, constants } from 'fs-extra' import { isAbsolute } from 'path' import { getAccessToken } from '../../shared/extra-utils' import { uploadVideo } from '../../shared/extra-utils/' -import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli' +import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' let command = program .name('upload') @@ -46,13 +46,14 @@ getServerCredentials(command) .catch(err => console.error(err)) async function run (url: string, username: string, password: string) { - const accessToken = await getAccessToken(url, username, password) + const token = await getAccessToken(url, username, password) + const server = buildServer(url, token) await access(options.file, constants.F_OK) console.log('Uploading %s video...', options.videoName) - const videoAttributes = await buildVideoAttributesFromCommander(url, program) + const videoAttributes = await buildVideoAttributesFromCommander(server, program) Object.assign(videoAttributes, { fixture: options.file, @@ -61,7 +62,7 @@ async function run (url: string, username: string, password: string) { }) try { - await uploadVideo(url, accessToken, videoAttributes) + await uploadVideo(url, token, videoAttributes) console.log(`Video ${options.videoName} uploaded.`) process.exit(0) } catch (err) { diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts new file mode 100644 index 000000000..76fd69435 --- /dev/null +++ b/server/tools/test-live.ts @@ -0,0 +1,102 @@ +import { program } from 'commander' +import { LiveVideoCreate, VideoPrivacy } from '@shared/models' +import { + flushAndRunServer, + killallServers, + sendRTMPStream, + ServerInfo, + setAccessTokensToServers, + setDefaultVideoChannel +} from '../../shared/extra-utils' +import { registerTSPaths } from '../helpers/register-ts-paths' + +registerTSPaths() + +type CommandType = 'live-mux' | 'live-transcoding' + +registerTSPaths() + +const command = program + .name('test-live') + .option('-t, --type ', 'live-muxing|live-transcoding') + .parse(process.argv) + +run() + .catch(err => { + console.error(err) + process.exit(-1) + }) + +async function run () { + const commandType: CommandType = command['type'] + if (!commandType) { + console.error('Miss command type') + process.exit(-1) + } + + console.log('Starting server.') + + const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) + + const cleanup = () => { + console.log('Killing server') + killallServers([ server ]) + } + + process.on('exit', cleanup) + process.on('SIGINT', cleanup) + + await setAccessTokensToServers([ server ]) + await setDefaultVideoChannel([ server ]) + + await buildConfig(server, commandType) + + const attributes: LiveVideoCreate = { + name: 'live', + saveReplay: true, + channelId: server.videoChannel.id, + privacy: VideoPrivacy.PUBLIC + } + + console.log('Creating live.') + + const { uuid: liveVideoUUID } = await server.liveCommand.create({ fields: attributes }) + + const live = await server.liveCommand.get({ videoId: liveVideoUUID }) + + console.log('Sending RTMP stream.') + + const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey) + + ffmpegCommand.on('error', err => { + console.error(err) + process.exit(-1) + }) + + ffmpegCommand.on('end', () => { + console.log('ffmpeg ended') + process.exit(0) + }) +} + +// ---------------------------------------------------------------------------- + +async function buildConfig (server: ServerInfo, commandType: CommandType) { + await server.configCommand.updateCustomSubConfig({ + newConfig: { + instance: { + customizations: { + javascript: '', + css: '' + } + }, + live: { + enabled: true, + allowReplay: true, + transcoding: { + enabled: commandType === 'live-transcoding' + } + } + } + }) +} diff --git a/server/tools/test.ts b/server/tools/test.ts deleted file mode 100644 index fbdbae0b0..000000000 --- a/server/tools/test.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { registerTSPaths } from '../helpers/register-ts-paths' -registerTSPaths() - -import { LiveVideo, LiveVideoCreate, VideoPrivacy } from '@shared/models' -import { program } from 'commander' -import { - createLive, - flushAndRunServer, - getLive, - killallServers, - sendRTMPStream, - ServerInfo, - setAccessTokensToServers, - setDefaultVideoChannel, - updateCustomSubConfig -} from '../../shared/extra-utils' - -type CommandType = 'live-mux' | 'live-transcoding' - -registerTSPaths() - -const command = program - .name('test') - .option('-t, --type ', 'live-muxing|live-transcoding') - .parse(process.argv) - -run() - .catch(err => { - console.error(err) - process.exit(-1) - }) - -async function run () { - const commandType: CommandType = command['type'] - if (!commandType) { - console.error('Miss command type') - process.exit(-1) - } - - console.log('Starting server.') - - const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) - - const cleanup = () => { - console.log('Killing server') - killallServers([ server ]) - } - - process.on('exit', cleanup) - process.on('SIGINT', cleanup) - - await setAccessTokensToServers([ server ]) - await setDefaultVideoChannel([ server ]) - - await buildConfig(server, commandType) - - const attributes: LiveVideoCreate = { - name: 'live', - saveReplay: true, - channelId: server.videoChannel.id, - privacy: VideoPrivacy.PUBLIC - } - - console.log('Creating live.') - - const res = await createLive(server.url, server.accessToken, attributes) - const liveVideoUUID = res.body.video.uuid - - const resLive = await getLive(server.url, server.accessToken, liveVideoUUID) - const live: LiveVideo = resLive.body - - console.log('Sending RTMP stream.') - - const ffmpegCommand = sendRTMPStream(live.rtmpUrl, live.streamKey) - - ffmpegCommand.on('error', err => { - console.error(err) - process.exit(-1) - }) - - ffmpegCommand.on('end', () => { - console.log('ffmpeg ended') - process.exit(0) - }) -} - -// ---------------------------------------------------------------------------- - -async function buildConfig (server: ServerInfo, commandType: CommandType) { - await updateCustomSubConfig(server.url, server.accessToken, { - instance: { - customizations: { - javascript: '', - css: '' - } - }, - live: { - enabled: true, - allowReplay: true, - transcoding: { - enabled: commandType === 'live-transcoding' - } - } - }) -} -- cgit v1.2.3 From 9293139fde7091e9badcafa9b570b83cea9a10ad Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 15:37:43 +0200 Subject: Introduce sql command --- server/tools/test-live.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/tools') diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts index 76fd69435..3c6a67cf7 100644 --- a/server/tools/test-live.ts +++ b/server/tools/test-live.ts @@ -40,7 +40,7 @@ async function run () { const cleanup = () => { console.log('Killing server') - killallServers([ server ]) + await killallServers([ server ]) } process.on('exit', cleanup) -- cgit v1.2.3 From dd0ebb715123dfa126a82d4e4fe3a04064ae77b8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Jul 2021 16:23:01 +0200 Subject: Introduce notifications command --- server/tools/test-live.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/tools') diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts index 3c6a67cf7..3d1198d9c 100644 --- a/server/tools/test-live.ts +++ b/server/tools/test-live.ts @@ -38,7 +38,7 @@ async function run () { const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) - const cleanup = () => { + const cleanup = async () => { console.log('Killing server') await killallServers([ server ]) } -- cgit v1.2.3 From 5e2fea3ab9357c228b5d21166092f9a2368e258e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 12 Jul 2021 16:06:57 +0200 Subject: Fix search tests --- server/tools/test-live.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/tools') diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts index 3d1198d9c..7004e98e8 100644 --- a/server/tools/test-live.ts +++ b/server/tools/test-live.ts @@ -38,7 +38,7 @@ async function run () { const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) - const cleanup = async () => { + const cleanup = async () => { console.log('Killing server') await killallServers([ server ]) } -- cgit v1.2.3 From d0a0fa429d4651710ed951a3c11af0219e408964 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 11:44:16 +0200 Subject: Adapt CLI to new commands --- server/tools/cli.ts | 31 +++++++++++++++++-------------- server/tools/peertube-auth.ts | 6 +++--- server/tools/peertube-get-access-token.ts | 10 ++++++---- server/tools/peertube-import-videos.ts | 12 ++++++------ server/tools/peertube-plugins.ts | 18 +++++++++--------- server/tools/peertube-redundancy.ts | 14 +++++++------- server/tools/peertube-upload.ts | 9 ++++----- 7 files changed, 52 insertions(+), 48 deletions(-) (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 0528859a4..3e0e03b97 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -3,12 +3,10 @@ import { Netrc } from 'netrc-parser' import { join } from 'path' import { createLogger, format, transports } from 'winston' import { assignCommands, ServerInfo } from '@shared/extra-utils' -import { getAccessToken } from '@shared/extra-utils/users/login' import { getMyUserInformation } from '@shared/extra-utils/users/users' import { User, UserRole } from '@shared/models' -import { root } from '../../shared/extra-utils/miscs/miscs' import { VideoPrivacy } from '../../shared/models/videos' -import { getAppNumber, isTestInstance } from '../helpers/core-utils' +import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' let configName = 'PeerTube/CLI' if (isTestInstance()) configName += `-${getAppNumber()}` @@ -17,9 +15,9 @@ const config = require('application-config')(configName) const version = require('../../../package.json').version -async function getAdminTokenOrDie (url: string, username: string, password: string) { - const accessToken = await getAccessToken(url, username, password) - const resMe = await getMyUserInformation(url, accessToken) +async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { + const accessToken = await server.loginCommand.getAccessToken(username, password) + const resMe = await getMyUserInformation(server.url, accessToken) const me: User = resMe.body if (me.role !== UserRole.ADMINISTRATOR) { @@ -30,10 +28,6 @@ async function getAdminTokenOrDie (url: string, username: string, password: stri return accessToken } -async function getAccessTokenOrDie (url: string, username: string, password: string) { - return getAccessToken(url, username, password) -} - interface Settings { remotes: any[] default: number @@ -187,13 +181,22 @@ function getServerCredentials (program: Command) { }) } -function buildServer (url: string, accessToken?: string): ServerInfo { - const server = { url, accessToken, internalServerNumber: undefined } +function buildServer (url: string): ServerInfo { + const server = { url, internalServerNumber: undefined } assignCommands(server) return server } +async function assignToken (server: ServerInfo, username: string, password: string) { + const bodyClient = await server.loginCommand.getClient() + const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } + + const body = await server.loginCommand.login({ client, user: { username, password } }) + + server.accessToken = body.access_token +} + function getLogger (logLevel = 'info') { const logLevels = { 0: 0, @@ -241,6 +244,6 @@ export { buildVideoAttributesFromCommander, getAdminTokenOrDie, - getAccessTokenOrDie, - buildServer + buildServer, + assignToken } 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() import { OptionValues, program } from 'commander' import * as prompt from 'prompt' -import { getNetrc, getSettings, writeSettings } from './cli' +import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli' import { isUserUsernameValid } from '../helpers/custom-validators/users' -import { getAccessToken } from '../../shared/extra-utils' import * as CliTable3 from 'cli-table3' async function delInstance (url: string) { @@ -97,7 +96,8 @@ program // @see https://github.com/Chocobozzz/PeerTube/issues/3520 result.url = stripExtraneousFromPeerTubeUrl(result.url) - await getAccessToken(result.url, result.username, result.password) + const server = buildServer(result.url) + await assignToken(server, result.username, result.password) } catch (err) { console.error(err.message) process.exit(-1) diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts index 5868d0548..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' registerTSPaths() import { program } from 'commander' -import { getAccessToken } from '../../shared/extra-utils' +import { assignToken, buildServer } from './cli' program .option('-u, --url ', 'Server url') @@ -24,9 +24,11 @@ if ( process.exit(-1) } -getAccessToken(options.url, options.username, options.password) - .then(accessToken => { - console.log(accessToken) +const server = buildServer(options.url) + +assignToken(server, options.username, options.password) + .then(() => { + console.log(server.accessToken) process.exit(0) }) .catch(err => { diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index a546a8dbe..0a4d6fa6e 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -14,10 +14,10 @@ import { sha256 } from '../helpers/core-utils' import { doRequestAndSaveToFile } from '../helpers/requests' import { CONSTRAINTS_FIELDS } from '../initializers/constants' import { + assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, - getAccessTokenOrDie, getLogger, getServerCredentials } from './cli' @@ -232,8 +232,8 @@ async function uploadVideoOnPeerTube (parameters: { tags } - let accessToken = await getAccessTokenOrDie(url, username, password) - const server = buildServer(url, accessToken) + const server = buildServer(url) + await assignToken(server, username, password) const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) @@ -247,14 +247,14 @@ async function uploadVideoOnPeerTube (parameters: { log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) try { - await uploadVideo(url, accessToken, videoAttributes) + await uploadVideo(url, server.accessToken, videoAttributes) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') - accessToken = await getAccessTokenOrDie(url, username, password) + server.accessToken = await server.loginCommand.getAccessToken(username, password) - await uploadVideo(url, accessToken, videoAttributes) + await uploadVideo(url, server.accessToken, videoAttributes) } else { exitError(err.message) } diff --git a/server/tools/peertube-plugins.ts b/server/tools/peertube-plugins.ts index 63541bf2c..22a09b779 100644 --- a/server/tools/peertube-plugins.ts +++ b/server/tools/peertube-plugins.ts @@ -4,7 +4,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() import { program, Command, OptionValues } from 'commander' -import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' +import { assignToken, buildServer, getServerCredentials } from './cli' import { PluginType } from '../../shared/models' import { isAbsolute } from 'path' import * as CliTable3 from 'cli-table3' @@ -62,8 +62,8 @@ program.parse(process.argv) async function pluginsListCLI (command: Command, options: OptionValues) { const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) let pluginType: PluginType if (options.onlyThemes) pluginType = PluginType.THEME @@ -105,8 +105,8 @@ async function installPluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) try { await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) @@ -132,8 +132,8 @@ async function updatePluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) try { await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) @@ -154,8 +154,8 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) { } const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) try { await server.pluginsCommand.uninstall({ npmName: options.npmName }) diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 76d633f9e..0f6b3086c 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -8,7 +8,7 @@ import { URL } from 'url' import validator from 'validator' import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' import { VideoRedundanciesTarget } from '@shared/models' -import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' +import { assignToken, buildServer, getServerCredentials } from './cli' import bytes = require('bytes') @@ -60,8 +60,8 @@ program.parse(process.argv) async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const { url, username, password } = await getServerCredentials(program) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) @@ -104,8 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { async function addRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to duplicate and it should be a number.\n') @@ -134,8 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { async function removeRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const token = await getAdminTokenOrDie(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to remove from your redundancies.\n') diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index d1c5348d1..c94b05857 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts @@ -4,9 +4,8 @@ registerTSPaths() import { program } from 'commander' import { access, constants } from 'fs-extra' import { isAbsolute } from 'path' -import { getAccessToken } from '../../shared/extra-utils' import { uploadVideo } from '../../shared/extra-utils/' -import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' +import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' let command = program .name('upload') @@ -46,8 +45,8 @@ getServerCredentials(command) .catch(err => console.error(err)) async function run (url: string, username: string, password: string) { - const token = await getAccessToken(url, username, password) - const server = buildServer(url, token) + const server = buildServer(url) + await assignToken(server, username, password) await access(options.file, constants.F_OK) @@ -62,7 +61,7 @@ async function run (url: string, username: string, password: string) { }) try { - await uploadVideo(url, token, videoAttributes) + await uploadVideo(url, server.accessToken, videoAttributes) console.log(`Video ${options.videoName} uploaded.`) process.exit(0) } catch (err) { -- cgit v1.2.3 From 7926c5f9b3ffcabb1ffb0dcfa5e48b8e0b88fbc0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 14:23:01 +0200 Subject: Introduce user command --- server/tools/cli.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 3e0e03b97..17c2e8c74 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -3,8 +3,7 @@ import { Netrc } from 'netrc-parser' import { join } from 'path' import { createLogger, format, transports } from 'winston' import { assignCommands, ServerInfo } from '@shared/extra-utils' -import { getMyUserInformation } from '@shared/extra-utils/users/users' -import { User, UserRole } from '@shared/models' +import { UserRole } from '@shared/models' import { VideoPrivacy } from '../../shared/models/videos' import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' @@ -16,16 +15,15 @@ const config = require('application-config')(configName) const version = require('../../../package.json').version async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { - const accessToken = await server.loginCommand.getAccessToken(username, password) - const resMe = await getMyUserInformation(server.url, accessToken) - const me: User = resMe.body + const token = await server.loginCommand.getAccessToken(username, password) + const me = await server.usersCommand.getMyUserInformation({ token }) if (me.role !== UserRole.ADMINISTRATOR) { console.error('You must be an administrator.') process.exit(-1) } - return accessToken + return token } interface Settings { -- cgit v1.2.3 From d23dd9fbfc4d26026352c10f81d2795ceaf2908a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Jul 2021 10:02:54 +0200 Subject: Introduce videos command --- server/tools/cli.ts | 2 +- server/tools/peertube-import-videos.ts | 29 +++++++++++++++-------------- server/tools/peertube-upload.ts | 11 ++++++----- 3 files changed, 22 insertions(+), 20 deletions(-) (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 17c2e8c74..163ed62d1 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -16,7 +16,7 @@ const version = require('../../../package.json').version async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { const token = await server.loginCommand.getAccessToken(username, password) - const me = await server.usersCommand.getMyUserInformation({ token }) + const me = await server.usersCommand.getMyInfo({ token }) if (me.role !== UserRole.ADMINISTRATOR) { console.error('You must be an administrator.') diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 0a4d6fa6e..fc76735b9 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -9,7 +9,6 @@ import { join } from 'path' import * as prompt from 'prompt' import { promisify } from 'util' import { YoutubeDL } from '@server/helpers/youtube-dl' -import { getVideoCategories, uploadVideo } from '../../shared/extra-utils/index' import { sha256 } from '../helpers/core-utils' import { doRequestAndSaveToFile } from '../helpers/requests' import { CONSTRAINTS_FIELDS } from '../initializers/constants' @@ -21,6 +20,7 @@ import { getLogger, getServerCredentials } from './cli' +import { ServerInfo } from '@shared/extra-utils' const processOptions = { maxBuffer: Infinity @@ -200,7 +200,10 @@ async function uploadVideoOnPeerTube (parameters: { }) { const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters - const category = await getCategory(videoInfo.categories, url) + const server = buildServer(url) + await assignToken(server, username, password) + + const category = await getCategory(server, videoInfo.categories) const licence = getLicence(videoInfo.license) let tags = [] if (Array.isArray(videoInfo.tags)) { @@ -232,29 +235,28 @@ async function uploadVideoOnPeerTube (parameters: { tags } - const server = buildServer(url) - await assignToken(server, username, password) + const baseAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) - const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) + const attributes = { + ...baseAttributes, - Object.assign(videoAttributes, { originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, thumbnailfile, previewfile: thumbnailfile, fixture: videoPath - }) + } - log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) + log.info('\nUploading on PeerTube video "%s".', attributes.name) try { - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') server.accessToken = await server.loginCommand.getAccessToken(username, password) - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) } else { exitError(err.message) } @@ -263,20 +265,19 @@ async function uploadVideoOnPeerTube (parameters: { await remove(videoPath) if (thumbnailfile) await remove(thumbnailfile) - log.warn('Uploaded video "%s"!\n', videoAttributes.name) + log.warn('Uploaded video "%s"!\n', attributes.name) } /* ---------------------------------------------------------- */ -async function getCategory (categories: string[], url: string) { +async function getCategory (server: ServerInfo, categories: string[]) { if (!categories) return undefined const categoryString = categories[0] if (categoryString === 'News & Politics') return 11 - const res = await getVideoCategories(url) - const categoriesServer = res.body + const categoriesServer = await server.videosCommand.getCategories() for (const key of Object.keys(categoriesServer)) { const categoryServer = categoriesServer[key] diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index c94b05857..597137e4c 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts @@ -4,7 +4,6 @@ registerTSPaths() import { program } from 'commander' import { access, constants } from 'fs-extra' import { isAbsolute } from 'path' -import { uploadVideo } from '../../shared/extra-utils/' import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' let command = program @@ -52,16 +51,18 @@ async function run (url: string, username: string, password: string) { console.log('Uploading %s video...', options.videoName) - const videoAttributes = await buildVideoAttributesFromCommander(server, program) + const baseAttributes = await buildVideoAttributesFromCommander(server, program) + + const attributes = { + ...baseAttributes, - Object.assign(videoAttributes, { fixture: options.file, thumbnailfile: options.thumbnail, previewfile: options.preview - }) + } try { - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) console.log(`Video ${options.videoName} uploaded.`) process.exit(0) } catch (err) { -- cgit v1.2.3 From 89d241a79c262b9775c233b73cff080043ebb5e6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 09:04:35 +0200 Subject: Shorter server command names --- server/tools/cli.ts | 10 +++++----- server/tools/peertube-import-videos.ts | 10 +++++----- server/tools/peertube-plugins.ts | 8 ++++---- server/tools/peertube-redundancy.ts | 10 +++++----- server/tools/peertube-upload.ts | 2 +- server/tools/test-live.ts | 8 ++++---- 6 files changed, 24 insertions(+), 24 deletions(-) (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 163ed62d1..7d5eb72ed 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -15,8 +15,8 @@ const config = require('application-config')(configName) const version = require('../../../package.json').version async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { - const token = await server.loginCommand.getAccessToken(username, password) - const me = await server.usersCommand.getMyInfo({ token }) + const token = await server.login.getAccessToken(username, password) + const me = await server.users.getMyInfo({ token }) if (me.role !== UserRole.ADMINISTRATOR) { console.error('You must be an administrator.') @@ -160,7 +160,7 @@ async function buildVideoAttributesFromCommander (server: ServerInfo, command: C Object.assign(videoAttributes, booleanAttributes) if (options.channelName) { - const videoChannel = await server.channelsCommand.get({ channelName: options.channelName }) + const videoChannel = await server.channels.get({ channelName: options.channelName }) Object.assign(videoAttributes, { channelId: videoChannel.id }) @@ -187,10 +187,10 @@ function buildServer (url: string): ServerInfo { } async function assignToken (server: ServerInfo, username: string, password: string) { - const bodyClient = await server.loginCommand.getClient() + const bodyClient = await server.login.getClient() const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } - const body = await server.loginCommand.login({ client, user: { username, password } }) + const body = await server.login.login({ client, user: { username, password } }) server.accessToken = body.access_token } diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index fc76735b9..caf1facc7 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -150,7 +150,7 @@ async function processVideo (parameters: { } const server = buildServer(url) - const { data } = await server.searchCommand.advancedVideoSearch({ + const { data } = await server.search.advancedVideoSearch({ search: { search: videoInfo.title, sort: '-match', @@ -249,14 +249,14 @@ async function uploadVideoOnPeerTube (parameters: { log.info('\nUploading on PeerTube video "%s".', attributes.name) try { - await server.videosCommand.upload({ attributes }) + await server.videos.upload({ attributes }) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') - server.accessToken = await server.loginCommand.getAccessToken(username, password) + server.accessToken = await server.login.getAccessToken(username, password) - await server.videosCommand.upload({ attributes }) + await server.videos.upload({ attributes }) } else { exitError(err.message) } @@ -277,7 +277,7 @@ async function getCategory (server: ServerInfo, categories: string[]) { if (categoryString === 'News & Politics') return 11 - const categoriesServer = await server.videosCommand.getCategories() + const categoriesServer = await server.videos.getCategories() for (const key of Object.keys(categoriesServer)) { const categoryServer = categoriesServer[key] diff --git a/server/tools/peertube-plugins.ts b/server/tools/peertube-plugins.ts index 22a09b779..d9c285115 100644 --- a/server/tools/peertube-plugins.ts +++ b/server/tools/peertube-plugins.ts @@ -69,7 +69,7 @@ async function pluginsListCLI (command: Command, options: OptionValues) { if (options.onlyThemes) pluginType = PluginType.THEME if (options.onlyPlugins) pluginType = PluginType.PLUGIN - const { data } = await server.pluginsCommand.list({ start: 0, count: 100, sort: 'name', pluginType }) + const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType }) const table = new CliTable3({ head: [ 'name', 'version', 'homepage' ], @@ -109,7 +109,7 @@ async function installPluginCLI (command: Command, options: OptionValues) { await assignToken(server, username, password) try { - await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) + await server.plugins.install({ npmName: options.npmName, path: options.path }) } catch (err) { console.error('Cannot install plugin.', err) process.exit(-1) @@ -136,7 +136,7 @@ async function updatePluginCLI (command: Command, options: OptionValues) { await assignToken(server, username, password) try { - await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) + await server.plugins.update({ npmName: options.npmName, path: options.path }) } catch (err) { console.error('Cannot update plugin.', err) process.exit(-1) @@ -158,7 +158,7 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) { await assignToken(server, username, password) try { - await server.pluginsCommand.uninstall({ npmName: options.npmName }) + await server.plugins.uninstall({ npmName: options.npmName }) } catch (err) { console.error('Cannot uninstall plugin.', err) process.exit(-1) diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 0f6b3086c..5fda68c8e 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -63,7 +63,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const server = buildServer(url) await assignToken(server, username, password) - const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) + const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target }) const table = new CliTable3({ head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] @@ -114,7 +114,7 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { } try { - await server.redundancyCommand.addVideo({ videoId: options.video }) + await server.redundancy.addVideo({ videoId: options.video }) console.log('Video will be duplicated by your instance!') @@ -145,11 +145,11 @@ async function removeRedundancyCLI (options: { video: number }, command: Command const videoId = parseInt(options.video + '', 10) - const myVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'my-videos' }) + const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' }) let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) if (!videoRedundancy) { - const remoteVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'remote-videos' }) + const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' }) videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) } @@ -164,7 +164,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command .map(r => r.id) for (const id of ids) { - await server.redundancyCommand.removeVideo({ redundancyId: id }) + await server.redundancy.removeVideo({ redundancyId: id }) } console.log('Video redundancy removed!') diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index 597137e4c..01fb1fe8d 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts @@ -62,7 +62,7 @@ async function run (url: string, username: string, password: string) { } try { - await server.videosCommand.upload({ attributes }) + await server.videos.upload({ attributes }) console.log(`Video ${options.videoName} uploaded.`) process.exit(0) } catch (err) { diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts index 7004e98e8..bc31b6926 100644 --- a/server/tools/test-live.ts +++ b/server/tools/test-live.ts @@ -54,15 +54,15 @@ async function run () { const attributes: LiveVideoCreate = { name: 'live', saveReplay: true, - channelId: server.videoChannel.id, + channelId: server.store.channel.id, privacy: VideoPrivacy.PUBLIC } console.log('Creating live.') - const { uuid: liveVideoUUID } = await server.liveCommand.create({ fields: attributes }) + const { uuid: liveVideoUUID } = await server.live.create({ fields: attributes }) - const live = await server.liveCommand.get({ videoId: liveVideoUUID }) + const live = await server.live.get({ videoId: liveVideoUUID }) console.log('Sending RTMP stream.') @@ -82,7 +82,7 @@ async function run () { // ---------------------------------------------------------------------------- async function buildConfig (server: ServerInfo, commandType: CommandType) { - await server.configCommand.updateCustomSubConfig({ + await server.config.updateCustomSubConfig({ newConfig: { instance: { customizations: { -- cgit v1.2.3 From 254d3579f5338f5fd775c17d15cdfc37078bcfb4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 09:47:51 +0200 Subject: Use an object to represent a server --- server/tools/cli.ts | 15 ++++++--------- server/tools/peertube-import-videos.ts | 4 ++-- server/tools/test-live.ts | 8 ++++---- 3 files changed, 12 insertions(+), 15 deletions(-) (limited to 'server/tools') diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 7d5eb72ed..52e6ea593 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts @@ -2,7 +2,7 @@ import { Command } from 'commander' import { Netrc } from 'netrc-parser' import { join } from 'path' import { createLogger, format, transports } from 'winston' -import { assignCommands, ServerInfo } from '@shared/extra-utils' +import { PeerTubeServer } from '@shared/extra-utils' import { UserRole } from '@shared/models' import { VideoPrivacy } from '../../shared/models/videos' import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' @@ -14,7 +14,7 @@ const config = require('application-config')(configName) const version = require('../../../package.json').version -async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { +async function getAdminTokenOrDie (server: PeerTubeServer, username: string, password: string) { const token = await server.login.getAccessToken(username, password) const me = await server.users.getMyInfo({ token }) @@ -124,7 +124,7 @@ function buildCommonVideoOptions (command: Command) { .option('-v, --verbose ', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info') } -async function buildVideoAttributesFromCommander (server: ServerInfo, command: Command, defaultAttributes: any = {}) { +async function buildVideoAttributesFromCommander (server: PeerTubeServer, command: Command, defaultAttributes: any = {}) { const options = command.opts() const defaultBooleanAttributes = { @@ -179,14 +179,11 @@ function getServerCredentials (program: Command) { }) } -function buildServer (url: string): ServerInfo { - const server = { url, internalServerNumber: undefined } - assignCommands(server) - - return server +function buildServer (url: string) { + return new PeerTubeServer({ url }) } -async function assignToken (server: ServerInfo, username: string, password: string) { +async function assignToken (server: PeerTubeServer, username: string, password: string) { const bodyClient = await server.login.getClient() const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index caf1facc7..52aae3d2c 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -20,7 +20,7 @@ import { getLogger, getServerCredentials } from './cli' -import { ServerInfo } from '@shared/extra-utils' +import { PeerTubeServer } from '@shared/extra-utils' const processOptions = { maxBuffer: Infinity @@ -270,7 +270,7 @@ async function uploadVideoOnPeerTube (parameters: { /* ---------------------------------------------------------- */ -async function getCategory (server: ServerInfo, categories: string[]) { +async function getCategory (server: PeerTubeServer, categories: string[]) { if (!categories) return undefined const categoryString = categories[0] diff --git a/server/tools/test-live.ts b/server/tools/test-live.ts index bc31b6926..0cb0c3668 100644 --- a/server/tools/test-live.ts +++ b/server/tools/test-live.ts @@ -1,10 +1,10 @@ import { program } from 'commander' import { LiveVideoCreate, VideoPrivacy } from '@shared/models' import { - flushAndRunServer, + createSingleServer, killallServers, sendRTMPStream, - ServerInfo, + PeerTubeServer, setAccessTokensToServers, setDefaultVideoChannel } from '../../shared/extra-utils' @@ -36,7 +36,7 @@ async function run () { console.log('Starting server.') - const server = await flushAndRunServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) + const server = await createSingleServer(1, {}, [], { hideLogs: false, execArgv: [ '--inspect' ] }) const cleanup = async () => { console.log('Killing server') @@ -81,7 +81,7 @@ async function run () { // ---------------------------------------------------------------------------- -async function buildConfig (server: ServerInfo, commandType: CommandType) { +async function buildConfig (server: PeerTubeServer, commandType: CommandType) { await server.config.updateCustomSubConfig({ newConfig: { instance: { -- cgit v1.2.3 From c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:42:24 +0200 Subject: Refactor requests --- server/tools/peertube-redundancy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/tools') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 5fda68c8e..bcaae63a3 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -6,7 +6,7 @@ import { Command, program } from 'commander' import { uniq } from 'lodash' import { URL } from 'url' import validator from 'validator' -import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { VideoRedundanciesTarget } from '@shared/models' import { assignToken, buildServer, getServerCredentials } from './cli' -- cgit v1.2.3 From 4c7e60bc17ee5830399bac4aa273356903421b4c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 14:27:30 +0200 Subject: Reorganize imports --- server/tools/peertube-redundancy.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'server/tools') diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index bcaae63a3..73b026ac8 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -6,8 +6,7 @@ import { Command, program } from 'commander' import { uniq } from 'lodash' import { URL } from 'url' import validator from 'validator' -import { HttpStatusCode } from '@shared/models' -import { VideoRedundanciesTarget } from '@shared/models' +import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models' import { assignToken, buildServer, getServerCredentials } from './cli' import bytes = require('bytes') -- cgit v1.2.3