1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
4 import { createUserAccountAndChannel } from '../lib'
5 import { createLocalAccount } from '../lib/user'
6 import { applicationExist, clientsExist, usersExist } from './checker'
7 import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
9 import { database as db } from './database'
11 async function installApplication () {
13 await db.sequelize.sync()
14 await removeCacheDirectories()
15 await createDirectoriesIfNotExist()
16 await createOAuthClientIfNotExist()
17 await createOAuthAdminIfNotExist()
18 await createApplicationIfNotExist()
20 logger.error('Cannot install application.', err)
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 function removeCacheDirectories () {
34 const cacheDirectories = CACHE.DIRECTORIES
36 const tasks: Promise<any>[] = []
39 for (const key of Object.keys(cacheDirectories)) {
40 const dir = cacheDirectories[key]
41 tasks.push(rimrafPromise(dir))
44 return Promise.all(tasks)
47 function createDirectoriesIfNotExist () {
48 const storage = CONFIG.STORAGE
49 const cacheDirectories = CACHE.DIRECTORIES
52 for (const key of Object.keys(storage)) {
53 const dir = storage[key]
54 tasks.push(mkdirpPromise(dir))
58 for (const key of Object.keys(cacheDirectories)) {
59 const dir = cacheDirectories[key]
60 tasks.push(mkdirpPromise(dir))
63 return Promise.all(tasks)
66 async function createOAuthClientIfNotExist () {
67 const exist = await clientsExist(db.OAuthClient)
68 // Nothing to do, clients already exist
69 if (exist === true) return undefined
71 logger.info('Creating a default OAuth Client.')
73 const id = passwordGenerator(32, false, /[a-z0-9]/)
74 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
75 const client = db.OAuthClient.build({
78 grants: [ 'password', 'refresh_token' ],
82 const createdClient = await client.save()
83 logger.info('Client id: ' + createdClient.clientId)
84 logger.info('Client secret: ' + createdClient.clientSecret)
89 async function createOAuthAdminIfNotExist () {
90 const exist = await usersExist(db.User)
91 // Nothing to do, users already exist
92 if (exist === true) return undefined
94 logger.info('Creating the administrator.')
96 const username = 'root'
97 const role = UserRole.ADMINISTRATOR
98 const email = CONFIG.ADMIN.EMAIL
99 let validatePassword = true
102 // Do not generate a random password for tests
103 if (process.env.NODE_ENV === 'test') {
106 if (process.env.NODE_APP_INSTANCE) {
107 password += process.env.NODE_APP_INSTANCE
110 // Our password is weak so do not validate it
111 validatePassword = false
113 password = passwordGenerator(8, true)
123 const user = db.User.build(userData)
125 await createUserAccountAndChannel(user, validatePassword)
126 logger.info('Username: ' + username)
127 logger.info('User password: ' + password)
130 async function createApplicationIfNotExist () {
131 const exist = await applicationExist(db.Application)
132 // Nothing to do, application already exist
133 if (exist === true) return undefined
135 logger.info('Creating Application table.')
136 const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
138 logger.info('Creating application account.')
139 return createLocalAccount(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)