-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.js
More file actions
169 lines (153 loc) · 4.51 KB
/
installer.js
File metadata and controls
169 lines (153 loc) · 4.51 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
author: Matteo Pallotta
*/
//installare il modulo i moduli sul server (e.g.): sudo npm install ncp
var mv = require('mv');
var Promise = require('lie');
var fs = require('fs');
var sleep = require('sleep');
var sys = require('sys')
var exec = require('child_process').exec;
var readline = require('readline');
var ncp = require('ncp').ncp;
ncp.limit = 32;
var adaptFile = function (filePath, nomeComune) {
return new Promise(
function(resolve, reject) {
fs.readFile(filePath, 'utf-8', function(err, data){
if (err) {
console.log('[ERROR] file not read');
return;
}
var result = data.replace(/(D|d)ocrs/g, nomeComune);
fs.writeFile(filePath, result, 'utf-8', function (err) {
resolve();
if (err) console.log('[ERROR] file not created');
});
});
}
);
}
function createDB(DBname) {
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec('mysql -uroot -e "create database ' + DBname + '"', puts); //TODO: PASSWORD
}
var initUsers = function(name) {
return new Promise(
function(resolve, reject) {
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("mysql -uroot " + name + " < queries" , puts); //TODO: PASSWORD
resolve();
}
);
}
var move = function(src, dst) {
return new Promise(
function(resolve, reject) {
mv(src, dst, function(err) {
if (err) console.log('[ERROR] file not moved');
resolve();
});
}
);
}
var copy = function(src, dst) {
return new Promise(
function(resolve, reject) {
ncp(src, dst, function(err) {
if (err) console.log('[ERROR] file not copied');
resolve();
});
}
);
}
var createInstance = function(dst, logoPath) {
return new Promise(
function(resolve, reject) {
ncp('./docrs',('./'+dst), function(err) {
if(err) {
console.log('[ERROR] folder not copied');
return;
} else {
createDB(dst);
adaptFile('./'+ dst +'/META-INF/maven/com.programmingfree/docrs/pom.xml',dst).then(
function(){adaptFile('./'+ dst +'/META-INF/maven/com.programmingfree/docrs/pom.properties',dst).then(
function(){adaptFile('./'+ dst +'/WEB-INF/classes/application.properties',dst).then(
function(){adaptFile('./'+ dst +'/WEB-INF/classes/static/js/main.js',dst).then(
function(){adaptFile('./'+ dst +'/WEB-INF/index.html',dst).then(
function(){adaptFile('./'+ dst +'/WEB-INF/login.html',dst).then(
function(){copy(logoPath, './'+ dst +'/WEB-INF/classes/static/images/logo_comune.png').then(
function(){
resolve();
}
);}
);}
);}
);}
);}
);}
);
}
});
}
);
}
var getLastLine = function() {
return new Promise(
function(resolve,reject) {
fs.readFile('../../logs/catalina.out', 'utf-8', function(err, data) {
if (err) throw err;
var lines = data.trim().split('\n');
var lastLine = lines.slice(-1)[0];
resolve(lastLine);
});
}
);
}
function regex(line, appName) {
var match = line.match('^.*'+appName+' has finished in .* ms$');
return (match !== null);
}
function userInterface(name){
promise = getLastLine();
promise.then(function(ll){
installed = regex(ll, name);
}).then(function(){
process.stdout.write('. ');
}).then(function(){
sleep.sleep(2);
}).then(function(resolve, reject){
if(!installed)
userInterface(name);
else{
initUsers(name).then(
function(){
console.log('\nINFO: THE INSTANCE HAS BEEN SUCCESSFULLY INSTALLED!\n\n####');
}
);
}
});
}
function install() {
console.log('####\n');
var r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r1.question('TYPE: INSERT THE NAME OF THE NEW INSTANCE (e.g: Roma): ', function(name) {
r1.close();
var r2 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r2.question('TYPE: INSERT THE ABSOLUTE LOGO PATH\n(must be a PNG file, e.g: /home/user/../logo.png): ', function(logo) {
r2.close();
console.log('INFO: INSTALLING THE NEW INSTANCE');
createInstance(name, logo).then(function(){
move('./'+name, '../'+name);
userInterface(name);
});
});
});
}
install();