]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
5 import { UserModel } from '../models/account/user'
6 import { ApplicationModel } from '../models/application/application'
7 import { OAuthClientModel } from '../models/oauth/oauth-client'
8 import { applicationExist, clientsExist, usersExist } from './checker-after-init'
9 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11 import { remove, ensureDir } from 'fs-extra'
12
13 async function installApplication () {
14 try {
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 ])
30 } catch (err) {
31 logger.error('Cannot install application.', { err })
32 process.exit(-1)
33 }
34 }
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 installApplication
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function removeCacheDirectories () {
45 const cacheDirectories = Object.keys(CACHE)
46 .map(k => CACHE[k].DIRECTORY)
47
48 const tasks: Promise<any>[] = []
49
50 // Cache directories
51 for (const key of Object.keys(cacheDirectories)) {
52 const dir = cacheDirectories[key]
53 tasks.push(remove(dir))
54 }
55
56 return Promise.all(tasks)
57 }
58
59 function createDirectoriesIfNotExist () {
60 const storage = CONFIG.STORAGE
61 const cacheDirectories = Object.keys(CACHE)
62 .map(k => CACHE[k].DIRECTORY)
63
64 const tasks: Promise<void>[] = []
65 for (const key of Object.keys(storage)) {
66 const dir = storage[key]
67 tasks.push(ensureDir(dir))
68 }
69
70 // Cache directories
71 for (const key of Object.keys(cacheDirectories)) {
72 const dir = cacheDirectories[key]
73 tasks.push(ensureDir(dir))
74 }
75
76 return Promise.all(tasks)
77 }
78
79 async function createOAuthClientIfNotExist () {
80 const exist = await clientsExist()
81 // Nothing to do, clients already exist
82 if (exist === true) return undefined
83
84 logger.info('Creating a default OAuth Client.')
85
86 const id = passwordGenerator(32, false, /[a-z0-9]/)
87 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
88 const client = new OAuthClientModel({
89 clientId: id,
90 clientSecret: secret,
91 grants: [ 'password', 'refresh_token' ],
92 redirectUris: null
93 })
94
95 const createdClient = await client.save()
96 logger.info('Client id: ' + createdClient.clientId)
97 logger.info('Client secret: ' + createdClient.clientSecret)
98
99 return undefined
100 }
101
102 async function createOAuthAdminIfNotExist () {
103 const exist = await usersExist()
104 // Nothing to do, users already exist
105 if (exist === true) return undefined
106
107 logger.info('Creating the administrator.')
108
109 const username = 'root'
110 const role = UserRole.ADMINISTRATOR
111 const email = CONFIG.ADMIN.EMAIL
112 let validatePassword = true
113 let password = ''
114
115 // Do not generate a random password for tests
116 if (process.env.NODE_ENV === 'test') {
117 password = 'test'
118
119 if (process.env.NODE_APP_INSTANCE) {
120 password += process.env.NODE_APP_INSTANCE
121 }
122
123 // Our password is weak so do not validate it
124 validatePassword = false
125 } else {
126 password = passwordGenerator(16, true)
127 }
128
129 const userData = {
130 username,
131 email,
132 password,
133 role,
134 verified: true,
135 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
136 videoQuota: -1,
137 videoQuotaDaily: -1
138 }
139 const user = new UserModel(userData)
140
141 await createUserAccountAndChannel(user, validatePassword)
142 logger.info('Username: ' + username)
143 logger.info('User password: ' + password)
144 }
145
146 async function createApplicationIfNotExist () {
147 const exist = await applicationExist()
148 // Nothing to do, application already exist
149 if (exist === true) return undefined
150
151 logger.info('Creating application account.')
152
153 const application = await ApplicationModel.create({
154 migrationVersion: LAST_MIGRATION_VERSION
155 })
156
157 return createApplicationActor(application.id)
158 }