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