1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { createApplicationActor, createUserAccountAndChannelAndPlaylist } 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 { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11 import { ensureDir, remove } from 'fs-extra'
12 import { CONFIG } from './config'
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 return Promise.all(tasks)
85 async function createOAuthClientIfNotExist () {
86 const exist = await clientsExist()
87 // Nothing to do, clients already exist
88 if (exist === true) return undefined
90 logger.info('Creating a default OAuth Client.')
92 const id = passwordGenerator(32, false, /[a-z0-9]/)
93 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
94 const client = new OAuthClientModel({
97 grants: [ 'password', 'refresh_token' ],
101 const createdClient = await client.save()
102 logger.info('Client id: ' + createdClient.clientId)
103 logger.info('Client secret: ' + createdClient.clientSecret)
108 async function createOAuthAdminIfNotExist () {
109 const exist = await usersExist()
110 // Nothing to do, users already exist
111 if (exist === true) return undefined
113 logger.info('Creating the administrator.')
115 const username = 'root'
116 const role = UserRole.ADMINISTRATOR
117 const email = CONFIG.ADMIN.EMAIL
118 let validatePassword = true
121 // Do not generate a random password for tests
122 if (process.env.NODE_ENV === 'test') {
125 if (process.env.NODE_APP_INSTANCE) {
126 password += process.env.NODE_APP_INSTANCE
129 // Our password is weak so do not validate it
130 validatePassword = false
131 } else if (process.env.PT_INITIAL_ROOT_PASSWORD) {
132 password = process.env.PT_INITIAL_ROOT_PASSWORD
134 password = passwordGenerator(16, true)
143 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
147 const user = new UserModel(userData)
149 await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
150 logger.info('Username: ' + username)
151 logger.info('User password: ' + password)
154 async function createApplicationIfNotExist () {
155 const exist = await applicationExist()
156 // Nothing to do, application already exist
157 if (exist === true) return undefined
159 logger.info('Creating application account.')
161 const application = await ApplicationModel.create({
162 migrationVersion: LAST_MIGRATION_VERSION
165 return createApplicationActor(application.id)