]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
4d4e5cd4 1import * as passwordGenerator from 'password-generator'
571389d4 2import { UserRole } from '../../shared'
da854ddd 3import { logger } from '../helpers/logger'
df0b219d 4import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
3fd3ab2d
C
5import { UserModel } from '../models/account/user'
6import { ApplicationModel } from '../models/application/application'
7import { OAuthClientModel } from '../models/oauth/oauth-client'
e5565833 8import { applicationExist, clientsExist, usersExist } from './checker-after-init'
6dd9de95 9import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
3fd3ab2d 10import { sequelizeTypescript } from './database'
6dd9de95
C
11import { ensureDir, remove } from 'fs-extra'
12import { CONFIG } from './config'
6fcd19ba 13
f5028693 14async function installApplication () {
e34c85e5 15 try {
0b2f03d3
C
16 await Promise.all([
17 // Database related
18 sequelizeTypescript.sync()
19 .then(() => {
20 return Promise.all([
21 createApplicationIfNotExist(),
22 createOAuthClientIfNotExist(),
23 createOAuthAdminIfNotExist()
24 ])
25 }),
26
27 // Directories
f4800714 28 removeCacheAndTmpDirectories()
0b2f03d3
C
29 .then(() => createDirectoriesIfNotExist())
30 ])
e34c85e5 31 } catch (err) {
d5b7d911 32 logger.error('Cannot install application.', { err })
2ccaeeb3 33 process.exit(-1)
e34c85e5 34 }
37dc07b2
C
35}
36
37// ---------------------------------------------------------------------------
38
65fcc311
C
39export {
40 installApplication
41}
37dc07b2
C
42
43// ---------------------------------------------------------------------------
44
f4800714 45function removeCacheAndTmpDirectories () {
d74d29ad
C
46 const cacheDirectories = Object.keys(FILES_CACHE)
47 .map(k => FILES_CACHE[k].DIRECTORY)
f981dae8 48
571389d4 49 const tasks: Promise<any>[] = []
f981dae8
C
50
51 // Cache directories
f5028693 52 for (const key of Object.keys(cacheDirectories)) {
f981dae8 53 const dir = cacheDirectories[key]
62689b94 54 tasks.push(remove(dir))
f5028693 55 }
f981dae8 56
f4800714
C
57 tasks.push(remove(CONFIG.STORAGE.TMP_DIR))
58
f981dae8
C
59 return Promise.all(tasks)
60}
61
6fcd19ba 62function createDirectoriesIfNotExist () {
b0f9f39e 63 const storage = CONFIG.STORAGE
d74d29ad
C
64 const cacheDirectories = Object.keys(FILES_CACHE)
65 .map(k => FILES_CACHE[k].DIRECTORY)
37dc07b2 66
62689b94 67 const tasks: Promise<void>[] = []
f5028693 68 for (const key of Object.keys(storage)) {
b0f9f39e 69 const dir = storage[key]
62689b94 70 tasks.push(ensureDir(dir))
f5028693 71 }
f981dae8
C
72
73 // Cache directories
f5028693 74 for (const key of Object.keys(cacheDirectories)) {
f981dae8 75 const dir = cacheDirectories[key]
62689b94 76 tasks.push(ensureDir(dir))
f5028693 77 }
37dc07b2 78
09209296 79 // Playlist directories
9c6ca37f 80 tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
09209296 81
6fcd19ba
C
82 return Promise.all(tasks)
83}
37dc07b2 84
f5028693 85async function createOAuthClientIfNotExist () {
3fd3ab2d 86 const exist = await clientsExist()
f5028693
C
87 // Nothing to do, clients already exist
88 if (exist === true) return undefined
69b0a27c 89
f5028693 90 logger.info('Creating a default OAuth Client.')
37dc07b2 91
f5028693
C
92 const id = passwordGenerator(32, false, /[a-z0-9]/)
93 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
3fd3ab2d 94 const client = new OAuthClientModel({
f5028693
C
95 clientId: id,
96 clientSecret: secret,
97 grants: [ 'password', 'refresh_token' ],
98 redirectUris: null
37dc07b2 99 })
37dc07b2 100
f5028693
C
101 const createdClient = await client.save()
102 logger.info('Client id: ' + createdClient.clientId)
103 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 104
f5028693
C
105 return undefined
106}
37dc07b2 107
f5028693 108async function createOAuthAdminIfNotExist () {
3fd3ab2d 109 const exist = await usersExist()
f5028693
C
110 // Nothing to do, users already exist
111 if (exist === true) return undefined
d14b3e37 112
f5028693 113 logger.info('Creating the administrator.')
d14b3e37 114
f5028693 115 const username = 'root'
954605a8 116 const role = UserRole.ADMINISTRATOR
f5028693
C
117 const email = CONFIG.ADMIN.EMAIL
118 let validatePassword = true
119 let password = ''
67bf9b96 120
f5028693
C
121 // Do not generate a random password for tests
122 if (process.env.NODE_ENV === 'test') {
123 password = 'test'
37dc07b2 124
f5028693
C
125 if (process.env.NODE_APP_INSTANCE) {
126 password += process.env.NODE_APP_INSTANCE
67bf9b96 127 }
69b0a27c 128
f5028693
C
129 // Our password is weak so do not validate it
130 validatePassword = false
131 } else {
490b595a 132 password = passwordGenerator(16, true)
f5028693
C
133 }
134
135 const userData = {
136 username,
137 email,
138 password,
139 role,
d9eaee39 140 verified: true,
0883b324 141 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
bee0abff
FA
142 videoQuota: -1,
143 videoQuotaDaily: -1
f5028693 144 }
3fd3ab2d 145 const user = new UserModel(userData)
f5028693 146
df0b219d 147 await createUserAccountAndChannelAndPlaylist(user, validatePassword)
f5028693
C
148 logger.info('Username: ' + username)
149 logger.info('User password: ' + password)
571389d4 150}
f5028693 151
571389d4 152async function createApplicationIfNotExist () {
3fd3ab2d 153 const exist = await applicationExist()
350e31d6
C
154 // Nothing to do, application already exist
155 if (exist === true) return undefined
156
571389d4 157 logger.info('Creating application account.')
47e0652b 158
50d6de9c
C
159 const application = await ApplicationModel.create({
160 migrationVersion: LAST_MIGRATION_VERSION
161 })
1b3989b0 162
50d6de9c 163 return createApplicationActor(application.id)
37dc07b2 164}