]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Add audit logs for video import
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
4d4e5cd4 1import * as passwordGenerator from 'password-generator'
571389d4 2import { UserRole } from '../../shared'
da854ddd
C
3import { mkdirpPromise, rimrafPromise } from '../helpers/core-utils'
4import { logger } from '../helpers/logger'
50d6de9c 5import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
3fd3ab2d
C
6import { UserModel } from '../models/account/user'
7import { ApplicationModel } from '../models/application/application'
8import { OAuthClientModel } from '../models/oauth/oauth-client'
350e31d6 9import { applicationExist, clientsExist, usersExist } from './checker'
50d6de9c 10import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
3fd3ab2d 11import { sequelizeTypescript } from './database'
6fcd19ba 12
f5028693 13async function installApplication () {
e34c85e5 14 try {
3fd3ab2d 15 await sequelizeTypescript.sync()
e34c85e5
C
16 await removeCacheDirectories()
17 await createDirectoriesIfNotExist()
efc32059 18 await createApplicationIfNotExist()
e34c85e5
C
19 await createOAuthClientIfNotExist()
20 await createOAuthAdminIfNotExist()
e34c85e5 21 } catch (err) {
d5b7d911 22 logger.error('Cannot install application.', { err })
2ccaeeb3 23 process.exit(-1)
e34c85e5 24 }
37dc07b2
C
25}
26
27// ---------------------------------------------------------------------------
28
65fcc311
C
29export {
30 installApplication
31}
37dc07b2
C
32
33// ---------------------------------------------------------------------------
34
f981dae8 35function removeCacheDirectories () {
f4001cf4
C
36 const cacheDirectories = Object.keys(CACHE)
37 .map(k => CACHE[k].DIRECTORY)
f981dae8 38
571389d4 39 const tasks: Promise<any>[] = []
f981dae8
C
40
41 // Cache directories
f5028693 42 for (const key of Object.keys(cacheDirectories)) {
f981dae8
C
43 const dir = cacheDirectories[key]
44 tasks.push(rimrafPromise(dir))
f5028693 45 }
f981dae8
C
46
47 return Promise.all(tasks)
48}
49
6fcd19ba 50function createDirectoriesIfNotExist () {
b0f9f39e 51 const storage = CONFIG.STORAGE
f4001cf4
C
52 const cacheDirectories = Object.keys(CACHE)
53 .map(k => CACHE[k].DIRECTORY)
37dc07b2 54
c1e791ba 55 const tasks: Promise<string>[] = []
f5028693 56 for (const key of Object.keys(storage)) {
b0f9f39e 57 const dir = storage[key]
f981dae8 58 tasks.push(mkdirpPromise(dir))
f5028693 59 }
f981dae8
C
60
61 // Cache directories
f5028693 62 for (const key of Object.keys(cacheDirectories)) {
f981dae8
C
63 const dir = cacheDirectories[key]
64 tasks.push(mkdirpPromise(dir))
f5028693 65 }
37dc07b2 66
6fcd19ba
C
67 return Promise.all(tasks)
68}
37dc07b2 69
f5028693 70async function createOAuthClientIfNotExist () {
3fd3ab2d 71 const exist = await clientsExist()
f5028693
C
72 // Nothing to do, clients already exist
73 if (exist === true) return undefined
69b0a27c 74
f5028693 75 logger.info('Creating a default OAuth Client.')
37dc07b2 76
f5028693
C
77 const id = passwordGenerator(32, false, /[a-z0-9]/)
78 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
3fd3ab2d 79 const client = new OAuthClientModel({
f5028693
C
80 clientId: id,
81 clientSecret: secret,
82 grants: [ 'password', 'refresh_token' ],
83 redirectUris: null
37dc07b2 84 })
37dc07b2 85
f5028693
C
86 const createdClient = await client.save()
87 logger.info('Client id: ' + createdClient.clientId)
88 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 89
f5028693
C
90 return undefined
91}
37dc07b2 92
f5028693 93async function createOAuthAdminIfNotExist () {
3fd3ab2d 94 const exist = await usersExist()
f5028693
C
95 // Nothing to do, users already exist
96 if (exist === true) return undefined
d14b3e37 97
f5028693 98 logger.info('Creating the administrator.')
d14b3e37 99
f5028693 100 const username = 'root'
954605a8 101 const role = UserRole.ADMINISTRATOR
f5028693
C
102 const email = CONFIG.ADMIN.EMAIL
103 let validatePassword = true
104 let password = ''
67bf9b96 105
f5028693
C
106 // Do not generate a random password for tests
107 if (process.env.NODE_ENV === 'test') {
108 password = 'test'
37dc07b2 109
f5028693
C
110 if (process.env.NODE_APP_INSTANCE) {
111 password += process.env.NODE_APP_INSTANCE
67bf9b96 112 }
69b0a27c 113
f5028693
C
114 // Our password is weak so do not validate it
115 validatePassword = false
116 } else {
490b595a 117 password = passwordGenerator(16, true)
f5028693
C
118 }
119
120 const userData = {
121 username,
122 email,
123 password,
124 role,
0883b324 125 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
f5028693
C
126 videoQuota: -1
127 }
3fd3ab2d 128 const user = new UserModel(userData)
f5028693 129
38fa2065 130 await createUserAccountAndChannel(user, validatePassword)
f5028693
C
131 logger.info('Username: ' + username)
132 logger.info('User password: ' + password)
571389d4 133}
f5028693 134
571389d4 135async function createApplicationIfNotExist () {
3fd3ab2d 136 const exist = await applicationExist()
350e31d6
C
137 // Nothing to do, application already exist
138 if (exist === true) return undefined
139
571389d4 140 logger.info('Creating application account.')
47e0652b 141
50d6de9c
C
142 const application = await ApplicationModel.create({
143 migrationVersion: LAST_MIGRATION_VERSION
144 })
1b3989b0 145
50d6de9c 146 return createApplicationActor(application.id)
37dc07b2 147}