ansibleutil.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # vim: expandtab:tabstop=4:shiftwidth=4
  2. import subprocess
  3. import os
  4. import json
  5. import re
  6. class AnsibleUtil(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 host['ec2_tag_environment'] not in inst_by_env:
  69. inst_by_env[host['ec2_tag_environment']] = {}
  70. host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id'])
  71. inst_by_env[host['ec2_tag_environment']][host_id] = host
  72. return inst_by_env
  73. # Display host_types
  74. def print_host_types(self):
  75. host_types = self.get_host_types()
  76. ht_format_str = "%35s"
  77. alias_format_str = "%-20s"
  78. combined_format_str = ht_format_str + " " + alias_format_str
  79. print
  80. print combined_format_str % ('Host Types', 'Aliases')
  81. print combined_format_str % ('----------', '-------')
  82. for ht in host_types:
  83. aliases = []
  84. if ht in self.host_type_aliases:
  85. aliases = self.host_type_aliases[ht]
  86. print combined_format_str % (ht, ", ".join(aliases))
  87. else:
  88. print ht_format_str % ht
  89. print
  90. # Convert host-type aliases to real a host-type
  91. def resolve_host_type(self, host_type):
  92. if self.alias_lookup.has_key(host_type):
  93. return self.alias_lookup[host_type]
  94. return host_type
  95. def gen_env_host_type_tag(self, host_type, env):
  96. """Generate the environment host type tag
  97. """
  98. host_type = self.resolve_host_type(host_type)
  99. return "tag_env-host-type_%s-%s" % (env, host_type)
  100. def get_host_list(self, host_type, env):
  101. """Get the list of hosts from the inventory using host-type and environment
  102. """
  103. inv = self.get_inventory()
  104. host_type_tag = self.gen_env_host_type_tag(host_type, env)
  105. return inv[host_type_tag]