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