X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-redundancy.ts;h=fd6c760b23de6fb212047b22d72076a5468a20da;hb=4765348107ddd21cd2a0b86093859aa2e23ac0f1;hp=5bc80ddb9df3ca9b82effcfaacaac18a4a47c14a;hpb=ba5a8d89bbf049e4afc41543bcc072cccdb02669;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 5bc80ddb9..fd6c760b2 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -1,23 +1,14 @@ -// eslint-disable @typescript-eslint/no-unnecessary-type-assertion - -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 { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' -import validator from 'validator' -import * as CliTable3 from 'cli-table3' +import CliTable3 from 'cli-table3' +import { Command, program } from 'commander' import { URL } from 'url' -import { uniq } from 'lodash' +import validator from 'validator' +import { forceNumber, uniqify } from '@shared/core-utils' +import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models' +import { assignToken, buildServer, getServerCredentials } from './shared' import bytes = require('bytes') -import commander = require('commander') - program - .name('plugins') + .name('redundancy') .usage('[command] [options]') program @@ -64,15 +55,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.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' ] }) as any - for (const redundancy of redundancies) { + for (const redundancy of data) { const webtorrentFiles = redundancy.redundancies.files const streamingPlaylists = redundancy.redundancies.streamingPlaylists @@ -84,7 +76,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { totalSize = bytes(tmp) } - const instances = uniq( + const instances = uniqify( webtorrentFiles.concat(streamingPlaylists) .map(r => r.fileUrl) .map(u => new URL(u).host) @@ -105,9 +97,10 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { process.exit(0) } -async function addRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) { +async function addRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + 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') @@ -116,11 +109,7 @@ async function addRedundancyCLI (options: { video: number }, command: commander. } try { - await addVideoRedundancy({ - url, - accessToken, - videoId: options.video - }) + await server.redundancy.addVideo({ videoId: options.video }) console.log('Video will be duplicated by your instance!') @@ -138,9 +127,10 @@ async function addRedundancyCLI (options: { video: number }, command: commander. } } -async function removeRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) { +async function removeRedundancyCLI (options: { video: number }, command: Command) { const { url, username, password } = await getServerCredentials(command) - const accessToken = await getAdminTokenOrDie(url, username, password) + 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') @@ -148,14 +138,14 @@ async function removeRedundancyCLI (options: { video: number }, command: command process.exit(-1) } - const videoId = parseInt(options.video + '', 10) + const videoId = forceNumber(options.video) - let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') - let videoRedundancy = redundancies.find(r => videoId === r.id) + const myVideoRedundancies = await server.redundancy.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.redundancy.listVideos({ target: 'remote-videos' }) + videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) } if (!videoRedundancy) { @@ -169,11 +159,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.redundancy.removeVideo({ redundancyId: id }) } console.log('Video redundancy removed!') @@ -184,16 +170,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[] -}