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