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