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, CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11 import { remove, ensureDir } from 'fs-extra'
13 async function installApplication () {
17 sequelizeTypescript.sync()
20 createApplicationIfNotExist(),
21 createOAuthClientIfNotExist(),
22 createOAuthAdminIfNotExist()
27 removeCacheAndTmpDirectories()
28 .then(() => createDirectoriesIfNotExist())
31 logger.error('Cannot install application.', { err })
36 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 function removeCacheAndTmpDirectories () {
45 const cacheDirectories = Object.keys(FILES_CACHE)
46 .map(k => FILES_CACHE[k].DIRECTORY)
48 const tasks: Promise<any>[] = []
51 for (const key of Object.keys(cacheDirectories)) {
52 const dir = cacheDirectories[key]
53 tasks.push(remove(dir))
56 tasks.push(remove(CONFIG.STORAGE.TMP_DIR))
58 return Promise.all(tasks)
61 function createDirectoriesIfNotExist () {
62 const storage = CONFIG.STORAGE
63 const cacheDirectories = Object.keys(FILES_CACHE)
64 .map(k => FILES_CACHE[k].DIRECTORY)
66 const tasks: Promise<void>[] = []
67 for (const key of Object.keys(storage)) {
68 const dir = storage[key]
69 tasks.push(ensureDir(dir))
73 for (const key of Object.keys(cacheDirectories)) {
74 const dir = cacheDirectories[key]
75 tasks.push(ensureDir(dir))
78 // Playlist directories
79 tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
81 return Promise.all(tasks)
84 async function createOAuthClientIfNotExist () {
85 const exist = await clientsExist()
86 // Nothing to do, clients already exist
87 if (exist === true) return undefined
89 logger.info('Creating a default OAuth Client.')
91 const id = passwordGenerator(32, false, /[a-z0-9]/)
92 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
93 const client = new OAuthClientModel({
96 grants: [ 'password', 'refresh_token' ],
100 const createdClient = await client.save()
101 logger.info('Client id: ' + createdClient.clientId)
102 logger.info('Client secret: ' + createdClient.clientSecret)
107 async function createOAuthAdminIfNotExist () {
108 const exist = await usersExist()
109 // Nothing to do, users already exist
110 if (exist === true) return undefined
112 logger.info('Creating the administrator.')
114 const username = 'root'
115 const role = UserRole.ADMINISTRATOR
116 const email = CONFIG.ADMIN.EMAIL
117 let validatePassword = true
120 // Do not generate a random password for tests
121 if (process.env.NODE_ENV === 'test') {
124 if (process.env.NODE_APP_INSTANCE) {
125 password += process.env.NODE_APP_INSTANCE
128 // Our password is weak so do not validate it
129 validatePassword = false
131 password = passwordGenerator(16, true)
140 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
144 const user = new UserModel(userData)
146 await createUserAccountAndChannelAndPlaylist(user, validatePassword)
147 logger.info('Username: ' + username)
148 logger.info('User password: ' + password)
151 async function createApplicationIfNotExist () {
152 const exist = await applicationExist()
153 // Nothing to do, application already exist
154 if (exist === true) return undefined
156 logger.info('Creating application account.')
158 const application = await ApplicationModel.create({
159 migrationVersion: LAST_MIGRATION_VERSION
162 return createApplicationActor(application.id)