From 37fc134d1102adaae2d7097c47669e1f8d52afb9 Mon Sep 17 00:00:00 2001
From: whoami-rajat <rajatdhasmana@gmail.com>
Date: Tue, 23 Oct 2018 23:33:43 +0530
Subject: [PATCH] Add zaqar-status upgrade check command framework

This adds basic framework for zaqar-status upgrade
check commands. For now it has only "check_placeholder"
check implemented.
Real checks can be added to this tool in the future.

Change-Id: I855b4b72284df308c3bb250fd3a3f0fc643b4abf
Story: 2003657
Task: 26165
---
 doc/source/cli/index.rst                      |  7 ++
 doc/source/cli/zaqar-status.rst               | 78 +++++++++++++++++++
 doc/source/index.rst                          |  1 +
 lower-constraints.txt                         |  1 +
 ...rade-check-framework-09caa1f741f6119d.yaml | 13 ++++
 requirements.txt                              |  1 +
 setup.cfg                                     |  1 +
 zaqar/cmd/status.py                           | 54 +++++++++++++
 zaqar/tests/unit/cmd/__init__.py              |  0
 zaqar/tests/unit/cmd/test_status.py           | 30 +++++++
 10 files changed, 186 insertions(+)
 create mode 100644 doc/source/cli/index.rst
 create mode 100644 doc/source/cli/zaqar-status.rst
 create mode 100644 releasenotes/notes/zaqar-status-upgrade-check-framework-09caa1f741f6119d.yaml
 create mode 100644 zaqar/cmd/status.py
 create mode 100644 zaqar/tests/unit/cmd/__init__.py
 create mode 100644 zaqar/tests/unit/cmd/test_status.py

diff --git a/doc/source/cli/index.rst b/doc/source/cli/index.rst
new file mode 100644
index 000000000..4ddec0e5e
--- /dev/null
+++ b/doc/source/cli/index.rst
@@ -0,0 +1,7 @@
+CLI Reference
+=============
+
+.. toctree::
+   :maxdepth: 1
+
+   zaqar-status
diff --git a/doc/source/cli/zaqar-status.rst b/doc/source/cli/zaqar-status.rst
new file mode 100644
index 000000000..65d4cac33
--- /dev/null
+++ b/doc/source/cli/zaqar-status.rst
@@ -0,0 +1,78 @@
+============
+zaqar-status
+============
+
+Synopsis
+========
+
+::
+
+  zaqar-status <category> <command> [<args>]
+
+Description
+===========
+
+:program:`zaqar-status` is a tool that provides routines for checking the
+status of a Zaqar deployment.
+
+Options
+=======
+
+The standard pattern for executing a :program:`zaqar-status` command is::
+
+    zaqar-status <category> <command> [<args>]
+
+Run without arguments to see a list of available command categories::
+
+    zaqar-status
+
+Categories are:
+
+* ``upgrade``
+
+Detailed descriptions are below.
+
+You can also run with a category argument such as ``upgrade`` to see a list of
+all commands in that category::
+
+    zaqar-status upgrade
+
+These sections describe the available categories and arguments for
+:program:`zaqar-status`.
+
+Upgrade
+~~~~~~~
+
+.. _zaqar-status-checks:
+
+``zaqar-status upgrade check``
+  Performs a release-specific readiness check before restarting services with
+  new code. This command expects to have complete configuration and access
+  to databases and services.
+
+  **Return Codes**
+
+  .. list-table::
+     :widths: 20 80
+     :header-rows: 1
+
+     * - Return code
+       - Description
+     * - 0
+       - All upgrade readiness checks passed successfully and there is nothing
+         to do.
+     * - 1
+       - At least one check encountered an issue and requires further
+         investigation. This is considered a warning but the upgrade may be OK.
+     * - 2
+       - There was an upgrade status check failure that needs to be
+         investigated. This should be considered something that stops an
+         upgrade.
+     * - 255
+       - An unexpected error occurred.
+
+  **History of Checks**
+
+  **8.0.0 (Stein)**
+
+  * Placeholder to be filled in with checks as they are added in Stein.
diff --git a/doc/source/index.rst b/doc/source/index.rst
index fdab15bc4..2e21c667e 100644
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -94,6 +94,7 @@ Contents
    install/index
    configuration/index
    contributor/index
+   cli/index
 
 .. toctree::
    :maxdepth: 1
diff --git a/lower-constraints.txt b/lower-constraints.txt
index 5672547ec..b9f29a013 100644
--- a/lower-constraints.txt
+++ b/lower-constraints.txt
@@ -34,6 +34,7 @@ oslo.policy==1.30.0
 oslo.reports==1.18.0
 oslo.serialization==2.18.0
 oslo.service==1.24.0
+oslo.upgradecheck==0.1.0
 oslo.utils==3.33.0
 oslotest==3.2.0
 osprofiler==1.4.0
diff --git a/releasenotes/notes/zaqar-status-upgrade-check-framework-09caa1f741f6119d.yaml b/releasenotes/notes/zaqar-status-upgrade-check-framework-09caa1f741f6119d.yaml
new file mode 100644
index 000000000..aef62736a
--- /dev/null
+++ b/releasenotes/notes/zaqar-status-upgrade-check-framework-09caa1f741f6119d.yaml
@@ -0,0 +1,13 @@
+---
+prelude: >
+    Added new tool ``zaqar-status upgrade check``.
+features:
+  - |
+    New framework for ``zaqar-status upgrade check`` command is added.
+    This framework allows adding various checks which can be run before a
+    Zaqar upgrade to ensure if the upgrade can be performed safely.
+upgrade:
+  - |
+    Operator can now use new CLI tool ``zaqar-status upgrade check``
+    to check if Zaqar deployment can be safely upgraded from
+    N-1 to N release.
diff --git a/requirements.txt b/requirements.txt
index dea3f8b27..98e0f8430 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -23,6 +23,7 @@ oslo.log>=3.36.0 # Apache-2.0
 oslo.messaging>=5.29.0 # Apache-2.0
 oslo.reports>=1.18.0 # Apache-2.0
 oslo.serialization!=2.19.1,>=2.18.0 # Apache-2.0
+oslo.upgradecheck>=0.1.0 # Apache-2.0
 oslo.utils>=3.33.0 # Apache-2.0
 oslo.policy>=1.30.0 # Apache-2.0
 osprofiler>=1.4.0 # Apache-2.0
diff --git a/setup.cfg b/setup.cfg
index 59a9f496e..261d41312 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -27,6 +27,7 @@ console_scripts =
     zaqar-server = zaqar.cmd.server:run
     zaqar-gc = zaqar.cmd.gc:run
     zaqar-sql-db-manage = zaqar.storage.sqlalchemy.migration.cli:main
+    zaqar-status = zaqar.cmd.status:main
 
 zaqar.data.storage =
     mongodb = zaqar.storage.mongodb.driver:DataDriver
diff --git a/zaqar/cmd/status.py b/zaqar/cmd/status.py
new file mode 100644
index 000000000..c1551e278
--- /dev/null
+++ b/zaqar/cmd/status.py
@@ -0,0 +1,54 @@
+# Copyright (c) 2018 NEC, Corp.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import sys
+
+from oslo_config import cfg
+from oslo_upgradecheck import upgradecheck
+
+from zaqar.i18n import _
+
+
+class Checks(upgradecheck.UpgradeCommands):
+
+    """Upgrade checks for the zaqar-status upgrade check command
+
+    Upgrade checks should be added as separate methods in this class
+    and added to _upgrade_checks tuple.
+    """
+
+    def _check_placeholder(self):
+        # This is just a placeholder for upgrade checks, it should be
+        # removed when the actual checks are added
+        return upgradecheck.Result(upgradecheck.Code.SUCCESS)
+
+    # The format of the check functions is to return an
+    # oslo_upgradecheck.upgradecheck.Result
+    # object with the appropriate
+    # oslo_upgradecheck.upgradecheck.Code and details set.
+    # If the check hits warnings or failures then those should be stored
+    # in the returned Result's "details" attribute. The
+    # summary will be rolled up at the end of the check() method.
+    _upgrade_checks = (
+        # In the future there should be some real checks added here
+        (_('Placeholder'), _check_placeholder),
+    )
+
+
+def main():
+    return upgradecheck.main(
+        cfg.CONF, project='zaqar', upgrade_command=Checks())
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/zaqar/tests/unit/cmd/__init__.py b/zaqar/tests/unit/cmd/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/zaqar/tests/unit/cmd/test_status.py b/zaqar/tests/unit/cmd/test_status.py
new file mode 100644
index 000000000..fa8606f13
--- /dev/null
+++ b/zaqar/tests/unit/cmd/test_status.py
@@ -0,0 +1,30 @@
+# Copyright (c) 2018 NEC, Corp.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from oslo_upgradecheck.upgradecheck import Code
+
+from zaqar.cmd import status
+from zaqar.tests import base
+
+
+class TestUpgradeChecks(base.TestBase):
+
+    def setUp(self):
+        super(TestUpgradeChecks, self).setUp()
+        self.cmd = status.Checks()
+
+    def test__check_placeholder(self):
+        check_result = self.cmd._check_placeholder()
+        self.assertEqual(
+            Code.SUCCESS, check_result.code)