Extend dockerng_service.running for watch method

PROD-16646

Change-Id: Ic4e270bb6b5ff927131f5e047502bba2519831c5
diff --git a/_modules/dockerng_service.py b/_modules/dockerng_service.py
index 515cc0f..081b7e0 100644
--- a/_modules/dockerng_service.py
+++ b/_modules/dockerng_service.py
@@ -41,6 +41,16 @@
     return result
 
 
+def status_retcode(container, service):
+    cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
+    data =  __salt__['dockerng.run'](container, cmd)
+    data = data.splitlines()
+    result = dict(s.split('=') for s in data)
+    if result['ActiveState'] == "active" and result['SubState'] == "running":
+        return True
+    return False
+
+
 def restart(container, service):
     cmd = "systemctl restart " + service
     data =  __salt__['dockerng.run'](container, cmd)
@@ -87,4 +97,3 @@
     if len(data) > 0:
         return False
     return True
-
diff --git a/_states/dockerng_service.py b/_states/dockerng_service.py
index 9fde782..ed13798 100644
--- a/_states/dockerng_service.py
+++ b/_states/dockerng_service.py
@@ -19,13 +19,13 @@
 :depends:   - vnc_api Python module
 
 
-Enforce the service in container is started
+Enforce the service in container is running
 -------------------------------------------
 
 .. code-block:: yaml
 
-    contrail_control_started:
-      dockerng_service.start:
+    contrail_control_running:
+      dockerng_service.running:
         - container: f020d0d3efa8
         - service: contrail-control
 
@@ -33,19 +33,19 @@
 
 .. code-block:: yaml
 
-    contrail_control_started:
-      dockerng_service.start:
+    contrail_control_running:
+      dockerng_service.running:
         - container: contrail_controller
         - service: contrail-control
 
 
-Enforce the service in container is stoped
+Enforce the service in container is dead
 ------------------------------------------
 
 .. code-block:: yaml
 
-    contrail_control_stoped:
-      dockerng_service.stop:
+    contrail_control_dead:
+      dockerng_service.dead:
         - container: f020d0d3efa8
         - service: contrail-control
 
@@ -54,8 +54,8 @@
 
 .. code-block:: yaml
 
-    contrail_control_restart:
-      dockerng_service.restart:
+    contrail_control_restarted:
+      dockerng_service.restarted:
         - container: f020d0d3efa8
         - service: contrail-control
 
@@ -64,8 +64,8 @@
 
 .. code-block:: yaml
 
-    contrail_control_enable:
-      dockerng_service.enable:
+    contrail_control_enabled:
+      dockerng_service.enabled:
         - container: f020d0d3efa8
         - service: contrail-control
 
@@ -74,8 +74,8 @@
 
 .. code-block:: yaml
 
-    contrail_control_disable:
-      dockerng_service.disable:
+    contrail_control_disabled:
+      dockerng_service.disabled:
         - container: f020d0d3efa8
         - service: contrail-control
 
@@ -89,37 +89,41 @@
     return 'dockerng_service'
 
 
-def start(container, service, **kwargs):
+def running(container, service=None, services=None, **kwargs):
     '''
-    Ensures that the service in the container is started.
+    Ensures that the service in the container is running
 
     :param container:    ID or name of the container
+    :param services:     List of services
     :param service:      Service name
     '''
-    ret = {'name': service + " in " + container,
+    ret = {'name': kwargs.get('name', 'dockerng_service.running'),
            'changes': {},
            'result': True,
-           'comment': ''}
+           'comment': {}
+           }
 
-    status = __salt__['dockerng_service.status'](container, service)
+    if service and not services:
+        services = [service, ]
 
-    if status['ActiveState'] == "inactive" and status['SubState'] == "dead":
-        if __opts__['test']:
-            ret['result'] = None
-            ret['comment'] = service + " in  " + container + " will be started"
-            return ret
+    for service in services:
+        status = __salt__['dockerng_service.status'](container, service)
 
-        res = __salt__['dockerng_service.start'](container, service)
-        ret['comment'] = service + " in  " + container + " has been started"
-        ret['changes'] = {"old": "stoped", "new": "started"}
-        return ret
+        if status['ActiveState'] != "active" and status['SubState'] != "running":
+            if __opts__['test']:
+                ret['result'] = None
+                ret['comment'][service] = " will be started"
+            else:
+                __salt__['dockerng_service.start'](container, service)
+                ret['comment'] = service + " in  " + container + " has been started"
+                ret['changes'] = {service:  "started"}
 
     return ret
 
 
-def stop(container, service, **kwargs):
+def dead(container, service, **kwargs):
     '''
-    Ensures that the service in the container is stoped
+    Ensures that the service in the container is dead
 
     :param container:    ID or name of the container
     :param service:      Service name
@@ -137,7 +141,7 @@
             ret['comment'] = service + " in  " + container + " will be stoped"
             return ret
 
-        res = __salt__['dockerng_service.stop'](container, service)
+        __salt__['dockerng_service.stop'](container, service)
         ret['comment'] = service + " in  " + container + " has been stoped"
         ret['changes'] = {"new": "stoped", "old": "started"}
         return ret
@@ -145,7 +149,7 @@
     return ret
 
 
-def restart(container, service, **kwargs):
+def restarted(container, service, **kwargs):
     '''
     Service in the container will be restarted
 
@@ -157,19 +161,18 @@
            'result': True,
            'comment': ''}
 
-
     if __opts__['test']:
         ret['result'] = None
         ret['comment'] = service + " in  " + container + " will be restarted"
         return ret
 
     res = __salt__['dockerng_service.restart'](container, service)
-    ret['comment'] = service + " in  " + container + " has been stoped"
+    ret['comment'] = service + " in  " + container + " has been restarted"
     ret['changes'] = {"status": "restarted"}
     return ret
 
 
-def enable(container, service, **kwargs):
+def enabled(container, service, **kwargs):
     '''
     Ensures that the service in the container is enabled
 
@@ -189,7 +192,7 @@
             ret['comment'] = service + " in  " + container + " will be enabled"
             return ret
 
-        res = __salt__['dockerng_service.enable'](container, service)
+        __salt__['dockerng_service.enable'](container, service)
         ret['comment'] = service + " in  " + container + " has been enabled"
         ret['changes'] = {"new": "enabled", "old": "disabled"}
         return ret
@@ -197,7 +200,7 @@
     return ret
 
 
-def disable(container, service, **kwargs):
+def disabled(container, service, **kwargs):
     '''
     Ensures that the service in the container is disabled
 
@@ -217,9 +220,78 @@
             ret['comment'] = service + " in  " + container + " will be disabled"
             return ret
 
-        res = __salt__['dockerng_service.disable'](container, service)
+        __salt__['dockerng_service.disable'](container, service)
         ret['comment'] = service + " in  " + container + " has been disabled"
         ret['changes'] = {"old": "enabled", "new": "disabled"}
         return ret
 
     return ret
+
+
+def mod_watch(name,
+              contrainer=None,
+              sfun=None,
+              sig=None,
+              reload=False,
+              full_restart=False,
+              init_delay=None,
+              force=False,
+              **kwargs):
+    '''
+    The service watcher, called to invoke the watch command.
+
+    :param name:         The name of the init or rc script used to manage the
+                         service
+    :param sfun:         The original function which triggered the mod_watch
+                         call (`service.running`, for example).
+    :param sig:          The string to search for when looking for the service
+                         process with ps
+    :param reload:       Use reload instead of the default restart (exclusive
+                         option with full_restart, defaults to reload if both
+                         are used)
+    :param full_restart: Use service.full_restart instead of restart
+                         (exclusive option with reload)
+    :param force:        Use service.force_reload instead of reload
+                         (needs reload to be set to True)
+    :param  init_delay:  Add a sleep command (in seconds) before the service is
+                         restarted/reloaded
+    '''
+    ret = {'name': name,
+           'changes': {},
+           'result': True,
+           'comment': {}}
+
+    service = kwargs.get('service')
+    services = kwargs.get('services')
+    if not services and service:
+        services = [service, ]
+    elif not services and not service:
+        ret['result'] = False
+        ret['comment'] = "Service was not defined"
+        return ret
+
+    container = kwargs.get('container', None)
+    if not container:
+        ret['result'] = False
+        ret['comment'] = "Container was not defined"
+        return ret
+
+    ret['comment'] = {}
+    if sfun == 'running':
+
+        for service in services:
+            status = __salt__['dockerng_service.status'](container, service)
+
+
+            if __opts__['test']:
+                ret['result'] = None
+                ret['comment'][service] = "Services will be restarted"
+                ret['changes'][service] = "will be restarted"
+            else:
+                res = __salt__['dockerng_service.restart'](container, service)
+                ret['comment'] = "Services has been restarted"
+                ret['changes'][service] = "restarted"
+    else:
+        ret['comment'] = 'Unable to trigger watch for dockerng_service.{0}'.format(sfun)
+        ret['result'] = False
+    return ret