awsutil.py 4.2 KB

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