1 import * as passwordGenerator from 'password-generator'
2 import * as Promise from 'bluebird'
4 import { database as db } from './database'
5 import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
6 import { clientsExist, usersExist } from './checker'
7 import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
8 import { createUserAuthorAndChannel } from '../lib'
10 function installApplication () {
11 return db.sequelize.sync()
12 .then(() => removeCacheDirectories())
13 .then(() => createDirectoriesIfNotExist())
14 .then(() => createCertsIfNotExist())
15 .then(() => createOAuthClientIfNotExist())
16 .then(() => createOAuthAdminIfNotExist())
19 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 function removeCacheDirectories () {
28 const cacheDirectories = CACHE.DIRECTORIES
33 Object.keys(cacheDirectories).forEach(key => {
34 const dir = cacheDirectories[key]
35 tasks.push(rimrafPromise(dir))
38 return Promise.all(tasks)
41 function createDirectoriesIfNotExist () {
42 const storage = CONFIG.STORAGE
43 const cacheDirectories = CACHE.DIRECTORIES
46 Object.keys(storage).forEach(key => {
47 const dir = storage[key]
48 tasks.push(mkdirpPromise(dir))
52 Object.keys(cacheDirectories).forEach(key => {
53 const dir = cacheDirectories[key]
54 tasks.push(mkdirpPromise(dir))
57 return Promise.all(tasks)
60 function createOAuthClientIfNotExist () {
61 return clientsExist(db.OAuthClient).then(exist => {
62 // Nothing to do, clients already exist
63 if (exist === true) return undefined
65 logger.info('Creating a default OAuth Client.')
67 const id = passwordGenerator(32, false, /[a-z0-9]/)
68 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
69 const client = db.OAuthClient.build({
72 grants: [ 'password', 'refresh_token' ],
76 return client.save().then(createdClient => {
77 logger.info('Client id: ' + createdClient.clientId)
78 logger.info('Client secret: ' + createdClient.clientSecret)
85 function createOAuthAdminIfNotExist () {
86 return usersExist(db.User).then(exist => {
87 // Nothing to do, users already exist
88 if (exist === true) return undefined
90 logger.info('Creating the administrator.')
92 const username = 'root'
93 const role = USER_ROLES.ADMIN
94 const email = CONFIG.ADMIN.EMAIL
95 let validatePassword = true
98 // Do not generate a random password for tests
99 if (process.env.NODE_ENV === 'test') {
102 if (process.env.NODE_APP_INSTANCE) {
103 password += process.env.NODE_APP_INSTANCE
106 // Our password is weak so do not validate it
107 validatePassword = false
109 password = passwordGenerator(8, true)
119 const user = db.User.build(userData)
121 return createUserAuthorAndChannel(user, validatePassword)
122 .then(({ user }) => {
123 logger.info('Username: ' + username)
124 logger.info('User password: ' + password)
126 logger.info('Creating Application table.')
127 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })