]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as passwordGenerator from 'password-generator' |
6fcd19ba | 2 | import * as Promise from 'bluebird' |
37dc07b2 | 3 | |
e02643f3 | 4 | import { database as db } from './database' |
f981dae8 | 5 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' |
65fcc311 | 6 | import { clientsExist, usersExist } from './checker' |
0b7db72a | 7 | import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' |
72c7248b | 8 | import { createUserAuthorAndChannel } from '../lib' |
6fcd19ba C |
9 | |
10 | function installApplication () { | |
11 | return db.sequelize.sync() | |
f981dae8 | 12 | .then(() => removeCacheDirectories()) |
6fcd19ba C |
13 | .then(() => createDirectoriesIfNotExist()) |
14 | .then(() => createCertsIfNotExist()) | |
15 | .then(() => createOAuthClientIfNotExist()) | |
16 | .then(() => createOAuthAdminIfNotExist()) | |
37dc07b2 C |
17 | } |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
65fcc311 C |
21 | export { |
22 | installApplication | |
23 | } | |
37dc07b2 C |
24 | |
25 | // --------------------------------------------------------------------------- | |
26 | ||
f981dae8 C |
27 | function removeCacheDirectories () { |
28 | const cacheDirectories = CACHE.DIRECTORIES | |
29 | ||
30 | const tasks = [] | |
31 | ||
32 | // Cache directories | |
33 | Object.keys(cacheDirectories).forEach(key => { | |
34 | const dir = cacheDirectories[key] | |
35 | tasks.push(rimrafPromise(dir)) | |
36 | }) | |
37 | ||
38 | return Promise.all(tasks) | |
39 | } | |
40 | ||
6fcd19ba | 41 | function createDirectoriesIfNotExist () { |
b0f9f39e | 42 | const storage = CONFIG.STORAGE |
f981dae8 | 43 | const cacheDirectories = CACHE.DIRECTORIES |
37dc07b2 | 44 | |
6fcd19ba | 45 | const tasks = [] |
b0f9f39e C |
46 | Object.keys(storage).forEach(key => { |
47 | const dir = storage[key] | |
f981dae8 C |
48 | tasks.push(mkdirpPromise(dir)) |
49 | }) | |
50 | ||
51 | // Cache directories | |
52 | Object.keys(cacheDirectories).forEach(key => { | |
53 | const dir = cacheDirectories[key] | |
54 | tasks.push(mkdirpPromise(dir)) | |
6fcd19ba | 55 | }) |
37dc07b2 | 56 | |
6fcd19ba C |
57 | return Promise.all(tasks) |
58 | } | |
37dc07b2 | 59 | |
6fcd19ba | 60 | function createOAuthClientIfNotExist () { |
3482688c | 61 | return clientsExist(db.OAuthClient).then(exist => { |
37dc07b2 | 62 | // Nothing to do, clients already exist |
6fcd19ba | 63 | if (exist === true) return undefined |
37dc07b2 C |
64 | |
65 | logger.info('Creating a default OAuth Client.') | |
66 | ||
feb4bdfd C |
67 | const id = passwordGenerator(32, false, /[a-z0-9]/) |
68 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
69 | const client = db.OAuthClient.build({ | |
70 | clientId: id, | |
69b0a27c | 71 | clientSecret: secret, |
e02643f3 C |
72 | grants: [ 'password', 'refresh_token' ], |
73 | redirectUris: null | |
69b0a27c C |
74 | }) |
75 | ||
6fcd19ba | 76 | return client.save().then(createdClient => { |
feb4bdfd | 77 | logger.info('Client id: ' + createdClient.clientId) |
69b0a27c | 78 | logger.info('Client secret: ' + createdClient.clientSecret) |
37dc07b2 | 79 | |
6fcd19ba | 80 | return undefined |
37dc07b2 C |
81 | }) |
82 | }) | |
83 | } | |
84 | ||
6fcd19ba | 85 | function createOAuthAdminIfNotExist () { |
3482688c | 86 | return usersExist(db.User).then(exist => { |
37dc07b2 | 87 | // Nothing to do, users already exist |
6fcd19ba | 88 | if (exist === true) return undefined |
37dc07b2 C |
89 | |
90 | logger.info('Creating the administrator.') | |
91 | ||
bb1e6d0c | 92 | const username = 'root' |
65fcc311 C |
93 | const role = USER_ROLES.ADMIN |
94 | const email = CONFIG.ADMIN.EMAIL | |
72c7248b | 95 | let validatePassword = true |
d14b3e37 C |
96 | let password = '' |
97 | ||
98 | // Do not generate a random password for tests | |
99 | if (process.env.NODE_ENV === 'test') { | |
100 | password = 'test' | |
101 | ||
102 | if (process.env.NODE_APP_INSTANCE) { | |
103 | password += process.env.NODE_APP_INSTANCE | |
104 | } | |
67bf9b96 C |
105 | |
106 | // Our password is weak so do not validate it | |
72c7248b | 107 | validatePassword = false |
d14b3e37 C |
108 | } else { |
109 | password = passwordGenerator(8, true) | |
110 | } | |
37dc07b2 | 111 | |
67bf9b96 | 112 | const userData = { |
c4403b29 | 113 | username, |
ad4a8a1c | 114 | email, |
c4403b29 | 115 | password, |
b0f9f39e C |
116 | role, |
117 | videoQuota: -1 | |
67bf9b96 | 118 | } |
72c7248b | 119 | const user = db.User.build(userData) |
69b0a27c | 120 | |
72c7248b C |
121 | return createUserAuthorAndChannel(user, validatePassword) |
122 | .then(({ user }) => { | |
123 | logger.info('Username: ' + username) | |
124 | logger.info('User password: ' + password) | |
37dc07b2 | 125 | |
72c7248b C |
126 | logger.info('Creating Application table.') |
127 | return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) | |
128 | }) | |
37dc07b2 C |
129 | }) |
130 | } |