-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (95 loc) · 2.89 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var fs = require('graceful-fs'),
path = require('path'),
mkdirp = require('mkdirp'),
Mustache = require('mustache'),
exec = require('child_process').exec;
nginx = [];
gitHooks = [];
var config = require('./config/server');
fs.readdirSync("./config/nginx/").forEach(function(file) {
if (file != 'example.json') {
nginx.push(fs.readFileSync("./config/nginx/" + file));
}
});
fs.readdirSync("./config/gitHooks/").forEach(function(file) {
if (file != 'example.json') {
gitHooks.push(fs.readFileSync("./config/gitHooks/" + file));
}
});
updateHosts = function (servers) {
try {
var hostsFile = fs.readFileSync(config.hostsPath, 'utf8');
} catch (e) {
console.log(e);
}
if (hostsFile.lastIndexOf('### Server Manager') < 0) {
hostsFile += "\n\n### Server Manager\n";
}
servers.forEach(function (server) {
if (hostsFile.lastIndexOf(server.name) < 0 && server.enabled) {
hostsFile += "\n127.0.0.1 "+server.name;
}
});
try {
fs.writeFileSync(config.hostsPath, hostsFile);
} catch (e) {
console.log(e);
}
console.log(hostsFile);
}
nginx.forEach(function (jsonObj) {
// Remove all Enabled files.
fs.readdirSync(config.enabledPath).forEach(function(file) {
var filePath = config.enabledPath+'/'+file;
fs.unlink(filePath);
});
var servers = JSON.parse(jsonObj);
servers.forEach(function (server) {
var modelFile = fs.readFileSync('./models/nginx/'+server.type+'.mustache').toString();
var output = Mustache.render(modelFile, {logsFolder: config.logsFolder, server: server});
var availablePath = config.availablePath+"/"+server.name+".conf";
var enabledPath = config.enabledPath+"/"+server.name+".conf";
try {
fs.unlinkSync(enabledPath);
} catch (e) {
}
try {
fs.writeFileSync(availablePath, output);
console.log('Created config file for '+server.name);
} catch (e) {
console.log(e);
}
if (server.enabled) {
try {
mkdirp(config.logsFolder+'/'+server.name, function(err) {
if (err) {
console.log(err);
}
});
fs.symlinkSync(availablePath, enabledPath);
console.log('Linked config file for '+server.name);
} catch (e) {
console.log(e);
}
}
});
if (config.updateHosts) {
updateHosts(servers);
}
});
if (config.doRestart) {
exec(config.restartCommand, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
gitHooks.forEach(function (jsonObj) {
var gitHook = JSON.parse(jsonObj);
gitHook.hooks.forEach(function (hook) {
hook.actions.forEach(function (action) {
var modelFile = fs.readFileSync('./models/gitHooks/'+hook.type+'/'+hook.version+'.mustache').toString();
var output = Mustache.render(modelFile, {folder: config.logsFolder, action: action});
});
});
});