-
Notifications
You must be signed in to change notification settings - Fork 4
/
prepare_site
executable file
·101 lines (84 loc) · 4.59 KB
/
prepare_site
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const opt = require('node-getopt');
const mongodb = require('mongodb');
var params = opt.create([
['h', 'help' , 'output usage information'],
['' , 'show' , 'show all sites that already exist'],
['' , 'site-name=<arg>' , 'specify site name'],
['' , 'default-admin=<arg>' , 'specify default admin email'],
['' , 'domain-name=<arg>+' , 'specify domain name'],
['' , 'free-register=<arg>' , 'specify free register option for users (true|false)']
])
.bindHelp()
.parseSystem();
if (!params.options['site-name'] && !params.options['show']) {
console.log('Failed to prepare site. Please specify a site name.');
return;
}
var crypto_key = '', hash_key = '';
for (var i = 0; i < 5; i++) {
crypto_key += Math.random().toString(32).substr(2);
hash_key += Math.random().toString(32).substr(2);
}
var free_register = params.options['free-register'] ? (params.options['free-register'] === 'true') : true;
var site_to_update = {"_id": params.options['site-name']};
if (params.options['free-register']) site_to_update['free_register'] = free_register;
if (params.options['default-admin']) site_to_update['default_admin'] = params.options['default-admin'];
if (params.options['domain-name'] ) site_to_update['names'] = params.options['domain-name'];
var mongoClient = mongodb.MongoClient;
var mongoHost = process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/mongo-sites';
if(params.options['show']) {
mongoClient.connect(mongoHost, function (err, db) {
if (err) {
setTimeout(function () {
process.exit();
}, 5000);
} else {
db.createCollection('_sites', function (err, collection) {
collection.find(params.options['site-name'] ? {_id: params.options['site-name']} : {}).toArray(function (err, sites) {
console.log(sites);
db.close();
});
});
}
});
} else if(params.options['site-name']) {
mongoClient.connect(mongoHost, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server from environment variable MONGO_URL=' + mongoHost + '. Error:', err);
setTimeout(function () {
process.exit();
}, 5000);
} else {
console.log('Connected to mongodb.');
db.createCollection('_sites', function (err, collection) {
collection.find({"_id": params.options['site-name']}).toArray(function (err, sites) {
site_to_update.hash_key = sites[0] ? sites[0].hash_key || hash_key : hash_key;
site_to_update.crypto_key = sites[0] ? sites[0].crypto_key || crypto_key : crypto_key;
collection.update(
{"_id": params.options['site-name']},
site_to_update,
{upsert: true},
function () {
if (!err) console.log('Collection _sites created. Site ' + params.options['site-name'] + ' added.');
else console.log('Failed to create _sites collection.', err);
db.createCollection('site-' + params.options['site-name'], function (err, collection) {
if (!err) console.log('Collection site-' + params.options['site-name'] + ' created.');
else console.log('Failed to create site-' + params.options['site-name'] + 'collection.', err);
db.createCollection('site-' + params.options['site-name'] + '-users', function (err, collection) {
if (!err) console.log('Collection site-' + params.options['site-name'] + '-users created.');
else console.log('Failed to create site-' + params.options['site-name'] + '-users collection.', err);
db.createCollection('site-' + params.options['site-name'] + '-plugins', function (err, collection) {
if (!err) console.log('Collection site-' + params.options['site-name'] + '-plugins created.');
else console.log('Failed to create site-' + params.options['site-name'] + '-plugins collection.', err);
db.close();
});
});
});
}
);
});
});
}
});
}