opssh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.awsutil import ArgumentError
  14. DEFAULT_PSSH_PAR = 200
  15. PSSH = '/usr/bin/pssh'
  16. CONFIG_MAIN_SECTION = 'main'
  17. CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases'
  18. CONFIG_INVENTORY_OPTION = 'inventory'
  19. class Opssh(object):
  20. def __init__(self):
  21. self.inventory = None
  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.inventory, self.host_type_aliases)
  31. def run(self):
  32. if self.args.list_host_types:
  33. self.aws.print_host_types()
  34. return 0
  35. hosts = None
  36. if self.args.host_type is not None and \
  37. self.args.env is not None:
  38. # Both env and host-type specified
  39. hosts = self.aws.get_host_list(host_type=self.args.host_type, \
  40. env=self.args.env)
  41. if self.args.host_type is None and \
  42. self.args.env is not None:
  43. # Only env specified
  44. hosts = self.aws.get_host_list(env=self.args.env)
  45. if self.args.host_type is not None and \
  46. self.args.env is None:
  47. # Only host-type specified
  48. hosts = self.aws.get_host_list(host_type=self.args.host_type)
  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 hosts:
  53. print host
  54. return 0
  55. def run_pssh(self):
  56. """Actually run the pssh command based off of the supplied options
  57. """
  58. # Default set of options
  59. pssh_args = [PSSH, '-t', '0', '-p', str(self.args.par), '--user', self.args.user]
  60. if self.args.inline:
  61. pssh_args.append("--inline")
  62. if self.args.outdir:
  63. pssh_args.extend(["--outdir", self.args.outdir])
  64. if self.args.errdir:
  65. pssh_args.extend(["--errdir", self.args.errdir])
  66. hosts = self.aws.get_host_list(self.args.host_type, self.args.env)
  67. with tempfile.NamedTemporaryFile(prefix='opssh-', delete=True) as f:
  68. for h in hosts:
  69. f.write(h + os.linesep)
  70. f.flush()
  71. pssh_args.extend(["-h", f.name])
  72. pssh_args.append(self.args.command)
  73. print
  74. print "Running: %s" % ' '.join(pssh_args)
  75. print
  76. return subprocess.call(pssh_args)
  77. return None
  78. def parse_config_file(self):
  79. if os.path.isfile(self.config_path):
  80. config = ConfigParser.ConfigParser()
  81. config.read(self.config_path)
  82. if config.has_section(CONFIG_MAIN_SECTION) and \
  83. config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION):
  84. self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION)
  85. self.host_type_aliases = {}
  86. if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION):
  87. for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION):
  88. value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',')
  89. self.host_type_aliases[alias] = value
  90. def parse_cli_args(self):
  91. """Setup the command line parser with the options we want
  92. """
  93. parser = argparse.ArgumentParser(description='Openshift Online PSSH Tool.')
  94. parser.add_argument('--list-host-types', default=False, action='store_true',
  95. help='List all of the host types')
  96. parser.add_argument('-e', '--env', action="store",
  97. help="Which environment to use")
  98. parser.add_argument('-t', '--host-type', action="store", default=None,
  99. help="Which host type to use")
  100. parser.add_argument('-c', '--command', action='store',
  101. help='Command to run on remote host(s)')
  102. parser.add_argument('--user', action='store', default='root',
  103. help='username')
  104. parser.add_argument('-i', '--inline', default=False, action='store_true',
  105. help='inline aggregated output and error for each server')
  106. parser.add_argument('-p', '--par', action='store', default=DEFAULT_PSSH_PAR,
  107. help=('max number of parallel threads (default %s)' % DEFAULT_PSSH_PAR))
  108. parser.add_argument('--outdir', action='store',
  109. help='output directory for stdout files')
  110. parser.add_argument('--errdir', action='store',
  111. help='output directory for stderr files')
  112. self.args = parser.parse_args()
  113. if __name__ == '__main__':
  114. if len(sys.argv) == 1:
  115. print "\nError: No options given. Use --help to see the available options\n"
  116. sys.exit(0)
  117. try:
  118. opssh = Opssh()
  119. exitcode = opssh.run()
  120. sys.exit(exitcode)
  121. except ArgumentError as e:
  122. print "\nError: %s\n" % e.message