diff options
Diffstat (limited to 'shared/server-commands/server/server.ts')
-rw-r--r-- | shared/server-commands/server/server.ts | 450 |
1 files changed, 0 insertions, 450 deletions
diff --git a/shared/server-commands/server/server.ts b/shared/server-commands/server/server.ts deleted file mode 100644 index 38568a890..000000000 --- a/shared/server-commands/server/server.ts +++ /dev/null | |||
@@ -1,450 +0,0 @@ | |||
1 | import { ChildProcess, fork } from 'child_process' | ||
2 | import { copy } from 'fs-extra' | ||
3 | import { join } from 'path' | ||
4 | import { parallelTests, randomInt, root } from '@shared/core-utils' | ||
5 | import { Video, VideoChannel, VideoChannelSync, VideoCreateResult, VideoDetails } from '@shared/models' | ||
6 | import { BulkCommand } from '../bulk' | ||
7 | import { CLICommand } from '../cli' | ||
8 | import { CustomPagesCommand } from '../custom-pages' | ||
9 | import { FeedCommand } from '../feeds' | ||
10 | import { LogsCommand } from '../logs' | ||
11 | import { AbusesCommand } from '../moderation' | ||
12 | import { OverviewsCommand } from '../overviews' | ||
13 | import { RunnerJobsCommand, RunnerRegistrationTokensCommand, RunnersCommand } from '../runners' | ||
14 | import { SearchCommand } from '../search' | ||
15 | import { SocketIOCommand } from '../socket' | ||
16 | import { | ||
17 | AccountsCommand, | ||
18 | BlocklistCommand, | ||
19 | LoginCommand, | ||
20 | NotificationsCommand, | ||
21 | RegistrationsCommand, | ||
22 | SubscriptionsCommand, | ||
23 | TwoFactorCommand, | ||
24 | UsersCommand | ||
25 | } from '../users' | ||
26 | import { | ||
27 | BlacklistCommand, | ||
28 | CaptionsCommand, | ||
29 | ChangeOwnershipCommand, | ||
30 | ChannelsCommand, | ||
31 | ChannelSyncsCommand, | ||
32 | HistoryCommand, | ||
33 | ImportsCommand, | ||
34 | LiveCommand, | ||
35 | VideoPasswordsCommand, | ||
36 | PlaylistsCommand, | ||
37 | ServicesCommand, | ||
38 | StoryboardCommand, | ||
39 | StreamingPlaylistsCommand, | ||
40 | VideosCommand, | ||
41 | VideoStudioCommand, | ||
42 | VideoTokenCommand, | ||
43 | ViewsCommand | ||
44 | } from '../videos' | ||
45 | import { CommentsCommand } from '../videos/comments-command' | ||
46 | import { VideoStatsCommand } from '../videos/video-stats-command' | ||
47 | import { ConfigCommand } from './config-command' | ||
48 | import { ContactFormCommand } from './contact-form-command' | ||
49 | import { DebugCommand } from './debug-command' | ||
50 | import { FollowsCommand } from './follows-command' | ||
51 | import { JobsCommand } from './jobs-command' | ||
52 | import { MetricsCommand } from './metrics-command' | ||
53 | import { PluginsCommand } from './plugins-command' | ||
54 | import { RedundancyCommand } from './redundancy-command' | ||
55 | import { ServersCommand } from './servers-command' | ||
56 | import { StatsCommand } from './stats-command' | ||
57 | |||
58 | export type RunServerOptions = { | ||
59 | hideLogs?: boolean | ||
60 | nodeArgs?: string[] | ||
61 | peertubeArgs?: string[] | ||
62 | env?: { [ id: string ]: string } | ||
63 | } | ||
64 | |||
65 | export class PeerTubeServer { | ||
66 | app?: ChildProcess | ||
67 | |||
68 | url: string | ||
69 | host?: string | ||
70 | hostname?: string | ||
71 | port?: number | ||
72 | |||
73 | rtmpPort?: number | ||
74 | rtmpsPort?: number | ||
75 | |||
76 | parallel?: boolean | ||
77 | internalServerNumber: number | ||
78 | |||
79 | serverNumber?: number | ||
80 | customConfigFile?: string | ||
81 | |||
82 | store?: { | ||
83 | client?: { | ||
84 | id?: string | ||
85 | secret?: string | ||
86 | } | ||
87 | |||
88 | user?: { | ||
89 | username: string | ||
90 | password: string | ||
91 | email?: string | ||
92 | } | ||
93 | |||
94 | channel?: VideoChannel | ||
95 | videoChannelSync?: Partial<VideoChannelSync> | ||
96 | |||
97 | video?: Video | ||
98 | videoCreated?: VideoCreateResult | ||
99 | videoDetails?: VideoDetails | ||
100 | |||
101 | videos?: { id: number, uuid: string }[] | ||
102 | } | ||
103 | |||
104 | accessToken?: string | ||
105 | refreshToken?: string | ||
106 | |||
107 | bulk?: BulkCommand | ||
108 | cli?: CLICommand | ||
109 | customPage?: CustomPagesCommand | ||
110 | feed?: FeedCommand | ||
111 | logs?: LogsCommand | ||
112 | abuses?: AbusesCommand | ||
113 | overviews?: OverviewsCommand | ||
114 | search?: SearchCommand | ||
115 | contactForm?: ContactFormCommand | ||
116 | debug?: DebugCommand | ||
117 | follows?: FollowsCommand | ||
118 | jobs?: JobsCommand | ||
119 | metrics?: MetricsCommand | ||
120 | plugins?: PluginsCommand | ||
121 | redundancy?: RedundancyCommand | ||
122 | stats?: StatsCommand | ||
123 | config?: ConfigCommand | ||
124 | socketIO?: SocketIOCommand | ||
125 | accounts?: AccountsCommand | ||
126 | blocklist?: BlocklistCommand | ||
127 | subscriptions?: SubscriptionsCommand | ||
128 | live?: LiveCommand | ||
129 | services?: ServicesCommand | ||
130 | blacklist?: BlacklistCommand | ||
131 | captions?: CaptionsCommand | ||
132 | changeOwnership?: ChangeOwnershipCommand | ||
133 | playlists?: PlaylistsCommand | ||
134 | history?: HistoryCommand | ||
135 | imports?: ImportsCommand | ||
136 | channelSyncs?: ChannelSyncsCommand | ||
137 | streamingPlaylists?: StreamingPlaylistsCommand | ||
138 | channels?: ChannelsCommand | ||
139 | comments?: CommentsCommand | ||
140 | notifications?: NotificationsCommand | ||
141 | servers?: ServersCommand | ||
142 | login?: LoginCommand | ||
143 | users?: UsersCommand | ||
144 | videoStudio?: VideoStudioCommand | ||
145 | videos?: VideosCommand | ||
146 | videoStats?: VideoStatsCommand | ||
147 | views?: ViewsCommand | ||
148 | twoFactor?: TwoFactorCommand | ||
149 | videoToken?: VideoTokenCommand | ||
150 | registrations?: RegistrationsCommand | ||
151 | videoPasswords?: VideoPasswordsCommand | ||
152 | |||
153 | storyboard?: StoryboardCommand | ||
154 | |||
155 | runners?: RunnersCommand | ||
156 | runnerRegistrationTokens?: RunnerRegistrationTokensCommand | ||
157 | runnerJobs?: RunnerJobsCommand | ||
158 | |||
159 | constructor (options: { serverNumber: number } | { url: string }) { | ||
160 | if ((options as any).url) { | ||
161 | this.setUrl((options as any).url) | ||
162 | } else { | ||
163 | this.setServerNumber((options as any).serverNumber) | ||
164 | } | ||
165 | |||
166 | this.store = { | ||
167 | client: { | ||
168 | id: null, | ||
169 | secret: null | ||
170 | }, | ||
171 | user: { | ||
172 | username: null, | ||
173 | password: null | ||
174 | } | ||
175 | } | ||
176 | |||
177 | this.assignCommands() | ||
178 | } | ||
179 | |||
180 | setServerNumber (serverNumber: number) { | ||
181 | this.serverNumber = serverNumber | ||
182 | |||
183 | this.parallel = parallelTests() | ||
184 | |||
185 | this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber | ||
186 | this.rtmpPort = this.parallel ? this.randomRTMP() : 1936 | ||
187 | this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937 | ||
188 | this.port = 9000 + this.internalServerNumber | ||
189 | |||
190 | this.url = `http://127.0.0.1:${this.port}` | ||
191 | this.host = `127.0.0.1:${this.port}` | ||
192 | this.hostname = '127.0.0.1' | ||
193 | } | ||
194 | |||
195 | setUrl (url: string) { | ||
196 | const parsed = new URL(url) | ||
197 | |||
198 | this.url = url | ||
199 | this.host = parsed.host | ||
200 | this.hostname = parsed.hostname | ||
201 | this.port = parseInt(parsed.port) | ||
202 | } | ||
203 | |||
204 | getDirectoryPath (directoryName: string) { | ||
205 | const testDirectory = 'test' + this.internalServerNumber | ||
206 | |||
207 | return join(root(), testDirectory, directoryName) | ||
208 | } | ||
209 | |||
210 | async flushAndRun (configOverride?: object, options: RunServerOptions = {}) { | ||
211 | await ServersCommand.flushTests(this.internalServerNumber) | ||
212 | |||
213 | return this.run(configOverride, options) | ||
214 | } | ||
215 | |||
216 | async run (configOverrideArg?: any, options: RunServerOptions = {}) { | ||
217 | // These actions are async so we need to be sure that they have both been done | ||
218 | const serverRunString = { | ||
219 | 'HTTP server listening': false | ||
220 | } | ||
221 | const key = 'Database peertube_test' + this.internalServerNumber + ' is ready' | ||
222 | serverRunString[key] = false | ||
223 | |||
224 | const regexps = { | ||
225 | client_id: 'Client id: (.+)', | ||
226 | client_secret: 'Client secret: (.+)', | ||
227 | user_username: 'Username: (.+)', | ||
228 | user_password: 'User password: (.+)' | ||
229 | } | ||
230 | |||
231 | await this.assignCustomConfigFile() | ||
232 | |||
233 | const configOverride = this.buildConfigOverride() | ||
234 | |||
235 | if (configOverrideArg !== undefined) { | ||
236 | Object.assign(configOverride, configOverrideArg) | ||
237 | } | ||
238 | |||
239 | // Share the environment | ||
240 | const env = { ...process.env } | ||
241 | env['NODE_ENV'] = 'test' | ||
242 | env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString() | ||
243 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | ||
244 | |||
245 | if (options.env) { | ||
246 | Object.assign(env, options.env) | ||
247 | } | ||
248 | |||
249 | const execArgv = options.nodeArgs || [] | ||
250 | // FIXME: too slow :/ | ||
251 | // execArgv.push('--enable-source-maps') | ||
252 | |||
253 | const forkOptions = { | ||
254 | silent: true, | ||
255 | env, | ||
256 | detached: false, | ||
257 | execArgv | ||
258 | } | ||
259 | |||
260 | const peertubeArgs = options.peertubeArgs || [] | ||
261 | |||
262 | return new Promise<void>((res, rej) => { | ||
263 | const self = this | ||
264 | let aggregatedLogs = '' | ||
265 | |||
266 | this.app = fork(join(root(), 'dist', 'server.js'), peertubeArgs, forkOptions) | ||
267 | |||
268 | const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs)) | ||
269 | const onParentExit = () => { | ||
270 | if (!this.app?.pid) return | ||
271 | |||
272 | try { | ||
273 | process.kill(self.app.pid) | ||
274 | } catch { /* empty */ } | ||
275 | } | ||
276 | |||
277 | this.app.on('exit', onPeerTubeExit) | ||
278 | process.on('exit', onParentExit) | ||
279 | |||
280 | this.app.stdout.on('data', function onStdout (data) { | ||
281 | let dontContinue = false | ||
282 | |||
283 | const log: string = data.toString() | ||
284 | aggregatedLogs += log | ||
285 | |||
286 | // Capture things if we want to | ||
287 | for (const key of Object.keys(regexps)) { | ||
288 | const regexp = regexps[key] | ||
289 | const matches = log.match(regexp) | ||
290 | if (matches !== null) { | ||
291 | if (key === 'client_id') self.store.client.id = matches[1] | ||
292 | else if (key === 'client_secret') self.store.client.secret = matches[1] | ||
293 | else if (key === 'user_username') self.store.user.username = matches[1] | ||
294 | else if (key === 'user_password') self.store.user.password = matches[1] | ||
295 | } | ||
296 | } | ||
297 | |||
298 | // Check if all required sentences are here | ||
299 | for (const key of Object.keys(serverRunString)) { | ||
300 | if (log.includes(key)) serverRunString[key] = true | ||
301 | if (serverRunString[key] === false) dontContinue = true | ||
302 | } | ||
303 | |||
304 | // If no, there is maybe one thing not already initialized (client/user credentials generation...) | ||
305 | if (dontContinue === true) return | ||
306 | |||
307 | if (options.hideLogs === false) { | ||
308 | console.log(log) | ||
309 | } else { | ||
310 | process.removeListener('exit', onParentExit) | ||
311 | self.app.stdout.removeListener('data', onStdout) | ||
312 | self.app.removeListener('exit', onPeerTubeExit) | ||
313 | } | ||
314 | |||
315 | res() | ||
316 | }) | ||
317 | }) | ||
318 | } | ||
319 | |||
320 | kill () { | ||
321 | if (!this.app) return Promise.resolve() | ||
322 | |||
323 | process.kill(this.app.pid) | ||
324 | |||
325 | this.app = null | ||
326 | |||
327 | return Promise.resolve() | ||
328 | } | ||
329 | |||
330 | private randomServer () { | ||
331 | const low = 2500 | ||
332 | const high = 10000 | ||
333 | |||
334 | return randomInt(low, high) | ||
335 | } | ||
336 | |||
337 | private randomRTMP () { | ||
338 | const low = 1900 | ||
339 | const high = 2100 | ||
340 | |||
341 | return randomInt(low, high) | ||
342 | } | ||
343 | |||
344 | private async assignCustomConfigFile () { | ||
345 | if (this.internalServerNumber === this.serverNumber) return | ||
346 | |||
347 | const basePath = join(root(), 'config') | ||
348 | |||
349 | const tmpConfigFile = join(basePath, `test-${this.internalServerNumber}.yaml`) | ||
350 | await copy(join(basePath, `test-${this.serverNumber}.yaml`), tmpConfigFile) | ||
351 | |||
352 | this.customConfigFile = tmpConfigFile | ||
353 | } | ||
354 | |||
355 | private buildConfigOverride () { | ||
356 | if (!this.parallel) return {} | ||
357 | |||
358 | return { | ||
359 | listen: { | ||
360 | port: this.port | ||
361 | }, | ||
362 | webserver: { | ||
363 | port: this.port | ||
364 | }, | ||
365 | database: { | ||
366 | suffix: '_test' + this.internalServerNumber | ||
367 | }, | ||
368 | storage: { | ||
369 | tmp: this.getDirectoryPath('tmp') + '/', | ||
370 | tmp_persistent: this.getDirectoryPath('tmp-persistent') + '/', | ||
371 | bin: this.getDirectoryPath('bin') + '/', | ||
372 | avatars: this.getDirectoryPath('avatars') + '/', | ||
373 | web_videos: this.getDirectoryPath('web-videos') + '/', | ||
374 | streaming_playlists: this.getDirectoryPath('streaming-playlists') + '/', | ||
375 | redundancy: this.getDirectoryPath('redundancy') + '/', | ||
376 | logs: this.getDirectoryPath('logs') + '/', | ||
377 | previews: this.getDirectoryPath('previews') + '/', | ||
378 | thumbnails: this.getDirectoryPath('thumbnails') + '/', | ||
379 | storyboards: this.getDirectoryPath('storyboards') + '/', | ||
380 | torrents: this.getDirectoryPath('torrents') + '/', | ||
381 | captions: this.getDirectoryPath('captions') + '/', | ||
382 | cache: this.getDirectoryPath('cache') + '/', | ||
383 | plugins: this.getDirectoryPath('plugins') + '/', | ||
384 | well_known: this.getDirectoryPath('well-known') + '/' | ||
385 | }, | ||
386 | admin: { | ||
387 | email: `admin${this.internalServerNumber}@example.com` | ||
388 | }, | ||
389 | live: { | ||
390 | rtmp: { | ||
391 | port: this.rtmpPort | ||
392 | } | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | private assignCommands () { | ||
398 | this.bulk = new BulkCommand(this) | ||
399 | this.cli = new CLICommand(this) | ||
400 | this.customPage = new CustomPagesCommand(this) | ||
401 | this.feed = new FeedCommand(this) | ||
402 | this.logs = new LogsCommand(this) | ||
403 | this.abuses = new AbusesCommand(this) | ||
404 | this.overviews = new OverviewsCommand(this) | ||
405 | this.search = new SearchCommand(this) | ||
406 | this.contactForm = new ContactFormCommand(this) | ||
407 | this.debug = new DebugCommand(this) | ||
408 | this.follows = new FollowsCommand(this) | ||
409 | this.jobs = new JobsCommand(this) | ||
410 | this.metrics = new MetricsCommand(this) | ||
411 | this.plugins = new PluginsCommand(this) | ||
412 | this.redundancy = new RedundancyCommand(this) | ||
413 | this.stats = new StatsCommand(this) | ||
414 | this.config = new ConfigCommand(this) | ||
415 | this.socketIO = new SocketIOCommand(this) | ||
416 | this.accounts = new AccountsCommand(this) | ||
417 | this.blocklist = new BlocklistCommand(this) | ||
418 | this.subscriptions = new SubscriptionsCommand(this) | ||
419 | this.live = new LiveCommand(this) | ||
420 | this.services = new ServicesCommand(this) | ||
421 | this.blacklist = new BlacklistCommand(this) | ||
422 | this.captions = new CaptionsCommand(this) | ||
423 | this.changeOwnership = new ChangeOwnershipCommand(this) | ||
424 | this.playlists = new PlaylistsCommand(this) | ||
425 | this.history = new HistoryCommand(this) | ||
426 | this.imports = new ImportsCommand(this) | ||
427 | this.channelSyncs = new ChannelSyncsCommand(this) | ||
428 | this.streamingPlaylists = new StreamingPlaylistsCommand(this) | ||
429 | this.channels = new ChannelsCommand(this) | ||
430 | this.comments = new CommentsCommand(this) | ||
431 | this.notifications = new NotificationsCommand(this) | ||
432 | this.servers = new ServersCommand(this) | ||
433 | this.login = new LoginCommand(this) | ||
434 | this.users = new UsersCommand(this) | ||
435 | this.videos = new VideosCommand(this) | ||
436 | this.videoStudio = new VideoStudioCommand(this) | ||
437 | this.videoStats = new VideoStatsCommand(this) | ||
438 | this.views = new ViewsCommand(this) | ||
439 | this.twoFactor = new TwoFactorCommand(this) | ||
440 | this.videoToken = new VideoTokenCommand(this) | ||
441 | this.registrations = new RegistrationsCommand(this) | ||
442 | |||
443 | this.storyboard = new StoryboardCommand(this) | ||
444 | |||
445 | this.runners = new RunnersCommand(this) | ||
446 | this.runnerRegistrationTokens = new RunnerRegistrationTokensCommand(this) | ||
447 | this.runnerJobs = new RunnerJobsCommand(this) | ||
448 | this.videoPasswords = new VideoPasswordsCommand(this) | ||
449 | } | ||
450 | } | ||