]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as passwordGenerator from 'password-generator' |
571389d4 | 2 | import { UserRole } from '../../shared' |
da854ddd | 3 | import { logger } from '../helpers/logger' |
df0b219d | 4 | import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user' |
3fd3ab2d C |
5 | import { UserModel } from '../models/account/user' |
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' |
6dd9de95 | 9 | import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants' |
3fd3ab2d | 10 | import { sequelizeTypescript } from './database' |
6dd9de95 C |
11 | import { ensureDir, remove } from 'fs-extra' |
12 | import { CONFIG } from './config' | |
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] |
62689b94 | 54 | tasks.push(remove(dir)) |
f5028693 | 55 | } |
f981dae8 | 56 | |
f4800714 C |
57 | tasks.push(remove(CONFIG.STORAGE.TMP_DIR)) |
58 | ||
f981dae8 C |
59 | return Promise.all(tasks) |
60 | } | |
61 | ||
6fcd19ba | 62 | function createDirectoriesIfNotExist () { |
b0f9f39e | 63 | const storage = CONFIG.STORAGE |
d74d29ad C |
64 | const cacheDirectories = Object.keys(FILES_CACHE) |
65 | .map(k => FILES_CACHE[k].DIRECTORY) | |
37dc07b2 | 66 | |
62689b94 | 67 | const tasks: Promise<void>[] = [] |
f5028693 | 68 | for (const key of Object.keys(storage)) { |
b0f9f39e | 69 | const dir = storage[key] |
62689b94 | 70 | tasks.push(ensureDir(dir)) |
f5028693 | 71 | } |
f981dae8 C |
72 | |
73 | // Cache directories | |
f5028693 | 74 | for (const key of Object.keys(cacheDirectories)) { |
f981dae8 | 75 | const dir = cacheDirectories[key] |
62689b94 | 76 | tasks.push(ensureDir(dir)) |
f5028693 | 77 | } |
37dc07b2 | 78 | |
09209296 | 79 | // Playlist directories |
9c6ca37f | 80 | tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY)) |
09209296 | 81 | |
6fcd19ba C |
82 | return Promise.all(tasks) |
83 | } | |
37dc07b2 | 84 | |
f5028693 | 85 | async function createOAuthClientIfNotExist () { |
3fd3ab2d | 86 | const exist = await clientsExist() |
f5028693 C |
87 | // Nothing to do, clients already exist |
88 | if (exist === true) return undefined | |
69b0a27c | 89 | |
f5028693 | 90 | logger.info('Creating a default OAuth Client.') |
37dc07b2 | 91 | |
f5028693 C |
92 | const id = passwordGenerator(32, false, /[a-z0-9]/) |
93 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
3fd3ab2d | 94 | const client = new OAuthClientModel({ |
f5028693 C |
95 | clientId: id, |
96 | clientSecret: secret, | |
97 | grants: [ 'password', 'refresh_token' ], | |
98 | redirectUris: null | |
37dc07b2 | 99 | }) |
37dc07b2 | 100 | |
f5028693 C |
101 | const createdClient = await client.save() |
102 | logger.info('Client id: ' + createdClient.clientId) | |
103 | logger.info('Client secret: ' + createdClient.clientSecret) | |
37dc07b2 | 104 | |
f5028693 C |
105 | return undefined |
106 | } | |
37dc07b2 | 107 | |
f5028693 | 108 | async function createOAuthAdminIfNotExist () { |
3fd3ab2d | 109 | const exist = await usersExist() |
f5028693 C |
110 | // Nothing to do, users already exist |
111 | if (exist === true) return undefined | |
d14b3e37 | 112 | |
f5028693 | 113 | logger.info('Creating the administrator.') |
d14b3e37 | 114 | |
f5028693 | 115 | const username = 'root' |
954605a8 | 116 | const role = UserRole.ADMINISTRATOR |
f5028693 C |
117 | const email = CONFIG.ADMIN.EMAIL |
118 | let validatePassword = true | |
119 | let password = '' | |
67bf9b96 | 120 | |
f5028693 C |
121 | // Do not generate a random password for tests |
122 | if (process.env.NODE_ENV === 'test') { | |
123 | password = 'test' | |
37dc07b2 | 124 | |
f5028693 C |
125 | if (process.env.NODE_APP_INSTANCE) { |
126 | password += process.env.NODE_APP_INSTANCE | |
67bf9b96 | 127 | } |
69b0a27c | 128 | |
f5028693 C |
129 | // Our password is weak so do not validate it |
130 | validatePassword = false | |
3daaa192 AV |
131 | } else if (process.env.PT_INITIAL_ROOT_PASSWORD) { |
132 | password = process.env.PT_INITIAL_ROOT_PASSWORD | |
f5028693 | 133 | } else { |
490b595a | 134 | password = passwordGenerator(16, true) |
f5028693 C |
135 | } |
136 | ||
137 | const userData = { | |
138 | username, | |
139 | email, | |
140 | password, | |
141 | role, | |
d9eaee39 | 142 | verified: true, |
0883b324 | 143 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
bee0abff FA |
144 | videoQuota: -1, |
145 | videoQuotaDaily: -1 | |
f5028693 | 146 | } |
3fd3ab2d | 147 | const user = new UserModel(userData) |
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 | } |