From 0457b96cd5b5252010208ee8f74d0a3da49573b2 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Thu, 8 Jan 2015 12:39:33 -0500 Subject: added opssh.py --- bin/ansibleutil.py | 49 +++++++++++++++++++++++++++++++++++++ bin/opssh.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ inventory/multi_ec2.py | 1 + 3 files changed, 116 insertions(+) create mode 100644 bin/ansibleutil.py create mode 100755 bin/opssh.py diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py new file mode 100644 index 000000000..7ba2c461e --- /dev/null +++ b/bin/ansibleutil.py @@ -0,0 +1,49 @@ +# vim: expandtab:tabstop=4:shiftwidth=4 + +import subprocess +import sys +import os +import json +import re + +class AnsibleUtil(object): + def __init__(self): + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + self.multi_ec2_path = os.path.realpath(os.path.join(self.file_path, '..','inventory','multi_ec2.py')) + + def get_inventory(self): + cmd = [self.multi_ec2_path] + env = {} + p = subprocess.Popen(cmd, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, env=env) + + out,err = p.communicate() + + if p.returncode != 0: + raise RuntimeError(err) + + return json.loads(out) + + def get_environments(self): + pattern = re.compile(r'^tag_environment_(.*)') + + envs = [] + inv = self.get_inventory() + for key in inv.keys(): + m = pattern.match(key) + if m: + envs.append(m.group(1)) + + return envs + + def get_security_groups(self): + pattern = re.compile(r'^security_group_(.*)') + + groups = [] + inv = self.get_inventory() + for key in inv.keys(): + m = pattern.match(key) + if m: + groups.append(m.group(1)) + + return groups diff --git a/bin/opssh.py b/bin/opssh.py new file mode 100755 index 000000000..6ee390c4c --- /dev/null +++ b/bin/opssh.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# vim: expandtab:tabstop=4:shiftwidth=4 + +import argparse +import os +import ansibleutil +import sys + +class Program(object): + def __init__(self): + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + self.parse_cli_args() + self.ansible = ansibleutil.AnsibleUtil() + + inv = self.ansible.get_inventory() + #print inv.keys() + #sys.exit() + + if self.args.list_environments: + self.list_environments() + sys.exit() + + if self.args.list_groups: + self.list_security_groups() + sys.exit() + + def parse_cli_args(self): + parser = argparse.ArgumentParser( + description='OpenShift Online Operations Parallel SSH' + ) + + parser.add_argument("-v", '--verbosity', action="count", + help="increase output verbosity") + + group = parser.add_mutually_exclusive_group() + + group.add_argument('--list-environments', action="store_true", + help='List all environments') + group.add_argument('--list-groups', action="store_true", + help='List all security groups') + group.add_argument('-e', '--environment', + help='Set the environment') + + self.args = parser.parse_args() + + def list_environments(self): + envs = self.ansible.get_environments() + print + print "Environments" + print "------------" + for env in envs: + print env + print + + def list_security_groups(self): + envs = self.ansible.get_security_groups() + print + print "Groups" + print "------" + for env in envs: + print env + print + + +if __name__ == '__main__': + p = Program() diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index d8c2dc854..c60bb50d2 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# vim: expandtab:tabstop=4:shiftwidth=4 from time import time import argparse -- cgit v1.2.3