public
Last active

Heroku Hubot Script

  • Download Gist
heroku.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# A limited way to interact with the Heroku API.
#
# INSTALLATION:
# 1. Create file in scripts folder in hubot folder
# 2. Update package.json for hubot and add dependency on "sprintf": "0.1.1"
# 3. Set heroku config variable HEROKU_USER to heroku user account to use
# 4. Set heroku config variable HEROKU_APIKEY to heroku user account apikey (from My Account page)
#
# heroku status - Retrieve the most recent tweet from the @herokustatus account
# heroku ps --app <appname> - Get process information for application
# heroku ps:scale <process>=<quantity> --app <appname> - Scale the process to quantity for application
# heroku ps:restart <process> --app <appname> - Restart the specified process for application#
# heroku ps:stop <process> --app <appname> - Stop the specified process for application
#
# This is a WORK IN PROGRESS
#
sprintf = require("sprintf").sprintf
 
user = process.env.HEROKU_USER
apiKey = process.env.HEROKU_APIKEY
 
module.exports = (robot) ->
# heroku ps --app <appname> - returns process information of heroku app
robot.respond /heroku ps --app (.*)/i, (msg) ->
application = msg.match[1]
uri = 'https://api.heroku.com/apps/' + application + '/ps'
# get(uri)
msg.http(uri)
.headers(Authorization: "Basic #{new Buffer("#{user}:#{apiKey}").toString("base64")}", Accept: "application/json")
.get() (err, res, body) ->
if res.statusCode is 404
msg.send "Application not found"
return
 
results = JSON.parse(body)
 
if res.statusCode is 403
msg.send results.error
return
 
output = ""
output += "Process State Command\n"
output += "------------ ------------------ ------------------------------\n"
 
for process in results
output += sprintf("%-12s %-18s %-s", process.process, process.pretty_state, process.command) + "\n"
msg.send output
 
# heroku ps:scale <process>=<count> --app <appname>
robot.respond /heroku ps:scale (.*)=(\d+) --app (.*)/i, (msg) ->
type = msg.match[1]
qty = msg.match[2]
application = msg.match[3]
uri = 'https://api.heroku.com/apps/' + application + '/ps/scale'
msg.http(uri)
.headers(Authorization: "Basic #{new Buffer("#{user}:#{apiKey}").toString("base64")}", Accept: "application/json", 'Content-Length': 0)
.query(type: type, qty: qty)
.post() (err, res, body) ->
if res.statusCode is 404
msg.send "Application not found"
return
if res.statusCode is 403
msg.send JSON.parse(body).error
return
 
msg.send "Now running: " + body
 
# heroku ps:restart <process> --app <appname>
robot.respond /heroku ps:restart (.*) --app (.*)/i, (msg) ->
type = msg.match[1]
application = msg.match[2]
uri = 'https://api.heroku.com/apps/' + application + '/ps/restart'
msg.http(uri)
.headers(Authorization: "Basic #{new Buffer("#{user}:#{apiKey}").toString("base64")}", Accept: "application/json", 'Content-Length': 0)
.query(type: type)
.post() (err, res, body) ->
if res.statusCode is 404
msg.send "Application not found"
return
 
if res.statusCode is 403
msg.send JSON.parse(body).error
return
 
msg.send "Restarted " + type + " processes"
 
# heroku ps:stop <process> --app <appname>
robot.respond /heroku ps:stop (.*) --app (.*)/i, (msg) ->
type = msg.match[1]
application = msg.match[2]
uri = 'https://api.heroku.com/apps/' + application + '/ps/stop'
msg.http(uri)
.headers(Authorization: "Basic #{new Buffer("#{user}:#{apiKey}").toString("base64")}", Accept: "application/json", 'Content-Length': 0)
.query(type: type)
.post() (err, res, body) ->
if res.statusCode is 404
msg.send "Application not found"
return
 
if res.statusCode is 403
msg.send JSON.parse(body).error
return
 
msg.send "Stopped " + type + " processes"
 
# heroku status
robot.respond /heroku status/, (msg) ->
msg.http("http://api.twitter.com/1/statuses/user_timeline/herokustatus.json?count=1")
.get() (err, res, body) ->
response = JSON.parse body
if response[0]
msg.send "#{response[0]['created_at']} > #{response[0]['text']}"
else
msg.send "Error"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.