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