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