[module] add a list_backups function
authorDavid Douard <david.douard@logilab.fr>
Fri, 22 May 2015 09:07:23 +0200
changeset 27 9e89d6d03ca9
parent 26 1c03667c96dd
child 28 245e16e3c9af
[module] add a list_backups function
_modules/cubicweb.py
--- a/_modules/cubicweb.py	Tue May 19 14:34:01 2015 +0200
+++ b/_modules/cubicweb.py	Fri May 22 09:07:23 2015 +0200
@@ -222,7 +222,7 @@
         if dbuser:
             cmd.append('--username={0}'.format(dbuser))
         for k, v in kw.items():
-            cmd.append('--{0}={1}'.format(k, v))    
+            cmd.append('--{0}={1}'.format(k, v))
         return cmd
 
 def restore(name, dumpfileurl, drop=False):
@@ -244,7 +244,7 @@
         cfgfile = os.path.join('/etc/cubicweb.d', name, 'sources')
         ret['cfgfile'] = (cfgfile, os.path.isfile(cfgfile))
         ret['cfg'] = cwsrc.read(cfgfile)
-        
+
         src = dict(cwsrc.items('system'))
 
         pgpass = os.path.join(tmpdir, '.pgpass')
@@ -254,7 +254,7 @@
                                                         src['db-password']))
         os.chmod(pgpass, 0600)
         env = {'PGPASSFILE': pgpass}
-        cmd = _pgdbcmd('pg_restore', 
+        cmd = _pgdbcmd('pg_restore',
                        src.get('db-host'),
                        src.get('db-port'),
                        src.get('db-user'),
@@ -302,7 +302,7 @@
     result = __salt__['cmd.run'](cmd)
     log.debug('restart {0}: {1!r}'.format(name, result))
     return result
- 
+
 def reset_password(name, password):
     '''Reset the admin password for an instance
     '''
@@ -310,4 +310,21 @@
     result = __salt__['cmd.run'](cmd)
     log.debug('reset_password {0}: {1!r}'.format(name, result))
     return result
-    
+
+def list_backups(name):
+    '''Return the list of known backups for the given instance
+    '''
+    if name not in _cwlist()['instances']:
+        log.warning('backups: instance {0} not found'.format(name))
+        return
+
+    # XXX missing a cubicweb-ctl command/option to get this path
+    backupsdir = '/var/lib/cubicweb/instances/'
+    dumpsdir = os.path.join(backupsdir, name, 'backup')
+    if not __salt__['file.access'](dumpsdir, 'x'):
+        log.warning('backups: directory {0} not found or not accessible'.format(dumpsdir))
+        return
+
+    files = __salt__['file.readdir'](dumpsdir)
+    return [os.path.join(dumpsdir, filename)
+            for filename in files if filename.endswith('.tar.gz')]