[states] fix cubicweb_instance.present
authorJulien Cristau <julien.cristau@logilab.fr>
Fri, 04 Mar 2016 12:31:46 +0100
changeset 77 62d8dbe7c966
parent 76 49a436f14557
child 78 bc9fc4cffa1e
[states] fix cubicweb_instance.present - don't check if an instance is running, check if it exists - check and report errors from create and initialize
_modules/cubicweb.py
_states/cubicweb_instance.py
--- a/_modules/cubicweb.py	Fri Mar 04 12:29:42 2016 +0100
+++ b/_modules/cubicweb.py	Fri Mar 04 12:31:46 2016 +0100
@@ -288,7 +288,7 @@
     cmd = ('cubicweb-ctl db-init --automatic --config-level 0 {0}').format(name)
     if drop:
         cmd += ' --drop'
-    return __salt__['cmd.run'](cmd)
+    return __salt__['cmd.run_all'](cmd)
 
 def _pgdbcmd(cmd, dbhost, dbport, dbuser, *args, **kw):
         cmd = [cmd]
--- a/_states/cubicweb_instance.py	Fri Mar 04 12:29:42 2016 +0100
+++ b/_states/cubicweb_instance.py	Fri Mar 04 12:31:46 2016 +0100
@@ -4,11 +4,11 @@
 
 
 '''
-
-
 import logging
 
 import salt.utils
+from salt.exceptions import CommandExecutionError
+
 
 log = logging.getLogger(__name__)
 
@@ -54,22 +54,36 @@
            'changes': {},
            'result': True,
            'comment': 'Instance {0} is already present'.format(name)}
+    skip_create = False
 
     # check if an instance of that name already runs
-    if __salt__['cubicweb.status'](name):
-        # XXX check the instance is at leaset the same cubes as
+    if name in __salt__['cubicweb.instances']():
+        skip_create = True
+        # XXX check the instance is at least the same cubes as
         # described
-        return ret
+        if not create_db:
+            return ret
+        if __salt__['cubicweb.versions'](name):
+            return ret
 
-    # is the instance does not exists, create it
+    # is the instance does not exist, create it
     if __opts__['test']:
         ret['result'] = None
         ret['comment'] = 'Instance {0} is set to be created'.format(name)
         return ret
-    __salt__['cubicweb.create'](name, cubes, install_type=install_type, **kwargs)
+    if not skip_create:
+        try:
+            __salt__['cubicweb.create'](name, cubes, install_type=install_type, **kwargs)
+        except CommandExecutionError as ex:
+            ret['result'] = False
+            ret['comment'] = 'Could not create instance {0}: {1}'.format(name, ex.strerror)
     if create_db:
-        __salt__['cubicweb.initialize'](name)
-        ret['comment'] = 'Instance {0} has been installed and database initialized'.format(name)
+        init = __salt__['cubicweb.initialize'](name)
+        if init['retcode']:
+            ret['result'] = False
+            ret['comment'] = 'Could not initialize instance {0}: {1}'.format(name, init['stderr'])
+        else:
+            ret['comment'] = 'Instance {0} has been installed and database initialized'.format(name)
     else:
         ret['comment'] = 'Instance {0} has been installed'.format(name)
     return ret