]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Try to fix weird CI test crashes
[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'
7d9ba5c0 5import { UserModel } from '../models/user/user'
3fd3ab2d
C
6import { ApplicationModel } from '../models/application/application'
7import { OAuthClientModel } from '../models/oauth/oauth-client'
e5565833 8import { applicationExist, clientsExist, usersExist } from './checker-after-init'
f6d6e7f8 9import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } 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
f6d6e7f8 82 // Resumable upload directory
83 tasks.push(ensureDir(RESUMABLE_UPLOAD_DIRECTORY))
84
6fcd19ba
C
85 return Promise.all(tasks)
86}
37dc07b2 87
f5028693 88async function createOAuthClientIfNotExist () {
3fd3ab2d 89 const exist = await clientsExist()
f5028693
C
90 // Nothing to do, clients already exist
91 if (exist === true) return undefined
69b0a27c 92
f5028693 93 logger.info('Creating a default OAuth Client.')
37dc07b2 94
f5028693
C
95 const id = passwordGenerator(32, false, /[a-z0-9]/)
96 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
3fd3ab2d 97 const client = new OAuthClientModel({
f5028693
C
98 clientId: id,
99 clientSecret: secret,
100 grants: [ 'password', 'refresh_token' ],
101 redirectUris: null
37dc07b2 102 })
37dc07b2 103
f5028693
C
104 const createdClient = await client.save()
105 logger.info('Client id: ' + createdClient.clientId)
106 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 107
f5028693
C
108 return undefined
109}
37dc07b2 110
f5028693 111async function createOAuthAdminIfNotExist () {
3fd3ab2d 112 const exist = await usersExist()
f5028693
C
113 // Nothing to do, users already exist
114 if (exist === true) return undefined
d14b3e37 115
f5028693 116 logger.info('Creating the administrator.')
d14b3e37 117
f5028693 118 const username = 'root'
954605a8 119 const role = UserRole.ADMINISTRATOR
f5028693
C
120 const email = CONFIG.ADMIN.EMAIL
121 let validatePassword = true
122 let password = ''
67bf9b96 123
f5028693
C
124 // Do not generate a random password for tests
125 if (process.env.NODE_ENV === 'test') {
126 password = 'test'
37dc07b2 127
f5028693
C
128 if (process.env.NODE_APP_INSTANCE) {
129 password += process.env.NODE_APP_INSTANCE
67bf9b96 130 }
69b0a27c 131
f5028693
C
132 // Our password is weak so do not validate it
133 validatePassword = false
3daaa192
AV
134 } else if (process.env.PT_INITIAL_ROOT_PASSWORD) {
135 password = process.env.PT_INITIAL_ROOT_PASSWORD
f5028693 136 } else {
490b595a 137 password = passwordGenerator(16, true)
f5028693
C
138 }
139
140 const userData = {
141 username,
142 email,
143 password,
144 role,
d9eaee39 145 verified: true,
0883b324 146 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
bee0abff
FA
147 videoQuota: -1,
148 videoQuotaDaily: -1
f5028693 149 }
3fd3ab2d 150 const user = new UserModel(userData)
f5028693 151
1f20622f 152 await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
f5028693
C
153 logger.info('Username: ' + username)
154 logger.info('User password: ' + password)
571389d4 155}
f5028693 156
571389d4 157async function createApplicationIfNotExist () {
3fd3ab2d 158 const exist = await applicationExist()
350e31d6
C
159 // Nothing to do, application already exist
160 if (exist === true) return undefined
161
571389d4 162 logger.info('Creating application account.')
47e0652b 163
50d6de9c
C
164 const application = await ApplicationModel.create({
165 migrationVersion: LAST_MIGRATION_VERSION
166 })
1b3989b0 167
50d6de9c 168 return createApplicationActor(application.id)
37dc07b2 169}