aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-redundancy.ts
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/peertube-redundancy.ts
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/peertube-redundancy.ts')
-rw-r--r--server/tools/peertube-redundancy.ts60
1 files changed, 19 insertions, 41 deletions
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}