Adding states to disable nova-compute service

Change-Id: Id6042a5c18b6867fc90e5005e1edeb2eb3270f8e
Related-PROD: PROD-23088 (PROD:23088)
diff --git a/_modules/novav21/__init__.py b/_modules/novav21/__init__.py
index 8ea591c..bb567c0 100644
--- a/_modules/novav21/__init__.py
+++ b/_modules/novav21/__init__.py
@@ -23,6 +23,7 @@
 import keypairs
 import quotas
 import servers
+import services
 
 aggregate_add_host = aggregates.add_host
 aggregate_create = aggregates.create
@@ -53,6 +54,9 @@
 server_resume = servers.resume
 server_suspend = servers.suspend
 server_unlock = servers.unlock
+services_list = services.list_
+services_update = services.update
+services_wait = services.wait_for_services
 
 
 __all__ = (
@@ -64,7 +68,8 @@
     'keypair_delete', 'keypair_get', 'keypair_list', 'quota_delete',
     'quota_list', 'quota_update', 'server_create', 'server_delete',
     'server_get', 'server_list', 'server_lock', 'server_resume',
-    'server_suspend', 'server_unlock')
+    'server_suspend', 'server_unlock', 'services_list', 'services_update',
+    'services_wait')
 
 
 def __virtual__():
diff --git a/_modules/novav21/services.py b/_modules/novav21/services.py
new file mode 100644
index 0000000..f5093a1
--- /dev/null
+++ b/_modules/novav21/services.py
@@ -0,0 +1,72 @@
+#    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 common
+import six.moves.urllib.parse as urllib_parse
+import time
+from salt.exceptions import CommandExecutionError
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+    'list_': 'list'
+}
+
+
+@common.function_descriptor('find', 'Compute services', 'services')
+@common.send('get')
+def list_(*args, **kwargs):
+    """Return list of nova services."""
+    url = '/os-services?{}'.format(urllib_parse.urlencode(kwargs))
+    return url, {}
+
+
+@common.function_descriptor('update', 'Compute service', 'service')
+@common.send('put')
+def update(host, service, action, **kwargs):
+    """Enable/Disable nova service"""
+    if kwargs.get('disabled_reason') and action == 'disable':
+      url = '/os-services/disable-log-reason'
+      req = {"host": host, "binary": service, "disabled_reason": kwargs['disabled_reason']}
+    else:
+      url = '/os-services/%s' % action
+      req = {"host": host, "binary": service}
+    return url, {"json": req}
+
+
+def wait_for_services(cloud_name, host=None, service=None, admin_up_only=True, retries=18, timeout=10, **kwargs):
+    """Ensure the service is up and running on specified host.
+
+    :param host:           name of a host where service is running
+    :param admin_up_only:  do not check status for admin disabled service
+    :param service:        name of the service (by default nova-compute)
+    :param timeout:        number of seconds to wait before retries
+    :param retries:        number of retries
+    """
+    kwargs = {}
+    if host is not None:
+        kwargs['host'] = host
+    if service is not None:
+        kwargs['service'] = service
+
+    for i in range(retries):
+        services = list_(cloud_name=cloud_name, **kwargs)['body'].get('services')
+
+        if admin_up_only:
+            down_services = [s for s in services if (not service or s['binary'] == service) and s['status'] == 'enabled' and s['state'] == 'down']
+        else:
+            down_services = [s for s in services if (not service or s['binary'] == service) and s['state'] == 'down']
+
+        if len(down_services) == 0:
+            return 'Compute services with admin_up_only=%s are up or disabled administratively' % (admin_up_only)
+        time.sleep(timeout)
+
+    raise CommandExecutionError("Compute services {} are still down or disabled".format(down_services))
diff --git a/_states/novav21.py b/_states/novav21.py
index 1feefee..27e7f43 100644
--- a/_states/novav21.py
+++ b/_states/novav21.py
@@ -13,8 +13,9 @@
 import logging
 import six
 from six.moves import zip_longest
-
+import time
 import salt
+from salt.exceptions import CommandExecutionError
 
 LOG = logging.getLogger(__name__)
 
@@ -434,6 +435,56 @@
     return ret
 
 
+@_error_handler
+def service_enabled(name, cloud_name, binary="nova-compute"):
+    """Ensures that the service is enabled on the host
+
+    :param name:    name of a host where service is running
+    :param service: name of the service have to be run
+    """
+    changes = {}
+
+    services = _call_nova_salt_module('services_list', name)(
+        name, service=binary, cloud_name=cloud_name)
+    enabled_service = [s for s in services if s['binary'] == binary
+                       and s['status'] == 'enabled' and s['host'] == name]
+    if len(enabled_service) > 0:
+        ret = _no_change(name, 'Compute services')
+    else:
+        changes = _call_nova_salt_module('services_update', name)(
+            name, binary, 'enable', cloud_name=cloud_name)
+        ret = _updated(name, 'Compute services', changes)
+
+    return ret
+
+@_error_handler
+def service_disabled(name, cloud_name, binary="nova-compute", disabled_reason=None):
+    """Ensures that the service is disabled on the host
+
+    :param name:    name of a host where service is running
+    :param service: name of the service have to be disabled
+    """
+
+    changes = {}
+    kwargs = {}
+
+    if disabled_reason is not None:
+        kwargs['disabled_reason'] = disabled_reason
+
+    services = _call_nova_salt_module('services_list', name)(
+        name, service=binary, cloud_name=cloud_name)
+    disabled_service = [s for s in services if s['binary'] == binary
+                       and s['status'] == 'disabled' and s['host'] == name]
+    if len(disabled_service) > 0:
+        ret = _no_change(name, 'Compute services')
+    else:
+        changes = _call_nova_salt_module('services_update', name)(
+            name, binary, 'disable', cloud_name=cloud_name, **kwargs)
+        ret = _updated(name, 'Compute services', changes)
+
+    return ret
+
+
 def _find_failed(name, resource):
     return {
         'name': name, 'changes': {}, 'result': False,
diff --git a/nova/upgrade/upgrade/post.sls b/nova/upgrade/upgrade/post.sls
index 92dc005..e532c1d 100644
--- a/nova/upgrade/upgrade/post.sls
+++ b/nova/upgrade/upgrade/post.sls
@@ -5,3 +5,13 @@
     - name: "dump_message_upgrade_nova_post"
     - text: "Running nova.upgrade.upgrade.post"
 
+{%- if compute.get('enabled') %}
+{% set host_id = salt['network.get_hostname']() %}
+
+novav21_service_enabled:
+  novav21.service_enabled:
+  - binary: nova-compute
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+
+{% endif %}
diff --git a/nova/upgrade/upgrade/pre.sls b/nova/upgrade/upgrade/pre.sls
index 31be4d5..e9b48ea 100644
--- a/nova/upgrade/upgrade/pre.sls
+++ b/nova/upgrade/upgrade/pre.sls
@@ -5,3 +5,14 @@
     - name: "dump_message_upgrade_nova_pre"
     - text: "Running nova.upgrade.upgrade.pre"
 
+{%- if compute.get('enabled') %}
+{% set host_id = salt['network.get_hostname']() %}
+
+novav21_service_disabled:
+  novav21.service_disabled:
+  - binary: nova-compute
+  - disabled_reason: Disabled for upgrade
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+
+{% endif %}
diff --git a/nova/upgrade/verify/_service.sls b/nova/upgrade/verify/_service.sls
new file mode 100644
index 0000000..97d016b
--- /dev/null
+++ b/nova/upgrade/verify/_service.sls
@@ -0,0 +1,21 @@
+{%- from "nova/map.jinja" import controller, compute, compute_driver_mapping with context %}
+
+nova_task_uprade_verify_service:
+  test.show_notification:
+    - text: "Running nova.upgrade.verify.service"
+
+{%- if compute.get('enabled') %}
+{% set host_id = salt['network.get_hostname']() %}
+{% endif %}
+
+wait_for_service:
+  module.run:
+    - name: novav21.services_wait
+    - cloud_name: admin_identity
+    - admin_up_only: False
+{%- if host_id is defined %}
+    - host: {{ host_id }}
+{%- endif %}
+    - retries: 20
+    - timeout: 10
+
diff --git a/nova/upgrade/verify/init.sls b/nova/upgrade/verify/init.sls
index a377c18..0cbfec4 100644
--- a/nova/upgrade/verify/init.sls
+++ b/nova/upgrade/verify/init.sls
@@ -5,3 +5,4 @@
 
 include:
  - nova.upgrade.verify._api
+ - nova.upgrade.verify._service