diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/bootstrap.js | 10 | ||||
-rw-r--r-- | shared/updates.js | 153 | ||||
-rw-r--r-- | shared/updates.sh | 80 |
3 files changed, 243 insertions, 0 deletions
diff --git a/shared/bootstrap.js b/shared/bootstrap.js new file mode 100644 index 0000000..566be3a --- /dev/null +++ b/shared/bootstrap.js @@ -0,0 +1,10 @@ +const chalk = require('chalk'); + +for (let i = 0; i < __CLUSTER_CPUSCOUNT; i++) { + cluster.fork(); +} + +cluster.on('exit', (worker, code, signal) => { + console.log(chalk.yellow("warn: ") + `worker #${worker.id} with pid ${worker.process.pid} died with code ${code}, signal ${signal}`); + cluster.fork(); +});
\ No newline at end of file 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() +} diff --git a/shared/updates.sh b/shared/updates.sh new file mode 100644 index 0000000..4291665 --- /dev/null +++ b/shared/updates.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +echo "updater: Downloading update..." + +wget https://github.com/Minteck-Projects/Neutron-Core/archive/trunk.zip &>/dev/null +code=$? +if [[ $code -eq 0 ]]; then + echo "updater: Success" +else + echo "updater: Failed with code $code" + echo "updater: Cleaning up..." + rm -dr trunk.zip* + exit $code +fi + +echo "updater: Extracting..." + +unzip trunk.zip +code=$? +if [[ $code -eq 0 || $code -eq 2 ]]; then + echo "updater: Success" +else + echo "updater: Failed with code $code" + echo "updater: Cleaning up..." + rm -dr trunk.zip* + exit $code +fi + +echo "updater: Please wait while Updater prepare Neutron-Core to be installed..." + +rm -dr ./public/api +rm -dr ./public/cms-special +rm -dr ./public/widgets +rm -dr ./public/resources/css +rm -dr ./public/resources/image +rm -dr ./public/resources/fonts +rm -dr ./public/resources/i18n +rm -dr ./public/resources/js +rm -dr ./public/resources/lib +rm -dr ./public/resources/private +rm -dr ./public/index.php + +echo "updater: Installing..." + +cp -r Neutron-Core-trunk/* ./public/ +code=$? +if [[ $code -eq 0 ]]; then + echo "updater: Success" +else + echo "updater: Failed with code $code" + echo "updater: Cleaning up..." + rm -dr trunk.zip* + exit $code +fi + +rm -dr Neutron-Core-trunk/ +code=$? +if [[ $code -eq 0 ]]; then + echo "updater: Success" +else + echo "updater: Failed with code $code" + echo "updater: Cleaning up..." + rm -dr trunk.zip* + exit $code +fi + +echo "updater: Updating version..." +date +%Y%m%d-#-electrode > ./public/api/version +code=$? +if [[ $code -eq 0 ]]; then + echo "updater: Success" +else + echo "updater: Failed with code $code" + echo "updater: Cleaning up..." + rm -dr trunk.zip* + exit $code +fi + +echo "updater: Cleaning up..." +rm -dr trunk.zip* |