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