awsutil.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # vim: expandtab:tabstop=4:shiftwidth=4
  2. import subprocess
  3. import os
  4. import json
  5. import re
  6. class AwsUtil(object):
  7. def __init__(self):
  8. self.host_type_aliases = {
  9. 'legacy-openshift-broker': ['broker', 'ex-srv'],
  10. 'openshift-node': ['node', 'ex-node'],
  11. 'openshift-messagebus': ['msg'],
  12. 'openshift-customer-database': ['mongo'],
  13. 'openshift-website-proxy': ['proxy'],
  14. 'openshift-community-website': ['drupal'],
  15. 'package-mirror': ['mirror'],
  16. }
  17. self.alias_lookup = {}
  18. for key, values in self.host_type_aliases.iteritems():
  19. for value in values:
  20. self.alias_lookup[value] = key
  21. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  22. self.multi_ec2_path = os.path.realpath(os.path.join(self.file_path, '..','inventory','multi_ec2.py'))
  23. def get_inventory(self,args=[]):
  24. cmd = [self.multi_ec2_path]
  25. if args:
  26. cmd.extend(args)
  27. env = os.environ
  28. p = subprocess.Popen(cmd, stderr=subprocess.PIPE,
  29. stdout=subprocess.PIPE, env=env)
  30. out,err = p.communicate()
  31. if p.returncode != 0:
  32. raise RuntimeError(err)
  33. return json.loads(out.strip())
  34. def get_environments(self):
  35. pattern = re.compile(r'^tag_environment_(.*)')
  36. envs = []
  37. inv = self.get_inventory()
  38. for key in inv.keys():
  39. m = pattern.match(key)
  40. if m:
  41. envs.append(m.group(1))
  42. envs.sort()
  43. return envs
  44. def get_host_types(self):
  45. pattern = re.compile(r'^tag_host-type_(.*)')
  46. host_types = []
  47. inv = self.get_inventory()
  48. for key in inv.keys():
  49. m = pattern.match(key)
  50. if m:
  51. host_types.append(m.group(1))
  52. host_types.sort()
  53. return host_types
  54. def get_security_groups(self):
  55. pattern = re.compile(r'^security_group_(.*)')
  56. groups = []
  57. inv = self.get_inventory()
  58. for key in inv.keys():
  59. m = pattern.match(key)
  60. if m:
  61. groups.append(m.group(1))
  62. groups.sort()
  63. return groups
  64. def build_host_dict_by_env(self, args=[]):
  65. inv = self.get_inventory(args)
  66. inst_by_env = {}
  67. for dns, host in inv['_meta']['hostvars'].items():
  68. # If you don't have an environment tag, we're going to ignore you
  69. if 'ec2_tag_environment' not in host:
  70. continue
  71. if host['ec2_tag_environment'] not in inst_by_env:
  72. inst_by_env[host['ec2_tag_environment']] = {}
  73. host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id'])
  74. inst_by_env[host['ec2_tag_environment']][host_id] = host
  75. return inst_by_env
  76. # Display host_types
  77. def print_host_types(self):
  78. host_types = self.get_host_types()
  79. ht_format_str = "%35s"
  80. alias_format_str = "%-20s"
  81. combined_format_str = ht_format_str + " " + alias_format_str
  82. print
  83. print combined_format_str % ('Host Types', 'Aliases')
  84. print combined_format_str % ('----------', '-------')
  85. for ht in host_types:
  86. aliases = []
  87. if ht in self.host_type_aliases:
  88. aliases = self.host_type_aliases[ht]
  89. print combined_format_str % (ht, ", ".join(aliases))
  90. else:
  91. print ht_format_str % ht
  92. print
  93. # Convert host-type aliases to real a host-type
  94. def resolve_host_type(self, host_type):
  95. if self.alias_lookup.has_key(host_type):
  96. return self.alias_lookup[host_type]
  97. return host_type
  98. def gen_env_host_type_tag(self, host_type, env):
  99. """Generate the environment host type tag
  100. """
  101. host_type = self.resolve_host_type(host_type)
  102. return "tag_env-host-type_%s-%s" % (env, host_type)
  103. def get_host_list(self, host_type, env):
  104. """Get the list of hosts from the inventory using host-type and environment
  105. """
  106. inv = self.get_inventory()
  107. host_type_tag = self.gen_env_host_type_tag(host_type, env)
  108. return inv[host_type_tag]