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