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