Skip to content

Commit f8edeb2

Browse files
committed
fixed migration and setup
more info: knex/knex#2820
1 parent e2e9835 commit f8edeb2

File tree

2 files changed

+122
-104
lines changed

2 files changed

+122
-104
lines changed

backend/migrations/20190227065017_settings.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ exports.up = function (knex/*, Promise*/) {
2222
})
2323
.then(() => {
2424
logger.info('[' + migrate_name + '] setting Table created');
25-
26-
// TODO: add settings
27-
let settingModel = require('../models/setting');
28-
29-
return settingModel
30-
.query()
31-
.insert({
32-
id: 'default-site',
33-
name: 'Default Site',
34-
description: 'What to show when Nginx is hit with an unknown Host',
35-
value: 'congratulations',
36-
meta: {}
37-
});
38-
})
39-
.then(() => {
40-
logger.info('[' + migrate_name + '] Default settings added');
4125
});
4226
};
4327

backend/setup.js

Lines changed: 122 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,111 +5,145 @@ const logger = require('./logger').setup;
55
const userModel = require('./models/user');
66
const userPermissionModel = require('./models/user_permission');
77
const authModel = require('./models/auth');
8+
const settingModel = require('./models/setting');
89
const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
910

10-
module.exports = function () {
11-
return new Promise((resolve, reject) => {
12-
// Now go and check if the jwt gpg keys have been created and if not, create them
13-
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
14-
logger.info('Creating a new JWT key pair...');
11+
function setupJwt(resolve, reject) {
12+
// Now go and check if the jwt gpg keys have been created and if not, create them
13+
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
14+
logger.info('Creating a new JWT key pair...');
1515

16-
// jwt keys are not configured properly
17-
const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
18-
let config_data = {};
16+
// jwt keys are not configured properly
17+
const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
18+
let config_data = {};
1919

20-
try {
21-
config_data = require(filename);
22-
} catch (err) {
23-
// do nothing
24-
if (debug_mode) {
25-
logger.debug(filename + ' config file could not be required');
26-
}
20+
try {
21+
config_data = require(filename);
22+
} catch (err) {
23+
// do nothing
24+
if (debug_mode) {
25+
logger.debug(filename + ' config file could not be required');
2726
}
27+
}
2828

29-
// Now create the keys and save them in the config.
30-
let key = new NodeRSA({b: 2048});
31-
key.generateKeyPair();
32-
33-
config_data.jwt = {
34-
key: key.exportKey('private').toString(),
35-
pub: key.exportKey('public').toString()
36-
};
29+
// Now create the keys and save them in the config.
30+
let key = new NodeRSA({b: 2048});
31+
key.generateKeyPair();
3732

38-
// Write config
39-
fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
40-
if (err) {
41-
logger.error('Could not write JWT key pair to config file: ' + filename);
42-
reject(err);
43-
} else {
44-
logger.info('Wrote JWT key pair to config file: ' + filename);
33+
config_data.jwt = {
34+
key: key.exportKey('private').toString(),
35+
pub: key.exportKey('public').toString()
36+
};
4537

46-
logger.warn('Restarting interface to apply new configuration');
47-
process.exit(0);
48-
}
49-
});
38+
// Write config
39+
fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
40+
if (err) {
41+
logger.error('Could not write JWT key pair to config file: ' + filename);
42+
reject(err);
43+
} else {
44+
logger.info('Wrote JWT key pair to config file: ' + filename);
5045

51-
} else {
52-
// JWT key pair exists
53-
if (debug_mode) {
54-
logger.debug('JWT Keypair already exists');
46+
logger.warn('Restarting interface to apply new configuration');
47+
process.exit(0);
5548
}
49+
});
5650

57-
resolve();
51+
} else {
52+
// JWT key pair exists
53+
if (debug_mode) {
54+
logger.debug('JWT Keypair already exists');
5855
}
59-
})
60-
.then(() => {
56+
57+
resolve();
58+
}
59+
}
60+
61+
function setupDefaultUser() {
62+
(userModel
63+
.query()
64+
.select(userModel.raw('COUNT(`id`) as `count`'))
65+
.where('is_deleted', 0)
66+
.first()
67+
).then( (row) => {
68+
if (!row.count) {
69+
// Create a new user and set password
70+
logger.info('Creating a new user: [email protected] with password: changeme');
71+
72+
let data = {
73+
is_deleted: 0,
74+
75+
name: 'Administrator',
76+
nickname: 'Admin',
77+
avatar: '',
78+
roles: ['admin']
79+
};
80+
6181
return userModel
6282
.query()
63-
.select(userModel.raw('COUNT(`id`) as `count`'))
64-
.where('is_deleted', 0)
65-
.first();
66-
})
67-
.then((row) => {
68-
if (!row.count) {
69-
// Create a new user and set password
70-
logger.info('Creating a new user: [email protected] with password: changeme');
71-
72-
let data = {
73-
is_deleted: 0,
74-
75-
name: 'Administrator',
76-
nickname: 'Admin',
77-
avatar: '',
78-
roles: ['admin']
79-
};
83+
.insertAndFetch(data)
84+
.then( (user) => {
85+
return authModel
86+
.query()
87+
.insert({
88+
user_id: user.id,
89+
type: 'password',
90+
secret: 'changeme',
91+
meta: {}
92+
})
93+
.then(() => {
94+
return userPermissionModel
95+
.query()
96+
.insert({
97+
user_id: user.id,
98+
visibility: 'all',
99+
proxy_hosts: 'manage',
100+
redirection_hosts: 'manage',
101+
dead_hosts: 'manage',
102+
streams: 'manage',
103+
access_lists: 'manage',
104+
certificates: 'manage'
105+
});
106+
});
107+
})
108+
.then(() => {
109+
logger.info('Initial admin setup completed');
110+
});
111+
} else if (debug_mode) {
112+
logger.debug('Admin user setup not required');
113+
}
114+
});
115+
}
80116

81-
return userModel
117+
function setupDefaultSettings() {
118+
return settingModel
119+
.query()
120+
.select(userModel.raw('COUNT(`id`) as `count`'))
121+
.first()
122+
.then( (row) => {
123+
if (!row.count) {
124+
settingModel
82125
.query()
83-
.insertAndFetch(data)
84-
.then((user) => {
85-
return authModel
86-
.query()
87-
.insert({
88-
user_id: user.id,
89-
type: 'password',
90-
secret: 'changeme',
91-
meta: {}
92-
})
93-
.then(() => {
94-
return userPermissionModel
95-
.query()
96-
.insert({
97-
user_id: user.id,
98-
visibility: 'all',
99-
proxy_hosts: 'manage',
100-
redirection_hosts: 'manage',
101-
dead_hosts: 'manage',
102-
streams: 'manage',
103-
access_lists: 'manage',
104-
certificates: 'manage'
105-
});
106-
});
107-
})
108-
.then(() => {
109-
logger.info('Initial setup completed');
126+
.insert({
127+
id: 'default-site',
128+
name: 'Default Site',
129+
description: 'What to show when Nginx is hit with an unknown Host',
130+
value: 'congratulations',
131+
meta: {}
132+
}).then(() => {
133+
logger.info('Default settings added');
110134
});
111-
} else if (debug_mode) {
112-
logger.debug('Admin user setup not required');
135+
} if (debug_mode) {
136+
logger.debug('Default setting setup not required');
113137
}
114138
});
139+
}
140+
141+
module.exports = function () {
142+
return new Promise((resolve, reject) => {
143+
return setupJwt(resolve, reject);
144+
}).then(() => {
145+
return setupDefaultUser();
146+
}).then(() => {
147+
return setupDefaultSettings();
148+
});
115149
};

0 commit comments

Comments
 (0)