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