asjon/scripts/shell.coffee
2015-04-29 17:34:38 +02:00

73 lines
2.4 KiB
CoffeeScript

# Description:
# abilità di asjon di usare la shell *nix
#
# Requires:
# None
#
# Commands:
# asjon run/esegui/shell git/npm args...
#
# Author:
# Enrico Fasoli (fazo96)
cp = require 'child_process'
nope = ['BZBZ 400-BAD-REQUEST', 'BZBZ DOES-NOT-COMPUTE', 'BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED', 'BZBZ ACCESS-DENIED']
isFromAdmin = (res) ->
res.message.user.room is process.env.ADMIN_ROOM?.replace(':','#') or res.robot.adapterName is 'shell'
runCmd = (cmd,res,cb) ->
if res?.send? then res.send 'Operazione in corso: '+cmd
if process.env.TESTING_ASJON
# Fingi di eseguire l'operazione
if res?.send? then res.send 'Operazione "completata": '+cmd
if cb?.call? then cb null,'',''
else cp.exec cmd, (err,stdout,stderr) ->
if res?.send?
if err
res.send 'Operazione fallita:\n'+stdout+stderr
else
res.send 'Operazione completata:\n'+stdout
if cb?.call? then cb err,stdout,stderr
module.exports = (robot) ->
# Lancia i test all'avvio
if process.env.AUTO_RUN_TESTS and process.env.ADMIN_ROOM
dest = room: process.env.ADMIN_ROOM.replace(':','#')
dest.send = (x) -> robot.send dest, x
runCmd 'npm test', dest
if process.env.AUTO_INFORM_ON_START
r = room: process.env.AUTO_INFORM_ON_START.replace(':','#')
robot.send r, 'asjon avviato e operativo!'
robot.respond /aggiornati|scarica (?:gli )?aggiornamenti/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'git pull && npm install', res
robot.respond /(?:controlla gli )?aggiornamenti/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'git fetch && git status', res
robot.respond /(?:installa (?:le )?)?dipendenze/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'npm install', res
robot.respond /(?:esegui (?:i )?)?test/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'npm test', res
robot.on 'githubhook', (data,params) ->
if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE
dest = name: params.name, room: params.room.replace(':','#')
res = send: (x) -> robot.send dest, x
runCmd 'git pull && npm install', res, ->
robot.send dest, 'riavvio in 5 SECONDI'
reboot = -> process.exit 0
unless process.env.TESTING_ASJON
# Non riavviare durante i test
setTimeout reboot, 5000