Guest User

Untitled

a guest
Apr 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. # Description:
  2. # Launch Ansible Tower play
  3. #
  4. # Dependencies:
  5. # None
  6. #
  7. # Configuration:
  8. # None
  9. #
  10. # Commands:
  11. # hugbot ansible deploy - Deploy master code to production site
  12. # hugbot ansible test <site> <user> - Run tests on <site> and post the results to <user>'s slack channel
  13. #
  14. # Author:
  15. # Reuben
  16.  
  17. {exec} = require 'child_process'
  18.  
  19. # Get the host for later links
  20. tower_host = require('child_process').execSync("tower-cli config host").toString().split("host: ").pop().trim()
  21. console.log("ansible_tower.coffee tower host: " + tower_host)
  22.  
  23. # Info for how to run each play. Only "id" is required
  24. #
  25. # Documenting usernames for "authorized_users"
  26. # Reuben: runruh
  27. plays = {
  28. "deploy": {
  29. "id": 175,
  30. "hard_coded_args": [
  31. "site_name=www branch=master"
  32. ],
  33. "authorized_users": [
  34. "runruh"
  35. ]
  36. }
  37. "test": {
  38. "id": 153,
  39. "args": [
  40. "site_name",
  41. "slack_user"
  42. ]
  43. }
  44. }
  45.  
  46. module.exports = (robot) ->
  47.  
  48. robot.respond /ansible\s(\S+)\s?(.*)/i, (res) ->
  49. play = plays[res.match[1]]
  50. console.log("ansible_tower.coffee play: ")
  51. console.log(play)
  52. extra_variables = ""
  53. data = {}
  54.  
  55. if play.authorized_users
  56. requestor = res.message.user.name
  57. console.log("ansible_tower.coffee Slack user: " + requestor)
  58. if requestor not in play.authorized_users
  59. console.log("Not authorized")
  60. return res.reply "Sorry, #{requestor}. You're not on the list. Only these meatbags can run this play: `#{play.authorized_users.toString()}`."
  61.  
  62. # Get any play options
  63. # TODO: vars is user input that will be executed in a shell.
  64. # It should be sanitized of shell metacharacters.
  65. vars = res.match[2].split " " if res.match[2]
  66. if play.args
  67. for arg, index in play.args
  68. extra_variables += arg + "=" + vars[index] + " "
  69. extra_variables += play.hard_coded_args if play.hard_coded_args
  70.  
  71. # Start the play and send a link to the play to Slack
  72. command = "tower-cli job launch -J #{play.id} -e '#{extra_variables}'"
  73. console.log("ansible_tower.coffee running: " + command)
  74. exec command, (error, stdout, stderr) ->
  75. res.send error if error
  76. res.send stderr if stderr
  77.  
  78. # Get job id for links we post to Slack and later tower-cli commands
  79. data = JSON.parse(stdout) if stdout
  80. job_id = data.id if data.id
  81.  
  82. # Build the job link
  83. job_link = "#{tower_host}/#/jobs/#{job_id}"
  84.  
  85. res.send "Started <#{job_link}|#{command}>"
  86.  
  87. # Wait for the play to finish
  88. command = "tower-cli job wait -f id #{job_id}"
  89. console.log("ansible_tower.coffee running: " + command)
  90. exec command, (error, stdout, stderr) ->
  91. res.send error if error
  92. res.send stderr if stderr
  93.  
  94. # Post success/fail to Slack when it's done
  95. command = "tower-cli job status #{job_id}"
  96. console.log("ansible_tower.coffee running: " + command)
  97. exec command, (error, stdout, stderr) ->
  98. res.send error if error
  99. res.send stderr if stderr
  100. console.log("ansible_tower.coffee play status: ")
  101. console.log(stdout)
  102.  
  103. data = JSON.parse(stdout)
  104.  
  105. # Convert seconds to minutes with 2 decimal places
  106. elapsed_minutes = data.elapsed / 60
  107. elapsed = Math.round(elapsed_minutes * 100) / 100
  108.  
  109. # Tada
  110. res.send "`#{res.match[0]}` completed after `#{elapsed} minutes` with status: <#{job_link}|#{data.status}>"
Add Comment
Please, Sign In to add comment