1 import { join } from 'path'
2 import * as config from 'config'
3 import { each, series } from 'async'
4 import * as mkdirp from 'mkdirp'
5 import * as passwordGenerator from 'password-generator'
7 import { database as db } from './database'
8 import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
9 import { clientsExist, usersExist } from './checker'
10 import { logger, createCertsIfNotExist, root } from '../helpers'
12 function installApplication (callback: (err: Error) => void) {
14 function createDatabase (callbackAsync) {
15 db.sequelize.sync().asCallback(callbackAsync)
16 // db.sequelize.sync({ force: true }).asCallback(callbackAsync)
19 function createDirectories (callbackAsync) {
20 createDirectoriesIfNotExist(callbackAsync)
23 function createCertificates (callbackAsync) {
24 createCertsIfNotExist(callbackAsync)
27 function createOAuthClient (callbackAsync) {
28 createOAuthClientIfNotExist(callbackAsync)
31 function createOAuthUser (callbackAsync) {
32 createOAuthAdminIfNotExist(callbackAsync)
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 function createDirectoriesIfNotExist (callback: (err: Error) => void) {
46 const storages = config.get('storage')
48 each(Object.keys(storages), function (key, callbackEach) {
49 const dir = storages[key]
50 mkdirp(join(root(), dir), callbackEach)
54 function createOAuthClientIfNotExist (callback: (err: Error) => void) {
55 clientsExist(function (err, exist) {
56 if (err) return callback(err)
58 // Nothing to do, clients already exist
59 if (exist === true) return callback(null)
61 logger.info('Creating a default OAuth Client.')
63 const id = passwordGenerator(32, false, /[a-z0-9]/)
64 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
65 const client = db.OAuthClient.build({
68 grants: [ 'password', 'refresh_token' ],
72 client.save().asCallback(function (err, createdClient) {
73 if (err) return callback(err)
75 logger.info('Client id: ' + createdClient.clientId)
76 logger.info('Client secret: ' + createdClient.clientSecret)
83 function createOAuthAdminIfNotExist (callback: (err: Error) => void) {
84 usersExist(function (err, exist) {
85 if (err) return callback(err)
87 // Nothing to do, users already exist
88 if (exist === true) return callback(null)
90 logger.info('Creating the administrator.')
92 const username = 'root'
93 const role = USER_ROLES.ADMIN
94 const email = CONFIG.ADMIN.EMAIL
95 const createOptions: { validate?: boolean } = {}
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 createOptions.validate = false
109 password = passwordGenerator(8, true)
119 db.User.create(userData, createOptions).asCallback(function (err, createdUser) {
120 if (err) return callback(err)
122 logger.info('Username: ' + username)
123 logger.info('User password: ' + password)
125 logger.info('Creating Application table.')
126 db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }).asCallback(callback)