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