]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
41fb13c3
C
1import { ensureDir, remove } from 'fs-extra'
2import passwordGenerator from 'password-generator'
571389d4 3import { UserRole } from '../../shared'
da854ddd 4import { logger } from '../helpers/logger'
df0b219d 5import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
3fd3ab2d
C
6import { ApplicationModel } from '../models/application/application'
7import { OAuthClientModel } from '../models/oauth/oauth-client'
41fb13c3 8import { UserModel } from '../models/user/user'
e5565833 9import { applicationExist, clientsExist, usersExist } from './checker-after-init'
41fb13c3 10import { CONFIG } from './config'
f6d6e7f8 11import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants'
3fd3ab2d 12import { sequelizeTypescript } from './database'
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,
b65de1be 147 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
bee0abff
FA
148 videoQuota: -1,
149 videoQuotaDaily: -1
f5028693 150 }
3fd3ab2d 151 const user = new UserModel(userData)
f5028693 152
1f20622f 153 await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
f5028693
C
154 logger.info('Username: ' + username)
155 logger.info('User password: ' + password)
571389d4 156}
f5028693 157
571389d4 158async function createApplicationIfNotExist () {
3fd3ab2d 159 const exist = await applicationExist()
350e31d6
C
160 // Nothing to do, application already exist
161 if (exist === true) return undefined
162
571389d4 163 logger.info('Creating application account.')
47e0652b 164
50d6de9c
C
165 const application = await ApplicationModel.create({
166 migrationVersion: LAST_MIGRATION_VERSION
167 })
1b3989b0 168
50d6de9c 169 return createApplicationActor(application.id)
37dc07b2 170}