diff options
Diffstat (limited to 'server/tools/peertube-redundancy.ts')
-rw-r--r-- | server/tools/peertube-redundancy.ts | 60 |
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 | |||
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, VideoRedundanciesTarget } from '@shared/models' | ||
10 | import { assignToken, buildServer, getServerCredentials } from './cli' | ||
15 | 11 | ||
16 | import bytes = require('bytes') | 12 | import bytes = require('bytes') |
17 | 13 | ||
@@ -63,15 +59,16 @@ program.parse(process.argv) | |||
63 | 59 | ||
64 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | 60 | async 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 | ||
107 | async function addRedundancyCLI (options: { video: number }, command: Command) { | 104 | async 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 | ||
140 | async function removeRedundancyCLI (options: { video: number }, command: Command) { | 134 | async 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 | |||
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 | } | ||