Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Description:
- # Launch Ansible Tower play
- #
- # Dependencies:
- # None
- #
- # Configuration:
- # None
- #
- # Commands:
- # hugbot ansible deploy - Deploy master code to production site
- # hugbot ansible test <site> <user> - Run tests on <site> and post the results to <user>'s slack channel
- #
- # Author:
- # Reuben
- {exec} = require 'child_process'
- # Get the host for later links
- tower_host = require('child_process').execSync("tower-cli config host").toString().split("host: ").pop().trim()
- console.log("ansible_tower.coffee tower host: " + tower_host)
- # Info for how to run each play. Only "id" is required
- #
- # Documenting usernames for "authorized_users"
- # Reuben: runruh
- plays = {
- "deploy": {
- "id": 175,
- "hard_coded_args": [
- "site_name=www branch=master"
- ],
- "authorized_users": [
- "runruh"
- ]
- }
- "test": {
- "id": 153,
- "args": [
- "site_name",
- "slack_user"
- ]
- }
- }
- module.exports = (robot) ->
- robot.respond /ansible\s(\S+)\s?(.*)/i, (res) ->
- play = plays[res.match[1]]
- console.log("ansible_tower.coffee play: ")
- console.log(play)
- extra_variables = ""
- data = {}
- if play.authorized_users
- requestor = res.message.user.name
- console.log("ansible_tower.coffee Slack user: " + requestor)
- if requestor not in play.authorized_users
- console.log("Not authorized")
- return res.reply "Sorry, #{requestor}. You're not on the list. Only these meatbags can run this play: `#{play.authorized_users.toString()}`."
- # Get any play options
- # TODO: vars is user input that will be executed in a shell.
- # It should be sanitized of shell metacharacters.
- vars = res.match[2].split " " if res.match[2]
- if play.args
- for arg, index in play.args
- extra_variables += arg + "=" + vars[index] + " "
- extra_variables += play.hard_coded_args if play.hard_coded_args
- # Start the play and send a link to the play to Slack
- command = "tower-cli job launch -J #{play.id} -e '#{extra_variables}'"
- console.log("ansible_tower.coffee running: " + command)
- exec command, (error, stdout, stderr) ->
- res.send error if error
- res.send stderr if stderr
- # Get job id for links we post to Slack and later tower-cli commands
- data = JSON.parse(stdout) if stdout
- job_id = data.id if data.id
- # Build the job link
- job_link = "#{tower_host}/#/jobs/#{job_id}"
- res.send "Started <#{job_link}|#{command}>"
- # Wait for the play to finish
- command = "tower-cli job wait -f id #{job_id}"
- console.log("ansible_tower.coffee running: " + command)
- exec command, (error, stdout, stderr) ->
- res.send error if error
- res.send stderr if stderr
- # Post success/fail to Slack when it's done
- command = "tower-cli job status #{job_id}"
- console.log("ansible_tower.coffee running: " + command)
- exec command, (error, stdout, stderr) ->
- res.send error if error
- res.send stderr if stderr
- console.log("ansible_tower.coffee play status: ")
- console.log(stdout)
- data = JSON.parse(stdout)
- # Convert seconds to minutes with 2 decimal places
- elapsed_minutes = data.elapsed / 60
- elapsed = Math.round(elapsed_minutes * 100) / 100
- # Tada
- res.send "`#{res.match[0]}` completed after `#{elapsed} minutes` with status: <#{job_link}|#{data.status}>"
Add Comment
Please, Sign In to add comment