]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/servers.ts
Introduce login command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / servers.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
89231874 2
6c5065a0
C
3import { ChildProcess, fork } from 'child_process'
4import { copy, ensureDir } from 'fs-extra'
c655c9ef 5import { join } from 'path'
6c5065a0 6import { root } from '@server/helpers/core-utils'
7c3b7976 7import { randomInt } from '../../core-utils/miscs/miscs'
a35a2279 8import { VideoChannel } from '../../models/videos'
329619b3
C
9import { BulkCommand } from '../bulk'
10import { CLICommand } from '../cli'
e8bd7ce7 11import { CustomPagesCommand } from '../custom-pages'
c1bc8ee4 12import { FeedCommand } from '../feeds'
a92ddacb 13import { LogsCommand } from '../logs'
6c5065a0 14import { isGithubCI, parallelTests, SQLCommand } from '../miscs'
0c1a77e9 15import { AbusesCommand } from '../moderation'
23a3a882 16import { OverviewsCommand } from '../overviews'
af971e06 17import { SearchCommand } from '../search'
87e2635a 18import { SocketIOCommand } from '../socket'
41d1d075 19import { AccountsCommand, BlocklistCommand, LoginCommand, NotificationsCommand, SubscriptionsCommand } from '../users'
6910f20f
C
20import {
21 BlacklistCommand,
22 CaptionsCommand,
23 ChangeOwnershipCommand,
a5461888 24 ChannelsCommand,
6910f20f
C
25 HistoryCommand,
26 ImportsCommand,
27 LiveCommand,
28 PlaylistsCommand,
57f879a5
C
29 ServicesCommand,
30 StreamingPlaylistsCommand
6910f20f 31} from '../videos'
12edc149 32import { CommentsCommand } from '../videos/comments-command'
65e6e260 33import { ConfigCommand } from './config-command'
a9c58393 34import { ContactFormCommand } from './contact-form-command'
883a9019 35import { DebugCommand } from './debug-command'
c3d29f69 36import { FollowsCommand } from './follows-command'
9c6327f8 37import { JobsCommand } from './jobs-command'
ae2abfd3 38import { PluginsCommand } from './plugins-command'
dab04709 39import { RedundancyCommand } from './redundancy-command'
6c5065a0 40import { ServersCommand } from './servers-command'
bc809041 41import { StatsCommand } from './stats-command'
0e1dc3e7
C
42
43interface ServerInfo {
078f17e6 44 app?: ChildProcess
af4ae64f 45
0e1dc3e7 46 url: string
078f17e6
C
47 host?: string
48 hostname?: string
49 port?: number
af4ae64f 50
078f17e6 51 rtmpPort?: number
c655c9ef 52
078f17e6 53 parallel?: boolean
86ebdf8c 54 internalServerNumber: number
078f17e6 55 serverNumber?: number
0e1dc3e7 56
078f17e6
C
57 client?: {
58 id?: string
59 secret?: string
0e1dc3e7
C
60 }
61
078f17e6 62 user?: {
a1587156
C
63 username: string
64 password: string
0e1dc3e7
C
65 email?: string
66 }
67
7c3b7976
C
68 customConfigFile?: string
69
0e1dc3e7 70 accessToken?: string
f43db2f4 71 refreshToken?: string
df0b219d 72 videoChannel?: VideoChannel
0e1dc3e7
C
73
74 video?: {
75 id: number
76 uuid: string
d4a8e7a6 77 shortUUID: string
310b5219 78 name?: string
a59f210f 79 url?: string
aea0b0e7 80
310b5219 81 account?: {
b64c950a
C
82 name: string
83 }
aea0b0e7
C
84
85 embedPath?: string
0e1dc3e7
C
86 }
87
88 remoteVideo?: {
89 id: number
90 uuid: string
91 }
df0b219d
C
92
93 videos?: { id: number, uuid: string }[]
329619b3
C
94
95 bulkCommand?: BulkCommand
96 cliCommand?: CLICommand
e8bd7ce7 97 customPageCommand?: CustomPagesCommand
c1bc8ee4 98 feedCommand?: FeedCommand
a92ddacb 99 logsCommand?: LogsCommand
0c1a77e9 100 abusesCommand?: AbusesCommand
23a3a882 101 overviewsCommand?: OverviewsCommand
af971e06 102 searchCommand?: SearchCommand
a9c58393 103 contactFormCommand?: ContactFormCommand
883a9019 104 debugCommand?: DebugCommand
c3d29f69 105 followsCommand?: FollowsCommand
9c6327f8 106 jobsCommand?: JobsCommand
ae2abfd3 107 pluginsCommand?: PluginsCommand
dab04709 108 redundancyCommand?: RedundancyCommand
bc809041 109 statsCommand?: StatsCommand
65e6e260 110 configCommand?: ConfigCommand
87e2635a 111 socketIOCommand?: SocketIOCommand
9fff08cf 112 accountsCommand?: AccountsCommand
5f8bd4cb 113 blocklistCommand?: BlocklistCommand
2c27e704 114 subscriptionsCommand?: SubscriptionsCommand
4f219914 115 liveCommand?: LiveCommand
d897210c 116 servicesCommand?: ServicesCommand
e3d15a6a 117 blacklistCommand?: BlacklistCommand
a2470c9f 118 captionsCommand?: CaptionsCommand
72cbfc56 119 changeOwnershipCommand?: ChangeOwnershipCommand
e6346d59 120 playlistsCommand?: PlaylistsCommand
313228e9 121 historyCommand?: HistoryCommand
6910f20f 122 importsCommand?: ImportsCommand
57f879a5 123 streamingPlaylistsCommand?: StreamingPlaylistsCommand
a5461888 124 channelsCommand?: ChannelsCommand
12edc149 125 commentsCommand?: CommentsCommand
9293139f 126 sqlCommand?: SQLCommand
dd0ebb71 127 notificationsCommand?: NotificationsCommand
6c5065a0 128 serversCommand?: ServersCommand
41d1d075 129 loginCommand?: LoginCommand
7c3b7976
C
130}
131
b36f41ca 132function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) {
a1587156 133 const apps = []
0e1dc3e7
C
134 let i = 0
135
136 return new Promise<ServerInfo[]>(res => {
137 function anotherServerDone (serverNumber, app) {
138 apps[serverNumber - 1] = app
139 i++
140 if (i === totalServers) {
141 return res(apps)
142 }
143 }
144
210feb6c
C
145 for (let j = 1; j <= totalServers; j++) {
146 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
147 }
0e1dc3e7
C
148 })
149}
150
86ebdf8c
C
151function randomServer () {
152 const low = 10
153 const high = 10000
154
7c3b7976 155 return randomInt(low, high)
86ebdf8c
C
156}
157
c655c9ef
C
158function randomRTMP () {
159 const low = 1900
160 const high = 2100
161
162 return randomInt(low, high)
163}
164
bd2e2f11
C
165type RunServerOptions = {
166 hideLogs?: boolean
167 execArgv?: string[]
168}
169
170async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = [], options: RunServerOptions = {}) {
7c3b7976 171 const parallel = parallelTests()
86ebdf8c
C
172
173 const internalServerNumber = parallel ? randomServer() : serverNumber
3e8584b9 174 const rtmpPort = parallel ? randomRTMP() : 1936
86ebdf8c
C
175 const port = 9000 + internalServerNumber
176
6c5065a0 177 await ServersCommand.flushTests(internalServerNumber)
42e1ec25 178
0e1dc3e7
C
179 const server: ServerInfo = {
180 app: null,
86ebdf8c
C
181 port,
182 internalServerNumber,
c655c9ef 183 rtmpPort,
86ebdf8c 184 parallel,
7c3b7976 185 serverNumber,
86ebdf8c
C
186 url: `http://localhost:${port}`,
187 host: `localhost:${port}`,
af4ae64f 188 hostname: 'localhost',
0e1dc3e7
C
189 client: {
190 id: null,
191 secret: null
192 },
193 user: {
194 username: null,
195 password: null
196 }
197 }
198
bd2e2f11 199 return runServer(server, configOverride, args, options)
913b1d71
C
200}
201
bd2e2f11 202async function runServer (server: ServerInfo, configOverrideArg?: any, args = [], options: RunServerOptions = {}) {
0e1dc3e7
C
203 // These actions are async so we need to be sure that they have both been done
204 const serverRunString = {
fcb77122 205 'HTTP server listening': false
0e1dc3e7 206 }
913b1d71 207 const key = 'Database peertube_test' + server.internalServerNumber + ' is ready'
0e1dc3e7
C
208 serverRunString[key] = false
209
210 const regexps = {
211 client_id: 'Client id: (.+)',
212 client_secret: 'Client secret: (.+)',
213 user_username: 'Username: (.+)',
214 user_password: 'User password: (.+)'
215 }
216
7c3b7976
C
217 if (server.internalServerNumber !== server.serverNumber) {
218 const basePath = join(root(), 'config')
219
220 const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`)
221 await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile)
222
223 server.customConfigFile = tmpConfigFile
224 }
fdbda9e3 225
7c3b7976 226 const configOverride: any = {}
86ebdf8c 227
913b1d71 228 if (server.parallel) {
7c3b7976 229 Object.assign(configOverride, {
86ebdf8c 230 listen: {
913b1d71 231 port: server.port
86ebdf8c
C
232 },
233 webserver: {
913b1d71 234 port: server.port
86ebdf8c
C
235 },
236 database: {
913b1d71 237 suffix: '_test' + server.internalServerNumber
86ebdf8c
C
238 },
239 storage: {
913b1d71
C
240 tmp: `test${server.internalServerNumber}/tmp/`,
241 avatars: `test${server.internalServerNumber}/avatars/`,
242 videos: `test${server.internalServerNumber}/videos/`,
243 streaming_playlists: `test${server.internalServerNumber}/streaming-playlists/`,
244 redundancy: `test${server.internalServerNumber}/redundancy/`,
245 logs: `test${server.internalServerNumber}/logs/`,
246 previews: `test${server.internalServerNumber}/previews/`,
247 thumbnails: `test${server.internalServerNumber}/thumbnails/`,
248 torrents: `test${server.internalServerNumber}/torrents/`,
249 captions: `test${server.internalServerNumber}/captions/`,
89cd1275
C
250 cache: `test${server.internalServerNumber}/cache/`,
251 plugins: `test${server.internalServerNumber}/plugins/`
86ebdf8c
C
252 },
253 admin: {
913b1d71 254 email: `admin${server.internalServerNumber}@example.com`
c655c9ef
C
255 },
256 live: {
257 rtmp: {
258 port: server.rtmpPort
259 }
86ebdf8c 260 }
7c3b7976 261 })
86ebdf8c
C
262 }
263
264 if (configOverrideArg !== undefined) {
265 Object.assign(configOverride, configOverrideArg)
fdbda9e3
C
266 }
267
7c3b7976
C
268 // Share the environment
269 const env = Object.create(process.env)
270 env['NODE_ENV'] = 'test'
271 env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString()
86ebdf8c
C
272 env['NODE_CONFIG'] = JSON.stringify(configOverride)
273
bd2e2f11 274 const forkOptions = {
0e1dc3e7 275 silent: true,
7c3b7976 276 env,
bd2e2f11
C
277 detached: true,
278 execArgv: options.execArgv || []
0e1dc3e7
C
279 }
280
281 return new Promise<ServerInfo>(res => {
bd2e2f11 282 server.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
42e1ec25
C
283 server.app.stdout.on('data', function onStdout (data) {
284 let dontContinue = false
285
286 // Capture things if we want to
287 for (const key of Object.keys(regexps)) {
a1587156 288 const regexp = regexps[key]
42e1ec25
C
289 const matches = data.toString().match(regexp)
290 if (matches !== null) {
a1587156
C
291 if (key === 'client_id') server.client.id = matches[1]
292 else if (key === 'client_secret') server.client.secret = matches[1]
293 else if (key === 'user_username') server.user.username = matches[1]
294 else if (key === 'user_password') server.user.password = matches[1]
42e1ec25
C
295 }
296 }
297
298 // Check if all required sentences are here
299 for (const key of Object.keys(serverRunString)) {
a1587156
C
300 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
301 if (serverRunString[key] === false) dontContinue = true
42e1ec25
C
302 }
303
304 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
305 if (dontContinue === true) return
306
bd2e2f11 307 if (options.hideLogs === false) {
5a547f69
C
308 console.log(data.toString())
309 } else {
310 server.app.stdout.removeListener('data', onStdout)
311 }
42e1ec25
C
312
313 process.on('exit', () => {
314 try {
315 process.kill(server.app.pid)
316 } catch { /* empty */ }
dc094603 317 })
42e1ec25 318
078f17e6 319 assignCommands(server)
329619b3 320
42e1ec25
C
321 res(server)
322 })
0e1dc3e7
C
323 })
324}
325
078f17e6
C
326function assignCommands (server: ServerInfo) {
327 server.bulkCommand = new BulkCommand(server)
328 server.cliCommand = new CLICommand(server)
329 server.customPageCommand = new CustomPagesCommand(server)
330 server.feedCommand = new FeedCommand(server)
331 server.logsCommand = new LogsCommand(server)
332 server.abusesCommand = new AbusesCommand(server)
333 server.overviewsCommand = new OverviewsCommand(server)
334 server.searchCommand = new SearchCommand(server)
335 server.contactFormCommand = new ContactFormCommand(server)
336 server.debugCommand = new DebugCommand(server)
337 server.followsCommand = new FollowsCommand(server)
338 server.jobsCommand = new JobsCommand(server)
339 server.pluginsCommand = new PluginsCommand(server)
340 server.redundancyCommand = new RedundancyCommand(server)
341 server.statsCommand = new StatsCommand(server)
342 server.configCommand = new ConfigCommand(server)
343 server.socketIOCommand = new SocketIOCommand(server)
344 server.accountsCommand = new AccountsCommand(server)
345 server.blocklistCommand = new BlocklistCommand(server)
346 server.subscriptionsCommand = new SubscriptionsCommand(server)
347 server.liveCommand = new LiveCommand(server)
348 server.servicesCommand = new ServicesCommand(server)
349 server.blacklistCommand = new BlacklistCommand(server)
350 server.captionsCommand = new CaptionsCommand(server)
351 server.changeOwnershipCommand = new ChangeOwnershipCommand(server)
352 server.playlistsCommand = new PlaylistsCommand(server)
353 server.historyCommand = new HistoryCommand(server)
354 server.importsCommand = new ImportsCommand(server)
355 server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server)
356 server.channelsCommand = new ChannelsCommand(server)
357 server.commentsCommand = new CommentsCommand(server)
9293139f 358 server.sqlCommand = new SQLCommand(server)
dd0ebb71 359 server.notificationsCommand = new NotificationsCommand(server)
6c5065a0 360 server.serversCommand = new ServersCommand(server)
41d1d075 361 server.loginCommand = new LoginCommand(server)
078f17e6
C
362}
363
e5565833 364async function reRunServer (server: ServerInfo, configOverride?: any) {
913b1d71 365 const newServer = await runServer(server, configOverride)
7bc29171
C
366 server.app = newServer.app
367
368 return server
369}
370
9293139f 371async function killallServers (servers: ServerInfo[]) {
0e1dc3e7 372 for (const server of servers) {
7c3b7976
C
373 if (!server.app) continue
374
9293139f
C
375 await server.sqlCommand.cleanup()
376
0e1dc3e7 377 process.kill(-server.app.pid)
9293139f 378
7c3b7976 379 server.app = null
0e1dc3e7
C
380 }
381}
382
83ef31fe 383async function cleanupTests (servers: ServerInfo[]) {
9293139f 384 await killallServers(servers)
86ebdf8c 385
83ef31fe
C
386 if (isGithubCI()) {
387 await ensureDir('artifacts')
388 }
389
6c5065a0 390 let p: Promise<any>[] = []
86ebdf8c 391 for (const server of servers) {
6c5065a0 392 p = p.concat(server.serversCommand.cleanupTests())
86ebdf8c
C
393 }
394
395 return Promise.all(p)
396}
397
0e1dc3e7
C
398// ---------------------------------------------------------------------------
399
400export {
401 ServerInfo,
86ebdf8c 402 cleanupTests,
0e1dc3e7 403 flushAndRunMultipleServers,
210feb6c 404 flushAndRunServer,
7bc29171 405 killallServers,
792e5b8e 406 reRunServer,
6c5065a0 407 assignCommands
0e1dc3e7 408}