oscp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. import argparse
  4. import ansibleutil
  5. import traceback
  6. import sys
  7. import os
  8. import re
  9. class Oscp(object):
  10. def __init__(self):
  11. self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
  12. self.parse_cli_args()
  13. # parse host and user
  14. self.process_host()
  15. self.ansible = ansibleutil.AnsibleUtil()
  16. # get a dict of host inventory
  17. if self.args.list:
  18. self.get_hosts()
  19. else:
  20. self.get_hosts(True)
  21. if (self.args.src == '' or self.args.dest == '') and not self.args.list:
  22. self.parser.print_help()
  23. return
  24. if self.args.debug:
  25. print self.host
  26. print self.args
  27. # perform the scp
  28. if self.args.list:
  29. self.list_hosts()
  30. else:
  31. self.scp()
  32. def parse_cli_args(self):
  33. parser = argparse.ArgumentParser(description='Openshift Online SSH Tool.')
  34. parser.add_argument('-e', '--env',
  35. action="store", help="Environment where this server exists.")
  36. parser.add_argument('-d', '--debug', default=False,
  37. action="store_true", help="debug mode")
  38. parser.add_argument('-v', '--verbose', default=False,
  39. action="store_true", help="Verbose?")
  40. parser.add_argument('--list', default=False,
  41. action="store_true", help="list out hosts")
  42. parser.add_argument('-r', '--recurse', action='store_true', default=False,
  43. help='Recursively copy files to or from destination.')
  44. parser.add_argument('-o', '--ssh_opts', action='store',
  45. help='options to pass to SSH.\n \
  46. "-oPort=22,TCPKeepAlive=yes"')
  47. parser.add_argument('src', nargs='?', default='')
  48. parser.add_argument('dest',nargs='?', default='')
  49. self.args = parser.parse_args()
  50. self.parser = parser
  51. def process_host(self):
  52. '''Determine host name and user name for SSH.
  53. '''
  54. self.user = ''
  55. # is the first param passed a valid file?
  56. if os.path.isfile(self.args.src) or os.path.isdir(self.args.src):
  57. self.local_src = True
  58. self.host = self.args.dest
  59. else:
  60. self.local_src = False
  61. self.host = self.args.src
  62. if '@' in self.host:
  63. re_host = re.compile("(.*@)(.*)(:.*$)")
  64. else:
  65. re_host = re.compile("(.*)(:.*$)")
  66. search = re_host.search(self.host)
  67. if search:
  68. if len(search.groups()) > 2:
  69. self.user = search.groups()[0]
  70. self.host = search.groups()[1]
  71. self.path = search.groups()[2]
  72. else:
  73. self.host = search.groups()[0]
  74. self.path = search.groups()[1]
  75. if self.args.env:
  76. self.env = self.args.env
  77. elif "." in self.host:
  78. self.host, self.env = self.host.split(".")
  79. else:
  80. print "HERE"
  81. self.env = None
  82. def get_hosts(self, cache_only=False):
  83. '''Query our host inventory and return a dict where the format
  84. equals:
  85. dict['environment'] = [{'servername' : {}}, ]
  86. '''
  87. if cache_only:
  88. self.host_inventory = self.ansible.build_host_dict_by_env(['--cache-only'])
  89. else:
  90. self.host_inventory = self.ansible.build_host_dict_by_env()
  91. def select_host(self):
  92. '''select host attempts to match the host specified
  93. on the command line with a list of hosts.
  94. '''
  95. results = []
  96. for env in self.host_inventory.keys():
  97. for hostname, server_info in self.host_inventory[env].items():
  98. if hostname.split(':')[0] == self.host:
  99. results.append((hostname, server_info))
  100. # attempt to select the correct environment if specified
  101. if self.env:
  102. results = filter(lambda result: result[1]['ec2_tag_environment'] == self.env, results)
  103. if results:
  104. return results
  105. else:
  106. print "Could not find specified host: %s." % self.host
  107. # default - no results found.
  108. return None
  109. def list_hosts(self, limit=None):
  110. '''Function to print out the host inventory.
  111. Takes a single parameter to limit the number of hosts printed.
  112. '''
  113. if self.env:
  114. results = self.select_host()
  115. if len(results) == 1:
  116. hostname, server_info = results[0]
  117. sorted_keys = server_info.keys()
  118. sorted_keys.sort()
  119. for key in sorted_keys:
  120. print '{0:<35} {1}'.format(key, server_info[key])
  121. else:
  122. for host_id, server_info in results[:limit]:
  123. name = server_info['ec2_tag_Name']
  124. ec2_id = server_info['ec2_id']
  125. ip = server_info['ec2_ip_address']
  126. print '{ec2_tag_Name:<35} {ec2_tag_environment:<8} {ec2_id:<15} {ec2_ip_address}'.format(**server_info)
  127. if limit:
  128. print
  129. print 'Showing only the first %d results...' % limit
  130. print
  131. else:
  132. for env, host_ids in self.host_inventory.items():
  133. for host_id, server_info in host_ids.items():
  134. name = server_info['ec2_tag_Name']
  135. ec2_id = server_info['ec2_id']
  136. ip = server_info['ec2_ip_address']
  137. print '{ec2_tag_Name:<35} {ec2_tag_environment:<5} {ec2_id:<15} {ec2_ip_address}'.format(**server_info)
  138. def scp(self):
  139. '''scp files to or from a specified host
  140. '''
  141. try:
  142. # shell args start with the program name in position 1
  143. scp_args = ['/usr/bin/scp']
  144. if self.args.verbose:
  145. scp_args.append('-v')
  146. if self.args.recurse:
  147. scp_args.append('-r')
  148. if self.args.ssh_opts:
  149. for arg in self.args.ssh_opts.split(","):
  150. scp_args.append("-o%s" % arg)
  151. results = self.select_host()
  152. if self.args.debug: print results
  153. if not results:
  154. return # early exit, no results
  155. if len(results) > 1:
  156. print "Multiple results found for %s." % self.host
  157. for result in results:
  158. print "{ec2_tag_Name:<35} {ec2_tag_environment:<5} {ec2_id:<10}".format(**result[1])
  159. return # early exit, too many results
  160. # Assume we have one and only one.
  161. hostname, server_info = results[0]
  162. dns = server_info['ec2_public_dns_name']
  163. host_str = "%s%s%s" % (self.user, dns, self.path)
  164. if self.local_src:
  165. scp_args.append(self.args.src)
  166. scp_args.append(host_str)
  167. else:
  168. scp_args.append(host_str)
  169. scp_args.append(self.args.dest)
  170. print "Running: %s\n" % ' '.join(scp_args)
  171. os.execve('/usr/bin/scp', scp_args, os.environ)
  172. except:
  173. print traceback.print_exc()
  174. print sys.exc_info()
  175. if __name__ == '__main__':
  176. oscp = Oscp()