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'
14 async function installApplication () {
18 sequelizeTypescript.sync()
21 createApplicationIfNotExist(),
22 createOAuthClientIfNotExist(),
23 createOAuthAdminIfNotExist()
28 removeCacheAndTmpDirectories()
29 .then(() => createDirectoriesIfNotExist())
32 logger.error('Cannot install application.', { err })
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 function removeCacheAndTmpDirectories () {
46 const cacheDirectories = Object.keys(FILES_CACHE)
47 .map(k => FILES_CACHE[k].DIRECTORY)
49 const tasks: Promise<any>[] = []
52 for (const key of Object.keys(cacheDirectories)) {
53 const dir = cacheDirectories[key]
54 tasks.push(remove(dir))
57 tasks.push(remove(CONFIG.STORAGE.TMP_DIR))
59 return Promise.all(tasks)
62 function createDirectoriesIfNotExist () {
63 const storage = CONFIG.STORAGE
64 const cacheDirectories = Object.keys(FILES_CACHE)
65 .map(k => FILES_CACHE[k].DIRECTORY)
67 const tasks: Promise<void>[] = []
68 for (const key of Object.keys(storage)) {
69 const dir = storage[key]
70 tasks.push(ensureDir(dir))
74 for (const key of Object.keys(cacheDirectories)) {
75 const dir = cacheDirectories[key]
76 tasks.push(ensureDir(dir))
79 // Playlist directories
80 tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
82 // Resumable upload directory
83 tasks.push(ensureDir(RESUMABLE_UPLOAD_DIRECTORY))
85 return Promise.all(tasks)
88 async function createOAuthClientIfNotExist () {
89 const exist = await clientsExist()
90 // Nothing to do, clients already exist
91 if (exist === true) return undefined
93 logger.info('Creating a default OAuth Client.')
95 const id = passwordGenerator(32, false, /[a-z0-9]/)
96 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
97 const client = new OAuthClientModel({
100 grants: [ 'password', 'refresh_token' ],
104 const createdClient = await client.save()
105 logger.info('Client id: ' + createdClient.clientId)
106 logger.info('Client secret: ' + createdClient.clientSecret)
111 async function createOAuthAdminIfNotExist () {
112 const exist = await usersExist()
113 // Nothing to do, users already exist
114 if (exist === true) return undefined
116 logger.info('Creating the administrator.')
118 const username = 'root'
119 const role = UserRole.ADMINISTRATOR
120 const email = CONFIG.ADMIN.EMAIL
121 let validatePassword = true
124 // Do not generate a random password for tests
125 if (process.env.NODE_ENV === 'test') {
128 if (process.env.NODE_APP_INSTANCE) {
129 password += process.env.NODE_APP_INSTANCE
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
137 password = passwordGenerator(16, true)
146 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
150 const user = new UserModel(userData)
152 await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
153 logger.info('Username: ' + username)
154 logger.info('User password: ' + password)
157 async function createApplicationIfNotExist () {
158 const exist = await applicationExist()
159 // Nothing to do, application already exist
160 if (exist === true) return undefined
162 logger.info('Creating application account.')
164 const application = await ApplicationModel.create({
165 migrationVersion: LAST_MIGRATION_VERSION
168 return createApplicationActor(application.id)