diff options
Diffstat (limited to 'server/initializers/installer.ts')
-rw-r--r-- | server/initializers/installer.ts | 75 |
1 files changed, 26 insertions, 49 deletions
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index f105c8292..1ec24c4ad 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts | |||
@@ -1,37 +1,19 @@ | |||
1 | import { join } from 'path' | 1 | import { join } from 'path' |
2 | import * as config from 'config' | 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' | 3 | import * as passwordGenerator from 'password-generator' |
4 | import * as Promise from 'bluebird' | ||
6 | 5 | ||
7 | import { database as db } from './database' | 6 | import { database as db } from './database' |
8 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' | 7 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' |
9 | import { clientsExist, usersExist } from './checker' | 8 | import { clientsExist, usersExist } from './checker' |
10 | import { logger, createCertsIfNotExist, root } from '../helpers' | 9 | import { logger, createCertsIfNotExist, root, mkdirpPromise } from '../helpers' |
11 | 10 | ||
12 | function installApplication (callback: (err: Error) => void) { | 11 | function installApplication () { |
13 | series([ | 12 | return db.sequelize.sync() |
14 | function createDatabase (callbackAsync) { | 13 | .then(() => createDirectoriesIfNotExist()) |
15 | db.sequelize.sync().asCallback(callbackAsync) | 14 | .then(() => createCertsIfNotExist()) |
16 | // db.sequelize.sync({ force: true }).asCallback(callbackAsync) | 15 | .then(() => createOAuthClientIfNotExist()) |
17 | }, | 16 | .then(() => createOAuthAdminIfNotExist()) |
18 | |||
19 | function createDirectories (callbackAsync) { | ||
20 | createDirectoriesIfNotExist(callbackAsync) | ||
21 | }, | ||
22 | |||
23 | function createCertificates (callbackAsync) { | ||
24 | createCertsIfNotExist(callbackAsync) | ||
25 | }, | ||
26 | |||
27 | function createOAuthClient (callbackAsync) { | ||
28 | createOAuthClientIfNotExist(callbackAsync) | ||
29 | }, | ||
30 | |||
31 | function createOAuthUser (callbackAsync) { | ||
32 | createOAuthAdminIfNotExist(callbackAsync) | ||
33 | } | ||
34 | ], callback) | ||
35 | } | 17 | } |
36 | 18 | ||
37 | // --------------------------------------------------------------------------- | 19 | // --------------------------------------------------------------------------- |
@@ -42,21 +24,22 @@ export { | |||
42 | 24 | ||
43 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
44 | 26 | ||
45 | function createDirectoriesIfNotExist (callback: (err: Error) => void) { | 27 | function createDirectoriesIfNotExist () { |
46 | const storages = config.get('storage') | 28 | const storages = config.get('storage') |
47 | 29 | ||
48 | each(Object.keys(storages), function (key, callbackEach) { | 30 | const tasks = [] |
31 | Object.keys(storages).forEach(key => { | ||
49 | const dir = storages[key] | 32 | const dir = storages[key] |
50 | mkdirp(join(root(), dir), callbackEach) | 33 | tasks.push(mkdirpPromise(join(root(), dir))) |
51 | }, callback) | 34 | }) |
52 | } | ||
53 | 35 | ||
54 | function createOAuthClientIfNotExist (callback: (err: Error) => void) { | 36 | return Promise.all(tasks) |
55 | clientsExist(function (err, exist) { | 37 | } |
56 | if (err) return callback(err) | ||
57 | 38 | ||
39 | function createOAuthClientIfNotExist () { | ||
40 | return clientsExist().then(exist => { | ||
58 | // Nothing to do, clients already exist | 41 | // Nothing to do, clients already exist |
59 | if (exist === true) return callback(null) | 42 | if (exist === true) return undefined |
60 | 43 | ||
61 | logger.info('Creating a default OAuth Client.') | 44 | logger.info('Creating a default OAuth Client.') |
62 | 45 | ||
@@ -69,23 +52,19 @@ function createOAuthClientIfNotExist (callback: (err: Error) => void) { | |||
69 | redirectUris: null | 52 | redirectUris: null |
70 | }) | 53 | }) |
71 | 54 | ||
72 | client.save().asCallback(function (err, createdClient) { | 55 | return client.save().then(createdClient => { |
73 | if (err) return callback(err) | ||
74 | |||
75 | logger.info('Client id: ' + createdClient.clientId) | 56 | logger.info('Client id: ' + createdClient.clientId) |
76 | logger.info('Client secret: ' + createdClient.clientSecret) | 57 | logger.info('Client secret: ' + createdClient.clientSecret) |
77 | 58 | ||
78 | return callback(null) | 59 | return undefined |
79 | }) | 60 | }) |
80 | }) | 61 | }) |
81 | } | 62 | } |
82 | 63 | ||
83 | function createOAuthAdminIfNotExist (callback: (err: Error) => void) { | 64 | function createOAuthAdminIfNotExist () { |
84 | usersExist(function (err, exist) { | 65 | return usersExist().then(exist => { |
85 | if (err) return callback(err) | ||
86 | |||
87 | // Nothing to do, users already exist | 66 | // Nothing to do, users already exist |
88 | if (exist === true) return callback(null) | 67 | if (exist === true) return undefined |
89 | 68 | ||
90 | logger.info('Creating the administrator.') | 69 | logger.info('Creating the administrator.') |
91 | 70 | ||
@@ -116,14 +95,12 @@ function createOAuthAdminIfNotExist (callback: (err: Error) => void) { | |||
116 | role | 95 | role |
117 | } | 96 | } |
118 | 97 | ||
119 | db.User.create(userData, createOptions).asCallback(function (err, createdUser) { | 98 | return db.User.create(userData, createOptions).then(createdUser => { |
120 | if (err) return callback(err) | ||
121 | |||
122 | logger.info('Username: ' + username) | 99 | logger.info('Username: ' + username) |
123 | logger.info('User password: ' + password) | 100 | logger.info('User password: ' + password) |
124 | 101 | ||
125 | logger.info('Creating Application table.') | 102 | logger.info('Creating Application table.') |
126 | db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }).asCallback(callback) | 103 | return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) |
127 | }) | 104 | }) |
128 | }) | 105 | }) |
129 | } | 106 | } |