Skip to content

Instantly share code, notes, and snippets.

@tcdw
Last active January 12, 2017 13:42
Show Gist options
  • Select an option

  • Save tcdw/61f9669de36fbf8a0ba956094f8f3066 to your computer and use it in GitHub Desktop.

Select an option

Save tcdw/61f9669de36fbf8a0ba956094f8f3066 to your computer and use it in GitHub Desktop.
#!/usr/bin/env nodejs
// DelogX (github.com/deluxghost/DelogX) Auto Upgrader
// By tcdw (github.com/tcdw)
// Released under The 3-Clause BSD License
var fs = require("fs");
var path = require('path');
var branch = 'master';
var dlxpath = path.resolve('/opt/delogx');
var suffix = Math.floor(new Date().getTime() / 1000);
var spawn = require('child_process').spawn;
var spawnSync = require('child_process').spawnSync;
var lp = '[Upgrader] '; // aka log prefix
mktemp = spawn('mktemp', ['-d']);
mktemp.stdout.on('data', (data) => {
console.log(lp + 'Downloading DelogX (' + branch + ') ...');
var tmpdir = (data + '').split("\n")[0];
dl = spawn('git', ['clone', 'git://github.com/deluxghost/DelogX', '-b', branch, tmpdir]);
dl.stdout.on('data', (data) => {
console.log(`${data}`);
});
dl.stderr.on('data', (data) => {
console.log(`${data}`);
});
dl.on('close', (code) => {
if (code != 0) {
console.error(lp + 'Git exec error: ' + code);
process.exit(1);
} else {
console.log(lp + 'Backing up old version of DelogX ...');
try {
// spawnSync('cp', ['-d', '-p', dlxpath, dlxpath + '-' + suffix]);
spawnSync('mv', [dlxpath, dlxpath + '-' + suffix]);
} catch (e) {
console.error(e);
console.log(lp + 'Backup failed');
console.log(lp + 'For data security reason, the upgrade process is terminated.');
process.exit(1);
}
console.log(lp + 'Upgrading ...');
try {
spawnSync('rm', ['-rf', tmpdir + '/posts']);
spawnSync('rm', ['-rf', tmpdir + '/pages']);
spawnSync('mv', [tmpdir + '/DelogX/templates', tmpdir + '/DelogX/templates-new']);
spawnSync('mv', [tmpdir + '/DelogX/static', tmpdir + '/DelogX/static-new']);
spawnSync('mv', [tmpdir + '/DelogX/config.py', tmpdir + '/DelogX/config.py-new']);
spawnSync('cp', ['-r', '-p', dlxpath + '-' + suffix + '/posts', tmpdir + '/posts']);
spawnSync('cp', ['-r', '-p', dlxpath + '-' + suffix + '/pages', tmpdir + '/pages']);
spawnSync('cp', ['-r', '-p', dlxpath + '-' + suffix + '/DelogX/templates', tmpdir + '/DelogX/templates']);
spawnSync('cp', ['-r', '-p', dlxpath + '-' + suffix + '/DelogX/static', tmpdir + '/DelogX/static']);
spawnSync('cp', ['-p', dlxpath + '-' + suffix + '/DelogX/config.py', tmpdir + '/DelogX/config.py']);
spawnSync('cp', ['-r', '-p', tmpdir, dlxpath]);
spawnSync('bash', ['cd ' + dlxpath + ' && chmod -R +x *.py']);
} catch (e) {
console.error(e);
console.log(lp + 'Upgrade failed');
console.log(lp + 'Don\'t panic, the old version backup is avaliable in ' + dlxpath + '-' + suffix);
console.log(lp + 'The upgrade process is terminated.');
process.exit(1);
}
console.log(lp + 'Finished!');
console.log(lp + 'Old version backup: ' + dlxpath + '-' + suffix);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment