1 import { join } from 'path'
2 import * as config from 'config'
3 import * as passwordGenerator from 'password-generator'
4 import * as Promise from 'bluebird'
6 import { database as db } from './database'
7 import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
8 import { clientsExist, usersExist } from './checker'
9 import { logger, createCertsIfNotExist, root, mkdirpPromise, rimrafPromise } from '../helpers'
11 function installApplication () {
12 return db.sequelize.sync()
13 .then(() => removeCacheDirectories())
14 .then(() => createDirectoriesIfNotExist())
15 .then(() => createCertsIfNotExist())
16 .then(() => createOAuthClientIfNotExist())
17 .then(() => createOAuthAdminIfNotExist())
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 function removeCacheDirectories () {
29 const cacheDirectories = CACHE.DIRECTORIES
34 Object.keys(cacheDirectories).forEach(key => {
35 const dir = cacheDirectories[key]
36 tasks.push(rimrafPromise(dir))
39 return Promise.all(tasks)
42 function createDirectoriesIfNotExist () {
43 const storages = CONFIG.STORAGE
44 const cacheDirectories = CACHE.DIRECTORIES
47 Object.keys(storages).forEach(key => {
48 const dir = storages[key]
49 tasks.push(mkdirpPromise(dir))
53 Object.keys(cacheDirectories).forEach(key => {
54 const dir = cacheDirectories[key]
55 tasks.push(mkdirpPromise(dir))
58 return Promise.all(tasks)
61 function createOAuthClientIfNotExist () {
62 return clientsExist().then(exist => {
63 // Nothing to do, clients already exist
64 if (exist === true) return undefined
66 logger.info('Creating a default OAuth Client.')
68 const id = passwordGenerator(32, false, /[a-z0-9]/)
69 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
70 const client = db.OAuthClient.build({
73 grants: [ 'password', 'refresh_token' ],
77 return client.save().then(createdClient => {
78 logger.info('Client id: ' + createdClient.clientId)
79 logger.info('Client secret: ' + createdClient.clientSecret)
86 function createOAuthAdminIfNotExist () {
87 return usersExist().then(exist => {
88 // Nothing to do, users already exist
89 if (exist === true) return undefined
91 logger.info('Creating the administrator.')
93 const username = 'root'
94 const role = USER_ROLES.ADMIN
95 const email = CONFIG.ADMIN.EMAIL
96 const createOptions: { validate?: boolean } = {}
99 // Do not generate a random password for tests
100 if (process.env.NODE_ENV === 'test') {
103 if (process.env.NODE_APP_INSTANCE) {
104 password += process.env.NODE_APP_INSTANCE
107 // Our password is weak so do not validate it
108 createOptions.validate = false
110 password = passwordGenerator(8, true)
120 return db.User.create(userData, createOptions).then(createdUser => {
121 logger.info('Username: ' + username)
122 logger.info('User password: ' + password)
124 logger.info('Creating Application table.')
125 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })