]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Generate application keys too
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
4d4e5cd4 1import * as passwordGenerator from 'password-generator'
571389d4
C
2import { UserRole } from '../../shared'
3import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
571389d4 4import { createUserAccountAndChannel } from '../lib'
47e0652b 5import { createLocalAccountWithoutKeys } from '../lib/user'
350e31d6
C
6import { applicationExist, clientsExist, usersExist } from './checker'
7import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
e02643f3 8import { database as db } from './database'
1b3989b0 9import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
6fcd19ba 10
f5028693 11async function installApplication () {
e34c85e5
C
12 try {
13 await db.sequelize.sync()
14 await removeCacheDirectories()
15 await createDirectoriesIfNotExist()
efc32059 16 await createApplicationIfNotExist()
e34c85e5
C
17 await createOAuthClientIfNotExist()
18 await createOAuthAdminIfNotExist()
e34c85e5
C
19 } catch (err) {
20 logger.error('Cannot install application.', err)
21 throw err
22 }
37dc07b2
C
23}
24
25// ---------------------------------------------------------------------------
26
65fcc311
C
27export {
28 installApplication
29}
37dc07b2
C
30
31// ---------------------------------------------------------------------------
32
f981dae8
C
33function removeCacheDirectories () {
34 const cacheDirectories = CACHE.DIRECTORIES
35
571389d4 36 const tasks: Promise<any>[] = []
f981dae8
C
37
38 // Cache directories
f5028693 39 for (const key of Object.keys(cacheDirectories)) {
f981dae8
C
40 const dir = cacheDirectories[key]
41 tasks.push(rimrafPromise(dir))
f5028693 42 }
f981dae8
C
43
44 return Promise.all(tasks)
45}
46
6fcd19ba 47function createDirectoriesIfNotExist () {
b0f9f39e 48 const storage = CONFIG.STORAGE
f981dae8 49 const cacheDirectories = CACHE.DIRECTORIES
37dc07b2 50
6fcd19ba 51 const tasks = []
f5028693 52 for (const key of Object.keys(storage)) {
b0f9f39e 53 const dir = storage[key]
f981dae8 54 tasks.push(mkdirpPromise(dir))
f5028693 55 }
f981dae8
C
56
57 // Cache directories
f5028693 58 for (const key of Object.keys(cacheDirectories)) {
f981dae8
C
59 const dir = cacheDirectories[key]
60 tasks.push(mkdirpPromise(dir))
f5028693 61 }
37dc07b2 62
6fcd19ba
C
63 return Promise.all(tasks)
64}
37dc07b2 65
f5028693
C
66async function createOAuthClientIfNotExist () {
67 const exist = await clientsExist(db.OAuthClient)
68 // Nothing to do, clients already exist
69 if (exist === true) return undefined
69b0a27c 70
f5028693 71 logger.info('Creating a default OAuth Client.')
37dc07b2 72
f5028693
C
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
37dc07b2 80 })
37dc07b2 81
f5028693
C
82 const createdClient = await client.save()
83 logger.info('Client id: ' + createdClient.clientId)
84 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 85
f5028693
C
86 return undefined
87}
37dc07b2 88
f5028693
C
89async function createOAuthAdminIfNotExist () {
90 const exist = await usersExist(db.User)
91 // Nothing to do, users already exist
92 if (exist === true) return undefined
d14b3e37 93
f5028693 94 logger.info('Creating the administrator.')
d14b3e37 95
f5028693 96 const username = 'root'
954605a8 97 const role = UserRole.ADMINISTRATOR
f5028693
C
98 const email = CONFIG.ADMIN.EMAIL
99 let validatePassword = true
100 let password = ''
67bf9b96 101
f5028693
C
102 // Do not generate a random password for tests
103 if (process.env.NODE_ENV === 'test') {
104 password = 'test'
37dc07b2 105
f5028693
C
106 if (process.env.NODE_APP_INSTANCE) {
107 password += process.env.NODE_APP_INSTANCE
67bf9b96 108 }
69b0a27c 109
f5028693
C
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
38fa2065 125 await createUserAccountAndChannel(user, validatePassword)
f5028693
C
126 logger.info('Username: ' + username)
127 logger.info('User password: ' + password)
571389d4 128}
f5028693 129
571389d4 130async function createApplicationIfNotExist () {
350e31d6
C
131 const exist = await applicationExist(db.Application)
132 // Nothing to do, application already exist
133 if (exist === true) return undefined
134
f5028693 135 logger.info('Creating Application table.')
571389d4
C
136 const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
137
138 logger.info('Creating application account.')
47e0652b 139
1b3989b0
C
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()
37dc07b2 147}