Add systemd interface and reboot/shutdown endpoints

This commit is contained in:
Pablo Carranza Vélez 2015-08-21 00:21:41 +00:00
parent 27786ebe65
commit 3da4dae190
7 changed files with 53 additions and 4 deletions

View File

@ -16,7 +16,7 @@ WORKDIR /app
COPY package.json postinstall.sh /app/
RUN apt-get -q update \
&& apt-get install -qqy g++ libsqlite3-dev make --no-install-recommends \
&& JOBS=MAX npm install --unsafe-perm --production \
&& JOBS=MAX npm install --unsafe-perm --production --no-optional \
&& npm dedupe \
&& npm cache clean \
&& rm -rf /tmp/* \

View File

@ -16,7 +16,7 @@ WORKDIR /app
COPY package.json postinstall.sh /app/
RUN apt-get -q update \
&& apt-get install -qqy g++ libsqlite3-dev make --no-install-recommends \
&& JOBS=MAX npm install --unsafe-perm --production \
&& JOBS=MAX npm install --unsafe-perm --production --no-optional \
&& npm dedupe \
&& npm cache clean \
&& rm -rf /tmp/* \

View File

@ -16,7 +16,7 @@ WORKDIR /app
COPY package.json postinstall.sh /app/
RUN apt-get -q update \
&& apt-get install -qqy g++ libsqlite3-dev make --no-install-recommends \
&& JOBS=MAX npm install --unsafe-perm --production \
&& JOBS=MAX npm install --unsafe-perm --production --no-optional \
&& npm dedupe \
&& npm cache clean \
&& rm -rf /tmp/* \

View File

@ -16,7 +16,7 @@ WORKDIR /app
COPY package.json postinstall.sh /app/
RUN apt-get -q update \
&& apt-get install -qqy g++ libsqlite3-dev make --no-install-recommends \
&& JOBS=MAX npm install --unsafe-perm --production \
&& JOBS=MAX npm install --unsafe-perm --production --no-optional \
&& npm dedupe \
&& npm cache clean \
&& rm -rf /tmp/* \

View File

@ -11,6 +11,7 @@
"bluebird": "^2.9.24",
"body-parser": "^1.12.0",
"coffee-script": "~1.9.1",
"dbus-native": "^0.2.0",
"dockerode": "~2.2.1",
"event-stream": "^3.0.20",
"express": "^4.0.0",

View File

@ -8,6 +8,7 @@ express = require 'express'
bodyParser = require 'body-parser'
request = require 'request'
config = require './config'
systemd = require './systemd'
module.exports = (secret) ->
api = express()
@ -62,6 +63,16 @@ module.exports = (secret) ->
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
api.post '/v1/reboot', (req, res) ->
utils.mixpanelTrack('Reboot')
res.sendStatus(200)
systemd.reboot()
api.post '/v1/shutdown', (req, res) ->
utils.mixpanelTrack('Shutdown')
res.sendStatus(200)
systemd.shutdown()
api.post '/v1/purge', (req, res) ->
appId = req.body.appId
utils.mixpanelTrack('Purge /data', appId)

37
src/systemd.coffee Normal file
View File

@ -0,0 +1,37 @@
Promise = require('bluebird')
dbus = require('dbus-native')
execAsync = Promise.promisify(require('child_process').exec)
systemBus = dbus.systemBus()
module.exports = systemd =
iface: null
init: ->
return new Promise (resolve, reject) ->
systemBus.getService('org.freedesktop.systemd1')
.getInterface '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', (err, iface) ->
return reject(err) if err?
systemd.iface = iface
resolve()
proxyMethod: (method)->
args = Array.prototype.slice.call(arguments, 1)
Promise.try ->
return systemd.init() if !systemd.iface
.then ->
new Promise (resolve, reject) ->
cb = (err) ->
output = Array.prototype.slice.call(arguments, 1)
return reject(err) if err?
resolve(output)
args.push(cb)
systemd.iface[method].apply(systemd.iface, args)
shutdown: ->
execAsync('sync')
.then ->
systemd.proxyMethod('PowerOff')
reboot: ->
execAsync('sync')
.then ->
systemd.proxyMethod('Reboot')