From d20c65ed4847c3eb74a4d8bd136ee364bfb48214 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Tue, 17 Dec 2019 15:37:04 +0000
Subject: [PATCH] Check supported host OS distributions in prechecks

This should help to ensure that users are running tested and supported
host OS distributions.

Change-Id: I6ee76463d284ad4f3646af1c7ec2b7e50e2f3b15
Partially-Implements: blueprint improve-prechecks
---
 ansible/roles/prechecks/defaults/main.yml      |  3 +++
 .../roles/prechecks/tasks/host_os_checks.yml   | 18 ++++++++++++++++++
 ansible/roles/prechecks/tasks/main.yml         |  3 +++
 ansible/roles/prechecks/vars/main.yml          | 14 ++++++++++++++
 .../host-os-precheck-e544f27f9012c9e2.yaml     |  7 +++++++
 5 files changed, 45 insertions(+)
 create mode 100644 ansible/roles/prechecks/defaults/main.yml
 create mode 100644 ansible/roles/prechecks/tasks/host_os_checks.yml
 create mode 100644 releasenotes/notes/host-os-precheck-e544f27f9012c9e2.yaml

diff --git a/ansible/roles/prechecks/defaults/main.yml b/ansible/roles/prechecks/defaults/main.yml
new file mode 100644
index 0000000000..6361801c83
--- /dev/null
+++ b/ansible/roles/prechecks/defaults/main.yml
@@ -0,0 +1,3 @@
+---
+# Whether to enable checks for host OS distribution and release.
+prechecks_enable_host_os_checks: true
diff --git a/ansible/roles/prechecks/tasks/host_os_checks.yml b/ansible/roles/prechecks/tasks/host_os_checks.yml
new file mode 100644
index 0000000000..0538419215
--- /dev/null
+++ b/ansible/roles/prechecks/tasks/host_os_checks.yml
@@ -0,0 +1,18 @@
+---
+- name: Checking host OS distribution
+  fail:
+    msg: >-
+      Host OS distribution {{ ansible_distribution }} is not supported.
+      Supported distributions are: {{ host_os_distributions.keys() | join(', ') }}
+  when: ansible_distribution not in host_os_distributions
+
+- name: Checking host OS release or version
+  fail:
+    msg: >-
+      {{ ansible_distribution }} release {{ ansible_distribution_release }} is
+      not supported. Supported releases are:
+      {{ host_os_distributions[ansible_distribution] | join(', ') }}
+  when:
+    - ansible_distribution_release not in host_os_distributions[ansible_distribution]
+    - ansible_distribution_version not in host_os_distributions[ansible_distribution]
+    - ansible_distribution_major_version not in host_os_distributions[ansible_distribution]
diff --git a/ansible/roles/prechecks/tasks/main.yml b/ansible/roles/prechecks/tasks/main.yml
index 2f0f8684b1..b0d1d99d2e 100644
--- a/ansible/roles/prechecks/tasks/main.yml
+++ b/ansible/roles/prechecks/tasks/main.yml
@@ -1,4 +1,7 @@
 ---
+- include_tasks: host_os_checks.yml
+  when: prechecks_enable_host_os_checks | bool
+
 - include_tasks: port_checks.yml
   when:
     - inventory_hostname not in groups['deployment']|default([])
diff --git a/ansible/roles/prechecks/vars/main.yml b/ansible/roles/prechecks/vars/main.yml
index 4f1616589e..5fbffcb478 100644
--- a/ansible/roles/prechecks/vars/main.yml
+++ b/ansible/roles/prechecks/vars/main.yml
@@ -3,3 +3,17 @@ docker_version_min: '1.10.0'
 docker_py_version_min: '2.0.0'
 ansible_version_min: '2.8'
 ansible_version_max: '2.9'
+
+# Top level keys should match ansible_distribution.
+# These map to lists of supported releases (ansible_distribution_release) or
+# versions (ansible_distribution_version or ansible_distribution_major_version)
+# for that distribution.
+host_os_distributions:
+  CentOS:
+    - "8"
+  Debian:
+    - "buster"
+  RHEL:
+    - "8"
+  Ubuntu:
+    - "bionic"
diff --git a/releasenotes/notes/host-os-precheck-e544f27f9012c9e2.yaml b/releasenotes/notes/host-os-precheck-e544f27f9012c9e2.yaml
new file mode 100644
index 0000000000..27cf1bf2a1
--- /dev/null
+++ b/releasenotes/notes/host-os-precheck-e544f27f9012c9e2.yaml
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    Adds a new precheck for supported host OS distributions. Currently
+    supported distributions are CentOS/RHEL 8, Debian Buster and Ubuntu Bionic.
+    This check can be disabled by setting ``prechecks_enable_host_os_checks``
+    to ``false``.