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