gce_helper.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'ostruct'
  2. module OpenShift
  3. module Ops
  4. class GceHelper
  5. MYDIR = File.expand_path(File.dirname(__FILE__))
  6. def self.get_hosts()
  7. cmd = "#{MYDIR}/../inventory/gce/gce.py --list"
  8. hosts = %x[#{cmd} 2>&1]
  9. raise "Error: failed to list hosts\n#{hosts}" unless $?.exitstatus == 0
  10. # invert the hash so that it's key is the host, and values is an array of metadata
  11. data = {}
  12. JSON.parse(hosts).each do |key,value|
  13. value.each { |h| (data[h] ||= []) << key }
  14. end
  15. # For now, we only care about the name. In the future, we may want the other metadata included.
  16. retval = []
  17. data.keys.sort.each { |k| retval << OpenStruct.new({ :name => k }) }
  18. return retval
  19. end
  20. def self.get_host_details(host)
  21. cmd = "#{MYDIR}/../inventory/gce/gce.py --host #{host}"
  22. details = %x[#{cmd} 2>&1]
  23. raise "Error: failed to get host details\n#{details}" unless $?.exitstatus == 0
  24. retval = JSON.parse(details)
  25. raise "Error: host not found [#{host}]" if retval.empty?
  26. # Convert OpenShift specific tags to entries
  27. retval['gce_tags'].each do |tag|
  28. if tag =~ /\Ahost-type-([\w\d-]+)\z/
  29. retval['host-type'] = $1
  30. end
  31. if tag =~ /\Aenv-([\w\d]+)\z/
  32. retval['env'] = $1
  33. end
  34. end
  35. return retval
  36. end
  37. def self.generate_env_tag(env)
  38. return "env-#{env}"
  39. end
  40. def self.generate_env_tag_name(env)
  41. return "tag_#{generate_env_tag(env)}"
  42. end
  43. def self.generate_host_type_tag(host_type)
  44. return "host-type-#{host_type}"
  45. end
  46. def self.generate_host_type_tag_name(host_type)
  47. return "tag_#{generate_host_type_tag(host_type)}"
  48. end
  49. def self.generate_env_host_type_tag(env, host_type)
  50. return "env-host-type-#{env}-#{host_type}"
  51. end
  52. def self.generate_env_host_type_tag_name(env, host_type)
  53. return "tag_#{generate_env_host_type_tag(env, host_type)}"
  54. end
  55. end
  56. end
  57. end