summaryrefslogtreecommitdiffstats
path: root/bin/openshift_ansible/awsutil.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/openshift_ansible/awsutil.py')
-rw-r--r--bin/openshift_ansible/awsutil.py85
1 files changed, 57 insertions, 28 deletions
diff --git a/bin/openshift_ansible/awsutil.py b/bin/openshift_ansible/awsutil.py
index 76b4f4f51..e03c0ab15 100644
--- a/bin/openshift_ansible/awsutil.py
+++ b/bin/openshift_ansible/awsutil.py
@@ -59,9 +59,23 @@ class AwsUtil(object):
minv.run()
return minv.result
+ def get_clusters(self):
+ """Searches for cluster tags in the inventory and returns all of the clusters found."""
+ pattern = re.compile(r'^oo_clusterid_(.*)')
+
+ clusters = []
+ inv = self.get_inventory()
+ for key in inv.keys():
+ matched = pattern.match(key)
+ if matched:
+ clusters.append(matched.group(1))
+
+ clusters.sort()
+ return clusters
+
def get_environments(self):
"""Searches for env tags in the inventory and returns all of the envs found."""
- pattern = re.compile(r'^tag_environment_(.*)')
+ pattern = re.compile(r'^oo_environment_(.*)')
envs = []
inv = self.get_inventory()
@@ -75,7 +89,7 @@ class AwsUtil(object):
def get_host_types(self):
"""Searches for host-type tags in the inventory and returns all host-types found."""
- pattern = re.compile(r'^tag_host-type_(.*)')
+ pattern = re.compile(r'^oo_host-type_(.*)')
host_types = []
inv = self.get_inventory()
@@ -154,6 +168,18 @@ class AwsUtil(object):
return host_type
@staticmethod
+ def gen_version_tag(ver):
+ """Generate the version tag
+ """
+ return "oo_version_%s" % ver
+
+ @staticmethod
+ def gen_clusterid_tag(clu):
+ """Generate the clusterid tag
+ """
+ return "tag_clusterid_%s" % clu
+
+ @staticmethod
def gen_env_tag(env):
"""Generate the environment tag
"""
@@ -165,41 +191,44 @@ class AwsUtil(object):
host_type = self.resolve_host_type(host_type)
return "tag_host-type_%s" % host_type
- def get_host_list(self, host_type=None, envs=None, version=None, cached=False):
+ # This function uses all of these params to perform a filters on our host inventory.
+ # pylint: disable=too-many-arguments
+ def get_host_list(self, clusters=None, host_type=None, envs=None, version=None, cached=False):
"""Get the list of hosts from the inventory using host-type and environment
"""
retval = set([])
envs = envs or []
+
inv = self.get_inventory(cached=cached)
- # We prefer to deal with a list of environments
- if issubclass(type(envs), basestring):
- if envs == 'all':
- envs = self.get_environments()
+ retval.update(inv.get('all_hosts', []))
+
+ if clusters:
+ cluster_hosts = set([])
+ if len(clusters) > 1:
+ for cluster in clusters:
+ clu_tag = AwsUtil.gen_clusterid_tag(cluster)
+ cluster_hosts.update(inv.get(clu_tag, []))
else:
- envs = [envs]
+ cluster_hosts.update(inv.get(AwsUtil.gen_clusterid_tag(clusters[0]), []))
+
+ retval.intersection_update(cluster_hosts)
+
+ if envs:
+ env_hosts = set([])
+ if len(envs) > 1:
+ for env in envs:
+ env_tag = AwsUtil.gen_env_tag(env)
+ env_hosts.update(inv.get(env_tag, []))
+ else:
+ env_hosts.update(inv.get(AwsUtil.gen_env_tag(envs[0]), []))
+
+ retval.intersection_update(env_hosts)
- if host_type and envs:
- # Both host type and environment were specified
- for env in envs:
- retval.update(inv.get('tag_environment_%s' % env, []))
+ if host_type:
retval.intersection_update(inv.get(self.gen_host_type_tag(host_type), []))
- elif envs and not host_type:
- # Just environment was specified
- for env in envs:
- env_tag = AwsUtil.gen_env_tag(env)
- if env_tag in inv.keys():
- retval.update(inv.get(env_tag, []))
-
- elif host_type and not envs:
- # Just host-type was specified
- host_type_tag = self.gen_host_type_tag(host_type)
- if host_type_tag in inv.keys():
- retval.update(inv.get(host_type_tag, []))
-
- # If version is specified then return only hosts in that version
- if version:
- retval.intersection_update(inv.get('oo_version_%s' % version, []))
+ if version != 'all':
+ retval.intersection_update(inv.get(AwsUtil.gen_version_tag(version), []))
return retval