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