awsutil.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # vim: expandtab:tabstop=4:shiftwidth=4
  2. import subprocess
  3. import os
  4. import json
  5. import re
  6. class ArgumentError(Exception):
  7. def __init__(self, message):
  8. self.message = message
  9. class AwsUtil(object):
  10. def __init__(self, inventory_path=None, host_type_aliases={}):
  11. self.host_type_aliases = host_type_aliases
  12. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  13. if inventory_path is None:
  14. inventory_path = os.path.realpath(os.path.join(self.file_path, \
  15. '..', '..', 'inventory', \
  16. 'multi_ec2.py'))
  17. if not os.path.isfile(inventory_path):
  18. raise Exception("Inventory file not found [%s]" % inventory_path)
  19. self.inventory_path = inventory_path
  20. self.setup_host_type_alias_lookup()
  21. def setup_host_type_alias_lookup(self):
  22. self.alias_lookup = {}
  23. for key, values in self.host_type_aliases.iteritems():
  24. for value in values:
  25. self.alias_lookup[value] = key
  26. def get_inventory(self,args=[]):
  27. cmd = [self.inventory_path]
  28. if args:
  29. cmd.extend(args)
  30. env = os.environ
  31. p = subprocess.Popen(cmd, stderr=subprocess.PIPE,
  32. stdout=subprocess.PIPE, env=env)
  33. out,err = p.communicate()
  34. if p.returncode != 0:
  35. raise RuntimeError(err)
  36. return json.loads(out.strip())
  37. def get_environments(self):
  38. pattern = re.compile(r'^tag_environment_(.*)')
  39. envs = []
  40. inv = self.get_inventory()
  41. for key in inv.keys():
  42. m = pattern.match(key)
  43. if m:
  44. envs.append(m.group(1))
  45. envs.sort()
  46. return envs
  47. def get_host_types(self):
  48. pattern = re.compile(r'^tag_host-type_(.*)')
  49. host_types = []
  50. inv = self.get_inventory()
  51. for key in inv.keys():
  52. m = pattern.match(key)
  53. if m:
  54. host_types.append(m.group(1))
  55. host_types.sort()
  56. return host_types
  57. def get_security_groups(self):
  58. pattern = re.compile(r'^security_group_(.*)')
  59. groups = []
  60. inv = self.get_inventory()
  61. for key in inv.keys():
  62. m = pattern.match(key)
  63. if m:
  64. groups.append(m.group(1))
  65. groups.sort()
  66. return groups
  67. def build_host_dict_by_env(self, args=[]):
  68. inv = self.get_inventory(args)
  69. inst_by_env = {}
  70. for dns, host in inv['_meta']['hostvars'].items():
  71. # If you don't have an environment tag, we're going to ignore you
  72. if 'ec2_tag_environment' not in host:
  73. continue
  74. if host['ec2_tag_environment'] not in inst_by_env:
  75. inst_by_env[host['ec2_tag_environment']] = {}
  76. host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id'])
  77. inst_by_env[host['ec2_tag_environment']][host_id] = host
  78. return inst_by_env
  79. # Display host_types
  80. def print_host_types(self):
  81. host_types = self.get_host_types()
  82. ht_format_str = "%35s"
  83. alias_format_str = "%-20s"
  84. combined_format_str = ht_format_str + " " + alias_format_str
  85. print
  86. print combined_format_str % ('Host Types', 'Aliases')
  87. print combined_format_str % ('----------', '-------')
  88. for ht in host_types:
  89. aliases = []
  90. if ht in self.host_type_aliases:
  91. aliases = self.host_type_aliases[ht]
  92. print combined_format_str % (ht, ", ".join(aliases))
  93. else:
  94. print ht_format_str % ht
  95. print
  96. # Convert host-type aliases to real a host-type
  97. def resolve_host_type(self, host_type):
  98. if self.alias_lookup.has_key(host_type):
  99. return self.alias_lookup[host_type]
  100. return host_type
  101. def gen_env_tag(self, env):
  102. """Generate the environment tag
  103. """
  104. return "tag_environment_%s" % env
  105. def gen_host_type_tag(self, host_type):
  106. """Generate the host type tag
  107. """
  108. host_type = self.resolve_host_type(host_type)
  109. return "tag_host-type_%s" % host_type
  110. def gen_env_host_type_tag(self, host_type, env):
  111. """Generate the environment host type tag
  112. """
  113. host_type = self.resolve_host_type(host_type)
  114. return "tag_env-host-type_%s-%s" % (env, host_type)
  115. def get_host_list(self, host_type=None, env=None):
  116. """Get the list of hosts from the inventory using host-type and environment
  117. """
  118. inv = self.get_inventory()
  119. if host_type is not None and \
  120. env is not None:
  121. # Both host type and environment were specified
  122. env_host_type_tag = self.gen_env_host_type_tag(host_type, env)
  123. return inv[env_host_type_tag]
  124. if host_type is None and \
  125. env is not None:
  126. # Just environment was specified
  127. host_type_tag = self.gen_env_tag(env)
  128. return inv[host_type_tag]
  129. if host_type is not None and \
  130. env is None:
  131. # Just host-type was specified
  132. host_type_tag = self.gen_host_type_tag(host_type)
  133. return inv[host_type_tag]
  134. # We should never reach here!
  135. raise ArgumentError("Invalid combination of parameters")