Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (69 sloc) 2.69 kb
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
# Description:
# Interact with Scalarium cloud hosting
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_SCALARIUM_TOKEN: for authenticating the requests (see https://manage.scalarium.com/users/<user-id>/api)
#
# Commands:
# hubot scalarium list (apps|clouds) - Lists all applications/clouds on scalarium
# hubot scalarium deploy <app id> <message> - Triggers an deployment of *app id* with *message*
#
# Author:
# nesQuick

https = require 'https'
token = process.env.HUBOT_SCALARIUM_TOKEN

class ScalariumClient

  constructor: (@https, @token) ->

  getApplications: (cb) ->
    @request '/applications', 'GET', {}, cb

  getClouds: (cb) ->
    @request '/clouds', 'GET', {}, cb

  deploy: (appId, msg, cb, finishedCb) ->
    that = this
    @request "/applications/#{appId}/deploy", 'POST', {command: 'deploy', comment: "Hubot deploy - #{msg}"}, (result) ->
      that.registerRunningDeploy(result, finishedCb)
      cb result

  registerRunningDeploy: (deploy, cb) ->
    that = this
    intervalId = setInterval () ->
      that.request "/applications/#{deploy.application_id}/deployments/#{deploy.id}", 'GET', {}, (result) ->
        if result.status isnt 'running'
          cb result
          clearInterval intervalId
    , 10000

  request: (path, method, body, cb) ->
    options =
      host: 'manage.scalarium.com'
      method: method
      path: "/api#{path}"
      headers:
        'X-Scalarium-Token': @token
        Accept: 'application/vnd.scalarium-v1+json'
    req = @https.request options, (res) ->
      data = ''
      res.on('data', (chunk) -> data = "#{data}#{chunk}")
      res.on('end', () -> cb JSON.parse data)
    req.write JSON.stringify body
    req.end()

client = new ScalariumClient https, token

module.exports = (robot)->

  robot.respond /scalarium list apps/i, (message)->
    client.getApplications (apps) ->
      for app in apps
        message.send "#{app.name} - #{app.id}"

  robot.respond /scalarium list clouds/i, (message)->
    client.getClouds (clouds) ->
      for cloud in clouds
        message.send "#{cloud.name} - #{cloud.id}"

  robot.respond /scalarium deploy ([0-9a-f]+) (.+)$/i, (message)->
    client.deploy message.match[1], message.match[2], (deploy) ->
      message.send "Yes Sir! Deployment triggered with id #{deploy.id}. Will drop a note when it's done."
    , (finished) ->
      success = finished.status == 'successful'
      message.send """#{if success then 'Success' else 'FAIL! FAIL! FAIL!!'}! Your deployment "#{finished.comment}" with id #{finished.id} #{if success then 'is done' else 'failed'}."""

  robot.scalarium =
    getApplications: client.getApplications
    getClouds: client.getClouds
Something went wrong with that request. Please try again.