From 6bb076355db1eafdf610c96735cfc72d0dac1862 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 16 Mar 2015 16:48:41 -0400 Subject: Initialized to use tito. --- bin/openshift-ansible-cmds.spec | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 bin/openshift-ansible-cmds.spec (limited to 'bin') diff --git a/bin/openshift-ansible-cmds.spec b/bin/openshift-ansible-cmds.spec new file mode 100644 index 000000000..24705ae31 --- /dev/null +++ b/bin/openshift-ansible-cmds.spec @@ -0,0 +1,34 @@ +Summary: OpenShift Operations files for mirror +Name: openshift-ansible-cmds +Version: 0.0.0 +Release: 1%{?dist} +License: ASL 2.0 +URL: https://github.com/openshift/openshift-ansible +Source0: %{name}-%{version}.tar.gz +Requires: python2 +BuildRequires: python2-devel +BuildArch: noarch + +%description +Scripts to make it nicer when working with hosts that are defined only by metadata. + +%prep +%setup -q + +%build + +%install +mkdir -p %{buildroot}/usr/bin +mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible +mkdir -p %{buildroot}/etc/bash_completion.d + +cp -p ossh oscp opssh %{buildroot}/usr/bin +cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible +cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d + +%files +/usr/bin/* +%{python_sitelib}/openshift_ansible/* +/etc/bash_completion.d/* + +%changelog -- cgit v1.2.3 From 17c69ff4a4b8b905e2db4dac6c9c0e8d1212b23f Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 16 Mar 2015 16:50:02 -0400 Subject: Automatic commit of package [openshift-ansible-cmds] release [0.0.1-1]. --- bin/README_BUILD | 4 ++++ bin/openshift-ansible-bin.spec | 34 ++++++++++++++++++++++++++++++++++ bin/openshift-ansible-cmds.spec | 34 ---------------------------------- 3 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 bin/README_BUILD create mode 100644 bin/openshift-ansible-bin.spec delete mode 100644 bin/openshift-ansible-cmds.spec (limited to 'bin') diff --git a/bin/README_BUILD b/bin/README_BUILD new file mode 100644 index 000000000..48d4ff4b3 --- /dev/null +++ b/bin/README_BUILD @@ -0,0 +1,4 @@ +# How to build openshift-ansible + + +Test build diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec new file mode 100644 index 000000000..2b83a7d0b --- /dev/null +++ b/bin/openshift-ansible-bin.spec @@ -0,0 +1,34 @@ +Summary: OpenShift Operations files for mirror +Name: openshift-ansible-bin +Version: 0.0.1 +Release: 1%{?dist} +License: ASL 2.0 +URL: https://github.com/openshift/openshift-ansible +Source0: %{name}-%{version}.tar.gz +Requires: python2 +BuildRequires: python2-devel +BuildArch: noarch + +%description +Scripts to make it nicer when working with hosts that are defined only by metadata. + +%prep +%setup -q + +%build + +%install +mkdir -p %{buildroot}/usr/bin +mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible +mkdir -p %{buildroot}/etc/bash_completion.d + +cp -p ossh oscp opssh %{buildroot}/usr/bin +cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible +cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d + +%files +/usr/bin/* +%{python_sitelib}/openshift_ansible/* +/etc/bash_completion.d/* + +%changelog diff --git a/bin/openshift-ansible-cmds.spec b/bin/openshift-ansible-cmds.spec deleted file mode 100644 index 24705ae31..000000000 --- a/bin/openshift-ansible-cmds.spec +++ /dev/null @@ -1,34 +0,0 @@ -Summary: OpenShift Operations files for mirror -Name: openshift-ansible-cmds -Version: 0.0.0 -Release: 1%{?dist} -License: ASL 2.0 -URL: https://github.com/openshift/openshift-ansible -Source0: %{name}-%{version}.tar.gz -Requires: python2 -BuildRequires: python2-devel -BuildArch: noarch - -%description -Scripts to make it nicer when working with hosts that are defined only by metadata. - -%prep -%setup -q - -%build - -%install -mkdir -p %{buildroot}/usr/bin -mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible -mkdir -p %{buildroot}/etc/bash_completion.d - -cp -p ossh oscp opssh %{buildroot}/usr/bin -cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible -cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d - -%files -/usr/bin/* -%{python_sitelib}/openshift_ansible/* -/etc/bash_completion.d/* - -%changelog -- cgit v1.2.3 From a7e3b2363935fc090ce7a41853ba27ba0050dc23 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 24 Mar 2015 11:36:30 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.1-1]. --- bin/README_BUILD | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/README_BUILD b/bin/README_BUILD index 48d4ff4b3..50010e562 100644 --- a/bin/README_BUILD +++ b/bin/README_BUILD @@ -1,4 +1,25 @@ -# How to build openshift-ansible +# openshift-ansible-bin RPM Build instructions +We use tito to make building and tracking revisions easy. +For more information on tito, please see the [Tito home page](http://rm-rf.ca/tito "Tito home page"). -Test build + +## Build a test package (no tagging needed) +``` +tito build --test --rpm +``` + + +## Tag a new build (bumps version number and adds log entries) +``` +tito tag +``` + +Follow the on screen tito instructions. + + + +## Build a new package based on the latest tag information +``` +tito build --rpm +``` -- cgit v1.2.3 From 7c7cb82fdd5583784fd5832b92886abf86934325 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Fri, 6 Mar 2015 13:52:20 -0700 Subject: Use ansible playbook to initialize openshift cluster * Added playbooks/gce/openshift-cluster * Added bin/cluster (will replace cluster.sh) --- bin/cluster | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 bin/cluster (limited to 'bin') diff --git a/bin/cluster b/bin/cluster new file mode 100755 index 000000000..7afdce0e5 --- /dev/null +++ b/bin/cluster @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# vim: expandtab:tabstop=4:shiftwidth=4 + +import argparse +import ConfigParser +import sys +import os + + +class Cluster(object): + """Python wrapper to ensure environment is correct for running ansible playbooks + """ + + def __init__(self, args): + self.args = args + + # setup ansible ssh environment + if 'ANSIBLE_SSH_ARGS' not in os.environ: + os.environ['ANSIBLE_SSH_ARGS'] = ( + '-o ForwardAgent=yes' + '-o StrictHostKeyChecking=no' + '-o UserKnownHostsFile=/dev/null' + '-o ControlMaster=auto' + '-o ControlPersist=600s' + ) + + def apply(self): + # setup ansible playbook environment + config = ConfigParser.ConfigParser() + if 'gce' == self.args.provider: + config.readfp(open('inventory/gce/gce.ini')) + + for key in config.options('gce'): + os.environ[key] = config.get('gce', key) + + inventory = '-i inventory/gce/gce.py' + elif 'aws' == self.args.provider: + config.readfp(open('inventory/aws/ec2.ini')) + + for key in config.options('ec2'): + os.environ[key] = config.get('ec2', key) + + inventory = '-i inventory/aws/ec2.py' + else: + assert False, "invalid PROVIDER {}".format(self.args.provider) + + env = {'cluster_id': self.args.cluster_id} + + if 'create' == self.args.action: + playbook = "playbooks/{}/openshift-cluster/launch.yml".format(self.args.provider) + env['masters'] = self.args.masters + env['nodes'] = self.args.nodes + + elif 'terminate' == self.args.action: + playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(self.args.provider) + elif 'list' == self.args.action: + # todo: implement cluster list + argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) + elif 'update' == self.args.action: + # todo: implement cluster update + argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) + else: + assert False, "invalid ACTION {}".format(self.args.action) + + verbose = '' + if self.args.verbose > 0: + verbose = '-{}'.format('v' * self.args.verbose) + + ansible_env = '-e \'{}\''.format( + ' '.join(['%s=%s' % (key, value) for (key, value) in env.items()]) + ) + + command = 'ansible-playbook {} {} {} {}'.format( + verbose, inventory, ansible_env, playbook + ) + + if self.args.verbose > 1: + command = 'time {}'.format(command) + + if self.args.verbose > 0: + sys.stderr.write('RUN [{}]\n'.format(command)) + sys.stderr.flush() + + os.system(command) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Manage OpenShift Cluster') + parser.add_argument('-p', '--provider', default='gce', choices=['gce', 'aws'], + help='One of the supported cloud providers') + parser.add_argument('-m', '--masters', default=1, type=int, help='number of masters to create in cluster') + parser.add_argument('-n', '--nodes', default=2, type=int, help='number of nodes to create in cluster') + parser.add_argument('-v', '--verbose', action='count', help='Multiple -v options increase the verbosity') + parser.add_argument('--version', action='version', version='%(prog)s 0.1') + parser.add_argument('action', choices=['create', 'terminate', 'update', 'list']) + parser.add_argument('provider', choices=['gce', 'aws']) + parser.add_argument('cluster_id', help='prefix for cluster VM names') + args = parser.parse_args() + + Cluster(args).apply() -- cgit v1.2.3 From f6b2eaf7d12ff1f74551662cea46a8bad6beac33 Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Fri, 13 Mar 2015 03:02:29 -0400 Subject: Add spacing to implicit string concatenation for python backwards compatibility --- bin/cluster | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index 7afdce0e5..ad6e74577 100755 --- a/bin/cluster +++ b/bin/cluster @@ -18,10 +18,10 @@ class Cluster(object): if 'ANSIBLE_SSH_ARGS' not in os.environ: os.environ['ANSIBLE_SSH_ARGS'] = ( '-o ForwardAgent=yes' - '-o StrictHostKeyChecking=no' - '-o UserKnownHostsFile=/dev/null' - '-o ControlMaster=auto' - '-o ControlPersist=600s' + ' -o StrictHostKeyChecking=no' + ' -o UserKnownHostsFile=/dev/null' + ' -o ControlMaster=auto' + ' -o ControlPersist=600s' ) def apply(self): -- cgit v1.2.3 From 2147b1608140f2688ac9781b394824c04e55d07e Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Fri, 20 Mar 2015 09:31:05 -0700 Subject: * Updates from code reviews --- bin/cluster | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index ad6e74577..ce17ee6d7 100755 --- a/bin/cluster +++ b/bin/cluster @@ -42,6 +42,7 @@ class Cluster(object): inventory = '-i inventory/aws/ec2.py' else: + # this code should never be reached assert False, "invalid PROVIDER {}".format(self.args.provider) env = {'cluster_id': self.args.cluster_id} @@ -55,11 +56,12 @@ class Cluster(object): playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(self.args.provider) elif 'list' == self.args.action: # todo: implement cluster list - argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) + raise argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) elif 'update' == self.args.action: # todo: implement cluster update - argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) + raise argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) else: + # this code should never be reached assert False, "invalid ACTION {}".format(self.args.action) verbose = '' @@ -81,13 +83,14 @@ class Cluster(object): sys.stderr.write('RUN [{}]\n'.format(command)) sys.stderr.flush() - os.system(command) + error = os.system(command) + if error != 0: + raise Exception("Ansible run failed with exit code %d".format(error)) + if __name__ == '__main__': parser = argparse.ArgumentParser(description='Manage OpenShift Cluster') - parser.add_argument('-p', '--provider', default='gce', choices=['gce', 'aws'], - help='One of the supported cloud providers') parser.add_argument('-m', '--masters', default=1, type=int, help='number of masters to create in cluster') parser.add_argument('-n', '--nodes', default=2, type=int, help='number of nodes to create in cluster') parser.add_argument('-v', '--verbose', action='count', help='Multiple -v options increase the verbosity') -- cgit v1.2.3 From 14b19e665b118349327a5c8c219cc49c96ae1d52 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Fri, 20 Mar 2015 09:36:34 -0700 Subject: * Replace asserts with raises --- bin/cluster | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index ce17ee6d7..af908155c 100755 --- a/bin/cluster +++ b/bin/cluster @@ -43,7 +43,7 @@ class Cluster(object): inventory = '-i inventory/aws/ec2.py' else: # this code should never be reached - assert False, "invalid PROVIDER {}".format(self.args.provider) + raise argparse.ArgumentError("invalid PROVIDER {}".format(self.args.provider)) env = {'cluster_id': self.args.cluster_id} @@ -62,7 +62,7 @@ class Cluster(object): raise argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) else: # this code should never be reached - assert False, "invalid ACTION {}".format(self.args.action) + raise argparse.ArgumentError("invalid ACTION {}".format(self.args.action)) verbose = '' if self.args.verbose > 0: -- cgit v1.2.3 From 557cc0ca9ecc22a9d90f9cf9ce549186fe286492 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Mon, 23 Mar 2015 09:15:08 -0700 Subject: * Updates from code reviews --- bin/cluster | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index af908155c..823f50671 100755 --- a/bin/cluster +++ b/bin/cluster @@ -83,9 +83,10 @@ class Cluster(object): sys.stderr.write('RUN [{}]\n'.format(command)) sys.stderr.flush() - error = os.system(command) - if error != 0: - raise Exception("Ansible run failed with exit code %d".format(error)) + status = os.system(command) + if status != 0: + sys.stderr.write("RUN [{}] failed with exit status %d".format(command, status)) + exit(status) @@ -100,4 +101,11 @@ if __name__ == '__main__': parser.add_argument('cluster_id', help='prefix for cluster VM names') args = parser.parse_args() + if 'terminate' == args.action: + sys.stderr.write("This will terminate the ENTIRE {} environment. Are you sure? [y/N] ".format(args.cluster_id)) + sys.stderr.flush() + answer = sys.stdin.read(1) + if answer not in ['y', 'Y']: + exit(0) + Cluster(args).apply() -- cgit v1.2.3 From 96729907e131f0cef6f37bcca062e9b092e67d29 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 24 Mar 2015 13:15:53 -0400 Subject: Added spec files and tito configs. --- bin/README_BUILD | 25 ------------------------- bin/openshift-ansible-bin.spec | 14 +++++++------- 2 files changed, 7 insertions(+), 32 deletions(-) delete mode 100644 bin/README_BUILD (limited to 'bin') diff --git a/bin/README_BUILD b/bin/README_BUILD deleted file mode 100644 index 50010e562..000000000 --- a/bin/README_BUILD +++ /dev/null @@ -1,25 +0,0 @@ -# openshift-ansible-bin RPM Build instructions -We use tito to make building and tracking revisions easy. - -For more information on tito, please see the [Tito home page](http://rm-rf.ca/tito "Tito home page"). - - -## Build a test package (no tagging needed) -``` -tito build --test --rpm -``` - - -## Tag a new build (bumps version number and adds log entries) -``` -tito tag -``` - -Follow the on screen tito instructions. - - - -## Build a new package based on the latest tag information -``` -tito build --rpm -``` diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 2b83a7d0b..7ca0cbe9d 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,11 +1,11 @@ -Summary: OpenShift Operations files for mirror +Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.1 +Version: 0.0.0 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible Source0: %{name}-%{version}.tar.gz -Requires: python2 +Requires: python2, openshift-ansible-inventory BuildRequires: python2-devel BuildArch: noarch @@ -18,17 +18,17 @@ Scripts to make it nicer when working with hosts that are defined only by metada %build %install -mkdir -p %{buildroot}/usr/bin +mkdir -p %{buildroot}%{_bindir} mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible mkdir -p %{buildroot}/etc/bash_completion.d -cp -p ossh oscp opssh %{buildroot}/usr/bin +cp -p ossh oscp opssh %{buildroot}%{_bindir} cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d %files -/usr/bin/* -%{python_sitelib}/openshift_ansible/* +%{_bindir}/* +%{python_sitelib}/openshift_ansible/ /etc/bash_completion.d/* %changelog -- cgit v1.2.3 From e7797109bb0cc162dce6f78ed57343a832330910 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 24 Mar 2015 16:14:53 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.1-1]. --- bin/openshift-ansible-bin.spec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 7ca0cbe9d..86b1d4fdf 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.0 +Version: 0.0.1 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -32,3 +32,6 @@ cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d /etc/bash_completion.d/* %changelog +* Tue Mar 24 2015 Thomas Wiest 0.0.1-1 +- new package built with tito + -- cgit v1.2.3 From 16cac480197bfe1738b785aed55204b53d06ad57 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Tue, 24 Mar 2015 17:05:13 -0700 Subject: * Refactor bin/cluster to use argparse.subparsers --- bin/cluster | 182 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 126 insertions(+), 56 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index 823f50671..b99286b46 100755 --- a/bin/cluster +++ b/bin/cluster @@ -8,33 +8,79 @@ import os class Cluster(object): - """Python wrapper to ensure environment is correct for running ansible playbooks """ - - def __init__(self, args): - self.args = args - + Control and Configuration Interface for OpenShift Clusters + """ + def __init__(self): # setup ansible ssh environment if 'ANSIBLE_SSH_ARGS' not in os.environ: os.environ['ANSIBLE_SSH_ARGS'] = ( - '-o ForwardAgent=yes' - ' -o StrictHostKeyChecking=no' - ' -o UserKnownHostsFile=/dev/null' - ' -o ControlMaster=auto' - ' -o ControlPersist=600s' + '-o ForwardAgent=yes ' + '-o StrictHostKeyChecking=no ' + '-o UserKnownHostsFile=/dev/null ' + '-o ControlMaster=auto ' + '-o ControlPersist=600s ' ) - def apply(self): - # setup ansible playbook environment + def create(self, args): + """ + Create an OpenShift cluster for given provider + :param args: command line arguments provided by user + :return: exit status from run command + """ + env = {'cluster_id': args.cluster_id} + playbook = "playbooks/{}/openshift-cluster/launch.yml".format(args.provider) + inventory = self.setup_provider(args.provider) + + env['masters'] = args.masters + env['nodes'] = args.nodes + + return self.action(args, inventory, env, playbook) + + def terminate(self, args): + """ + Destroy OpenShift cluster + :param args: command line arguments provided by user + :return: exit status from run command + """ + env = {'cluster_id': args.cluster_id} + playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(args.provider) + inventory = self.setup_provider(args.provider) + + return self.action(args, inventory, env, playbook) + + def list(self, args): + """ + List VMs in cluster + :param args: command line arguments provided by user + :return: exit status from run command + """ + raise NotImplementedError("ACTION [{}] not implemented".format(sys._getframe().f_code.co_name)) + + def update(self, args): + """ + Update OpenShift across clustered VMs + :param args: command line arguments provided by user + :return: exit status from run command + """ + raise NotImplementedError("ACTION [{}] not implemented".format(sys._getframe().f_code.co_name)) + + + def setup_provider(self, provider): + """ + Setup ansible playbook environment + :param provider: command line arguments provided by user + :return: path to inventory for given provider + """ config = ConfigParser.ConfigParser() - if 'gce' == self.args.provider: + if 'gce' == provider: config.readfp(open('inventory/gce/gce.ini')) for key in config.options('gce'): os.environ[key] = config.get('gce', key) inventory = '-i inventory/gce/gce.py' - elif 'aws' == self.args.provider: + elif 'aws' == provider: config.readfp(open('inventory/aws/ec2.ini')) for key in config.options('ec2'): @@ -43,30 +89,23 @@ class Cluster(object): inventory = '-i inventory/aws/ec2.py' else: # this code should never be reached - raise argparse.ArgumentError("invalid PROVIDER {}".format(self.args.provider)) - - env = {'cluster_id': self.args.cluster_id} - - if 'create' == self.args.action: - playbook = "playbooks/{}/openshift-cluster/launch.yml".format(self.args.provider) - env['masters'] = self.args.masters - env['nodes'] = self.args.nodes - - elif 'terminate' == self.args.action: - playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(self.args.provider) - elif 'list' == self.args.action: - # todo: implement cluster list - raise argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) - elif 'update' == self.args.action: - # todo: implement cluster update - raise argparse.ArgumentError("ACTION {} not implemented".format(self.args.action)) - else: - # this code should never be reached - raise argparse.ArgumentError("invalid ACTION {}".format(self.args.action)) + raise ValueError("invalid PROVIDER {}".format(provider)) + + return inventory + + def action(self, args, inventory, env, playbook): + """ + Build ansible-playbook command line and execute + :param args: command line arguments provided by user + :param inventory: derived provider library + :param env: environment variables for kubernetes + :param playbook: ansible playbook to execute + :return: exit status from ansible-playbook command + """ verbose = '' - if self.args.verbose > 0: - verbose = '-{}'.format('v' * self.args.verbose) + if args.verbose > 0: + verbose = '-{}'.format('v' * args.verbose) ansible_env = '-e \'{}\''.format( ' '.join(['%s=%s' % (key, value) for (key, value) in env.items()]) @@ -76,36 +115,67 @@ class Cluster(object): verbose, inventory, ansible_env, playbook ) - if self.args.verbose > 1: + if args.verbose > 1: command = 'time {}'.format(command) - if self.args.verbose > 0: + if args.verbose > 0: sys.stderr.write('RUN [{}]\n'.format(command)) sys.stderr.flush() - status = os.system(command) - if status != 0: - sys.stderr.write("RUN [{}] failed with exit status %d".format(command, status)) - exit(status) - + return os.system(command) if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Manage OpenShift Cluster') - parser.add_argument('-m', '--masters', default=1, type=int, help='number of masters to create in cluster') - parser.add_argument('-n', '--nodes', default=2, type=int, help='number of nodes to create in cluster') + """ + Implemented to support writing unit tests + """ + + cluster = Cluster() + + providers = ['gce', 'aws'] + parser = argparse.ArgumentParser( + description='Python wrapper to ensure proper environment for OpenShift ansible playbooks', + ) parser.add_argument('-v', '--verbose', action='count', help='Multiple -v options increase the verbosity') - parser.add_argument('--version', action='version', version='%(prog)s 0.1') - parser.add_argument('action', choices=['create', 'terminate', 'update', 'list']) - parser.add_argument('provider', choices=['gce', 'aws']) - parser.add_argument('cluster_id', help='prefix for cluster VM names') + parser.add_argument('--version', action='version', version='%(prog)s 0.2') + + meta_parser = argparse.ArgumentParser(add_help=False) + meta_parser.add_argument('provider', choices=providers, help='provider') + meta_parser.add_argument('cluster_id', help='prefix for cluster VM names') + + action_parser = parser.add_subparsers(dest='action', title='actions', description='Choose from valid actions') + + create_parser = action_parser.add_parser('create', help='Create a cluster', parents=[meta_parser]) + create_parser.add_argument('-m', '--masters', default=1, type=int, help='number of masters to create in cluster') + create_parser.add_argument('-n', '--nodes', default=2, type=int, help='number of nodes to create in cluster') + create_parser.set_defaults(func=cluster.create) + + terminate_parser = action_parser.add_parser('terminate', help='Destroy a cluster', parents=[meta_parser]) + terminate_parser.add_argument('-f', '--force', action='store_true', help='Destroy cluster without confirmation') + terminate_parser.set_defaults(func=cluster.terminate) + + update_parser = action_parser.add_parser('update', help='Update OpenShift across cluster', parents=[meta_parser]) + update_parser.add_argument('-f', '--force', action='store_true', help='Update cluster without confirmation') + update_parser.set_defaults(func=cluster.update) + + list_parser = action_parser.add_parser('list', help='List VMs in cluster', parents=[meta_parser]) + list_parser.set_defaults(func=cluster.list) + args = parser.parse_args() - if 'terminate' == args.action: - sys.stderr.write("This will terminate the ENTIRE {} environment. Are you sure? [y/N] ".format(args.cluster_id)) - sys.stderr.flush() - answer = sys.stdin.read(1) + if 'terminate' == args.action and not args.force: + answer = raw_input("This will destroy the ENTIRE {} environment. Are you sure? [y/N] ".format(args.cluster_id)) + if answer not in ['y', 'Y']: + sys.stderr.write('\nACTION [terminate] aborted by user!\n') + exit(1) + + if 'update' == args.action and not args.force: + answer = raw_input("This is destructive and could corrupt {} environment. Continue? [y/N] ".format(args.cluster_id)) if answer not in ['y', 'Y']: - exit(0) + sys.stderr.write('\nACTION [update] aborted by user!\n') + exit(1) - Cluster(args).apply() + status = args.func(args) + if status != 0: + sys.stderr.write("ACTION [{}] failed with exit status {}\n".format(args.action, status)) + exit(status) -- cgit v1.2.3 From b1b462f4db3ce1a26cfc251895d5f8fe2e15c484 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 30 Mar 2015 11:25:03 -0400 Subject: added config file support to opssh, ossh, and oscp --- bin/awsutil.py | 29 ++++++++++++++++------------- bin/openshift-ansible-bin.spec | 4 ++++ bin/openshift_ansible.conf.example | 6 ++++++ bin/opssh | 32 ++++++++++++++++++++++++++++++-- bin/oscp | 25 +++++++++++++++++++++++-- bin/ossh | 23 ++++++++++++++++++++++- 6 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 bin/openshift_ansible.conf.example (limited to 'bin') diff --git a/bin/awsutil.py b/bin/awsutil.py index 37259b946..78421e5f5 100644 --- a/bin/awsutil.py +++ b/bin/awsutil.py @@ -6,27 +6,30 @@ 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'], - } + def __init__(self, inventory_path=None, host_type_aliases={}): + self.host_type_aliases = host_type_aliases + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + + if inventory_path is None: + inventory_path = os.path.realpath(os.path.join(self.file_path, \ + '..','inventory','multi_ec2.py')) + + if not os.path.isfile(inventory_path): + raise Exception("Inventory file not found [%s]" % inventory_path) + self.inventory_path = inventory_path + self.setup_host_type_alias_lookup() + + def setup_host_type_alias_lookup(self): 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] + cmd = [self.inventory_path] if args: cmd.extend(args) diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 86b1d4fdf..1bd486bff 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -21,15 +21,19 @@ Scripts to make it nicer when working with hosts that are defined only by metada mkdir -p %{buildroot}%{_bindir} mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible mkdir -p %{buildroot}/etc/bash_completion.d +mkdir -p %{buildroot}/etc/openshift_ansible cp -p ossh oscp opssh %{buildroot}%{_bindir} cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d +cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshift_ansible.conf + %files %{_bindir}/* %{python_sitelib}/openshift_ansible/ /etc/bash_completion.d/* +%config(noreplace) /etc/openshift_ansible/ %changelog * Tue Mar 24 2015 Thomas Wiest 0.0.1-1 diff --git a/bin/openshift_ansible.conf.example b/bin/openshift_ansible.conf.example new file mode 100644 index 000000000..e891b855a --- /dev/null +++ b/bin/openshift_ansible.conf.example @@ -0,0 +1,6 @@ +#[main] +#inventory = /usr/share/ansible/inventory/multi_ec2.py + +#[host_type_aliases] +#host-type-one = aliasa,aliasb +#host-type-two = aliasfortwo diff --git a/bin/opssh b/bin/opssh index 71e5bf9f2..d64096fd4 100755 --- a/bin/opssh +++ b/bin/opssh @@ -10,16 +10,30 @@ import re import tempfile import time import subprocess +import ConfigParser -DEFAULT_PSSH_PAR=200 +DEFAULT_PSSH_PAR = 200 PSSH = '/usr/bin/pssh' +CONFIG_MAIN_SECTION = 'main' +CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases' +CONFIG_INVENTORY_OPTION = 'inventory' + class Opssh(object): def __init__(self): + self.inventory = None + self.host_type_aliases = {} self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) - self.aws = awsutil.AwsUtil() + + # Default the config path to /etc + self.config_path = os.path.join(os.path.sep, 'etc', \ + 'openshift_ansible', \ + 'openshift_ansible.conf') self.parse_cli_args() + self.parse_config_file() + + self.aws = awsutil.AwsUtil(self.inventory, self.host_type_aliases) if self.args.list_host_types: self.aws.print_host_types() @@ -66,6 +80,20 @@ class Opssh(object): return None + def parse_config_file(self): + if os.path.isfile(self.config_path): + config = ConfigParser.ConfigParser() + config.read(self.config_path) + + if config.has_section(CONFIG_MAIN_SECTION) and \ + config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION): + self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION) + + self.host_type_aliases = {} + if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION): + for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION): + value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',') + self.host_type_aliases[alias] = value def parse_cli_args(self): """Setup the command line parser with the options we want diff --git a/bin/oscp b/bin/oscp index 146bbbea5..011f37c7c 100755 --- a/bin/oscp +++ b/bin/oscp @@ -7,16 +7,28 @@ import traceback import sys import os import re +import ConfigParser + +CONFIG_MAIN_SECTION = 'main' +CONFIG_INVENTORY_OPTION = 'inventory' class Oscp(object): def __init__(self): + self.inventory = None self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + + # Default the config path to /etc + self.config_path = os.path.join(os.path.sep, 'etc', \ + 'openshift_ansible', \ + 'openshift_ansible.conf') + self.parse_cli_args() + self.parse_config_file() # parse host and user self.process_host() - self.aws = awsutil.AwsUtil() + self.aws = awsutil.AwsUtil(self.inventory) # get a dict of host inventory if self.args.list: @@ -38,9 +50,18 @@ class Oscp(object): else: self.scp() + def parse_config_file(self): + if os.path.isfile(self.config_path): + config = ConfigParser.ConfigParser() + config.read(self.config_path) + + if config.has_section(CONFIG_MAIN_SECTION) and \ + config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION): + self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION) + def parse_cli_args(self): parser = argparse.ArgumentParser(description='Openshift Online SSH Tool.') - parser.add_argument('-e', '--env', + parser.add_argument('-e', '--env', action="store", help="Environment where this server exists.") parser.add_argument('-d', '--debug', default=False, action="store_true", help="debug mode") diff --git a/bin/ossh b/bin/ossh index 66a4cfb5c..134f4c46a 100755 --- a/bin/ossh +++ b/bin/ossh @@ -7,13 +7,25 @@ import traceback import sys import os import re +import ConfigParser + +CONFIG_MAIN_SECTION = 'main' +CONFIG_INVENTORY_OPTION = 'inventory' class Ossh(object): def __init__(self): + self.inventory = None self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + + # Default the config path to /etc + self.config_path = os.path.join(os.path.sep, 'etc', \ + 'openshift_ansible', \ + 'openshift_ansible.conf') + self.parse_cli_args() + self.parse_config_file() - self.aws = awsutil.AwsUtil() + self.aws = awsutil.AwsUtil(self.inventory) # get a dict of host inventory if self.args.list: @@ -37,6 +49,15 @@ class Ossh(object): else: self.ssh() + def parse_config_file(self): + if os.path.isfile(self.config_path): + config = ConfigParser.ConfigParser() + config.read(self.config_path) + + if config.has_section(CONFIG_MAIN_SECTION) and \ + config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION): + self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION) + def parse_cli_args(self): parser = argparse.ArgumentParser(description='Openshift Online SSH Tool.') parser.add_argument('-e', '--env', action="store", -- cgit v1.2.3 From 1ba0619575f23e880d431ec2a15b9c02bfc5e3a9 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 30 Mar 2015 14:51:41 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.2-1]. --- bin/openshift-ansible-bin.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 1bd486bff..38e0a0d59 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.1 +Version: 0.0.2 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,8 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Mon Mar 30 2015 Thomas Wiest 0.0.2-1 +- added config file support to opssh, ossh, and oscp (twiest@redhat.com) * Tue Mar 24 2015 Thomas Wiest 0.0.1-1 - new package built with tito -- cgit v1.2.3 From 24ce165d9ca662f9a0438e658197ff41fd02ae03 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 30 Mar 2015 17:46:21 -0400 Subject: created a python package named openshift_ansible --- bin/awsutil.py | 141 ------------------------------------- bin/openshift-ansible-bin.spec | 2 +- bin/openshift_ansible/__init__.py | 0 bin/openshift_ansible/awsutil.py | 142 ++++++++++++++++++++++++++++++++++++++ bin/opssh | 3 +- bin/oscp | 3 +- bin/ossh | 3 +- 7 files changed, 149 insertions(+), 145 deletions(-) delete mode 100644 bin/awsutil.py create mode 100644 bin/openshift_ansible/__init__.py create mode 100644 bin/openshift_ansible/awsutil.py (limited to 'bin') diff --git a/bin/awsutil.py b/bin/awsutil.py deleted file mode 100644 index 78421e5f5..000000000 --- a/bin/awsutil.py +++ /dev/null @@ -1,141 +0,0 @@ -# vim: expandtab:tabstop=4:shiftwidth=4 - -import subprocess -import os -import json -import re - -class AwsUtil(object): - def __init__(self, inventory_path=None, host_type_aliases={}): - self.host_type_aliases = host_type_aliases - self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) - - if inventory_path is None: - inventory_path = os.path.realpath(os.path.join(self.file_path, \ - '..','inventory','multi_ec2.py')) - - if not os.path.isfile(inventory_path): - raise Exception("Inventory file not found [%s]" % inventory_path) - - self.inventory_path = inventory_path - self.setup_host_type_alias_lookup() - - def setup_host_type_alias_lookup(self): - self.alias_lookup = {} - for key, values in self.host_type_aliases.iteritems(): - for value in values: - self.alias_lookup[value] = key - - - - def get_inventory(self,args=[]): - cmd = [self.inventory_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 you don't have an environment tag, we're going to ignore you - if 'ec2_tag_environment' not in host: - continue - - 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/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 38e0a0d59..349cd3059 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -24,7 +24,7 @@ mkdir -p %{buildroot}/etc/bash_completion.d mkdir -p %{buildroot}/etc/openshift_ansible cp -p ossh oscp opssh %{buildroot}%{_bindir} -cp -p awsutil.py %{buildroot}%{python_sitelib}/openshift_ansible +cp -p openshift_ansible/* %{buildroot}%{python_sitelib}/openshift_ansible cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshift_ansible.conf diff --git a/bin/openshift_ansible/__init__.py b/bin/openshift_ansible/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/bin/openshift_ansible/awsutil.py b/bin/openshift_ansible/awsutil.py new file mode 100644 index 000000000..8fef0a24f --- /dev/null +++ b/bin/openshift_ansible/awsutil.py @@ -0,0 +1,142 @@ +# vim: expandtab:tabstop=4:shiftwidth=4 + +import subprocess +import os +import json +import re + +class AwsUtil(object): + def __init__(self, inventory_path=None, host_type_aliases={}): + self.host_type_aliases = host_type_aliases + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + + if inventory_path is None: + inventory_path = os.path.realpath(os.path.join(self.file_path, \ + '..', '..', 'inventory', \ + 'multi_ec2.py')) + + if not os.path.isfile(inventory_path): + raise Exception("Inventory file not found [%s]" % inventory_path) + + self.inventory_path = inventory_path + self.setup_host_type_alias_lookup() + + def setup_host_type_alias_lookup(self): + self.alias_lookup = {} + for key, values in self.host_type_aliases.iteritems(): + for value in values: + self.alias_lookup[value] = key + + + + def get_inventory(self,args=[]): + cmd = [self.inventory_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 you don't have an environment tag, we're going to ignore you + if 'ec2_tag_environment' not in host: + continue + + 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 d64096fd4..d8137fb20 100755 --- a/bin/opssh +++ b/bin/opssh @@ -2,7 +2,6 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import awsutil import traceback import sys import os @@ -12,6 +11,8 @@ import time import subprocess import ConfigParser +from openshift_ansible import awsutil + DEFAULT_PSSH_PAR = 200 PSSH = '/usr/bin/pssh' CONFIG_MAIN_SECTION = 'main' diff --git a/bin/oscp b/bin/oscp index 011f37c7c..461ad0a0f 100755 --- a/bin/oscp +++ b/bin/oscp @@ -2,13 +2,14 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import awsutil import traceback import sys import os import re import ConfigParser +from openshift_ansible import awsutil + CONFIG_MAIN_SECTION = 'main' CONFIG_INVENTORY_OPTION = 'inventory' diff --git a/bin/ossh b/bin/ossh index 134f4c46a..c16ea6eda 100755 --- a/bin/ossh +++ b/bin/ossh @@ -2,13 +2,14 @@ # vim: expandtab:tabstop=4:shiftwidth=4 import argparse -import awsutil import traceback import sys import os import re import ConfigParser +from openshift_ansible import awsutil + CONFIG_MAIN_SECTION = 'main' CONFIG_INVENTORY_OPTION = 'inventory' -- cgit v1.2.3 From 4a6d1c328d92047fcd924dce821613c8579f1745 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 30 Mar 2015 18:13:27 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.3-1]. --- bin/openshift-ansible-bin.spec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 349cd3059..7cca5ffba 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.2 +Version: 0.0.3 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,9 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Mon Mar 30 2015 Thomas Wiest 0.0.3-1 +- created a python package named openshift_ansible (twiest@redhat.com) + * Mon Mar 30 2015 Thomas Wiest 0.0.2-1 - added config file support to opssh, ossh, and oscp (twiest@redhat.com) * Tue Mar 24 2015 Thomas Wiest 0.0.1-1 -- cgit v1.2.3 From d9a178298ae8b6d487baf79559f4d82b2d71e49a Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 31 Mar 2015 15:36:29 -0400 Subject: Fixed when tag was missing and added opssh completion --- bin/ossh_bash_completion | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/ossh_bash_completion b/bin/ossh_bash_completion index 6a95ce6ee..1467de858 100755 --- a/bin/ossh_bash_completion +++ b/bin/ossh_bash_completion @@ -1,6 +1,7 @@ __ossh_known_hosts(){ if [[ -f ~/.ansible/tmp/multi_ec2_inventory.cache ]]; then - /usr/bin/python -c 'import json,os; z = json.loads(open("%s"%os.path.expanduser("~/.ansible/tmp/multi_ec2_inventory.cache")).read()); print "\n".join(["%s.%s" % (host["ec2_tag_Name"],host["ec2_tag_environment"]) for dns, host in z["_meta"]["hostvars"].items()])' + /usr/bin/python -c 'import json,os; z = json.loads(open("%s"%os.path.expanduser("~/.ansible/tmp/multi_ec2_inventory.cache")).read()); print "\n".join(["%s.%s" % (host["ec2_tag_Name"],host["ec2_tag_environment"]) for dns, host in z["_meta"]["hostvars"].items() if all(k in host for k in ("ec2_tag_Name", "ec2_tag_environment"))])' + fi } @@ -16,3 +17,23 @@ _ossh() return 0 } complete -F _ossh ossh oscp + +__opssh_known_hosts(){ + if [[ -f ~/.ansible/tmp/multi_ec2_inventory.cache ]]; then + /usr/bin/python -c 'import json,os; z = json.loads(open("%s"%os.path.expanduser("~/.ansible/tmp/multi_ec2_inventory.cache")).read()); print "\n".join(["%s" % (host["ec2_tag_host-type"]) for dns, host in z["_meta"]["hostvars"].items() if "ec2_tag_host-type" in host])' + fi +} + +_opssh() +{ + local cur prev known_hosts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + known_hosts="$(__opssh_known_hosts)" + COMPREPLY=( $(compgen -W "${known_hosts}" -- ${cur})) + + return 0 +} +complete -F _opssh opssh + -- cgit v1.2.3 From 5f0b024fedc826722306c159bbf91a3c74ec3b4e Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 31 Mar 2015 16:49:13 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.4-1]. --- bin/openshift-ansible-bin.spec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 7cca5ffba..f87002456 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.3 +Version: 0.0.4 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,9 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Tue Mar 31 2015 Thomas Wiest 0.0.4-1 +- Fixed when tag was missing and added opssh completion (kwoodson@redhat.com) + * Mon Mar 30 2015 Thomas Wiest 0.0.3-1 - created a python package named openshift_ansible (twiest@redhat.com) -- cgit v1.2.3 From 4712e72c912a1102bff0508c98bd97da3f33ae95 Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Mon, 23 Mar 2015 23:53:17 -0400 Subject: openshift_facts role/module refactor default settings - Add openshift_facts role and module - Created new role openshift_facts that contains an openshift_facts module - Refactor openshift_* roles to use openshift_facts instead of relying on defaults - Refactor playbooks to use openshift_facts - Cleanup inventory group_vars - Update defaults - update openshift_master role firewall defaults - remove etcd peer port, since we will not be supporting clustered embedded etcd - remove 8444 since console now runs on the api port by default - add 8444 and 7001 to disabled services to ensure removal if updating - Add new role os_env_extras_node that is a subset of the docker role - previously, we were starting/enabling docker which was causing issues with some installations - Does not install or start docker, since the openshift-node role will handle that for us - Only adds root to the dockerroot group - Update playbooks to use ops_env_extras_node role instead of docker role - os_firewall bug fixes - ignore ip6tables for now, since we are not configuring any ipv6 rules - if installing package do a daemon-reload before starting/enabling service - Add aws support to bin/cluster - Add list action to bin/cluster - Add update action to bin/cluster - cleanup some stray debug statements - some variable renaming for clarity --- bin/cluster | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index b99286b46..36ab1da1b 100755 --- a/bin/cluster +++ b/bin/cluster @@ -32,8 +32,8 @@ class Cluster(object): playbook = "playbooks/{}/openshift-cluster/launch.yml".format(args.provider) inventory = self.setup_provider(args.provider) - env['masters'] = args.masters - env['nodes'] = args.nodes + env['num_masters'] = args.masters + env['num_nodes'] = args.nodes return self.action(args, inventory, env, playbook) @@ -55,16 +55,23 @@ class Cluster(object): :param args: command line arguments provided by user :return: exit status from run command """ - raise NotImplementedError("ACTION [{}] not implemented".format(sys._getframe().f_code.co_name)) + env = {'cluster_id': args.cluster_id} + playbook = "playbooks/{}/openshift-cluster/list.yml".format(args.provider) + inventory = self.setup_provider(args.provider) + + return self.action(args, inventory, env, playbook) def update(self, args): """ - Update OpenShift across clustered VMs + Update to latest OpenShift across clustered VMs :param args: command line arguments provided by user :return: exit status from run command """ - raise NotImplementedError("ACTION [{}] not implemented".format(sys._getframe().f_code.co_name)) + env = {'cluster_id': args.cluster_id} + playbook = "playbooks/{}/openshift-cluster/update.yml".format(args.provider) + inventory = self.setup_provider(args.provider) + return self.action(args, inventory, env, playbook) def setup_provider(self, provider): """ -- cgit v1.2.3 From 185261ab927c6997c1bc3eefe2ab4cd804b8a7f0 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Tue, 7 Apr 2015 16:51:40 -0400 Subject: fixed the opssh default output behavior to be consistent with pssh. Also fixed a bug in how directories are named for --outdir and --errdir. --- bin/opssh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'bin') diff --git a/bin/opssh b/bin/opssh index d8137fb20..ad1aadc29 100755 --- a/bin/opssh +++ b/bin/opssh @@ -57,13 +57,16 @@ class Opssh(object): """ # Default set of options - pssh_args = [PSSH, '-i', '-t', '0', '-p', str(self.args.par), '--user', self.args.user] + pssh_args = [PSSH, '-t', '0', '-p', str(self.args.par), '--user', self.args.user] + + if self.args.inline: + pssh_args.append("--inline") if self.args.outdir: - pssh_args.append("--outdir='%s'" % self.args.outdir) + pssh_args.extend(["--outdir", self.args.outdir]) if self.args.errdir: - pssh_args.append("--errdir='%s'" % self.args.errdir) + pssh_args.extend(["--errdir", self.args.errdir]) hosts = self.aws.get_host_list(self.args.host_type, self.args.env) with tempfile.NamedTemporaryFile(prefix='opssh-', delete=True) as f: @@ -71,8 +74,8 @@ class Opssh(object): f.write(h + os.linesep) f.flush() - pssh_args.extend(["-h", "%s" % f.name]) - pssh_args.append("%s" % self.args.command) + pssh_args.extend(["-h", f.name]) + pssh_args.append(self.args.command) print print "Running: %s" % ' '.join(pssh_args) @@ -117,6 +120,9 @@ class Opssh(object): parser.add_argument('--user', action='store', default='root', help='username') + parser.add_argument('-i', '--inline', default=False, action='store_true', + help='inline aggregated output and error for each server') + parser.add_argument('-p', '--par', action='store', default=DEFAULT_PSSH_PAR, help=('max number of parallel threads (default %s)' % DEFAULT_PSSH_PAR)) -- cgit v1.2.3 From 4fe5e4645c099c69254b0e99081732a8b6af577a Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Wed, 8 Apr 2015 11:50:11 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.5-1]. --- bin/openshift-ansible-bin.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index f87002456..f509bdd79 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.4 +Version: 0.0.5 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,10 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Wed Apr 08 2015 Thomas Wiest 0.0.5-1 +- fixed the opssh default output behavior to be consistent with pssh. Also + fixed a bug in how directories are named for --outdir and --errdir. + (twiest@redhat.com) * Tue Mar 31 2015 Thomas Wiest 0.0.4-1 - Fixed when tag was missing and added opssh completion (kwoodson@redhat.com) -- cgit v1.2.3 From 6d0b77b9f3dbd439aa7e2d1d877e121214f284a8 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Thu, 9 Apr 2015 15:04:17 -0400 Subject: fixed bug where opssh would throw an exception if pssh returned a non-zero exit code --- bin/opssh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/opssh b/bin/opssh index ad1aadc29..453da65b4 100755 --- a/bin/opssh +++ b/bin/opssh @@ -19,6 +19,7 @@ CONFIG_MAIN_SECTION = 'main' CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases' CONFIG_INVENTORY_OPTION = 'inventory' +class ArgumentMismatchError(ValueError): pass class Opssh(object): def __init__(self): @@ -36,21 +37,18 @@ class Opssh(object): self.aws = awsutil.AwsUtil(self.inventory, self.host_type_aliases) + def run(self): if self.args.list_host_types: self.aws.print_host_types() - return + return 0 if self.args.env and \ self.args.host_type and \ self.args.command: - retval = self.run_pssh() - if retval != 0: - raise ValueError("pssh run failed") - - return + return self.run_pssh() # If it makes it here, we weren't able to determine what they wanted to do - raise ValueError("Invalid combination of arguments") + raise ArgumentMismatchError("Invalid combination of arguments") def run_pssh(self): """Actually run the pssh command based off of the supplied options @@ -142,5 +140,7 @@ if __name__ == '__main__': try: opssh = Opssh() - except ValueError as e: + exitcode = opssh.run() + sys.exit(exitcode) + except ArgumentMismatchError as e: print "\nError: %s\n" % e.message -- cgit v1.2.3 From f0ae24d8346ba8cafe6c8f9890433789b5367078 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Thu, 9 Apr 2015 15:20:31 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.6-1]. --- bin/openshift-ansible-bin.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index f509bdd79..695aebc28 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.5 +Version: 0.0.6 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,10 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Thu Apr 09 2015 Thomas Wiest 0.0.6-1 +- fixed bug where opssh would throw an exception if pssh returned a non-zero + exit code (twiest@redhat.com) + * Wed Apr 08 2015 Thomas Wiest 0.0.5-1 - fixed the opssh default output behavior to be consistent with pssh. Also fixed a bug in how directories are named for --outdir and --errdir. -- cgit v1.2.3 From 9fbec064d28a72963b1566258b4bcabcd63b2c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9na=C3=AFc=20Huard?= Date: Wed, 8 Apr 2015 16:33:55 +0200 Subject: Add libvirt as a provider --- bin/cluster | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/cluster b/bin/cluster index 36ab1da1b..ca227721e 100755 --- a/bin/cluster +++ b/bin/cluster @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # vim: expandtab:tabstop=4:shiftwidth=4 import argparse @@ -94,6 +94,8 @@ class Cluster(object): os.environ[key] = config.get('ec2', key) inventory = '-i inventory/aws/ec2.py' + elif 'libvirt' == provider: + inventory = '-i inventory/libvirt/hosts' else: # this code should never be reached raise ValueError("invalid PROVIDER {}".format(provider)) @@ -139,7 +141,7 @@ if __name__ == '__main__': cluster = Cluster() - providers = ['gce', 'aws'] + providers = ['gce', 'aws', 'libvirt'] parser = argparse.ArgumentParser( description='Python wrapper to ensure proper environment for OpenShift ansible playbooks', ) -- cgit v1.2.3 From 6b74f852258b51c1558aff3967288a57ca4efb86 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Fri, 10 Apr 2015 16:30:32 -0400 Subject: added ohi --- bin/ohi | 96 ++++++++++++++++++++++++++++++++++++++++++ bin/openshift-ansible-bin.spec | 2 +- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100755 bin/ohi (limited to 'bin') diff --git a/bin/ohi b/bin/ohi new file mode 100755 index 000000000..06a375cdb --- /dev/null +++ b/bin/ohi @@ -0,0 +1,96 @@ +#!/usr/bin/env python +# vim: expandtab:tabstop=4:shiftwidth=4 + +import argparse +import traceback +import sys +import os +import re +import tempfile +import time +import subprocess +import ConfigParser + +from openshift_ansible import awsutil + +CONFIG_MAIN_SECTION = 'main' +CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases' +CONFIG_INVENTORY_OPTION = 'inventory' + +class ArgumentMismatchError(ValueError): pass + +class Ohi(object): + def __init__(self): + self.inventory = None + self.host_type_aliases = {} + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + + # Default the config path to /etc + self.config_path = os.path.join(os.path.sep, 'etc', \ + 'openshift_ansible', \ + 'openshift_ansible.conf') + + self.parse_cli_args() + self.parse_config_file() + + self.aws = awsutil.AwsUtil(self.inventory, self.host_type_aliases) + + def run(self): + if self.args.list_host_types: + self.aws.print_host_types() + return 0 + + if self.args.env and \ + self.args.host_type: + hosts = self.aws.get_host_list(self.args.host_type, self.args.env) + for host in hosts: + print host + return 0 + + # If it makes it here, we weren't able to determine what they wanted to do + raise ArgumentMismatchError("Invalid combination of arguments") + + def parse_config_file(self): + if os.path.isfile(self.config_path): + config = ConfigParser.ConfigParser() + config.read(self.config_path) + + if config.has_section(CONFIG_MAIN_SECTION) and \ + config.has_option(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION): + self.inventory = config.get(CONFIG_MAIN_SECTION, CONFIG_INVENTORY_OPTION) + + self.host_type_aliases = {} + if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION): + for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION): + value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',') + self.host_type_aliases[alias] = value + + def parse_cli_args(self): + """Setup the command line parser with the options we want + """ + + parser = argparse.ArgumentParser(description='Openshift Host Inventory') + + parser.add_argument('--list-host-types', default=False, action='store_true', + help='List all of the host types') + + parser.add_argument('-e', '--env', action="store", + help="Which environment to use") + + parser.add_argument('-t', '--host-type', action="store", + help="Which host type to use") + + self.args = parser.parse_args() + + +if __name__ == '__main__': + if len(sys.argv) == 1: + print "\nError: No options given. Use --help to see the available options\n" + sys.exit(0) + + try: + ohi = Ohi() + exitcode = ohi.run() + sys.exit(exitcode) + except ArgumentMismatchError as e: + print "\nError: %s\n" % e.message diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 695aebc28..876bca1d7 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -23,7 +23,7 @@ mkdir -p %{buildroot}%{python_sitelib}/openshift_ansible mkdir -p %{buildroot}/etc/bash_completion.d mkdir -p %{buildroot}/etc/openshift_ansible -cp -p ossh oscp opssh %{buildroot}%{_bindir} +cp -p ossh oscp opssh ohi %{buildroot}%{_bindir} cp -p openshift_ansible/* %{buildroot}%{python_sitelib}/openshift_ansible cp -p ossh_bash_completion %{buildroot}/etc/bash_completion.d -- cgit v1.2.3 From 8a7a455abfcf8df7ddc706d11167cb904e1b52dd Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Fri, 10 Apr 2015 17:36:22 -0400 Subject: added the ability to run opssh and ohi on all hosts in an environment, as well as all hosts of the same host-type regardless of environment --- bin/ohi | 36 +++++++++++++++++++++++++----------- bin/openshift_ansible/awsutil.py | 40 +++++++++++++++++++++++++++++++++++++--- bin/opssh | 36 ++++++++++++++++++++++++++---------- 3 files changed, 88 insertions(+), 24 deletions(-) (limited to 'bin') diff --git a/bin/ohi b/bin/ohi index 06a375cdb..408961ee4 100755 --- a/bin/ohi +++ b/bin/ohi @@ -12,13 +12,12 @@ import subprocess import ConfigParser from openshift_ansible import awsutil +from openshift_ansible.awsutil import ArgumentError CONFIG_MAIN_SECTION = 'main' CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases' CONFIG_INVENTORY_OPTION = 'inventory' -class ArgumentMismatchError(ValueError): pass - class Ohi(object): def __init__(self): self.inventory = None @@ -40,15 +39,30 @@ class Ohi(object): self.aws.print_host_types() return 0 - if self.args.env and \ - self.args.host_type: - hosts = self.aws.get_host_list(self.args.host_type, self.args.env) - for host in hosts: - print host - return 0 + hosts = None + if self.args.host_type is not None and \ + self.args.env is not None: + # Both env and host-type specified + hosts = self.aws.get_host_list(host_type=self.args.host_type, \ + env=self.args.env) + + if self.args.host_type is None and \ + self.args.env is not None: + # Only env specified + hosts = self.aws.get_host_list(env=self.args.env) + + if self.args.host_type is not None and \ + self.args.env is None: + # Only host-type specified + hosts = self.aws.get_host_list(host_type=self.args.host_type) + + if hosts is None: + # We weren't able to determine what they wanted to do + raise ArgumentError("Invalid combination of arguments") - # If it makes it here, we weren't able to determine what they wanted to do - raise ArgumentMismatchError("Invalid combination of arguments") + for host in hosts: + print host + return 0 def parse_config_file(self): if os.path.isfile(self.config_path): @@ -92,5 +106,5 @@ if __name__ == '__main__': ohi = Ohi() exitcode = ohi.run() sys.exit(exitcode) - except ArgumentMismatchError as e: + except ArgumentError as e: print "\nError: %s\n" % e.message diff --git a/bin/openshift_ansible/awsutil.py b/bin/openshift_ansible/awsutil.py index 8fef0a24f..65b269930 100644 --- a/bin/openshift_ansible/awsutil.py +++ b/bin/openshift_ansible/awsutil.py @@ -5,6 +5,10 @@ import os import json import re +class ArgumentError(Exception): + def __init__(self, message): + self.message = message + class AwsUtil(object): def __init__(self, inventory_path=None, host_type_aliases={}): self.host_type_aliases = host_type_aliases @@ -128,15 +132,45 @@ class AwsUtil(object): return self.alias_lookup[host_type] return host_type + def gen_env_tag(self, env): + """Generate the environment tag + """ + return "tag_environment_%s" % env + + def gen_host_type_tag(self, host_type): + """Generate the host type tag + """ + host_type = self.resolve_host_type(host_type) + return "tag_host-type_%s" % 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): + def get_host_list(self, host_type=None, env=None): """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] + + if host_type is not None and \ + env is not None: + # Both host type and environment were specified + env_host_type_tag = self.gen_env_host_type_tag(host_type, env) + return inv[env_host_type_tag] + + if host_type is None and \ + env is not None: + # Just environment was specified + host_type_tag = self.gen_env_tag(env) + return inv[host_type_tag] + + if host_type is not None and \ + env is None: + # Just host-type was specified + host_type_tag = self.gen_host_type_tag(host_type) + return inv[host_type_tag] + + # We should never reach here! + raise ArgumentError("Invalid combination of parameters") diff --git a/bin/opssh b/bin/opssh index 453da65b4..5fb447318 100755 --- a/bin/opssh +++ b/bin/opssh @@ -12,6 +12,7 @@ import subprocess import ConfigParser from openshift_ansible import awsutil +from openshift_ansible.awsutil import ArgumentError DEFAULT_PSSH_PAR = 200 PSSH = '/usr/bin/pssh' @@ -19,8 +20,6 @@ CONFIG_MAIN_SECTION = 'main' CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases' CONFIG_INVENTORY_OPTION = 'inventory' -class ArgumentMismatchError(ValueError): pass - class Opssh(object): def __init__(self): self.inventory = None @@ -42,13 +41,30 @@ class Opssh(object): self.aws.print_host_types() return 0 - if self.args.env and \ - self.args.host_type and \ - self.args.command: - return self.run_pssh() + hosts = None + if self.args.host_type is not None and \ + self.args.env is not None: + # Both env and host-type specified + hosts = self.aws.get_host_list(host_type=self.args.host_type, \ + env=self.args.env) + + if self.args.host_type is None and \ + self.args.env is not None: + # Only env specified + hosts = self.aws.get_host_list(env=self.args.env) + + if self.args.host_type is not None and \ + self.args.env is None: + # Only host-type specified + hosts = self.aws.get_host_list(host_type=self.args.host_type) + + if hosts is None: + # We weren't able to determine what they wanted to do + raise ArgumentError("Invalid combination of arguments") - # If it makes it here, we weren't able to determine what they wanted to do - raise ArgumentMismatchError("Invalid combination of arguments") + for host in hosts: + print host + return 0 def run_pssh(self): """Actually run the pssh command based off of the supplied options @@ -109,7 +125,7 @@ class Opssh(object): parser.add_argument('-e', '--env', action="store", help="Which environment to use") - parser.add_argument('-t', '--host-type', action="store", + parser.add_argument('-t', '--host-type', action="store", default=None, help="Which host type to use") parser.add_argument('-c', '--command', action='store', @@ -142,5 +158,5 @@ if __name__ == '__main__': opssh = Opssh() exitcode = opssh.run() sys.exit(exitcode) - except ArgumentMismatchError as e: + except ArgumentError as e: print "\nError: %s\n" % e.message -- cgit v1.2.3 From 1cb4ba976599e6e4fd18568f7dc46b58db5b4161 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 13 Apr 2015 10:07:39 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.7-1]. --- bin/openshift-ansible-bin.spec | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 876bca1d7..04af4546c 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.6 +Version: 0.0.7 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,11 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Mon Apr 13 2015 Thomas Wiest 0.0.7-1 +- added the ability to run opssh and ohi on all hosts in an environment, as + well as all hosts of the same host-type regardless of environment + (twiest@redhat.com) +- added ohi (twiest@redhat.com) * Thu Apr 09 2015 Thomas Wiest 0.0.6-1 - fixed bug where opssh would throw an exception if pssh returned a non-zero exit code (twiest@redhat.com) -- cgit v1.2.3 From fa08dd34159e16c574f551cd48d7194f5888a128 Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 13 Apr 2015 16:35:55 -0400 Subject: fixed bug in opssh where it wouldn't actually run pssh --- bin/opssh | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) (limited to 'bin') diff --git a/bin/opssh b/bin/opssh index 5fb447318..a4fceb6a8 100755 --- a/bin/opssh +++ b/bin/opssh @@ -41,30 +41,12 @@ class Opssh(object): self.aws.print_host_types() return 0 - hosts = None - if self.args.host_type is not None and \ + if self.args.host_type is not None or \ self.args.env is not None: - # Both env and host-type specified - hosts = self.aws.get_host_list(host_type=self.args.host_type, \ - env=self.args.env) + return self.run_pssh() - if self.args.host_type is None and \ - self.args.env is not None: - # Only env specified - hosts = self.aws.get_host_list(env=self.args.env) - - if self.args.host_type is not None and \ - self.args.env is None: - # Only host-type specified - hosts = self.aws.get_host_list(host_type=self.args.host_type) - - if hosts is None: - # We weren't able to determine what they wanted to do - raise ArgumentError("Invalid combination of arguments") - - for host in hosts: - print host - return 0 + # We weren't able to determine what they wanted to do + raise ArgumentError("Invalid combination of arguments") def run_pssh(self): """Actually run the pssh command based off of the supplied options @@ -82,7 +64,9 @@ class Opssh(object): if self.args.errdir: pssh_args.extend(["--errdir", self.args.errdir]) - hosts = self.aws.get_host_list(self.args.host_type, self.args.env) + hosts = self.aws.get_host_list(host_type=self.args.host_type, + env=self.args.env) + with tempfile.NamedTemporaryFile(prefix='opssh-', delete=True) as f: for h in hosts: f.write(h + os.linesep) -- cgit v1.2.3 From eb64f8f819139e3901b032a4f2bfa53a3189531e Mon Sep 17 00:00:00 2001 From: Thomas Wiest Date: Mon, 13 Apr 2015 16:42:36 -0400 Subject: Automatic commit of package [openshift-ansible-bin] release [0.0.8-1]. --- bin/openshift-ansible-bin.spec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/openshift-ansible-bin.spec b/bin/openshift-ansible-bin.spec index 04af4546c..c7db6f684 100644 --- a/bin/openshift-ansible-bin.spec +++ b/bin/openshift-ansible-bin.spec @@ -1,6 +1,6 @@ Summary: OpenShift Ansible Scripts for working with metadata hosts Name: openshift-ansible-bin -Version: 0.0.7 +Version: 0.0.8 Release: 1%{?dist} License: ASL 2.0 URL: https://github.com/openshift/openshift-ansible @@ -36,6 +36,9 @@ cp -p openshift_ansible.conf.example %{buildroot}/etc/openshift_ansible/openshif %config(noreplace) /etc/openshift_ansible/ %changelog +* Mon Apr 13 2015 Thomas Wiest 0.0.8-1 +- fixed bug in opssh where it wouldn't actually run pssh (twiest@redhat.com) + * Mon Apr 13 2015 Thomas Wiest 0.0.7-1 - added the ability to run opssh and ohi on all hosts in an environment, as well as all hosts of the same host-type regardless of environment -- cgit v1.2.3