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