aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-13 11:44:16 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commitd0a0fa429d4651710ed951a3c11af0219e408964 (patch)
treec0638f2604e3442d1f15715b9b0f01ecab17ab20 /server/tools
parent41d1d075011174e73dccb74006181a92a618d7b4 (diff)
downloadPeerTube-d0a0fa429d4651710ed951a3c11af0219e408964.tar.gz
PeerTube-d0a0fa429d4651710ed951a3c11af0219e408964.tar.zst
PeerTube-d0a0fa429d4651710ed951a3c11af0219e408964.zip
Adapt CLI to new commands
Diffstat (limited to 'server/tools')
-rw-r--r--server/tools/cli.ts31
-rw-r--r--server/tools/peertube-auth.ts6
-rw-r--r--server/tools/peertube-get-access-token.ts10
-rw-r--r--server/tools/peertube-import-videos.ts12
-rw-r--r--server/tools/peertube-plugins.ts18
-rw-r--r--server/tools/peertube-redundancy.ts14
-rw-r--r--server/tools/peertube-upload.ts9
7 files changed, 52 insertions, 48 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 0528859a4..3e0e03b97 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -3,12 +3,10 @@ import { Netrc } from 'netrc-parser'
3import { join } from 'path' 3import { join } from 'path'
4import { createLogger, format, transports } from 'winston' 4import { createLogger, format, transports } from 'winston'
5import { assignCommands, ServerInfo } from '@shared/extra-utils' 5import { assignCommands, ServerInfo } from '@shared/extra-utils'
6import { getAccessToken } from '@shared/extra-utils/users/login'
7import { getMyUserInformation } from '@shared/extra-utils/users/users' 6import { getMyUserInformation } from '@shared/extra-utils/users/users'
8import { User, UserRole } from '@shared/models' 7import { User, UserRole } from '@shared/models'
9import { root } from '../../shared/extra-utils/miscs/miscs'
10import { VideoPrivacy } from '../../shared/models/videos' 8import { VideoPrivacy } from '../../shared/models/videos'
11import { getAppNumber, isTestInstance } from '../helpers/core-utils' 9import { getAppNumber, isTestInstance, root } from '../helpers/core-utils'
12 10
13let configName = 'PeerTube/CLI' 11let configName = 'PeerTube/CLI'
14if (isTestInstance()) configName += `-${getAppNumber()}` 12if (isTestInstance()) configName += `-${getAppNumber()}`
@@ -17,9 +15,9 @@ const config = require('application-config')(configName)
17 15
18const version = require('../../../package.json').version 16const version = require('../../../package.json').version
19 17
20async function getAdminTokenOrDie (url: string, username: string, password: string) { 18async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) {
21 const accessToken = await getAccessToken(url, username, password) 19 const accessToken = await server.loginCommand.getAccessToken(username, password)
22 const resMe = await getMyUserInformation(url, accessToken) 20 const resMe = await getMyUserInformation(server.url, accessToken)
23 const me: User = resMe.body 21 const me: User = resMe.body
24 22
25 if (me.role !== UserRole.ADMINISTRATOR) { 23 if (me.role !== UserRole.ADMINISTRATOR) {
@@ -30,10 +28,6 @@ async function getAdminTokenOrDie (url: string, username: string, password: stri
30 return accessToken 28 return accessToken
31} 29}
32 30
33async function getAccessTokenOrDie (url: string, username: string, password: string) {
34 return getAccessToken(url, username, password)
35}
36
37interface Settings { 31interface Settings {
38 remotes: any[] 32 remotes: any[]
39 default: number 33 default: number
@@ -187,13 +181,22 @@ function getServerCredentials (program: Command) {
187 }) 181 })
188} 182}
189 183
190function buildServer (url: string, accessToken?: string): ServerInfo { 184function buildServer (url: string): ServerInfo {
191 const server = { url, accessToken, internalServerNumber: undefined } 185 const server = { url, internalServerNumber: undefined }
192 assignCommands(server) 186 assignCommands(server)
193 187
194 return server 188 return server
195} 189}
196 190
191async function assignToken (server: ServerInfo, username: string, password: string) {
192 const bodyClient = await server.loginCommand.getClient()
193 const client = { id: bodyClient.client_id, secret: bodyClient.client_secret }
194
195 const body = await server.loginCommand.login({ client, user: { username, password } })
196
197 server.accessToken = body.access_token
198}
199
197function getLogger (logLevel = 'info') { 200function getLogger (logLevel = 'info') {
198 const logLevels = { 201 const logLevels = {
199 0: 0, 202 0: 0,
@@ -241,6 +244,6 @@ export {
241 buildVideoAttributesFromCommander, 244 buildVideoAttributesFromCommander,
242 245
243 getAdminTokenOrDie, 246 getAdminTokenOrDie,
244 getAccessTokenOrDie, 247 buildServer,
245 buildServer 248 assignToken
246} 249}
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
index 1934e7986..b9f4ef4f8 100644
--- a/server/tools/peertube-auth.ts
+++ b/server/tools/peertube-auth.ts
@@ -5,9 +5,8 @@ registerTSPaths()
5 5
6import { OptionValues, program } from 'commander' 6import { OptionValues, program } from 'commander'
7import * as prompt from 'prompt' 7import * as prompt from 'prompt'
8import { getNetrc, getSettings, writeSettings } from './cli' 8import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli'
9import { isUserUsernameValid } from '../helpers/custom-validators/users' 9import { isUserUsernameValid } from '../helpers/custom-validators/users'
10import { getAccessToken } from '../../shared/extra-utils'
11import * as CliTable3 from 'cli-table3' 10import * as CliTable3 from 'cli-table3'
12 11
13async function delInstance (url: string) { 12async function delInstance (url: string) {
@@ -97,7 +96,8 @@ program
97 // @see https://github.com/Chocobozzz/PeerTube/issues/3520 96 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
98 result.url = stripExtraneousFromPeerTubeUrl(result.url) 97 result.url = stripExtraneousFromPeerTubeUrl(result.url)
99 98
100 await getAccessToken(result.url, result.username, result.password) 99 const server = buildServer(result.url)
100 await assignToken(server, result.username, result.password)
101 } catch (err) { 101 } catch (err) {
102 console.error(err.message) 102 console.error(err.message)
103 process.exit(-1) 103 process.exit(-1)
diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts
index 5868d0548..a67de9180 100644
--- a/server/tools/peertube-get-access-token.ts
+++ b/server/tools/peertube-get-access-token.ts
@@ -2,7 +2,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
2registerTSPaths() 2registerTSPaths()
3 3
4import { program } from 'commander' 4import { program } from 'commander'
5import { getAccessToken } from '../../shared/extra-utils' 5import { assignToken, buildServer } from './cli'
6 6
7program 7program
8 .option('-u, --url <url>', 'Server url') 8 .option('-u, --url <url>', 'Server url')
@@ -24,9 +24,11 @@ if (
24 process.exit(-1) 24 process.exit(-1)
25} 25}
26 26
27getAccessToken(options.url, options.username, options.password) 27const server = buildServer(options.url)
28 .then(accessToken => { 28
29 console.log(accessToken) 29assignToken(server, options.username, options.password)
30 .then(() => {
31 console.log(server.accessToken)
30 process.exit(0) 32 process.exit(0)
31 }) 33 })
32 .catch(err => { 34 .catch(err => {
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index a546a8dbe..0a4d6fa6e 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -14,10 +14,10 @@ import { sha256 } from '../helpers/core-utils'
14import { doRequestAndSaveToFile } from '../helpers/requests' 14import { doRequestAndSaveToFile } from '../helpers/requests'
15import { CONSTRAINTS_FIELDS } from '../initializers/constants' 15import { CONSTRAINTS_FIELDS } from '../initializers/constants'
16import { 16import {
17 assignToken,
17 buildCommonVideoOptions, 18 buildCommonVideoOptions,
18 buildServer, 19 buildServer,
19 buildVideoAttributesFromCommander, 20 buildVideoAttributesFromCommander,
20 getAccessTokenOrDie,
21 getLogger, 21 getLogger,
22 getServerCredentials 22 getServerCredentials
23} from './cli' 23} from './cli'
@@ -232,8 +232,8 @@ async function uploadVideoOnPeerTube (parameters: {
232 tags 232 tags
233 } 233 }
234 234
235 let accessToken = await getAccessTokenOrDie(url, username, password) 235 const server = buildServer(url)
236 const server = buildServer(url, accessToken) 236 await assignToken(server, username, password)
237 237
238 const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) 238 const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes)
239 239
@@ -247,14 +247,14 @@ async function uploadVideoOnPeerTube (parameters: {
247 log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) 247 log.info('\nUploading on PeerTube video "%s".', videoAttributes.name)
248 248
249 try { 249 try {
250 await uploadVideo(url, accessToken, videoAttributes) 250 await uploadVideo(url, server.accessToken, videoAttributes)
251 } catch (err) { 251 } catch (err) {
252 if (err.message.indexOf('401') !== -1) { 252 if (err.message.indexOf('401') !== -1) {
253 log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') 253 log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.')
254 254
255 accessToken = await getAccessTokenOrDie(url, username, password) 255 server.accessToken = await server.loginCommand.getAccessToken(username, password)
256 256
257 await uploadVideo(url, accessToken, videoAttributes) 257 await uploadVideo(url, server.accessToken, videoAttributes)
258 } else { 258 } else {
259 exitError(err.message) 259 exitError(err.message)
260 } 260 }
diff --git a/server/tools/peertube-plugins.ts b/server/tools/peertube-plugins.ts
index 63541bf2c..22a09b779 100644
--- a/server/tools/peertube-plugins.ts
+++ b/server/tools/peertube-plugins.ts
@@ -4,7 +4,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths'
4registerTSPaths() 4registerTSPaths()
5 5
6import { program, Command, OptionValues } from 'commander' 6import { program, Command, OptionValues } from 'commander'
7import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' 7import { assignToken, buildServer, getServerCredentials } from './cli'
8import { PluginType } from '../../shared/models' 8import { PluginType } from '../../shared/models'
9import { isAbsolute } from 'path' 9import { isAbsolute } from 'path'
10import * as CliTable3 from 'cli-table3' 10import * as CliTable3 from 'cli-table3'
@@ -62,8 +62,8 @@ program.parse(process.argv)
62 62
63async function pluginsListCLI (command: Command, options: OptionValues) { 63async function pluginsListCLI (command: Command, options: OptionValues) {
64 const { url, username, password } = await getServerCredentials(command) 64 const { url, username, password } = await getServerCredentials(command)
65 const token = await getAdminTokenOrDie(url, username, password) 65 const server = buildServer(url)
66 const server = buildServer(url, token) 66 await assignToken(server, username, password)
67 67
68 let pluginType: PluginType 68 let pluginType: PluginType
69 if (options.onlyThemes) pluginType = PluginType.THEME 69 if (options.onlyThemes) pluginType = PluginType.THEME
@@ -105,8 +105,8 @@ async function installPluginCLI (command: Command, options: OptionValues) {
105 } 105 }
106 106
107 const { url, username, password } = await getServerCredentials(command) 107 const { url, username, password } = await getServerCredentials(command)
108 const token = await getAdminTokenOrDie(url, username, password) 108 const server = buildServer(url)
109 const server = buildServer(url, token) 109 await assignToken(server, username, password)
110 110
111 try { 111 try {
112 await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) 112 await server.pluginsCommand.install({ npmName: options.npmName, path: options.path })
@@ -132,8 +132,8 @@ async function updatePluginCLI (command: Command, options: OptionValues) {
132 } 132 }
133 133
134 const { url, username, password } = await getServerCredentials(command) 134 const { url, username, password } = await getServerCredentials(command)
135 const token = await getAdminTokenOrDie(url, username, password) 135 const server = buildServer(url)
136 const server = buildServer(url, token) 136 await assignToken(server, username, password)
137 137
138 try { 138 try {
139 await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) 139 await server.pluginsCommand.update({ npmName: options.npmName, path: options.path })
@@ -154,8 +154,8 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) {
154 } 154 }
155 155
156 const { url, username, password } = await getServerCredentials(command) 156 const { url, username, password } = await getServerCredentials(command)
157 const token = await getAdminTokenOrDie(url, username, password) 157 const server = buildServer(url)
158 const server = buildServer(url, token) 158 await assignToken(server, username, password)
159 159
160 try { 160 try {
161 await server.pluginsCommand.uninstall({ npmName: options.npmName }) 161 await server.pluginsCommand.uninstall({ npmName: options.npmName })
diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts
index 76d633f9e..0f6b3086c 100644
--- a/server/tools/peertube-redundancy.ts
+++ b/server/tools/peertube-redundancy.ts
@@ -8,7 +8,7 @@ import { URL } from 'url'
8import validator from 'validator' 8import validator from 'validator'
9import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' 9import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
10import { VideoRedundanciesTarget } from '@shared/models' 10import { VideoRedundanciesTarget } from '@shared/models'
11import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' 11import { assignToken, buildServer, getServerCredentials } from './cli'
12 12
13import bytes = require('bytes') 13import bytes = require('bytes')
14 14
@@ -60,8 +60,8 @@ program.parse(process.argv)
60 60
61async function listRedundanciesCLI (target: VideoRedundanciesTarget) { 61async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
62 const { url, username, password } = await getServerCredentials(program) 62 const { url, username, password } = await getServerCredentials(program)
63 const token = await getAdminTokenOrDie(url, username, password) 63 const server = buildServer(url)
64 const server = buildServer(url, token) 64 await assignToken(server, username, password)
65 65
66 const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) 66 const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target })
67 67
@@ -104,8 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
104 104
105async function addRedundancyCLI (options: { video: number }, command: Command) { 105async function addRedundancyCLI (options: { video: number }, command: Command) {
106 const { url, username, password } = await getServerCredentials(command) 106 const { url, username, password } = await getServerCredentials(command)
107 const token = await getAdminTokenOrDie(url, username, password) 107 const server = buildServer(url)
108 const server = buildServer(url, token) 108 await assignToken(server, username, password)
109 109
110 if (!options.video || validator.isInt('' + options.video) === false) { 110 if (!options.video || validator.isInt('' + options.video) === false) {
111 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')
@@ -134,8 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) {
134 134
135async function removeRedundancyCLI (options: { video: number }, command: Command) { 135async function removeRedundancyCLI (options: { video: number }, command: Command) {
136 const { url, username, password } = await getServerCredentials(command) 136 const { url, username, password } = await getServerCredentials(command)
137 const token = await getAdminTokenOrDie(url, username, password) 137 const server = buildServer(url)
138 const server = buildServer(url, token) 138 await assignToken(server, username, password)
139 139
140 if (!options.video || validator.isInt('' + options.video) === false) { 140 if (!options.video || validator.isInt('' + options.video) === false) {
141 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')
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts
index d1c5348d1..c94b05857 100644
--- a/server/tools/peertube-upload.ts
+++ b/server/tools/peertube-upload.ts
@@ -4,9 +4,8 @@ registerTSPaths()
4import { program } from 'commander' 4import { program } from 'commander'
5import { access, constants } from 'fs-extra' 5import { access, constants } from 'fs-extra'
6import { isAbsolute } from 'path' 6import { isAbsolute } from 'path'
7import { getAccessToken } from '../../shared/extra-utils'
8import { uploadVideo } from '../../shared/extra-utils/' 7import { uploadVideo } from '../../shared/extra-utils/'
9import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' 8import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
10 9
11let command = program 10let command = program
12 .name('upload') 11 .name('upload')
@@ -46,8 +45,8 @@ getServerCredentials(command)
46 .catch(err => console.error(err)) 45 .catch(err => console.error(err))
47 46
48async function run (url: string, username: string, password: string) { 47async function run (url: string, username: string, password: string) {
49 const token = await getAccessToken(url, username, password) 48 const server = buildServer(url)
50 const server = buildServer(url, token) 49 await assignToken(server, username, password)
51 50
52 await access(options.file, constants.F_OK) 51 await access(options.file, constants.F_OK)
53 52
@@ -62,7 +61,7 @@ async function run (url: string, username: string, password: string) {
62 }) 61 })
63 62
64 try { 63 try {
65 await uploadVideo(url, token, videoAttributes) 64 await uploadVideo(url, server.accessToken, videoAttributes)
66 console.log(`Video ${options.videoName} uploaded.`) 65 console.log(`Video ${options.videoName} uploaded.`)
67 process.exit(0) 66 process.exit(0)
68 } catch (err) { 67 } catch (err) {