]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Add ability to unfederate a local video (on blacklist)
[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'
50d6de9c 9import { CACHE, CONFIG, 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
6fcd19ba
C
76 return Promise.all(tasks)
77}
37dc07b2 78
f5028693 79async function createOAuthClientIfNotExist () {
3fd3ab2d 80 const exist = await clientsExist()
f5028693
C
81 // Nothing to do, clients already exist
82 if (exist === true) return undefined
69b0a27c 83
f5028693 84 logger.info('Creating a default OAuth Client.')
37dc07b2 85
f5028693
C
86 const id = passwordGenerator(32, false, /[a-z0-9]/)
87 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
3fd3ab2d 88 const client = new OAuthClientModel({
f5028693
C
89 clientId: id,
90 clientSecret: secret,
91 grants: [ 'password', 'refresh_token' ],
92 redirectUris: null
37dc07b2 93 })
37dc07b2 94
f5028693
C
95 const createdClient = await client.save()
96 logger.info('Client id: ' + createdClient.clientId)
97 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 98
f5028693
C
99 return undefined
100}
37dc07b2 101
f5028693 102async function createOAuthAdminIfNotExist () {
3fd3ab2d 103 const exist = await usersExist()
f5028693
C
104 // Nothing to do, users already exist
105 if (exist === true) return undefined
d14b3e37 106
f5028693 107 logger.info('Creating the administrator.')
d14b3e37 108
f5028693 109 const username = 'root'
954605a8 110 const role = UserRole.ADMINISTRATOR
f5028693
C
111 const email = CONFIG.ADMIN.EMAIL
112 let validatePassword = true
113 let password = ''
67bf9b96 114
f5028693
C
115 // Do not generate a random password for tests
116 if (process.env.NODE_ENV === 'test') {
117 password = 'test'
37dc07b2 118
f5028693
C
119 if (process.env.NODE_APP_INSTANCE) {
120 password += process.env.NODE_APP_INSTANCE
67bf9b96 121 }
69b0a27c 122
f5028693
C
123 // Our password is weak so do not validate it
124 validatePassword = false
125 } else {
490b595a 126 password = passwordGenerator(16, true)
f5028693
C
127 }
128
129 const userData = {
130 username,
131 email,
132 password,
133 role,
d9eaee39 134 verified: true,
0883b324 135 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
bee0abff
FA
136 videoQuota: -1,
137 videoQuotaDaily: -1
f5028693 138 }
3fd3ab2d 139 const user = new UserModel(userData)
f5028693 140
38fa2065 141 await createUserAccountAndChannel(user, validatePassword)
f5028693
C
142 logger.info('Username: ' + username)
143 logger.info('User password: ' + password)
571389d4 144}
f5028693 145
571389d4 146async function createApplicationIfNotExist () {
3fd3ab2d 147 const exist = await applicationExist()
350e31d6
C
148 // Nothing to do, application already exist
149 if (exist === true) return undefined
150
571389d4 151 logger.info('Creating application account.')
47e0652b 152
50d6de9c
C
153 const application = await ApplicationModel.create({
154 migrationVersion: LAST_MIGRATION_VERSION
155 })
1b3989b0 156
50d6de9c 157 return createApplicationActor(application.id)
37dc07b2 158}