update_config.py
changeset 571 a4aead244f75
parent 568 14abdd04ddf5
child 623 d7101e496795
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright (c) 2008 - 2012, Pascal Volk
       
     4 # See COPYING for distribution information.
       
     5 
       
     6 import os
       
     7 os.sys.path.remove(os.sys.path[0])
       
     8 from time import time
       
     9 from ConfigParser import ConfigParser
       
    10 from shutil import copy2
       
    11 try:
       
    12     from VirtualMailManager.constants.VERSION import VERSION
       
    13 except ImportError:
       
    14     os.sys.stderr.write('error: no pre 0.6.0 version information found\n')
       
    15     raise SystemExit(2)
       
    16 
       
    17 # we have to remove the old CamelCase files
       
    18 import VirtualMailManager
       
    19 vmm_inst_dir = os.path.dirname(VirtualMailManager.__file__)
       
    20 tmp_info = open('/tmp/vmm_inst_dir', 'w')
       
    21 tmp_info.write(vmm_inst_dir)
       
    22 tmp_info.close()
       
    23 
       
    24 try:
       
    25     import psycopg2
       
    26 except ImportError:
       
    27     has_psycopg2 = False
       
    28 else:
       
    29     has_psycopg2 = True
       
    30 
       
    31 def get_config_file():
       
    32     f = None
       
    33     for d in ('/root', '/usr/local/etc', '/etc'):
       
    34         tmp = os.path.join(d, 'vmm.cfg')
       
    35         if os.path.isfile(tmp):
       
    36             f = tmp
       
    37             break
       
    38     if f:
       
    39         return f
       
    40     else:
       
    41         os.sys.stderr.write('error: vmm.cfg not found\n')
       
    42         raise SystemExit(2)
       
    43 
       
    44 def update(cp):
       
    45     if VERSION == '0.5.2':
       
    46         upd_052(cp)
       
    47     elif VERSION == '0.6.0':
       
    48         os.sys.stdout.write('info: nothing to do for version %s\n' % VERSION)
       
    49         return
       
    50     else:
       
    51         os.sys.stderr.write(
       
    52             'error: the version %s is not supported by this script\n' % VERSION)
       
    53         raise SystemExit(3)
       
    54 
       
    55 def get_cfg_parser(cf):
       
    56     fh = open(cf, 'r')
       
    57     cp = ConfigParser()
       
    58     cp.readfp(fh)
       
    59     fh.close()
       
    60     return cp
       
    61 
       
    62 def update_cfg_file(cp, cf):
       
    63     copy2(cf, cf+'.bak.'+str(time()))
       
    64     fh = open(cf, 'w')
       
    65     cp.write(fh)
       
    66     fh.close()
       
    67 
       
    68 def add_sections(cp, sections):
       
    69     for section in sections:
       
    70         if not cp.has_section(section):
       
    71             cp.add_section(section)
       
    72 
       
    73 def move_option(cp, src, dst):
       
    74     ds, do = dst.split('.')
       
    75     if not cp.has_option(ds, do):
       
    76         ss, so = src.split('.')
       
    77         cp.set(ds, do, cp.get(ss, so))
       
    78         cp.remove_option(ss, so)
       
    79         sect_opt.append((dst, 'R'))
       
    80 
       
    81 def add_option(cp, dst, val):
       
    82     ds, do = dst.split('.')
       
    83     if not cp.has_option(ds, do):
       
    84         cp.set(ds, do, val)
       
    85         sect_opt.append((dst, 'N'))
       
    86 
       
    87 
       
    88 def set_dovecot_version(cp):
       
    89     if len(os.sys.argv) > 1:
       
    90         dovecot_version = os.sys.argv[1].strip()
       
    91         if not dovecot_version:
       
    92             dovecot_version = '1.2.11'
       
    93     else:
       
    94         dovecot_version = '1.2.11'
       
    95     cp.set('misc', 'dovecot_version', dovecot_version)
       
    96     sect_opt.append(('misc.dovecot_version', 'M'))
       
    97 
       
    98 
       
    99 def get_option(cp, src):
       
   100     ss, so = src.split('.')
       
   101     return cp.get(ss, so)
       
   102 
       
   103 def upd_052(cp):
       
   104     global had_config
       
   105     global had_gid_mail
       
   106 
       
   107     had_config = cp.remove_section('config')
       
   108     had_gid_mail = cp.remove_option('misc', 'gid_mail')
       
   109     add_sections(cp, ('domain', 'account', 'mailbox'))
       
   110     if cp.has_section('domdir'):
       
   111         for src, dst in (('domdir.mode',   'domain.directory_mode'),
       
   112                          ('domdir.delete', 'domain.delete_directory'),
       
   113                          ('domdir.base',   'misc.base_directory')):
       
   114             move_option(cp, src, dst)
       
   115         cp.remove_section('domdir')
       
   116     if cp.has_section('services'):
       
   117         for service in cp.options('services'):
       
   118             move_option(cp, 'services.%s' % service, 'domain.%s' % service)
       
   119         cp.remove_section('services')
       
   120     for src, dst in (('maildir.mode',      'account.directory_mode'),
       
   121                      ('maildir.diskusage', 'account.disk_usage'),
       
   122                      ('maildir.delete',    'account.delete_directory'),
       
   123                      ('maildir.folders',   'mailbox.folders'),
       
   124                      ('maildir.name',      'mailbox.root'),
       
   125                      ('misc.forcedel',     'domain.force_deletion'),
       
   126                      ('misc.transport',    'domain.transport'),
       
   127                      ('misc.passwdscheme', 'misc.password_scheme'),
       
   128                      ('misc.dovecotvers',  'misc.dovecot_version')):
       
   129         move_option(cp, src, dst)
       
   130     cp.remove_section('maildir')
       
   131     if not has_psycopg2:
       
   132         add_option(cp, 'database.module', 'pyPgSQL')
       
   133     set_dovecot_version(cp)
       
   134 
       
   135 
       
   136 # def main():
       
   137 if __name__ == '__main__':
       
   138     sect_opt = []
       
   139     had_config = False
       
   140     had_gid_mail = False
       
   141     cf = get_config_file()
       
   142     cp = get_cfg_parser(cf)
       
   143     update(cp)
       
   144     if len(sect_opt):
       
   145         update_cfg_file(cp, cf)
       
   146         sect_opt.sort()
       
   147         print 'Please have a look at your configuration: %s' %cf
       
   148         print 'This are your Modified/Renamed/New settings:'
       
   149         for s_o, st in sect_opt:
       
   150             print '%s   %s = %s' % (st, s_o, get_option(cp, s_o))
       
   151         if had_config:
       
   152             print '\nRemoved section "config" with option "done" (obsolte)'
       
   153         if had_gid_mail:
       
   154             print '\nRemoved option "gid_mail" from section "misc" (obsolte)\n'
       
   155         os.sys.exit(0)
       
   156     if had_config or had_gid_mail:
       
   157         update_cfg_file(cp, cf)
       
   158         if had_config:
       
   159             print '\nRemoved section "config" with option "done" (obsolte)'
       
   160         if had_gid_mail:
       
   161             print '\nRemoved option "gid_mail" from section "misc" (obsolte)\n'