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