aboutsummaryrefslogtreecommitdiff
path: root/shared/updates.js
diff options
context:
space:
mode:
authorMinteck <freeziv.ytb@gmail.com>2021-03-07 18:29:17 +0100
committerMinteck <freeziv.ytb@gmail.com>2021-03-07 18:29:17 +0100
commit0f79e708bf07721b73ea41e5d341be08e8ea4dce (patch)
treef3c63cd6a9f4ef0b26f95eec6a031600232e80c8 /shared/updates.js
downloadelectrode-0f79e708bf07721b73ea41e5d341be08e8ea4dce.tar.gz
electrode-0f79e708bf07721b73ea41e5d341be08e8ea4dce.tar.bz2
electrode-0f79e708bf07721b73ea41e5d341be08e8ea4dce.zip
Initial commit
Diffstat (limited to 'shared/updates.js')
-rw-r--r--shared/updates.js153
1 files changed, 153 insertions, 0 deletions
diff --git a/shared/updates.js b/shared/updates.js
new file mode 100644
index 0000000..b3ea281
--- /dev/null
+++ b/shared/updates.js
@@ -0,0 +1,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()
+}