X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-redundancy.ts;h=0f6b3086c9b7e8423597769618d2cb9c94176cf0;hb=d0a0fa429d4651710ed951a3c11af0219e408964;hp=a71f48104e6510f9dece3e98a44583d2011efe9a;hpb=cf59a2a0c367683ba35758419499bf6087c192ec;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index a71f48104..0f6b3086c 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -1,15 +1,16 @@ import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() -import * as program from 'commander' -import { getAdminTokenOrDie, getServerCredentials } from './cli' -import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' -import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy' -import validator from 'validator' -import bytes = require('bytes') import * as CliTable3 from 'cli-table3' -import { parse } 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 { assignToken, buildServer, getServerCredentials } from './cli' + +import bytes = require('bytes') program .name('plugins') @@ -38,7 +39,7 @@ program .option('-U, --username ', 'Username') .option('-p, --password ', 'Password') .option('-v, --video ', 'Video id to duplicate') - .action((options) => addRedundancyCLI(options)) + .action((options, command) => addRedundancyCLI(options, command)) program .command('remove') @@ -47,7 +48,7 @@ program .option('-U, --username ', 'Username') .option('-p, --password ', 'Password') .option('-v, --video ', 'Video id to remove from redundancies') - .action((options) => removeRedundancyCLI(options)) + .action((options, command) => removeRedundancyCLI(options, command)) if (!process.argv.slice(2).length) { program.outputHelp() @@ -59,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 server = buildServer(url) + await assignToken(server, username, password) - 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 CliTable3.HorizontalTable + }) as any - for (const redundancy of redundancies) { + for (const redundancy of data) { const webtorrentFiles = redundancy.redundancies.files const streamingPlaylists = redundancy.redundancies.streamingPlaylists @@ -82,7 +84,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const instances = uniq( webtorrentFiles.concat(streamingPlaylists) .map(r => r.fileUrl) - .map(u => parse(u).host) + .map(u => new URL(u).host) ) table.push([ @@ -100,30 +102,27 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { process.exit(0) } -async function addRedundancyCLI (options: { videoId: number }) { - const { url, username, password } = await getServerCredentials(program) - const accessToken = await getAdminTokenOrDie(url, username, password) +async function addRedundancyCLI (options: { video: number }, command: Command) { + const { url, username, password } = await getServerCredentials(command) + const server = buildServer(url) + await assignToken(server, username, password) - if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) { + 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') - program.outputHelp() + command.outputHelp() process.exit(-1) } try { - await addVideoRedundancy({ - url, - accessToken, - videoId: options[ 'video' ] - }) + await server.redundancyCommand.addVideo({ videoId: options.video }) console.log('Video will be duplicated by your instance!') process.exit(0) } catch (err) { - if (err.message.includes(409)) { + if (err.message.includes(HttpStatusCode.CONFLICT_409)) { console.error('This video is already duplicated by your instance.') - } else if (err.message.includes(404)) { + } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) { console.error('This video id does not exist.') } else { console.error(err) @@ -133,24 +132,25 @@ async function addRedundancyCLI (options: { videoId: number }) { } } -async function removeRedundancyCLI (options: { videoId: number }) { - const { url, username, password } = await getServerCredentials(program) - const accessToken = await getAdminTokenOrDie(url, username, password) +async function removeRedundancyCLI (options: { video: number }, command: Command) { + const { url, username, password } = await getServerCredentials(command) + const server = buildServer(url) + await assignToken(server, username, password) - if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) { + if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to remove from your redundancies.\n') - program.outputHelp() + command.outputHelp() process.exit(-1) } - const videoId = parseInt(options[ 'video' ] + '', 10) + 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) { @@ -164,11 +164,7 @@ async function removeRedundancyCLI (options: { videoId: number }) { .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!') @@ -179,16 +175,3 @@ async function removeRedundancyCLI (options: { videoId: number }) { 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[] -}