awsutil.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # vim: expandtab:tabstop=4:shiftwidth=4
  2. """This module comprises Aws specific utility functions."""
  3. import os
  4. import re
  5. from openshift_ansible import multi_ec2
  6. class ArgumentError(Exception):
  7. """This class is raised when improper arguments are passed."""
  8. def __init__(self, message):
  9. """Initialize an ArgumentError.
  10. Keyword arguments:
  11. message -- the exact error message being raised
  12. """
  13. super(ArgumentError, self).__init__()
  14. self.message = message
  15. class AwsUtil(object):
  16. """This class contains the AWS utility functions."""
  17. def __init__(self, host_type_aliases=None):
  18. """Initialize the AWS utility class.
  19. Keyword arguments:
  20. host_type_aliases -- a list of aliases to common host-types (e.g. ex-node)
  21. """
  22. host_type_aliases = host_type_aliases or {}
  23. self.host_type_aliases = host_type_aliases
  24. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  25. self.setup_host_type_alias_lookup()
  26. def setup_host_type_alias_lookup(self):
  27. """Sets up the alias to host-type lookup table."""
  28. self.alias_lookup = {}
  29. for key, values in self.host_type_aliases.iteritems():
  30. for value in values:
  31. self.alias_lookup[value] = key
  32. @staticmethod
  33. def get_inventory(args=None):
  34. """Calls the inventory script and returns a dictionary containing the inventory."
  35. Keyword arguments:
  36. args -- optional arguments to pass to the inventory script
  37. """
  38. mec2 = multi_ec2.MultiEc2(args)
  39. mec2.run()
  40. return mec2.result
  41. def get_environments(self):
  42. """Searches for env tags in the inventory and returns all of the envs found."""
  43. pattern = re.compile(r'^tag_environment_(.*)')
  44. envs = []
  45. inv = self.get_inventory()
  46. for key in inv.keys():
  47. matched = pattern.match(key)
  48. if matched:
  49. envs.append(matched.group(1))
  50. envs.sort()
  51. return envs
  52. def get_host_types(self):
  53. """Searches for host-type tags in the inventory and returns all host-types found."""
  54. pattern = re.compile(r'^tag_host-type_(.*)')
  55. host_types = []
  56. inv = self.get_inventory()
  57. for key in inv.keys():
  58. matched = pattern.match(key)
  59. if matched:
  60. host_types.append(matched.group(1))
  61. host_types.sort()
  62. return host_types
  63. def get_security_groups(self):
  64. """Searches for security_groups in the inventory and returns all SGs found."""
  65. pattern = re.compile(r'^security_group_(.*)')
  66. groups = []
  67. inv = self.get_inventory()
  68. for key in inv.keys():
  69. matched = pattern.match(key)
  70. if matched:
  71. groups.append(matched.group(1))
  72. groups.sort()
  73. return groups
  74. def build_host_dict_by_env(self, args=None):
  75. """Searches the inventory for hosts in an env and returns their hostvars."""
  76. args = args or []
  77. inv = self.get_inventory(args)
  78. inst_by_env = {}
  79. for _, host in inv['_meta']['hostvars'].items():
  80. # If you don't have an environment tag, we're going to ignore you
  81. if 'ec2_tag_environment' not in host:
  82. continue
  83. if host['ec2_tag_environment'] not in inst_by_env:
  84. inst_by_env[host['ec2_tag_environment']] = {}
  85. host_id = "%s:%s" % (host['ec2_tag_Name'], host['ec2_id'])
  86. inst_by_env[host['ec2_tag_environment']][host_id] = host
  87. return inst_by_env
  88. def print_host_types(self):
  89. """Gets the list of host types and aliases and outputs them in columns."""
  90. host_types = self.get_host_types()
  91. ht_format_str = "%35s"
  92. alias_format_str = "%-20s"
  93. combined_format_str = ht_format_str + " " + alias_format_str
  94. print
  95. print combined_format_str % ('Host Types', 'Aliases')
  96. print combined_format_str % ('----------', '-------')
  97. for host_type in host_types:
  98. aliases = []
  99. if host_type in self.host_type_aliases:
  100. aliases = self.host_type_aliases[host_type]
  101. print combined_format_str % (host_type, ", ".join(aliases))
  102. else:
  103. print ht_format_str % host_type
  104. print
  105. def resolve_host_type(self, host_type):
  106. """Converts a host-type alias into a host-type.
  107. Keyword arguments:
  108. host_type -- The alias or host_type to look up.
  109. Example (depends on aliases defined in config file):
  110. host_type = ex-node
  111. returns: openshift-node
  112. """
  113. if self.alias_lookup.has_key(host_type):
  114. return self.alias_lookup[host_type]
  115. return host_type
  116. @staticmethod
  117. def gen_env_tag(env):
  118. """Generate the environment tag
  119. """
  120. return "tag_environment_%s" % env
  121. def gen_host_type_tag(self, host_type):
  122. """Generate the host type tag
  123. """
  124. host_type = self.resolve_host_type(host_type)
  125. return "tag_host-type_%s" % host_type
  126. def gen_env_host_type_tag(self, host_type, env):
  127. """Generate the environment host type tag
  128. """
  129. host_type = self.resolve_host_type(host_type)
  130. return "tag_env-host-type_%s-%s" % (env, host_type)
  131. def get_host_list(self, host_type=None, envs=None):
  132. """Get the list of hosts from the inventory using host-type and environment
  133. """
  134. envs = envs or []
  135. inv = self.get_inventory()
  136. # We prefer to deal with a list of environments
  137. if issubclass(type(envs), basestring):
  138. if envs == 'all':
  139. envs = self.get_environments()
  140. else:
  141. envs = [envs]
  142. if host_type and envs:
  143. # Both host type and environment were specified
  144. retval = []
  145. for env in envs:
  146. env_host_type_tag = self.gen_env_host_type_tag(host_type, env)
  147. if env_host_type_tag in inv.keys():
  148. retval += inv[env_host_type_tag]
  149. return set(retval)
  150. if envs and not host_type:
  151. # Just environment was specified
  152. retval = []
  153. for env in envs:
  154. env_tag = AwsUtil.gen_env_tag(env)
  155. if env_tag in inv.keys():
  156. retval += inv[env_tag]
  157. return set(retval)
  158. if host_type and not envs:
  159. # Just host-type was specified
  160. retval = []
  161. host_type_tag = self.gen_host_type_tag(host_type)
  162. if host_type_tag in inv.keys():
  163. retval = inv[host_type_tag]
  164. return set(retval)
  165. # We should never reach here!
  166. raise ArgumentError("Invalid combination of parameters")