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