diff options
author | Rodolfo Carvalho <rhcarvalho@gmail.com> | 2017-01-30 18:29:06 +0100 |
---|---|---|
committer | Rodolfo Carvalho <rhcarvalho@gmail.com> | 2017-02-10 14:46:40 +0100 |
commit | bb38413fcec7fb2640939782d57e494b40e3b41e (patch) | |
tree | 89e589859935e059d899a8bc357206c6891901b2 /roles/openshift_health_checker/library/aos_version.py | |
parent | c6ef283bbcd1ab31934fb245d0c8ffacfd05bce1 (diff) | |
download | openshift-bb38413fcec7fb2640939782d57e494b40e3b41e.tar.gz openshift-bb38413fcec7fb2640939782d57e494b40e3b41e.tar.bz2 openshift-bb38413fcec7fb2640939782d57e494b40e3b41e.tar.xz openshift-bb38413fcec7fb2640939782d57e494b40e3b41e.zip |
Replace multi-role checks with action plugin
This approach should make it easier to add new checks without having to
write lots of YAML and doing things against Ansible (e.g.
ignore_errors).
A single action plugin determines what checks to run per each host,
including arguments to the check. A check is implemented as a class with
a run method, with the same signature as an action plugin and module,
and is normally backed by a regular Ansible module.
Each check is implemented as a separate Python file. This allows whoever
adds a new check to focus solely in a single Python module, and
potentially an Ansible module within library/ too.
All checks are automatically loaded, and only active checks that are
requested by the playbook get executed.
Diffstat (limited to 'roles/openshift_health_checker/library/aos_version.py')
-rwxr-xr-x | roles/openshift_health_checker/library/aos_version.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/roles/openshift_health_checker/library/aos_version.py b/roles/openshift_health_checker/library/aos_version.py new file mode 100755 index 000000000..37c8b483c --- /dev/null +++ b/roles/openshift_health_checker/library/aos_version.py @@ -0,0 +1,85 @@ +#!/usr/bin/python +# vim: expandtab:tabstop=4:shiftwidth=4 +''' +Ansible module for determining if multiple versions of an OpenShift package are +available, and if the version requested is available down to the given +precision. + +Multiple versions available suggest that multiple repos are enabled for the +different versions, which may cause installation problems. +''' + +import yum # pylint: disable=import-error + +from ansible.module_utils.basic import AnsibleModule + + +def main(): # pylint: disable=missing-docstring + module = AnsibleModule( + argument_spec=dict( + version=dict(required=True) + ), + supports_check_mode=True + ) + + def bail(error): # pylint: disable=missing-docstring + module.fail_json(msg=error) + + yb = yum.YumBase() # pylint: disable=invalid-name + + # search for package versions available for aos pkgs + expected_pkgs = [ + 'atomic-openshift', + 'atomic-openshift-master', + 'atomic-openshift-node', + ] + try: + pkgs = yb.pkgSack.returnPackages(patterns=expected_pkgs) + except yum.Errors.PackageSackError as e: # pylint: disable=invalid-name + # you only hit this if *none* of the packages are available + bail('Unable to find any atomic-openshift packages. \nCheck your subscription and repo settings. \n%s' % e) + + # determine what level of precision we're expecting for the version + expected_version = module.params['version'] + if expected_version.startswith('v'): # v3.3 => 3.3 + expected_version = expected_version[1:] + num_dots = expected_version.count('.') + + pkgs_by_name_version = {} + pkgs_precise_version_found = {} + for pkg in pkgs: + # get expected version precision + match_version = '.'.join(pkg.version.split('.')[:num_dots + 1]) + if match_version == expected_version: + pkgs_precise_version_found[pkg.name] = True + # get x.y version precision + minor_version = '.'.join(pkg.version.split('.')[:2]) + if pkg.name not in pkgs_by_name_version: + pkgs_by_name_version[pkg.name] = {} + pkgs_by_name_version[pkg.name][minor_version] = True + + # see if any packages couldn't be found at requested version + # see if any packages are available in more than one minor version + not_found = [] + multi_found = [] + for name in expected_pkgs: + if name not in pkgs_precise_version_found: + not_found.append(name) + if name in pkgs_by_name_version and len(pkgs_by_name_version[name]) > 1: + multi_found.append(name) + if not_found: + msg = 'Not all of the required packages are available at requested version %s:\n' % expected_version + for name in not_found: + msg += ' %s\n' % name + bail(msg + 'Please check your subscriptions and enabled repositories.') + if multi_found: + msg = 'Multiple minor versions of these packages are available\n' + for name in multi_found: + msg += ' %s\n' % name + bail(msg + "There should only be one OpenShift version's repository enabled at a time.") + + module.exit_json(changed=False) + + +if __name__ == '__main__': + main() |