From 4754af459d0b23fc54a9cc5bb2c3062aac02f299 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Fri, 13 Mar 2015 17:01:03 -0400 Subject: renamed AnsibleUtil to AwsUtil because that's what it really is. --- bin/ansibleutil.py | 134 ----------------------------------------------------- bin/awsutil.py | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/opssh | 10 ++-- bin/oscp | 8 ++-- bin/ossh | 8 ++-- 5 files changed, 147 insertions(+), 147 deletions(-) delete mode 100644 bin/ansibleutil.py create mode 100644 bin/awsutil.py diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py deleted file mode 100644 index b527ab140..000000000 --- a/bin/ansibleutil.py +++ /dev/null @@ -1,134 +0,0 @@ -# vim: expandtab:tabstop=4:shiftwidth=4 - -import subprocess -import os -import json -import re - -class AnsibleUtil(object): - def __init__(self): - self.host_type_aliases = { - 'legacy-openshift-broker': ['broker', 'ex-srv'], - 'openshift-node': ['node', 'ex-node'], - 'openshift-messagebus': ['msg'], - 'openshift-customer-database': ['mongo'], - 'openshift-website-proxy': ['proxy'], - 'openshift-community-website': ['drupal'], - 'package-mirror': ['mirror'], - } - - self.alias_lookup = {} - for key, values in self.host_type_aliases.iteritems(): - for value in values: - self.alias_lookup[value] = key - - 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,args=[]): - cmd = [self.multi_ec2_path] - - if args: - cmd.extend(args) - - env = os.environ - - 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.strip()) - - 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)) - - envs.sort() - return envs - - def get_host_types(self): - pattern = re.compile(r'^tag_host-type_(.*)') - - host_types = [] - inv = self.get_inventory() - for key in inv.keys(): - m = pattern.match(key) - if m: - host_types.append(m.group(1)) - - host_types.sort() - return host_types - - 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)) - - groups.sort() - return groups - - def build_host_dict_by_env(self, args=[]): - inv = self.get_inventory(args) - - 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']] = {} - host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id']) - inst_by_env[host['ec2_tag_environment']][host_id] = host - - return inst_by_env - - # Display host_types - def print_host_types(self): - host_types = self.get_host_types() - ht_format_str = "%35s" - alias_format_str = "%-20s" - combined_format_str = ht_format_str + " " + alias_format_str - - print - print combined_format_str % ('Host Types', 'Aliases') - print combined_format_str % ('----------', '-------') - - for ht in host_types: - aliases = [] - if ht in self.host_type_aliases: - aliases = self.host_type_aliases[ht] - print combined_format_str % (ht, ", ".join(aliases)) - else: - print ht_format_str % ht - print - - # Convert host-type aliases to real a host-type - def resolve_host_type(self, host_type): - if self.alias_lookup.has_key(host_type): - return self.alias_lookup[host_type] - return host_type - - def gen_env_host_type_tag(self, host_type, env): - """Generate the environment host type tag - """ - host_type = self.resolve_host_type(host_type) - return "tag_env-host-type_%s-%s" % (env, host_type) - - def get_host_list(self, host_type, env): - """Get the list of hosts from the inventory using host-type and environment - """ - inv = self.get_inventory() - host_type_tag = self.gen_env_host_type_tag(host_type, env) - return inv[host_type_tag] diff --git a/bin/awsutil.py b/bin/awsutil.py new file mode 100644 index 000000000..b6b323778 --- /dev/null +++ b/bin/awsutil.py @@ -0,0 +1,134 @@ +# vim: expandtab:tabstop=4:shiftwidth=4 + +import subprocess +import os +import json +import re + +class AwsUtil(object): + def __init__(self): + self.host_type_aliases = { + 'legacy-openshift-broker': ['broker', 'ex-srv'], + 'openshift-node': ['node', 'ex-node'], + 'openshift-messagebus': ['msg'], + 'openshift-customer-database': ['mongo'], + 'openshift-website-proxy': ['proxy'], + 'openshift-community-website': ['drupal'], + 'package-mirror': ['mirror'], + } + + self.alias_lookup = {} + for key, values in self.host_type_aliases.iteritems(): + for value in values: + self.alias_lookup[value] = key + + 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,args=[]): + cmd = [self.multi_ec2_path] + + if args: + cmd.extend(args) + + env = os.environ + + 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.strip()) + + 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)) + + envs.sort() + return envs + + def get_host_types(self): + pattern = re.compile(r'^tag_host-type_(.*)') + + host_types = [] + inv = self.get_inventory() + for key in inv.keys(): + m = pattern.match(key) + if m: + host_types.append(m.group(1)) + + host_types.sort() + return host_types + + 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)) + + groups.sort() + return groups + + def build_host_dict_by_env(self, args=[]): + inv = self.get_inventory(args) + + 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']] = {} + host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id']) + inst_by_env[host['ec2_tag_environment']][host_id] = host + + return inst_by_env + + # Display host_types + def print_host_types(self): + host_types = self.get_host_types() + ht_format_str = "%35s" + alias_format_str = "%-20s" + combined_format_str = ht_format_str + " " + alias_format_str + + print + print combined_format_str % ('Host Types', 'Aliases') + print combined_format_str % ('----------', '-------') + + for ht in host_types: + aliases = [] + if ht in self.host_type_aliases: + aliases = self.host_type_aliases[ht] + print combined_format_str % (ht, ", ".join(aliases)) + else: + print ht_format_str % ht + print + + # Convert host-type aliases to real a host-type + def resolve_host_type(self, host_type): + if self.alias_lookup.has_key(host_type): + return self.alias_lookup[host_type] + return host_type + + def gen_env_host_type_tag(self, host_type, env): + """Generate the environment host type tag + """ + host_type = self.resolve_host_type(host_type) + return "tag_env-host-type_%s-%s" % (env, host_type) + + def get_host_list(self, host_type, env): + """Get the list of hosts from the inventory using host-type and environment + """ + inv = self.get_inventory() + host_type_tag = self.gen_env_host_type_tag(host_type, env) + return inv[host_type_tag] diff --git a/bin/opssh b/bin/opssh index 0836f0354..71e5bf9f2 100755 --- a/bin/opssh +++ b/bin/opssh @@ -2,7 +2,7 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import ansibleutil +import awsutil import traceback import sys import os @@ -17,12 +17,12 @@ PSSH = '/usr/bin/pssh' class Opssh(object): def __init__(self): self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) - self.ansible = ansibleutil.AnsibleUtil() + self.aws = awsutil.AwsUtil() self.parse_cli_args() if self.args.list_host_types: - self.ansible.print_host_types() + self.aws.print_host_types() return if self.args.env and \ @@ -30,7 +30,7 @@ class Opssh(object): self.args.command: retval = self.run_pssh() if retval != 0: - raise ValueError("ansible run failed") + raise ValueError("pssh run failed") return @@ -50,7 +50,7 @@ class Opssh(object): if self.args.errdir: pssh_args.append("--errdir='%s'" % self.args.errdir) - hosts = self.ansible.get_host_list(self.args.host_type, self.args.env) + hosts = self.aws.get_host_list(self.args.host_type, self.args.env) with tempfile.NamedTemporaryFile(prefix='opssh-', delete=True) as f: for h in hosts: f.write(h + os.linesep) diff --git a/bin/oscp b/bin/oscp index 1cc336a6a..146bbbea5 100755 --- a/bin/oscp +++ b/bin/oscp @@ -2,7 +2,7 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import ansibleutil +import awsutil import traceback import sys import os @@ -16,7 +16,7 @@ class Oscp(object): # parse host and user self.process_host() - self.ansible = ansibleutil.AnsibleUtil() + self.aws = awsutil.AwsUtil() # get a dict of host inventory if self.args.list: @@ -104,9 +104,9 @@ class Oscp(object): dict['environment'] = [{'servername' : {}}, ] ''' if cache_only: - self.host_inventory = self.ansible.build_host_dict_by_env(['--cache-only']) + self.host_inventory = self.aws.build_host_dict_by_env(['--cache-only']) else: - self.host_inventory = self.ansible.build_host_dict_by_env() + self.host_inventory = self.aws.build_host_dict_by_env() def select_host(self): '''select host attempts to match the host specified diff --git a/bin/ossh b/bin/ossh index d409e120a..66a4cfb5c 100755 --- a/bin/ossh +++ b/bin/ossh @@ -2,7 +2,7 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import ansibleutil +import awsutil import traceback import sys import os @@ -13,7 +13,7 @@ class Ossh(object): self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) self.parse_cli_args() - self.ansible = ansibleutil.AnsibleUtil() + self.aws = awsutil.AwsUtil() # get a dict of host inventory if self.args.list: @@ -94,9 +94,9 @@ class Ossh(object): dict['servername'] = dns_name ''' if cache_only: - self.host_inventory = self.ansible.build_host_dict_by_env(['--cache-only']) + self.host_inventory = self.aws.build_host_dict_by_env(['--cache-only']) else: - self.host_inventory = self.ansible.build_host_dict_by_env() + self.host_inventory = self.aws.build_host_dict_by_env() def select_host(self): '''select host attempts to match the host specified -- cgit v1.2.3