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