diff --git a/server/package.json b/server/package.json index ce25d97..5e1abca 100644 --- a/server/package.json +++ b/server/package.json @@ -7,7 +7,7 @@ "start": "babel-node src/server.js", "start:prod": "NODE_ENV=production node dist/server.js", "build": "rm -rf dist && babel src -d dist -s", - "deploy": "rsync -vr -e ssh --exclude-from .rsync-exclude * ubuntu@gs-1:deighton-ar-server/ && ssh ubuntu@gs-1 'cd deighton-ar-server && npm install'", + "deploy": "rsync -vr -e ssh --exclude-from .rsync-exclude * $SNAP_DEPLOY_USER@$SNAP_DEPLOY_HOST:deighton-ar-server/ && ssh $SNAP_DEPLOY_USER@$SNAP_DEPLOYHOST 'cd deighton-ar-server && npm install & sudo ./ops restart & sudo ./ops --test restart'", "test": "jest", "actor:api": "monzilla 'src/api/**/*.js:src/database/**/*.js' -- babel-node src/api/index.js", "actor:api:debug": "babel-node --inspect-brk src/api/index.js", diff --git a/server/src/bin/restart.js b/server/src/bin/restart.js new file mode 100644 index 0000000..19a72c8 --- /dev/null +++ b/server/src/bin/restart.js @@ -0,0 +1,61 @@ +import child_process from "child_process" +import path from "path" +import os from "os" +import sleep from "sleep-promise" +import { promisify } from "util" +import config from "config" +import chalk from "chalk" +import autobind from "autobind-decorator" + +const exec = promisify(child_process.exec) + +@autobind +class RestartTool { + constructor(toolName, log) { + this.toolName = toolName + this.log = log + } + + async run(argv) { + let userInfo = os.userInfo() + + if (userInfo.username !== "root") { + this.log.error("Script must be run as root") + return -1 + } + + const serviceName = config.get("serviceName.system") + + this.log.info(`Restarting ${serviceName}`) + + await exec(`systemctl restart ${serviceName}`) + + this.log.info("Waiting for service to start...") + await sleep(2000) + await exec(`systemctl status ${serviceName} --no-pager`) + + this.log.info(`Service ${serviceName} successfully restarted`) + + return 0 + } +} + +const log = { + info: console.error, + error: function() { + console.error(chalk.red("error:", [...arguments].join(" "))) + }, + warning: function() { + console.error(chalk.yellow("warning:", [...arguments].join(" "))) + }, +} + +const tool = new RestartTool("restart", log) +tool + .run(process.argv.slice(2)) + .then((exitCode) => { + process.exitCode = exitCode + }) + .catch((err) => { + console.error(err) + })