]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Cleanup tmp directory at startup
[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'
9c6ca37f 9import { CACHE, CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
3fd3ab2d 10import { sequelizeTypescript } from './database'
62689b94 11import { remove, ensureDir } from 'fs-extra'
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 () {
f4001cf4
C
45 const cacheDirectories = Object.keys(CACHE)
46 .map(k => 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]
62689b94 53 tasks.push(remove(dir))
f5028693 54 }
f981dae8 55
f4800714
C
56 tasks.push(remove(CONFIG.STORAGE.TMP_DIR))
57
f981dae8
C
58 return Promise.all(tasks)
59}
60
6fcd19ba 61function createDirectoriesIfNotExist () {
b0f9f39e 62 const storage = CONFIG.STORAGE
f4001cf4
C
63 const cacheDirectories = Object.keys(CACHE)
64 .map(k => CACHE[k].DIRECTORY)
37dc07b2 65
62689b94 66 const tasks: Promise<void>[] = []
f5028693 67 for (const key of Object.keys(storage)) {
b0f9f39e 68 const dir = storage[key]
62689b94 69 tasks.push(ensureDir(dir))
f5028693 70 }
f981dae8
C
71
72 // Cache directories
f5028693 73 for (const key of Object.keys(cacheDirectories)) {
f981dae8 74 const dir = cacheDirectories[key]
62689b94 75 tasks.push(ensureDir(dir))
f5028693 76 }
37dc07b2 77
09209296 78 // Playlist directories
9c6ca37f 79 tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
09209296 80
6fcd19ba
C
81 return Promise.all(tasks)
82}
37dc07b2 83
f5028693 84async function createOAuthClientIfNotExist () {
3fd3ab2d 85 const exist = await clientsExist()
f5028693
C
86 // Nothing to do, clients already exist
87 if (exist === true) return undefined
69b0a27c 88
f5028693 89 logger.info('Creating a default OAuth Client.')
37dc07b2 90
f5028693
C
91 const id = passwordGenerator(32, false, /[a-z0-9]/)
92 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
3fd3ab2d 93 const client = new OAuthClientModel({
f5028693
C
94 clientId: id,
95 clientSecret: secret,
96 grants: [ 'password', 'refresh_token' ],
97 redirectUris: null
37dc07b2 98 })
37dc07b2 99
f5028693
C
100 const createdClient = await client.save()
101 logger.info('Client id: ' + createdClient.clientId)
102 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 103
f5028693
C
104 return undefined
105}
37dc07b2 106
f5028693 107async function createOAuthAdminIfNotExist () {
3fd3ab2d 108 const exist = await usersExist()
f5028693
C
109 // Nothing to do, users already exist
110 if (exist === true) return undefined
d14b3e37 111
f5028693 112 logger.info('Creating the administrator.')
d14b3e37 113
f5028693 114 const username = 'root'
954605a8 115 const role = UserRole.ADMINISTRATOR
f5028693
C
116 const email = CONFIG.ADMIN.EMAIL
117 let validatePassword = true
118 let password = ''
67bf9b96 119
f5028693
C
120 // Do not generate a random password for tests
121 if (process.env.NODE_ENV === 'test') {
122 password = 'test'
37dc07b2 123
f5028693
C
124 if (process.env.NODE_APP_INSTANCE) {
125 password += process.env.NODE_APP_INSTANCE
67bf9b96 126 }
69b0a27c 127
f5028693
C
128 // Our password is weak so do not validate it
129 validatePassword = false
130 } else {
490b595a 131 password = passwordGenerator(16, true)
f5028693
C
132 }
133
134 const userData = {
135 username,
136 email,
137 password,
138 role,
d9eaee39 139 verified: true,
0883b324 140 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
bee0abff
FA
141 videoQuota: -1,
142 videoQuotaDaily: -1
f5028693 143 }
3fd3ab2d 144 const user = new UserModel(userData)
f5028693 145
df0b219d 146 await createUserAccountAndChannelAndPlaylist(user, validatePassword)
f5028693
C
147 logger.info('Username: ' + username)
148 logger.info('User password: ' + password)
571389d4 149}
f5028693 150
571389d4 151async function createApplicationIfNotExist () {
3fd3ab2d 152 const exist = await applicationExist()
350e31d6
C
153 // Nothing to do, application already exist
154 if (exist === true) return undefined
155
571389d4 156 logger.info('Creating application account.')
47e0652b 157
50d6de9c
C
158 const application = await ApplicationModel.create({
159 migrationVersion: LAST_MIGRATION_VERSION
160 })
1b3989b0 161
50d6de9c 162 return createApplicationActor(application.id)
37dc07b2 163}