asjon/scripts/shell.coffee

70 lines
2.4 KiB
CoffeeScript
Raw Normal View History

# 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) ->
2015-04-27 17:29:58 +02:00
res.message.user.room is process.env.ADMIN_ROOM?.replace(':','#') or res.robot.adapterName is 'shell'
runCmd = (cmd,res,cb) ->
2015-04-27 19:27:35 +02:00
if res? then res.send 'Operazione in corso: '+cmd
2015-04-28 14:07:22 +02:00
if process.env.TESTING_ASJON
# Fingi di eseguire l'operazione
res.send 'Operazione "completata": '+cmd
else cp.exec cmd, (err,stdout,stderr) ->
if res?
if err
res.send 'Operazione fallita:\n'+stderr
else
res.send 'Operazione completata:\n'+stdout
if cb? then cb err,stdout,stderr
module.exports = (robot) ->
robot.respond /aggiornati|scarica (?:gli )?aggiornamenti/i, (res) ->
2015-04-28 14:07:22 +02:00
if !isFromAdmin(res) then return res.send res.random nope
2015-04-28 10:11:01 +02:00
runCmd 'git pull && npm install && npm install --dev', res
robot.respond /(?:controlla gli )?aggiornamenti/i, (res) ->
2015-04-28 14:07:22 +02:00
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'git fetch && git status', res
2015-04-28 14:07:22 +02:00
robot.respond /(?:installa (?:le )?)?dipendenze/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
2015-04-28 10:11:01 +02:00
runCmd 'npm install && npm install --dev', res
2015-04-28 14:07:22 +02:00
robot.respond /(?:esegui (?:i )?)?test/i, (res) ->
if !isFromAdmin(res) then return res.send res.random nope
runCmd 'npm test', res
2015-04-28 10:11:01 +02:00
# Run tests on boot and report to ADMIN
if process.env.AUTO_RUN_TESTS and process.env.ADMIN_ROOM
dest = room: process.env.ADMIN_ROOM.replace(':','#')
robot.send dest, 'Operazione in corso: npm test'
runCmd 'npm test', null, (err,stdout,stderr) ->
if err
robot.send dest, 'ATTENZIONE: TEST FAILURE\n'+stdout+stderr
else
robot.send dest, 'Test superati con successo:\n'+stdout
robot.on 'githubhook', (data,params) ->
if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE
2015-04-28 10:11:01 +02:00
runCmd 'git pull && npm install && npm install --dev', null, ->
dest = name: params.name, room: params.room.replace(':','#')
robot.send dest, 'riavvio in 5 SECONDI'
reboot = ->
console.log 'AUTO IMPICCAGIONE IN CORSO!'
process.exit 0
setTimeout reboot, 5000