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