Rewrote upgrade script and config update script
authorPascal Volk <neverseen@users.sourceforge.net>
Wed, 19 Aug 2009 09:52:24 +0000
changeset 118 cf85d78486ce
parent 117 c96b5768c76d
child 119 014335f38962
Rewrote upgrade script and config update script
update_config_0.4.x-0.5.py
upgrade.sh
--- a/update_config_0.4.x-0.5.py	Wed Aug 19 02:28:43 2009 +0000
+++ b/update_config_0.4.x-0.5.py	Wed Aug 19 09:52:24 2009 +0000
@@ -3,47 +3,107 @@
 # Copyright (c) 2008 - 2009, VEB IT
 # See COPYING for distribution information.
 
-import sys
+import os
+os.sys.path.remove(os.sys.path[0])
+from time import time
 from ConfigParser import ConfigParser
 from shutil import copy2
+from VirtualMailManager.constants.VERSION import VERSION
 
-cf = '/usr/local/etc/vmm.cfg'
-fh = file(cf, 'r')
-cp = ConfigParser()
-cp.readfp(fh)
-fh.close()
+
+def get_config_file():
+    f = None
+    for d in ('/root', '/usr/local/etc', '/etc'):
+        if os.path.isfile(d+'/vmm.cfg'):
+            f = d+'/vmm.cfg'
+            break
+    if f:
+        return f
+    else:
+        os.sys.stderr.write('error: vmm.cfg not found\n')
+        os.sys.exit(2)
 
-if not cp.has_option('maildir', 'name') or not cp.has_option('maildir',
-        'folders') or cp.has_option('maildir', 'folder'):
-    copy2(cf, cf+'.bak_upd_0.4.x-0.5')
+def update(cp):
+    if VERSION == '0.4':
+        upd_040(cp)
+    elif VERSION == '0.5':
+        upd_050(cp)
+    elif VERSION == '0.5.1':
+        upd_051(cp)
+    else:
+        os.sys.stderr.write(
+            'error: the version %s is not supported by this script\n' % VERSION)
+        os.sys.exit(3)
+
+def get_cfg_parser(cf):
+    fh = file(cf, 'r')
+    cp = ConfigParser()
+    cp.readfp(fh)
+    fh.close()
+    return cp
+
+def update_cfg_file(cp, cf):
+    copy2(cf, cf+'.bak.'+str(time()))
     fh = file(cf, 'w')
-    if not cp.has_option('maildir', 'name'):
-        if cp.has_option('maildir', 'folder'):
-            cp.set('maildir', 'name', cp.get('maildir', 'folder'))
-            cp.remove_option('maildir', 'folder')
-        else:
-            cp.set('maildir', 'name', 'Maildir')
-    if not cp.has_option('maildir', 'folders'):
-        cp.set('maildir', 'folders', 'Drafts:Sent:Templates:Trash')
-    if cp.has_option('maildir', 'folder'):
-        cp.remove_option('maildir', 'folder')
     cp.write(fh)
     fh.close()
 
-if not cp.has_option('bin', 'postconf'):
-    fh = file(cf, 'w')
-    try:
-        postconf = sys.argv[1].strip()
-        if len(postconf):
-            cp.set('bin', 'postconf', postconf)
-        else: # possible?
+def upd_040(cp):
+    if not cp.has_option('maildir', 'name') or not cp.has_option('maildir',
+        'folders') or cp.has_option('maildir', 'folder'):
+        if not cp.has_option('maildir', 'name'):
+            if cp.has_option('maildir', 'folder'):
+                cp.set('maildir', 'name', cp.get('maildir', 'folder'))
+                cp.remove_option('maildir', 'folder')
+                sect_opt.append(('maildir', 'name'))
+            else:
+                cp.set('maildir', 'name', 'Maildir')
+                sect_opt.append(('maildir', 'name'))
+        if not cp.has_option('maildir', 'folders'):
+            cp.set('maildir', 'folders', 'Drafts:Sent:Templates:Trash')
+            sect_opt.append(('maildir', 'folders'))
+        if cp.has_option('maildir', 'folder'):
+            cp.remove_option('maildir', 'folder')
+    upd_050(cp)
+
+def upd_050(cp):
+    if not cp.has_option('bin', 'postconf'):
+        try:
+            postconf = os.sys.argv[1].strip()
+            if len(postconf):
+                cp.set('bin', 'postconf', postconf)
+                sect_opt.append(('bin', 'postconf'))
+            else: # possible?
+                cp.set('bin', 'postconf', '/usr/sbin/postconf')
+                sect_opt.append(('bin', 'postconf'))
+        except IndexError:
             cp.set('bin', 'postconf', '/usr/sbin/postconf')
-    except IndexError:
-        cp.set('bin', 'postconf', '/usr/sbin/postconf')
-    cp.write(fh)
-    fh.close()
-    print
-    print "Please have a look at your %s" %cf
-    print "and verify the value from option 'postconf' in section 'bin'."
-    print
+            sect_opt.append(('bin', 'postconf'))
+    upd_051(cp)
+
+def upd_051(cp):
+    if not cp.has_option('misc', 'dovecotvers') or cp.has_option('services',
+            'managesieve'):
+        if not cp.has_option('misc', 'dovecotvers'):
+            cp.set('misc', 'dovecotvers', os.sys.argv[2].strip())
+            sect_opt.append(('misc', 'dovecotvers'))
+        if cp.has_option('services', 'managesieve'):
+            cp.set('services','sieve',cp.getboolean('services', 'managesieve'))
+            cp.remove_option('services', 'managesieve')
+            sect_opt.append(('services', 'sieve'))
 
+# def main():
+if __name__ == '__main__':
+    sect_opt = []
+    cf = get_config_file()
+    cp = get_cfg_parser(cf)
+    update(cp)
+    if len(sect_opt): 
+        update_cfg_file(cp, cf)
+        print 'Please have a look at your configuration: %s' %cf
+        print 'and verify the value from:'
+        for s_o in sect_opt:
+            print '  [%s] %s' % s_o
+        print
+
+
--- a/upgrade.sh	Wed Aug 19 02:28:43 2009 +0000
+++ b/upgrade.sh	Wed Aug 19 09:52:24 2009 +0000
@@ -10,6 +10,7 @@
 PF_CONFDIR=$(postconf -h config_directory)
 PF_GID=$(id -g $(postconf -h mail_owner))
 POSTCONF=$(which postconf)
+DOVECOT_VERS=$(dovecot --version | awk -F . '{print $1 $2}')
 LOCALE_DIR=${PREFIX}/share/locale
 DOC_DIR=${PREFIX}/share/doc/vmm
 if [ ${PREFIX} = "/usr" ]; then
@@ -27,6 +28,21 @@
     exit 1
 fi
 
+# update config file before installing the new files.
+./update_config_0.4.x-0.5.py ${POSTCONF} ${DOVECOT_VERS:-10}
+rv=$?
+if [ $rv -eq 2 ]; then
+	echo "please run the install.sh script"
+	exit 1
+elif [ $rv -eq 3 ]; then
+    echo "please read the upgrade instructions at http://vmm.localdomain.org/"
+    exit 1
+elif [ $rv -ne 0 ]; then
+    echo "Sorry, something went wrong. Please file a bug:"
+    echo "https://sourceforge.net/tracker/?group_id=213727"
+    exit 1
+fi
+
 python setup.py -q install --prefix ${PREFIX}
 python setup.py clean --all >/dev/null
 
@@ -81,6 +97,3 @@
 install -m 0644 ${INSTALL_OPTS} pgsql-*.cf ${DOC_DIR}/examples
 install -m 0644 ${INSTALL_OPTS} vmm.cfg ${DOC_DIR}/examples
 
-# update config file
-./update_config_0.4.x-0.5.py $POSTCONF
-