Improve service validation during upgrade Manila

  * Added modules which allows to check and manage services statuses.
  * Added states which allows enable/disable one/all services by host name.

Change-Id: I078d089b221180ff2355087bc0601f904f8dbd94
Related-PROD: PROD-25158
diff --git a/_modules/manilang/__init__.py b/_modules/manilang/__init__.py
index 4d0b401..d8b7f90 100644
--- a/_modules/manilang/__init__.py
+++ b/_modules/manilang/__init__.py
@@ -21,18 +21,21 @@
     REQUIREMENTS_MET = False
 
 from manilang import share_types
+from manilang import services
 
 list_share_types = share_types.list_share_types
 create_share_type = share_types.create_share_type
 set_share_type_extra_specs = share_types.set_share_type_extra_specs
 unset_share_type_extra_specs = share_types.unset_share_type_extra_specs
 delete_share_type = share_types.delete_share_type
-
+service_list = services.service_list
+service_update = services.service_update
+service_wait = services.wait_for_service
 
 __all__ = (
     'list_share_types', 'create_share_type',
     'set_share_type_extra_specs', 'unset_share_type_extra_specs',
-    'delete_share_type',
+    'delete_share_type','service_list', 'service_update', 'service_wait',
 )
 
 
diff --git a/_modules/manilang/services.py b/_modules/manilang/services.py
new file mode 100644
index 0000000..ded8e95
--- /dev/null
+++ b/_modules/manilang/services.py
@@ -0,0 +1,70 @@
+#    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 six.moves.urllib.parse as urllib_parse
+import time
+
+from salt.exceptions import CommandExecutionError
+from manilang.common import send, MANILA_HEADER
+
+@send('get', MANILA_HEADER)
+def service_list(*args, **kwargs):
+    """Return list of Manila services."""
+
+    # TODO For API versions 2.6 and prior, replace services in the URLs with os-services.
+    # https://developer.openstack.org/api-ref/shared-file-system/#list-services
+    url = '/os-services?{}'.format(urllib_parse.urlencode(kwargs))
+    return url, {}
+
+
+@send('put', MANILA_HEADER)
+def service_update(host, binary, action, **kwargs):
+    """Enable/Disable Manila service"""
+
+    url = '/services/{}'.format(action)
+    req = {"host": host, "binary": binary}
+
+    return url, req
+
+def wait_for_service(cloud_name, host=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 timeout:        number of seconds to wait before retries
+    :param retries:        number of retries
+    """
+    if admin_up_only:
+        kwargs['status'] = 'enabled'
+
+    kwargs['state'] = 'down'
+
+    for _i in range(retries):
+
+        services = service_list(cloud_name=cloud_name, **kwargs)['services']
+
+        # You are able to wait status either certain manila-share instance by host@driver
+        # or all existed instances on a node by host.
+        # Also you are able to wait status of all instances from every node if you don't define host.
+        if host:
+           if '@' in str(host):
+               down_services  = [s for s in services if host == s['host']]
+           else:
+               down_services = [s for s in services if host == s['host'].split('@')[0]]
+        else:
+            down_services = [s for s in services]
+
+        if len(down_services) == 0:
+            return 'Manila services with admin_up_only={} are up or disabled administratively'.format(admin_up_only)
+        time.sleep(timeout)
+
+    raise CommandExecutionError("Manila services {} are still down or disabled".format(down_services))
diff --git a/_states/example.sls b/_states/example.sls
index 55afcba..66da3a0 100644
--- a/_states/example.sls
+++ b/_states/example.sls
@@ -11,3 +11,17 @@
     manilang.share_type_absent:
       - microversion: '2.4'
       - cloud_name: admin
+
+manilang_service_disabled:
+  manilang.service_disabled:
+  - binary: manila-share
+  - cloud_name: admin_identity
+  - microversion: '2.40'
+  - name: cmp2
+
+manilang_service_enabled:
+  manilang.service_enabled:
+  - binary: manila-share
+  - cloud_name: admin_identity
+  - microversion: '2.40'
+  - name: cmp2
diff --git a/_states/manilang.py b/_states/manilang.py
index 964518a..97609c1 100644
--- a/_states/manilang.py
+++ b/_states/manilang.py
@@ -30,6 +30,8 @@
     'set_type_specs': 'manilang.set_share_type_extra_specs',
     'unset_type_specs': 'manilang.unset_share_type_extra_specs',
     'delete_type': 'manilang.delete_share_type',
+    'service_list': 'manilang.service_list',
+    'service_update': 'manilang.service_update',
 }
 
 
@@ -151,6 +153,64 @@
         return _find_failed(name, 'share_type')
 
 
+def service_enabled(name, binary, cloud_name, microversion=None):
+    """
+    Method allows enable service by binary and name.
+
+    :param name:    name of a host where service is running
+    :param binary:  name of the service have to be run
+    """
+
+    ret = {}
+
+    services = __salt__[manilang_func['service_list']](binary=binary, cloud_name=cloud_name)['services']
+
+    # You are able to either enable certain manila-share instance by name@driver
+    # or all existed instances on a node by name
+    if '@' in name:
+        disabled_services = [s for s in services if name == s['host'] and s['status'] == 'disabled']
+    else:
+        disabled_services = [s for s in services if name == s['host'].split('@')[0] and s['status'] == 'disabled']
+
+    if len(disabled_services):
+        for service in disabled_services:
+
+            changes = __salt__[manilang_func['service_update']](service['host'], binary, 'enable', cloud_name=cloud_name, microversion=microversion)
+            ret[service['host']] = {'disabled': changes['disabled']}
+
+        return _updated(name, binary, ret)
+    return _no_changes(name, binary)
+
+
+def service_disabled(name, binary, cloud_name, microversion=None):
+    """
+    Method allows disable service by binary and name.
+
+    :param name:    name of a host where service is running
+    :param binary:  name of the service have to be disabled
+    """
+
+    ret = {}
+
+    services = __salt__[manilang_func['service_list']](binary=binary, cloud_name=cloud_name)['services']
+
+    # You are able to either disable certain manila-share instance by name@driver
+    # or all existed instances on a node by name
+    if '@' in name:
+        enabled_services = [s for s in services if name == s['host'] and s['status'] == 'enabled']
+    else:
+        enabled_services = [s for s in services if name == s['host'].split('@')[0] and s['status'] == 'enabled']
+
+    if len(enabled_services):
+        for service in enabled_services:
+
+            changes = __salt__[manilang_func['service_update']](service['host'], binary, 'disable', cloud_name=cloud_name, microversion=microversion)
+            ret[service['host']] = {'disabled': changes['disabled']}
+
+        return _updated(name, binary, ret)
+    return _no_changes(name, binary)
+
+
 def _created(name, resource, resource_definition):
     changes_dict = {
         'name': name,
diff --git a/manila/upgrade/upgrade/post.sls b/manila/upgrade/upgrade/post.sls
index 3a1c664..83e9fd7 100644
--- a/manila/upgrade/upgrade/post.sls
+++ b/manila/upgrade/upgrade/post.sls
@@ -1,3 +1,30 @@
 manila_upgrade_uprade_post:
   test.show_notification:
+    - name: "dump_message_upgrade_manila_post"
     - text: "Running manila.upgrade.upgrade.post"
+
+{% set host_id = salt['network.get_hostname']() %}
+
+{%- if pillar.manila.scheduler is defined %}
+
+# It turns on every manila-scheduler instances on the host.
+manila_controller_service_enabled:
+  manilang.service_enabled:
+  - binary: manila-scheduler
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+  - microversion: '2.40'
+
+{%- endif %}
+
+{%- if pillar.manila.share is defined %}
+
+# It turns on every manila-share instances on the host.
+manila_share_service_enabled:
+  manilang.service_enabled:
+  - binary: manila-share
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+  - microversion: '2.40'
+
+{%- endif %}
diff --git a/manila/upgrade/upgrade/pre.sls b/manila/upgrade/upgrade/pre.sls
index cc98242..f00b1f5 100644
--- a/manila/upgrade/upgrade/pre.sls
+++ b/manila/upgrade/upgrade/pre.sls
@@ -1,3 +1,30 @@
 manila_upgrade_upgrade_pre:
   test.show_notification:
-    - text: "Running manila.upgrade.upgrade.pre"
+     - name: "dump_message_upgrade_manila_pre"
+     - text: "Running manila.upgrade.upgrade.pre"
+
+{% set host_id = salt['network.get_hostname']() %}
+
+{%- if pillar.manila.scheduler is defined %}
+
+# It turns off every manila-scheduler instances on the host.
+manila_controller_service_disabled:
+  manilang.service_disabled:
+  - binary: manila-scheduler
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+  - microversion: '2.40'
+
+{%- endif %}
+
+{%- if pillar.manila.share is defined %}
+
+# It turns off every manila-share instances on the host.
+manila_share_service_disabled:
+  manilang.service_disabled:
+  - binary: manila-share
+  - cloud_name: admin_identity
+  - name: {{ host_id }}
+  - microversion: '2.40'
+
+{%- endif %}
diff --git a/manila/upgrade/verify/_service.sls b/manila/upgrade/verify/_service.sls
new file mode 100644
index 0000000..405901c
--- /dev/null
+++ b/manila/upgrade/verify/_service.sls
@@ -0,0 +1,14 @@
+manila_task_uprade_verify_service:
+  test.show_notification:
+    - text: "Running manila.upgrade.verify.service"
+
+  {% set host_id = salt['network.get_hostname']() %}
+
+wait_for_services:
+  module.run:
+    - name: manilang.service_wait
+    - cloud_name: admin_identity
+    - host: {{ host_id }}
+    - microversion: '2.40'
+    - retries: 30
+    - timeout: 10
diff --git a/manila/upgrade/verify/init.sls b/manila/upgrade/verify/init.sls
index fe2e2d1..5f37964 100644
--- a/manila/upgrade/verify/init.sls
+++ b/manila/upgrade/verify/init.sls
@@ -1,2 +1,3 @@
 include:
- - manila.upgrade.verify._api
+  - manila.upgrade.verify._service
+  - manila.upgrade.verify._api