ohi 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. CONFIG_MAIN_SECTION = 'main'
  14. CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases'
  15. CONFIG_INVENTORY_OPTION = 'inventory'
  16. class ArgumentMismatchError(ValueError): pass
  17. class Ohi(object):
  18. def __init__(self):
  19. self.inventory = None
  20. self.host_type_aliases = {}
  21. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  22. # Default the config path to /etc
  23. self.config_path = os.path.join(os.path.sep, 'etc', \
  24. 'openshift_ansible', \
  25. 'openshift_ansible.conf')
  26. self.parse_cli_args()
  27. self.parse_config_file()
  28. self.aws = awsutil.AwsUtil(self.inventory, self.host_type_aliases)
  29. def run(self):
  30. if self.args.list_host_types:
  31. self.aws.print_host_types()
  32. return 0
  33. if self.args.env and \
  34. self.args.host_type:
  35. hosts = self.aws.get_host_list(self.args.host_type, self.args.env)
  36. for host in hosts:
  37. print host
  38. return 0
  39. # If it makes it here, we weren't able to determine what they wanted to do
  40. raise ArgumentMismatchError("Invalid combination of arguments")
  41. def parse_config_file(self):
  42. if os.path.isfile(self.config_path):
  43. config = ConfigParser.ConfigParser()
  44. config.read(self.config_path)
  45. if config.has_section(CONFIG_MAIN_SECTION) and \
  46. config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION):
  47. self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION)
  48. self.host_type_aliases = {}
  49. if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION):
  50. for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION):
  51. value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',')
  52. self.host_type_aliases[alias] = value
  53. def parse_cli_args(self):
  54. """Setup the command line parser with the options we want
  55. """
  56. parser = argparse.ArgumentParser(description='Openshift Host Inventory')
  57. parser.add_argument('--list-host-types', default=False, action='store_true',
  58. help='List all of the host types')
  59. parser.add_argument('-e', '--env', action="store",
  60. help="Which environment to use")
  61. parser.add_argument('-t', '--host-type', action="store",
  62. help="Which host type to use")
  63. self.args = parser.parse_args()
  64. if __name__ == '__main__':
  65. if len(sys.argv) == 1:
  66. print "\nError: No options given. Use --help to see the available options\n"
  67. sys.exit(0)
  68. try:
  69. ohi = Ohi()
  70. exitcode = ohi.run()
  71. sys.exit(exitcode)
  72. except ArgumentMismatchError as e:
  73. print "\nError: %s\n" % e.message