diff options
Diffstat (limited to 'server/tools/peertube-redundancy.ts')
-rw-r--r-- | server/tools/peertube-redundancy.ts | 61 |
1 files changed, 20 insertions, 41 deletions
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 @@ | |||
1 | // eslint-disable @typescript-eslint/no-unnecessary-type-assertion | ||
2 | |||
3 | import { registerTSPaths } from '../helpers/register-ts-paths' | 1 | import { registerTSPaths } from '../helpers/register-ts-paths' |
4 | registerTSPaths() | 2 | registerTSPaths() |
5 | 3 | ||
6 | import { program, Command } from 'commander' | ||
7 | import { getAdminTokenOrDie, getServerCredentials } from './cli' | ||
8 | import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' | ||
9 | import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy' | ||
10 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
11 | import validator from 'validator' | ||
12 | import * as CliTable3 from 'cli-table3' | 4 | import * as CliTable3 from 'cli-table3' |
13 | import { URL } from 'url' | 5 | import { Command, program } from 'commander' |
14 | import { uniq } from 'lodash' | 6 | import { uniq } from 'lodash' |
7 | import { URL } from 'url' | ||
8 | import validator from 'validator' | ||
9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
10 | import { VideoRedundanciesTarget } from '@shared/models' | ||
11 | import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' | ||
15 | 12 | ||
16 | import bytes = require('bytes') | 13 | import bytes = require('bytes') |
17 | 14 | ||
@@ -63,15 +60,16 @@ program.parse(process.argv) | |||
63 | 60 | ||
64 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | 61 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { |
65 | const { url, username, password } = await getServerCredentials(program) | 62 | const { url, username, password } = await getServerCredentials(program) |
66 | const accessToken = await getAdminTokenOrDie(url, username, password) | 63 | const token = await getAdminTokenOrDie(url, username, password) |
64 | const server = buildServer(url, token) | ||
67 | 65 | ||
68 | const redundancies = await listVideoRedundanciesData(url, accessToken, target) | 66 | const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) |
69 | 67 | ||
70 | const table = new CliTable3({ | 68 | const table = new CliTable3({ |
71 | head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] | 69 | head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] |
72 | }) as any | 70 | }) as any |
73 | 71 | ||
74 | for (const redundancy of redundancies) { | 72 | for (const redundancy of data) { |
75 | const webtorrentFiles = redundancy.redundancies.files | 73 | const webtorrentFiles = redundancy.redundancies.files |
76 | const streamingPlaylists = redundancy.redundancies.streamingPlaylists | 74 | const streamingPlaylists = redundancy.redundancies.streamingPlaylists |
77 | 75 | ||
@@ -106,7 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | |||
106 | 104 | ||
107 | async function addRedundancyCLI (options: { video: number }, command: Command) { | 105 | async function addRedundancyCLI (options: { video: number }, command: Command) { |
108 | const { url, username, password } = await getServerCredentials(command) | 106 | const { url, username, password } = await getServerCredentials(command) |
109 | const accessToken = await getAdminTokenOrDie(url, username, password) | 107 | const token = await getAdminTokenOrDie(url, username, password) |
108 | const server = buildServer(url, token) | ||
110 | 109 | ||
111 | if (!options.video || validator.isInt('' + options.video) === false) { | 110 | 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') | 111 | 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) { | |||
115 | } | 114 | } |
116 | 115 | ||
117 | try { | 116 | try { |
118 | await addVideoRedundancy({ | 117 | await server.redundancyCommand.addVideo({ videoId: options.video }) |
119 | url, | ||
120 | accessToken, | ||
121 | videoId: options.video | ||
122 | }) | ||
123 | 118 | ||
124 | console.log('Video will be duplicated by your instance!') | 119 | console.log('Video will be duplicated by your instance!') |
125 | 120 | ||
@@ -139,7 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { | |||
139 | 134 | ||
140 | async function removeRedundancyCLI (options: { video: number }, command: Command) { | 135 | async function removeRedundancyCLI (options: { video: number }, command: Command) { |
141 | const { url, username, password } = await getServerCredentials(command) | 136 | const { url, username, password } = await getServerCredentials(command) |
142 | const accessToken = await getAdminTokenOrDie(url, username, password) | 137 | const token = await getAdminTokenOrDie(url, username, password) |
138 | const server = buildServer(url, token) | ||
143 | 139 | ||
144 | if (!options.video || validator.isInt('' + options.video) === false) { | 140 | if (!options.video || validator.isInt('' + options.video) === false) { |
145 | console.error('You need to specify the video id to remove from your redundancies.\n') | 141 | 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 | |||
149 | 145 | ||
150 | const videoId = parseInt(options.video + '', 10) | 146 | const videoId = parseInt(options.video + '', 10) |
151 | 147 | ||
152 | let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') | 148 | const myVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'my-videos' }) |
153 | let videoRedundancy = redundancies.find(r => videoId === r.id) | 149 | let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) |
154 | 150 | ||
155 | if (!videoRedundancy) { | 151 | if (!videoRedundancy) { |
156 | redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos') | 152 | const remoteVideoRedundancies = await server.redundancyCommand.listVideos({ target: 'remote-videos' }) |
157 | videoRedundancy = redundancies.find(r => videoId === r.id) | 153 | videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) |
158 | } | 154 | } |
159 | 155 | ||
160 | if (!videoRedundancy) { | 156 | if (!videoRedundancy) { |
@@ -168,11 +164,7 @@ async function removeRedundancyCLI (options: { video: number }, command: Command | |||
168 | .map(r => r.id) | 164 | .map(r => r.id) |
169 | 165 | ||
170 | for (const id of ids) { | 166 | for (const id of ids) { |
171 | await removeVideoRedundancy({ | 167 | await server.redundancyCommand.removeVideo({ redundancyId: id }) |
172 | url, | ||
173 | accessToken, | ||
174 | redundancyId: id | ||
175 | }) | ||
176 | } | 168 | } |
177 | 169 | ||
178 | console.log('Video redundancy removed!') | 170 | console.log('Video redundancy removed!') |
@@ -183,16 +175,3 @@ async function removeRedundancyCLI (options: { video: number }, command: Command | |||
183 | process.exit(-1) | 175 | process.exit(-1) |
184 | } | 176 | } |
185 | } | 177 | } |
186 | |||
187 | async 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 | } | ||