1
0
Fork 0
peertube/server/tools/peertube-redundancy.ts

173 lines
5.5 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import CliTable3 from 'cli-table3'
2021-07-09 09:03:44 -04:00
import { Command, program } from 'commander'
import { URL } from 'url'
import validator from 'validator'
import { forceNumber, uniqify } from '@shared/core-utils'
2021-07-16 08:27:30 -04:00
import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models'
import { assignToken, buildServer, getServerCredentials } from './shared'
2020-01-28 05:07:23 -05:00
2020-01-31 10:56:52 -05:00
import bytes = require('bytes')
2020-01-28 05:07:23 -05:00
program
2021-12-13 12:56:12 -05:00
.name('redundancy')
2020-01-28 05:07:23 -05:00
.usage('[command] [options]')
program
.command('list-remote-redundancies')
.description('List remote redundancies on your videos')
.option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.action(() => listRedundanciesCLI('my-videos'))
program
.command('list-my-redundancies')
.description('List your redundancies of remote videos')
.option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.action(() => listRedundanciesCLI('remote-videos'))
program
.command('add')
.description('Duplicate a video in your redundancy system')
.option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.option('-v, --video <videoId>', 'Video id to duplicate')
2021-02-03 03:33:05 -05:00
.action((options, command) => addRedundancyCLI(options, command))
2020-01-28 05:07:23 -05:00
program
.command('remove')
.description('Remove a video from your redundancies')
.option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.option('-v, --video <videoId>', 'Video id to remove from redundancies')
2021-02-03 03:33:05 -05:00
.action((options, command) => removeRedundancyCLI(options, command))
2020-01-28 05:07:23 -05:00
if (!process.argv.slice(2).length) {
program.outputHelp()
}
program.parse(process.argv)
// ----------------------------------------------------------------------------
async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
const { url, username, password } = await getServerCredentials(program)
2021-07-13 05:44:16 -04:00
const server = buildServer(url)
await assignToken(server, username, password)
2020-01-28 05:07:23 -05:00
2021-07-16 03:04:35 -04:00
const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })
2020-01-28 05:07:23 -05:00
const table = new CliTable3({
head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
2020-01-31 10:56:52 -05:00
}) as any
2020-01-28 05:07:23 -05:00
2021-07-09 09:03:44 -04:00
for (const redundancy of data) {
2020-01-28 05:07:23 -05:00
const webtorrentFiles = redundancy.redundancies.files
const streamingPlaylists = redundancy.redundancies.streamingPlaylists
let totalSize = ''
if (target === 'remote-videos') {
const tmp = webtorrentFiles.concat(streamingPlaylists)
.reduce((a, b) => a + b.size, 0)
totalSize = bytes(tmp)
}
2022-08-17 09:36:03 -04:00
const instances = uniqify(
2020-01-28 05:07:23 -05:00
webtorrentFiles.concat(streamingPlaylists)
.map(r => r.fileUrl)
2020-01-31 10:56:52 -05:00
.map(u => new URL(u).host)
2020-01-28 05:07:23 -05:00
)
table.push([
redundancy.id.toString(),
redundancy.name,
redundancy.url,
webtorrentFiles.length,
streamingPlaylists.length,
instances.join('\n'),
totalSize
])
}
console.log(table.toString())
process.exit(0)
}
2021-06-25 11:48:27 -04:00
async function addRedundancyCLI (options: { video: number }, command: Command) {
2021-02-03 03:33:05 -05:00
const { url, username, password } = await getServerCredentials(command)
2021-07-13 05:44:16 -04:00
const server = buildServer(url)
await assignToken(server, username, password)
2020-01-28 05:07:23 -05:00
2021-02-03 03:33:05 -05:00
if (!options.video || validator.isInt('' + options.video) === false) {
2020-01-28 05:07:23 -05:00
console.error('You need to specify the video id to duplicate and it should be a number.\n')
2021-02-03 03:33:05 -05:00
command.outputHelp()
2020-01-28 05:07:23 -05:00
process.exit(-1)
}
try {
2021-07-16 03:04:35 -04:00
await server.redundancy.addVideo({ videoId: options.video })
2020-01-28 05:07:23 -05:00
console.log('Video will be duplicated by your instance!')
process.exit(0)
} catch (err) {
if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
2020-01-28 05:07:23 -05:00
console.error('This video is already duplicated by your instance.')
} else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
2020-01-28 05:07:23 -05:00
console.error('This video id does not exist.')
} else {
console.error(err)
}
process.exit(-1)
}
}
2021-06-25 11:48:27 -04:00
async function removeRedundancyCLI (options: { video: number }, command: Command) {
2021-02-03 03:33:05 -05:00
const { url, username, password } = await getServerCredentials(command)
2021-07-13 05:44:16 -04:00
const server = buildServer(url)
await assignToken(server, username, password)
2020-01-28 05:07:23 -05:00
2021-02-03 03:33:05 -05:00
if (!options.video || validator.isInt('' + options.video) === false) {
2020-01-28 05:07:23 -05:00
console.error('You need to specify the video id to remove from your redundancies.\n')
2021-02-03 03:33:05 -05:00
command.outputHelp()
2020-01-28 05:07:23 -05:00
process.exit(-1)
}
const videoId = forceNumber(options.video)
2020-01-28 05:07:23 -05:00
2021-07-16 03:04:35 -04:00
const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
2021-07-09 09:03:44 -04:00
let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
2020-01-28 05:07:23 -05:00
if (!videoRedundancy) {
2021-07-16 03:04:35 -04:00
const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
2021-07-09 09:03:44 -04:00
videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
2020-01-28 05:07:23 -05:00
}
if (!videoRedundancy) {
console.error('Video redundancy not found.')
process.exit(-1)
}
try {
const ids = videoRedundancy.redundancies.files
.concat(videoRedundancy.redundancies.streamingPlaylists)
.map(r => r.id)
for (const id of ids) {
2021-07-16 03:04:35 -04:00
await server.redundancy.removeVideo({ redundancyId: id })
2020-01-28 05:07:23 -05:00
}
console.log('Video redundancy removed!')
process.exit(0)
} catch (err) {
console.error(err)
process.exit(-1)
}
}