From bb38413fcec7fb2640939782d57e494b40e3b41e Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Mon, 30 Jan 2017 18:29:06 +0100 Subject: 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. --- playbooks/byo/openshift-preflight/check.yml | 41 +++++++++-------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'playbooks/byo') diff --git a/playbooks/byo/openshift-preflight/check.yml b/playbooks/byo/openshift-preflight/check.yml index 32673d01d..935531e02 100644 --- a/playbooks/byo/openshift-preflight/check.yml +++ b/playbooks/byo/openshift-preflight/check.yml @@ -1,31 +1,14 @@ --- - hosts: OSEv3 - roles: - - openshift_preflight/init - -- hosts: OSEv3 - name: checks that apply to all hosts - gather_facts: no - ignore_errors: yes - roles: - - openshift_preflight/common - -- hosts: masters - name: checks that apply to masters - gather_facts: no - ignore_errors: yes - roles: - - openshift_preflight/masters - -- hosts: nodes - name: checks that apply to nodes - gather_facts: no - ignore_errors: yes - roles: - - openshift_preflight/nodes - -- hosts: OSEv3 - name: verify check results - gather_facts: no - roles: - - openshift_preflight/verify_status + name: run OpenShift health checks + roles: + - openshift_health_checker + post_tasks: + # NOTE: we need to use the old "action: name" syntax until + # https://github.com/ansible/ansible/issues/20513 is fixed. + - action: openshift_health_check + args: + checks: + - package_availability + - package_update + - package_version -- cgit v1.2.3 From c838e0f0b79b1471c47addf50c46fdb12281812c Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Tue, 31 Jan 2017 18:15:19 +0100 Subject: Introduce tag notation for checks This allows us to refer to a group of checks using a single handle. --- playbooks/byo/openshift-preflight/check.yml | 4 +--- .../action_plugins/openshift_health_check.py | 23 +++++++++++++++++++++- .../openshift_checks/__init__.py | 9 +++++++++ .../openshift_checks/package_availability.py | 1 + .../openshift_checks/package_update.py | 1 + .../openshift_checks/package_version.py | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) (limited to 'playbooks/byo') diff --git a/playbooks/byo/openshift-preflight/check.yml b/playbooks/byo/openshift-preflight/check.yml index 935531e02..c5f05d0f0 100644 --- a/playbooks/byo/openshift-preflight/check.yml +++ b/playbooks/byo/openshift-preflight/check.yml @@ -9,6 +9,4 @@ - action: openshift_health_check args: checks: - - package_availability - - package_update - - package_version + - '@preflight' diff --git a/roles/openshift_health_checker/action_plugins/openshift_health_check.py b/roles/openshift_health_checker/action_plugins/openshift_health_check.py index 36defde0a..0411797b1 100644 --- a/roles/openshift_health_checker/action_plugins/openshift_health_check.py +++ b/roles/openshift_health_checker/action_plugins/openshift_health_check.py @@ -41,7 +41,7 @@ class ActionModule(ActionBase): return result args = self._task.args - requested_checks = set(args.get("checks", [])) + requested_checks = resolve_checks(args.get("checks", []), known_checks.values()) unknown_checks = requested_checks - set(known_checks) if unknown_checks: @@ -93,3 +93,24 @@ class ActionModule(ActionBase): known_checks[check_name] = cls(module_executor=self._execute_module) return known_checks + + +def resolve_checks(names, all_checks): + """Returns a set of resolved check names. + + Resolving a check name involves expanding tag references (e.g., '@tag') with + all the checks that contain the given tag. + + names should be a sequence of strings. + + all_checks should be a sequence of check classes/instances. + """ + resolved = set() + for name in names: + if name.startswith("@"): + for check in all_checks: + if name[1:] in check.tags: + resolved.add(check.name) + else: + resolved.add(name) + return resolved diff --git a/roles/openshift_health_checker/openshift_checks/__init__.py b/roles/openshift_health_checker/openshift_checks/__init__.py index d893ba591..ff99e7b4c 100644 --- a/roles/openshift_health_checker/openshift_checks/__init__.py +++ b/roles/openshift_health_checker/openshift_checks/__init__.py @@ -26,6 +26,15 @@ class OpenShiftCheck(object): """The name of this check, usually derived from the class name.""" return "openshift_check" + @property + def tags(self): + """A list of tags that this check satisfy. + + Tags are used to reference multiple checks with a single '@tagname' + special check name. + """ + return [] + @classmethod def is_active(cls, task_vars): # pylint: disable=unused-argument """Returns true if this check applies to the ansible-playbook run.""" diff --git a/roles/openshift_health_checker/openshift_checks/package_availability.py b/roles/openshift_health_checker/openshift_checks/package_availability.py index 4260cbf7c..31277a3b9 100644 --- a/roles/openshift_health_checker/openshift_checks/package_availability.py +++ b/roles/openshift_health_checker/openshift_checks/package_availability.py @@ -7,6 +7,7 @@ class PackageAvailability(NotContainerized, OpenShiftCheck): """Check that required RPM packages are available.""" name = "package_availability" + tags = ["preflight"] def run(self, tmp, task_vars): try: diff --git a/roles/openshift_health_checker/openshift_checks/package_update.py b/roles/openshift_health_checker/openshift_checks/package_update.py index 316a776f5..86b7b6245 100644 --- a/roles/openshift_health_checker/openshift_checks/package_update.py +++ b/roles/openshift_health_checker/openshift_checks/package_update.py @@ -7,6 +7,7 @@ class PackageUpdate(NotContainerized, OpenShiftCheck): """Check that there are no conflicts in RPM packages.""" name = "package_update" + tags = ["preflight"] def run(self, tmp, task_vars): args = {"packages": []} diff --git a/roles/openshift_health_checker/openshift_checks/package_version.py b/roles/openshift_health_checker/openshift_checks/package_version.py index a473119f3..9394466f2 100644 --- a/roles/openshift_health_checker/openshift_checks/package_version.py +++ b/roles/openshift_health_checker/openshift_checks/package_version.py @@ -7,6 +7,7 @@ class PackageVersion(NotContainerized, OpenShiftCheck): """Check that available RPM packages match the required versions.""" name = "package_version" + tags = ["preflight"] @classmethod def is_active(cls, task_vars): -- cgit v1.2.3