]>
Commit | Line | Data |
---|---|---|
1 | import { ensureDir, readdir, remove } from 'fs-extra' | |
2 | import passwordGenerator from 'password-generator' | |
3 | import { join } from 'path' | |
4 | import { isTestOrDevInstance } from '@server/helpers/core-utils' | |
5 | import { getNodeABIVersion } from '@server/helpers/version' | |
6 | import { UserRole } from '@shared/models' | |
7 | import { logger } from '../helpers/logger' | |
8 | import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user' | |
9 | import { ApplicationModel } from '../models/application/application' | |
10 | import { OAuthClientModel } from '../models/oauth/oauth-client' | |
11 | import { applicationExist, clientsExist, usersExist } from './checker-after-init' | |
12 | import { CONFIG } from './config' | |
13 | import { DIRECTORIES, FILES_CACHE, LAST_MIGRATION_VERSION } from './constants' | |
14 | import { sequelizeTypescript } from './database' | |
15 | ||
16 | async function installApplication () { | |
17 | try { | |
18 | await Promise.all([ | |
19 | // Database related | |
20 | sequelizeTypescript.sync() | |
21 | .then(() => { | |
22 | return Promise.all([ | |
23 | createApplicationIfNotExist(), | |
24 | createOAuthClientIfNotExist(), | |
25 | createOAuthAdminIfNotExist() | |
26 | ]) | |
27 | }), | |
28 | ||
29 | // Directories | |
30 | removeCacheAndTmpDirectories() | |
31 | .then(() => createDirectoriesIfNotExist()) | |
32 | ]) | |
33 | } catch (err) { | |
34 | logger.error('Cannot install application.', { err }) | |
35 | process.exit(-1) | |
36 | } | |
37 | } | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | export { | |
42 | installApplication | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | function removeCacheAndTmpDirectories () { | |
48 | const cacheDirectories = Object.keys(FILES_CACHE) | |
49 | .map(k => FILES_CACHE[k].DIRECTORY) | |
50 | ||
51 | const tasks: Promise<any>[] = [] | |
52 | ||
53 | // Cache directories | |
54 | for (const dir of cacheDirectories) { | |
55 | tasks.push(removeDirectoryOrContent(dir)) | |
56 | } | |
57 | ||
58 | tasks.push(removeDirectoryOrContent(CONFIG.STORAGE.TMP_DIR)) | |
59 | ||
60 | return Promise.all(tasks) | |
61 | } | |
62 | ||
63 | async function removeDirectoryOrContent (dir: string) { | |
64 | try { | |
65 | await remove(dir) | |
66 | } catch (err) { | |
67 | logger.debug('Cannot remove directory %s. Removing content instead.', dir, { err }) | |
68 | ||
69 | const files = await readdir(dir) | |
70 | ||
71 | for (const file of files) { | |
72 | await remove(join(dir, file)) | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | function createDirectoriesIfNotExist () { | |
78 | const storage = CONFIG.STORAGE | |
79 | const cacheDirectories = Object.keys(FILES_CACHE) | |
80 | .map(k => FILES_CACHE[k].DIRECTORY) | |
81 | ||
82 | const tasks: Promise<void>[] = [] | |
83 | for (const key of Object.keys(storage)) { | |
84 | const dir = storage[key] | |
85 | tasks.push(ensureDir(dir)) | |
86 | } | |
87 | ||
88 | // Cache directories | |
89 | for (const dir of cacheDirectories) { | |
90 | tasks.push(ensureDir(dir)) | |
91 | } | |
92 | ||
93 | tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE)) | |
94 | tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC)) | |
95 | tasks.push(ensureDir(DIRECTORIES.VIDEOS.PUBLIC)) | |
96 | tasks.push(ensureDir(DIRECTORIES.VIDEOS.PRIVATE)) | |
97 | ||
98 | // Resumable upload directory | |
99 | tasks.push(ensureDir(DIRECTORIES.RESUMABLE_UPLOAD)) | |
100 | ||
101 | return Promise.all(tasks) | |
102 | } | |
103 | ||
104 | async function createOAuthClientIfNotExist () { | |
105 | const exist = await clientsExist() | |
106 | // Nothing to do, clients already exist | |
107 | if (exist === true) return undefined | |
108 | ||
109 | logger.info('Creating a default OAuth Client.') | |
110 | ||
111 | const id = passwordGenerator(32, false, /[a-z0-9]/) | |
112 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
113 | const client = new OAuthClientModel({ | |
114 | clientId: id, | |
115 | clientSecret: secret, | |
116 | grants: [ 'password', 'refresh_token' ], | |
117 | redirectUris: null | |
118 | }) | |
119 | ||
120 | const createdClient = await client.save() | |
121 | logger.info('Client id: ' + createdClient.clientId) | |
122 | logger.info('Client secret: ' + createdClient.clientSecret) | |
123 | ||
124 | return undefined | |
125 | } | |
126 | ||
127 | async function createOAuthAdminIfNotExist () { | |
128 | const exist = await usersExist() | |
129 | // Nothing to do, users already exist | |
130 | if (exist === true) return undefined | |
131 | ||
132 | logger.info('Creating the administrator.') | |
133 | ||
134 | const username = 'root' | |
135 | const role = UserRole.ADMINISTRATOR | |
136 | const email = CONFIG.ADMIN.EMAIL | |
137 | let validatePassword = true | |
138 | let password = '' | |
139 | ||
140 | // Do not generate a random password for test and dev environments | |
141 | if (isTestOrDevInstance()) { | |
142 | password = 'test' | |
143 | ||
144 | if (process.env.NODE_APP_INSTANCE) { | |
145 | password += process.env.NODE_APP_INSTANCE | |
146 | } | |
147 | ||
148 | // Our password is weak so do not validate it | |
149 | validatePassword = false | |
150 | } else if (process.env.PT_INITIAL_ROOT_PASSWORD) { | |
151 | password = process.env.PT_INITIAL_ROOT_PASSWORD | |
152 | } else { | |
153 | password = passwordGenerator(16, true) | |
154 | } | |
155 | ||
156 | const user = buildUser({ | |
157 | username, | |
158 | email, | |
159 | password, | |
160 | role, | |
161 | emailVerified: true, | |
162 | videoQuota: -1, | |
163 | videoQuotaDaily: -1 | |
164 | }) | |
165 | ||
166 | await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword }) | |
167 | logger.info('Username: ' + username) | |
168 | logger.info('User password: ' + password) | |
169 | } | |
170 | ||
171 | async function createApplicationIfNotExist () { | |
172 | const exist = await applicationExist() | |
173 | // Nothing to do, application already exist | |
174 | if (exist === true) return undefined | |
175 | ||
176 | logger.info('Creating application account.') | |
177 | ||
178 | const application = await ApplicationModel.create({ | |
179 | migrationVersion: LAST_MIGRATION_VERSION, | |
180 | nodeVersion: process.version, | |
181 | nodeABIVersion: getNodeABIVersion() | |
182 | }) | |
183 | ||
184 | return createApplicationActor(application.id) | |
185 | } |