]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as passwordGenerator from 'password-generator' |
571389d4 | 2 | import { UserRole } from '../../shared' |
da854ddd C |
3 | import { mkdirpPromise, rimrafPromise } from '../helpers/core-utils' |
4 | import { logger } from '../helpers/logger' | |
50d6de9c | 5 | import { createApplicationActor, createUserAccountAndChannel } from '../lib/user' |
3fd3ab2d C |
6 | import { UserModel } from '../models/account/user' |
7 | import { ApplicationModel } from '../models/application/application' | |
8 | import { OAuthClientModel } from '../models/oauth/oauth-client' | |
350e31d6 | 9 | import { applicationExist, clientsExist, usersExist } from './checker' |
50d6de9c | 10 | import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants' |
3fd3ab2d | 11 | import { sequelizeTypescript } from './database' |
6fcd19ba | 12 | |
f5028693 | 13 | async function installApplication () { |
e34c85e5 | 14 | try { |
3fd3ab2d | 15 | await sequelizeTypescript.sync() |
e34c85e5 C |
16 | await removeCacheDirectories() |
17 | await createDirectoriesIfNotExist() | |
efc32059 | 18 | await createApplicationIfNotExist() |
e34c85e5 C |
19 | await createOAuthClientIfNotExist() |
20 | await createOAuthAdminIfNotExist() | |
e34c85e5 C |
21 | } catch (err) { |
22 | logger.error('Cannot install application.', err) | |
2ccaeeb3 | 23 | process.exit(-1) |
e34c85e5 | 24 | } |
37dc07b2 C |
25 | } |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
65fcc311 C |
29 | export { |
30 | installApplication | |
31 | } | |
37dc07b2 C |
32 | |
33 | // --------------------------------------------------------------------------- | |
34 | ||
f981dae8 C |
35 | function removeCacheDirectories () { |
36 | const cacheDirectories = CACHE.DIRECTORIES | |
37 | ||
571389d4 | 38 | const tasks: Promise<any>[] = [] |
f981dae8 C |
39 | |
40 | // Cache directories | |
f5028693 | 41 | for (const key of Object.keys(cacheDirectories)) { |
f981dae8 C |
42 | const dir = cacheDirectories[key] |
43 | tasks.push(rimrafPromise(dir)) | |
f5028693 | 44 | } |
f981dae8 C |
45 | |
46 | return Promise.all(tasks) | |
47 | } | |
48 | ||
6fcd19ba | 49 | function createDirectoriesIfNotExist () { |
b0f9f39e | 50 | const storage = CONFIG.STORAGE |
f981dae8 | 51 | const cacheDirectories = CACHE.DIRECTORIES |
37dc07b2 | 52 | |
6fcd19ba | 53 | const tasks = [] |
f5028693 | 54 | for (const key of Object.keys(storage)) { |
b0f9f39e | 55 | const dir = storage[key] |
f981dae8 | 56 | tasks.push(mkdirpPromise(dir)) |
f5028693 | 57 | } |
f981dae8 C |
58 | |
59 | // Cache directories | |
f5028693 | 60 | for (const key of Object.keys(cacheDirectories)) { |
f981dae8 C |
61 | const dir = cacheDirectories[key] |
62 | tasks.push(mkdirpPromise(dir)) | |
f5028693 | 63 | } |
37dc07b2 | 64 | |
6fcd19ba C |
65 | return Promise.all(tasks) |
66 | } | |
37dc07b2 | 67 | |
f5028693 | 68 | async function createOAuthClientIfNotExist () { |
3fd3ab2d | 69 | const exist = await clientsExist() |
f5028693 C |
70 | // Nothing to do, clients already exist |
71 | if (exist === true) return undefined | |
69b0a27c | 72 | |
f5028693 | 73 | logger.info('Creating a default OAuth Client.') |
37dc07b2 | 74 | |
f5028693 C |
75 | const id = passwordGenerator(32, false, /[a-z0-9]/) |
76 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
3fd3ab2d | 77 | const client = new OAuthClientModel({ |
f5028693 C |
78 | clientId: id, |
79 | clientSecret: secret, | |
80 | grants: [ 'password', 'refresh_token' ], | |
81 | redirectUris: null | |
37dc07b2 | 82 | }) |
37dc07b2 | 83 | |
f5028693 C |
84 | const createdClient = await client.save() |
85 | logger.info('Client id: ' + createdClient.clientId) | |
86 | logger.info('Client secret: ' + createdClient.clientSecret) | |
37dc07b2 | 87 | |
f5028693 C |
88 | return undefined |
89 | } | |
37dc07b2 | 90 | |
f5028693 | 91 | async function createOAuthAdminIfNotExist () { |
3fd3ab2d | 92 | const exist = await usersExist() |
f5028693 C |
93 | // Nothing to do, users already exist |
94 | if (exist === true) return undefined | |
d14b3e37 | 95 | |
f5028693 | 96 | logger.info('Creating the administrator.') |
d14b3e37 | 97 | |
f5028693 | 98 | const username = 'root' |
954605a8 | 99 | const role = UserRole.ADMINISTRATOR |
f5028693 C |
100 | const email = CONFIG.ADMIN.EMAIL |
101 | let validatePassword = true | |
102 | let password = '' | |
67bf9b96 | 103 | |
f5028693 C |
104 | // Do not generate a random password for tests |
105 | if (process.env.NODE_ENV === 'test') { | |
106 | password = 'test' | |
37dc07b2 | 107 | |
f5028693 C |
108 | if (process.env.NODE_APP_INSTANCE) { |
109 | password += process.env.NODE_APP_INSTANCE | |
67bf9b96 | 110 | } |
69b0a27c | 111 | |
f5028693 C |
112 | // Our password is weak so do not validate it |
113 | validatePassword = false | |
114 | } else { | |
115 | password = passwordGenerator(8, true) | |
116 | } | |
117 | ||
118 | const userData = { | |
119 | username, | |
120 | email, | |
121 | password, | |
122 | role, | |
123 | videoQuota: -1 | |
124 | } | |
3fd3ab2d | 125 | const user = new UserModel(userData) |
f5028693 | 126 | |
38fa2065 | 127 | await createUserAccountAndChannel(user, validatePassword) |
f5028693 C |
128 | logger.info('Username: ' + username) |
129 | logger.info('User password: ' + password) | |
571389d4 | 130 | } |
f5028693 | 131 | |
571389d4 | 132 | async function createApplicationIfNotExist () { |
3fd3ab2d | 133 | const exist = await applicationExist() |
350e31d6 C |
134 | // Nothing to do, application already exist |
135 | if (exist === true) return undefined | |
136 | ||
f5028693 | 137 | logger.info('Creating Application table.') |
571389d4 C |
138 | |
139 | logger.info('Creating application account.') | |
47e0652b | 140 | |
50d6de9c C |
141 | const application = await ApplicationModel.create({ |
142 | migrationVersion: LAST_MIGRATION_VERSION | |
143 | }) | |
1b3989b0 | 144 | |
50d6de9c | 145 | return createApplicationActor(application.id) |
37dc07b2 | 146 | } |