]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
Generate application keys too
[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 import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
10
11 async function installApplication () {
12 try {
13 await db.sequelize.sync()
14 await removeCacheDirectories()
15 await createDirectoriesIfNotExist()
16 await createApplicationIfNotExist()
17 await createOAuthClientIfNotExist()
18 await createOAuthAdminIfNotExist()
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 const exist = await applicationExist(db.Application)
132 // Nothing to do, application already exist
133 if (exist === true) return undefined
134
135 logger.info('Creating Application table.')
136 const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
137
138 logger.info('Creating application account.')
139
140 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
141
142 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
143 accountCreated.set('publicKey', publicKey)
144 accountCreated.set('privateKey', privateKey)
145
146 return accountCreated.save()
147 }