]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker-before-init.ts
79430374399ebda9ad0bc11d6dc077f0464be1a7
[github/Chocobozzz/PeerTube.git] / server / initializers / checker-before-init.ts
1 import { IConfig } from 'config'
2 import { parseSemVersion, promisify0 } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
4
5 // Special behaviour for config because we can reload it
6 const config: IConfig = require('config')
7
8 // ONLY USE CORE MODULES IN THIS FILE!
9
10 // Check the config files
11 function checkMissedConfig () {
12 const required = [ 'listen.port', 'listen.hostname',
13 'webserver.https', 'webserver.hostname', 'webserver.port',
14 'trust_proxy',
15 'database.hostname', 'database.port', 'database.username', 'database.password', 'database.pool.max',
16 'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
17 'email.body.signature', 'email.subject.prefix',
18 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
19 'storage.redundancy', 'storage.tmp', 'storage.streaming_playlists', 'storage.plugins',
20 'log.level',
21 'user.video_quota', 'user.video_quota_daily',
22 'video_channels.max_per_user',
23 'csp.enabled', 'csp.report_only', 'csp.report_uri',
24 'security.frameguard.enabled',
25 'cache.previews.size', 'cache.captions.size', 'cache.torrents.size', 'admin.email', 'contact_form.enabled',
26 'signup.enabled', 'signup.limit', 'signup.requires_email_verification', 'signup.minimum_age',
27 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
28 'redundancy.videos.strategies', 'redundancy.videos.check_interval',
29 'transcoding.enabled', 'transcoding.threads', 'transcoding.allow_additional_extensions', 'transcoding.hls.enabled',
30 'transcoding.profile', 'transcoding.concurrency',
31 'transcoding.resolutions.0p', 'transcoding.resolutions.144p', 'transcoding.resolutions.240p', 'transcoding.resolutions.360p',
32 'transcoding.resolutions.480p', 'transcoding.resolutions.720p', 'transcoding.resolutions.1080p', 'transcoding.resolutions.1440p',
33 'transcoding.resolutions.2160p', 'video_studio.enabled',
34 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'import.videos.concurrency', 'auto_blacklist.videos.of_users.enabled',
35 'trending.videos.interval_days',
36 'client.videos.miniature.display_author_avatar',
37 'client.videos.miniature.prefer_author_display_name', 'client.menu.login.redirect_on_single_external_auth',
38 'defaults.publish.download_enabled', 'defaults.publish.comments_enabled', 'defaults.publish.privacy', 'defaults.publish.licence',
39 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
40 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
41 'services.twitter.username', 'services.twitter.whitelisted',
42 'followers.instance.enabled', 'followers.instance.manual_approval',
43 'tracker.enabled', 'tracker.private', 'tracker.reject_too_many_announces',
44 'history.videos.max_age', 'views.videos.remote.max_age', 'views.videos.local_buffer_update_interval', 'views.videos.ip_view_expiration',
45 'rates_limit.login.window', 'rates_limit.login.max', 'rates_limit.ask_send_email.window', 'rates_limit.ask_send_email.max',
46 'theme.default',
47 'geo_ip.enabled', 'geo_ip.country.database_url',
48 'remote_redundancy.videos.accept_from',
49 'federation.videos.federate_unlisted', 'federation.videos.cleanup_remote_interactions',
50 'peertube.check_latest_version.enabled', 'peertube.check_latest_version.url',
51 'search.remote_uri.users', 'search.remote_uri.anonymous', 'search.search_index.enabled', 'search.search_index.url',
52 'search.search_index.disable_local_search', 'search.search_index.is_default_search',
53 'live.enabled', 'live.allow_replay', 'live.latency_setting.enabled', 'live.max_duration',
54 'live.max_user_lives', 'live.max_instance_lives',
55 'live.rtmp.enabled', 'live.rtmp.port', 'live.rtmp.hostname', 'live.rtmp.public_hostname',
56 'live.rtmps.enabled', 'live.rtmps.port', 'live.rtmps.hostname', 'live.rtmps.public_hostname',
57 'live.rtmps.key_file', 'live.rtmps.cert_file',
58 'live.transcoding.enabled', 'live.transcoding.threads', 'live.transcoding.profile',
59 'live.transcoding.resolutions.144p', 'live.transcoding.resolutions.240p', 'live.transcoding.resolutions.360p',
60 'live.transcoding.resolutions.480p', 'live.transcoding.resolutions.720p', 'live.transcoding.resolutions.1080p',
61 'live.transcoding.resolutions.1440p', 'live.transcoding.resolutions.2160p'
62 ]
63
64 const requiredAlternatives = [
65 [ // set
66 [ 'redis.hostname', 'redis.port' ], // alternative
67 [ 'redis.socket' ]
68 ]
69 ]
70 const miss: string[] = []
71
72 for (const key of required) {
73 if (!config.has(key)) {
74 miss.push(key)
75 }
76 }
77
78 const redundancyVideos = config.get<any>('redundancy.videos.strategies')
79
80 if (Array.isArray(redundancyVideos)) {
81 for (const r of redundancyVideos) {
82 if (!r.size) miss.push('redundancy.videos.strategies.size')
83 if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
84 }
85 }
86
87 const missingAlternatives = requiredAlternatives.filter(
88 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
89 )
90
91 missingAlternatives
92 .forEach(set => set[0].forEach(key => miss.push(key)))
93
94 return miss
95 }
96
97 // Check the available codecs
98 // We get CONFIG by param to not import it in this file (import orders)
99 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
100 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
101
102 const Ffmpeg = require('fluent-ffmpeg')
103 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
104 const codecs = await getAvailableCodecsPromise()
105 const canEncode = [ 'libx264' ]
106
107 for (const codec of canEncode) {
108 if (codecs[codec] === undefined) {
109 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
110 }
111
112 if (codecs[codec].canEncode !== true) {
113 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
114 }
115 }
116 }
117
118 function checkNodeVersion () {
119 const v = process.version
120 const { major } = parseSemVersion(v)
121
122 logger.debug('Checking NodeJS version %s.', v)
123
124 if (major <= 12) {
125 throw new Error('Your NodeJS version ' + v + ' is not supported. Please upgrade.')
126 }
127 }
128
129 // ---------------------------------------------------------------------------
130
131 export {
132 checkFFmpeg,
133 checkMissedConfig,
134 checkNodeVersion
135 }