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
|
const http = require('http');
const https = require('https');
const chalk = require('chalk');
const fs = require('fs');
const tar = require('tar-fs');
const ProgressBar = require('progress');
var ncp = require('ncp').ncp;
ncp.limit = 4;
function erase(dir) {
if (fs.existsSync("./public/" + dir)) {
try {
fs.rmdirSync("./public/" + dir, { recursive: true })
} catch (e) {
console.log(chalk.yellow("warn: ") + "unable to delete folder " + dir);
}
}
}
function erasef(file) {
if (fs.existsSync("./public/" + file)) {
try {
fs.unlinkSync("./public/" + file)
} catch (e) {
console.log(chalk.yellow("warn: ") + "unable to delete file " + file);
}
}
}
module.exports = () => {
console.log(chalk.green("verb: ") + "loading update config...");
conf = require('../config/updates.json');
if (conf.check.ssl) {
lib = https;
} else {
lib = http;
}
const options = {
hostname: conf.check.host,
port: conf.check.port,
path: conf.check.path,
method: 'GET'
}
data = "";
const req = lib.request(options, res => {
if (res.statusCode != 200) {
console.log(chalk.yellow("warn: ") + "unable to check for updates: code " + res.statusCode);
global.__RUNNING = true;
if (!__SRUNNING) { require('./bootstrap.js'); global.__SRUNNING = true; }
return;
}
res.on('data', d => {
data = data + d;
})
res.on('end', () => {
trimmed = data.trim().split("-")[0]
console.log(chalk.blue("info: ") + "latest version: " + trimmed);
if (fs.existsSync("./public/api/version")) {
current = fs.readFileSync("./public/api/version").toString().trim().split("-")[0];
console.log(chalk.blue("info: ") + "installed version: " + current);
} else {
console.log(chalk.yellow("warn: ") + "the Neutron stack doesn't appears to be installed");
current = "0";
}
if (current == trimmed) {
console.log(chalk.blue("info: ") + "up to date, starting webserver...");
global.__RUNNING = true;
if (!__SRUNNING) { require('./bootstrap.js'); global.__SRUNNING = true; }
return;
} else {
console.log(chalk.blue("info: ") + "updating to " + trimmed + "...");
if (conf.update.ssl) {
lib = https;
} else {
lib = http;
}
var req = lib.request({
host: conf.update.host,
port: conf.update.port,
path: conf.update.path
});
console.log(chalk.blue("info: ") + "starting download...");
file = fs.createWriteStream("./cache/download.tar.gz");
req.on('response', function(res){
res.pipe(file);
console.log();
console.log(chalk.blue("info: ") + "writing on disk...");
res.on('end', function () {
console.log("\n" + chalk.blue("info: ") + "verifying update...");
fs.mkdirSync("./download");
fs.createReadStream("./cache/download.tar").pipe(tar.extract("./download", {
finish: () => {
fdir = fs.readdirSync("./download")[0];
dfile = fs.readdirSync("./download/" + fdir);
dfile.forEach((f) => {
fs.renameSync("./download/" + fdir + "/" + f, "./download/" + f);
})
fs.rmdirSync("./download/" + fdir);
erase("/api");
erase("/cms-special");
erase("/widgets");
erase("/resources/css");
erase("/resources/fonts");
erase("/resources/i18n");
erase("/resources/image");
erase("/resources/js");
erase("/resources/lib");
erase("/resources/private");
erasef("/resources/.htaccess");
erasef("/index.php");
console.log(chalk.blue("info: ") + "installing update...");
ncp("./download", "./public", function (err) {
if (err) {
console.log(chalk.yellow("warn: ") + "unable to install update: " + err.message);
global.__RUNNING = true;
if (!__SRUNNING) { require('./bootstrap.js'); global.__SRUNNING = true; }
return;
}
console.log(chalk.blue("info: ") + "update installed, starting webserver...");
global.__RUNNING = true;
if (!__SRUNNING) { require('./bootstrap.js'); global.__SRUNNING = true; }
return;
});
}
}));
});
});
}
})
})
req.on('error', error => {
console.log(chalk.yellow("warn: ") + "unable to check for updates: " + error.message);
global.__RUNNING = true;
if (!__SRUNNING) { require('./bootstrap.js'); global.__SRUNNING = true; }
return;
})
req.end()
}
|