Guest User

Untitled

a guest
Jun 25th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'chef'
  5. require 'chef/node'
  6. require 'chef/rest'
  7. require 'optparse'
  8. require 'ostruct'
  9.  
  10. Chef::Config.from_file("/etc/chef/server.rb")
  11. API_USERNAME=ENV['CHEF_USERNAME']
  12. API_PASSWORD=ENV['CHEF_PASSWORD']
  13. @rest = Chef::REST.new(Chef::Config[:registration_url])
  14. @rest.authenticate(API_USERNAME, API_PASSWORD)
  15.  
  16. def get_nodes_with_webapp(webapp)
  17. distinct = {}
  18. results = @rest.get_rest("search/node?q=#{webapp}&a=recipes")
  19. results.each do |result|
  20. distinct[result['id']] = 1
  21. end
  22. rv = []
  23. distinct.each do |node|
  24. node_name = node[0].sub('node_','')
  25. node_name.gsub!(".", "_")
  26. begin
  27. fullnode = @rest.get_rest("nodes/#{node_name}")
  28. if fullnode.fqdn
  29. rv << node_name
  30. end
  31. rescue
  32. end
  33. end
  34. rv
  35. end
  36.  
  37. def update_webapps(webapp, update_type)
  38. node_names = get_nodes_with_webapp(webapp)
  39. node_names.each do |node_name|
  40. node = @rest.get_rest("nodes/#{node_name}")
  41. # for each webapp under management on each node
  42. # there's an attribute set "#{webapp}_deploy_action"
  43. # that's normally set to "nothing" -- here we're
  44. # resetting it "deploy" or "rollback"
  45. attr = webapp + "_deploy_action"
  46. node[attr] = update_type
  47. puts "Storing node data for #{node_name}..."
  48. begin
  49. retries = 5
  50. @rest.put_rest("nodes/#{node_name}", node)
  51. rescue Net::HTTPFatalError
  52. retry if (retries -= 1) > 0
  53. end
  54. puts "Done."
  55. end
  56. end
  57.  
  58.  
  59. if __FILE__ == $0
  60. options = OpenStruct.new
  61. opts = OptionParser.new do |opts|
  62. opts.on("-w", "--webapp [ARG]", "Web app to update") do |w|
  63. if w.nil?
  64. puts "--webapp cannot be null"
  65. exit
  66. else
  67. options.webapp = w
  68. end
  69. end
  70. opts.on("-u", "--update [ARG]", "Type of update (deploy|rollback)") do |u|
  71. if u.nil?
  72. puts "--update must be 'deploy' or 'rollback'"
  73. exit
  74. else
  75. options.update = u
  76. end
  77. end
  78. end
  79. begin
  80. opts.parse!(ARGV)
  81. rescue OptionParser::ParseError => e
  82. puts e
  83. # put a usage message here
  84. end
  85. if options.webapp.nil? || options.update.nil?
  86. puts "Usage: #{$0} --webapp <webapp> --update <update>"
  87. exit
  88. end
  89. update_webapps(options.webapp, options.update)
  90. end
Add Comment
Please, Sign In to add comment