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 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 bin/ansibleutil.py (limited to 'bin/ansibleutil.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 -- cgit v1.2.3 From d3dee2db1c87b074075b3bbcbe8931862ee7eca3 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 30 Jan 2015 16:29:56 -0500 Subject: Adding ossh.py --- bin/ansibleutil.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'bin/ansibleutil.py') diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index 7ba2c461e..a16a4dca9 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -22,7 +22,9 @@ class AnsibleUtil(object): if p.returncode != 0: raise RuntimeError(err) - return json.loads(out) + with open('/tmp/ans.out','w') as fd: + fd.writelines(out) + return json.loads(out.strip()) def get_environments(self): pattern = re.compile(r'^tag_environment_(.*)') @@ -47,3 +49,19 @@ class AnsibleUtil(object): groups.append(m.group(1)) return groups + + def get_host_address(self): + pattern = re.compile(r'^tag_Name_(.*)') + inv = self.get_inventory() + + inst_names = {} + for key in inv.keys(): + m = pattern.match(key) + if m: inst_names[m.group(1)] = inv[key] + + + return inst_names + + + + -- cgit v1.2.3 From 6481ca629cc2bcf5bd9c7f15be14a77e57086514 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 3 Feb 2015 15:08:26 -0500 Subject: ossh script added for ssh meta query capabilities --- bin/ansibleutil.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'bin/ansibleutil.py') diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index a16a4dca9..26fb25493 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -22,8 +22,8 @@ class AnsibleUtil(object): if p.returncode != 0: raise RuntimeError(err) - with open('/tmp/ans.out','w') as fd: - fd.writelines(out) + #with open('/tmp/ans.out','w') as fd: + #fd.writelines(out) return json.loads(out.strip()) def get_environments(self): @@ -50,18 +50,22 @@ class AnsibleUtil(object): return groups - def get_host_address(self): - pattern = re.compile(r'^tag_Name_(.*)') + def build_host_dict(self): inv = self.get_inventory() - inst_names = {} - for key in inv.keys(): - m = pattern.match(key) - if m: inst_names[m.group(1)] = inv[key] + inst_by_env = {} + for dns, host in inv['_meta']['hostvars'].items(): + if host['ec2_tag_environment'] not in inst_by_env: + inst_by_env[host['ec2_tag_environment']] = {} + + #if inst_by_env[host['ec2_tag_environment']][host['ec2_tag_Name']]: + #raise Exception('Duplicate ec2_tag_Name found: %s' % host['ec2_tag_Name']) + host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id']) + inst_by_env[host['ec2_tag_environment']][host_id] = host - return inst_names + return inst_by_env -- cgit v1.2.3 From fe7d30b762357ac4ec1fe2b173320d463267ac82 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Wed, 4 Feb 2015 12:06:41 -0500 Subject: Renamed ossh.py and added bash completion function --- bin/ansibleutil.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'bin/ansibleutil.py') diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index 26fb25493..ed35ef8f9 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -57,10 +57,6 @@ class AnsibleUtil(object): for dns, host in inv['_meta']['hostvars'].items(): if host['ec2_tag_environment'] not in inst_by_env: inst_by_env[host['ec2_tag_environment']] = {} - - #if inst_by_env[host['ec2_tag_environment']][host['ec2_tag_Name']]: - #raise Exception('Duplicate ec2_tag_Name found: %s' % host['ec2_tag_Name']) - host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id']) inst_by_env[host['ec2_tag_environment']][host_id] = host -- cgit v1.2.3 From ee96928a2d1c21c5d2319418f4cf6ca774a3e010 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 5 Feb 2015 16:02:36 -0500 Subject: Attempting to only refresh cache when doing --list on ossh. --- bin/ansibleutil.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'bin/ansibleutil.py') diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index ed35ef8f9..6df3e7126 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -11,8 +11,12 @@ class AnsibleUtil(object): 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): + def get_inventory(self,args=[]): cmd = [self.multi_ec2_path] + + if args: + cmd.extend(args) + env = {} p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env) @@ -50,8 +54,8 @@ class AnsibleUtil(object): return groups - def build_host_dict(self): - inv = self.get_inventory() + def build_host_dict(self, args=[]): + inv = self.get_inventory(args) inst_by_env = {} for dns, host in inv['_meta']['hostvars'].items(): -- cgit v1.2.3 From cedef18d9450a1b3c8c0f72c10174735529cda04 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 5 Feb 2015 17:01:06 -0500 Subject: Removed comments and cleaned up code. --- bin/ansibleutil.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'bin/ansibleutil.py') diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index 6df3e7126..b12b7b447 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -26,8 +26,6 @@ class AnsibleUtil(object): if p.returncode != 0: raise RuntimeError(err) - #with open('/tmp/ans.out','w') as fd: - #fd.writelines(out) return json.loads(out.strip()) def get_environments(self): -- cgit v1.2.3