ohi 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. import argparse
  4. import traceback
  5. import sys
  6. import os
  7. import re
  8. import tempfile
  9. import time
  10. import subprocess
  11. import ConfigParser
  12. from openshift_ansible import awsutil
  13. from openshift_ansible import utils
  14. from openshift_ansible.awsutil import ArgumentError
  15. CONFIG_MAIN_SECTION = 'main'
  16. CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases'
  17. CONFIG_INVENTORY_OPTION = 'inventory'
  18. class Ohi(object):
  19. def __init__(self):
  20. self.inventory = None
  21. self.host_type_aliases = {}
  22. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  23. # Default the config path to /etc
  24. self.config_path = os.path.join(os.path.sep, 'etc', \
  25. 'openshift_ansible', \
  26. 'openshift_ansible.conf')
  27. self.parse_cli_args()
  28. self.parse_config_file()
  29. self.aws = awsutil.AwsUtil(self.inventory, self.host_type_aliases)
  30. def run(self):
  31. if self.args.list_host_types:
  32. self.aws.print_host_types()
  33. return 0
  34. hosts = None
  35. if self.args.host_type is not None and \
  36. self.args.env is not None:
  37. # Both env and host-type specified
  38. hosts = self.aws.get_host_list(host_type=self.args.host_type, \
  39. env=self.args.env)
  40. if self.args.host_type is None and \
  41. self.args.env is not None:
  42. # Only env specified
  43. hosts = self.aws.get_host_list(env=self.args.env)
  44. if self.args.host_type is not None and \
  45. self.args.env is None:
  46. # Only host-type specified
  47. hosts = self.aws.get_host_list(host_type=self.args.host_type)
  48. if hosts is None:
  49. # We weren't able to determine what they wanted to do
  50. raise ArgumentError("Invalid combination of arguments")
  51. for host in sorted(hosts, key=utils.normalize_dnsname):
  52. print host
  53. return 0
  54. def parse_config_file(self):
  55. if os.path.isfile(self.config_path):
  56. config = ConfigParser.ConfigParser()
  57. config.read(self.config_path)
  58. if config.has_section(CONFIG_MAIN_SECTION) and \
  59. config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION):
  60. self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION)
  61. self.host_type_aliases = {}
  62. if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION):
  63. for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION):
  64. value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',')
  65. self.host_type_aliases[alias] = value
  66. def parse_cli_args(self):
  67. """Setup the command line parser with the options we want
  68. """
  69. parser = argparse.ArgumentParser(description='Openshift Host Inventory')
  70. parser.add_argument('--list-host-types', default=False, action='store_true',
  71. help='List all of the host types')
  72. parser.add_argument('-e', '--env', action="store",
  73. help="Which environment to use")
  74. parser.add_argument('-t', '--host-type', action="store",
  75. help="Which host type to use")
  76. self.args = parser.parse_args()
  77. if __name__ == '__main__':
  78. if len(sys.argv) == 1:
  79. print "\nError: No options given. Use --help to see the available options\n"
  80. sys.exit(0)
  81. try:
  82. ohi = Ohi()
  83. exitcode = ohi.run()
  84. sys.exit(exitcode)
  85. except ArgumentError as e:
  86. print "\nError: %s\n" % e.message