ohi 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. '''
  3. Ohi = Openshift Host Inventory
  4. This script provides an easy way to look at your host inventory.
  5. This depends on multi_inventory being setup correctly.
  6. '''
  7. # vim: expandtab:tabstop=4:shiftwidth=4
  8. import argparse
  9. import sys
  10. import os
  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. class Ohi(object):
  18. '''
  19. Class for managing openshift host inventory
  20. '''
  21. def __init__(self):
  22. self.host_type_aliases = {}
  23. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  24. # Default the config path to /etc
  25. self.config_path = os.path.join(os.path.sep, 'etc', \
  26. 'openshift_ansible', \
  27. 'openshift_ansible.conf')
  28. self.parse_cli_args()
  29. self.parse_config_file()
  30. self.aws = awsutil.AwsUtil(self.host_type_aliases)
  31. def run(self):
  32. '''
  33. Call into awsutil and retrieve the desired hosts and environments
  34. '''
  35. if self.args.list_host_types:
  36. self.aws.print_host_types()
  37. return 0
  38. if self.args.v3:
  39. version = '3'
  40. elif self.args.all_versions:
  41. version = 'all'
  42. else:
  43. version = '2'
  44. hosts = self.aws.get_host_list(clusters=self.args.cluster,
  45. host_type=self.args.host_type,
  46. envs=self.args.env,
  47. version=version,
  48. cached=self.args.cache_only)
  49. if hosts is None:
  50. # We weren't able to determine what they wanted to do
  51. raise ArgumentError("Invalid combination of arguments")
  52. for host in sorted(hosts, key=utils.normalize_dnsname):
  53. if self.args.user:
  54. print "%s@%s" % (self.args.user, host)
  55. else:
  56. print host
  57. return 0
  58. def parse_config_file(self):
  59. '''
  60. Parse the config file for ohi
  61. '''
  62. if os.path.isfile(self.config_path):
  63. config = ConfigParser.ConfigParser()
  64. config.read(self.config_path)
  65. self.host_type_aliases = {}
  66. if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION):
  67. for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION):
  68. value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',')
  69. self.host_type_aliases[alias] = value
  70. def parse_cli_args(self):
  71. """Setup the command line parser with the options we want
  72. """
  73. parser = argparse.ArgumentParser(description='OpenShift Host Inventory')
  74. parser.add_argument('--list-host-types', default=False, action='store_true', help='List all of the host types')
  75. parser.add_argument('--list', default=False, action='store_true', help='List all hosts')
  76. parser.add_argument('-c', '--cluster', action="append", help="Which clusterid to use")
  77. parser.add_argument('-e', '--env', action="append", help="Which environment to use")
  78. parser.add_argument('-t', '--host-type', action="store", help="Which host type to use")
  79. parser.add_argument('-l', '--user', action='store', default=None, help='username')
  80. parser.add_argument('--cache-only', action='store_true', default=False,
  81. help='Retrieve the host inventory by cache only. Default is false.')
  82. parser.add_argument('--v2', action='store_true', default=True,
  83. help='Specify the openshift version. Default is 2')
  84. parser.add_argument('--v3', action='store_true', default=False,
  85. help='Specify the openshift version.')
  86. parser.add_argument('--all-versions', action='store_true', default=False,
  87. help='Specify the openshift version. Return all versions')
  88. self.args = parser.parse_args()
  89. def main():
  90. '''
  91. Ohi will do its work here
  92. '''
  93. if len(sys.argv) == 1:
  94. print "\nError: No options given. Use --help to see the available options\n"
  95. sys.exit(0)
  96. try:
  97. ohi = Ohi()
  98. exitcode = ohi.run()
  99. sys.exit(exitcode)
  100. except ArgumentError as err:
  101. print "\nError: %s\n" % err.message
  102. if __name__ == '__main__':
  103. main()