devtools/migrtest.py
changeset 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """Migration test script
       
     2 
       
     3 * migration will be played into a chroot of the local machine
       
     4 * the database server used can be configured
       
     5 * test tested instance may be on another host
       
     6 
       
     7 
       
     8 We are using postgres'.pgpass file. Here is a copy of postgres documentation
       
     9 about that:
       
    10 
       
    11 The file .pgpass in a user's home directory or the file referenced by
       
    12 PGPASSFILE can contain passwords to be used if the connection requires
       
    13 a password (and no password has been specified otherwise).
       
    14 
       
    15 
       
    16 This file should contain lines of the following format:
       
    17 
       
    18 hostname:port:database:username:password
       
    19 
       
    20 Each of the first four fields may be a literal value, or *, which
       
    21 matches anything. The password field from the first line that matches
       
    22 the current connection parameters will be used. (Therefore, put
       
    23 more-specific entries first when you are using wildcards.) If an entry
       
    24 needs to contain : or \, escape this character with \. A hostname of
       
    25 localhost matches both host (TCP) and local (Unix domain socket)
       
    26 connections coming from the local machine.
       
    27 
       
    28 The permissions on .pgpass must disallow any access to world or group;
       
    29 achieve this by the command chmod 0600 ~/.pgpass. If the permissions
       
    30 are less strict than this, the file will be ignored. 
       
    31 
       
    32 :organization: Logilab
       
    33 :copyright: 2001-2006 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
    34 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
    35 """
       
    36 __docformat__ = "restructuredtext en"
       
    37 
       
    38 from os import system
       
    39 from os.path import join, basename
       
    40 
       
    41 from logilab.common.shellutils import cp, rm
       
    42 
       
    43 from cubicweb.toolsutils import read_config
       
    44 from cubicweb.server.serverctl import generate_sources_file
       
    45 
       
    46 # XXXX use db-copy instead
       
    47 
       
    48 # test environment configuration
       
    49 chrootpath = '/sandbox/cubicwebtest'
       
    50 tmpdbhost = 'crater'
       
    51 tmpdbuser = 'syt' 
       
    52 tmpdbpasswd = 'syt'
       
    53 
       
    54 def play_migration(applhome, applhost='', sudo=False):
       
    55     applid = dbname = basename(applhome)
       
    56     testapplhome = join(chrootpath, applhome)
       
    57     # copy instance into the chroot
       
    58     if applhost:
       
    59         system('scp -r %s:%s %s' % (applhost, applhome, testapplhome))
       
    60     else:
       
    61         cp(applhome, testapplhome)
       
    62 ##     # extract db parameters
       
    63 ##     sources = read_config(join(testapplhome, 'sources'))
       
    64 ##     dbname = sources['system']['db-name']
       
    65 ##     dbhost = sources['system'].get('db-host') or ''
       
    66 ##     dbuser = sources['system'].get('db-user') or ''
       
    67 ##     dbpasswd = sources['system'].get('db-password') or ''
       
    68     # generate sources file
       
    69     # XXX multisources
       
    70     sources = {'system': {}}
       
    71     sources['system']['db-encoding'] = 'UTF8' # XXX
       
    72     sources['system']['db-name'] = dbname
       
    73     sources['system']['db-host'] = None
       
    74     sources['system']['db-user'] = tmpdbuser
       
    75     sources['system']['db-password'] = None
       
    76     generate_sources_file(join(testapplhome, 'sources'), sources)
       
    77 ##     # create postgres password file so we won't need anymore passwords
       
    78 ##     # XXX may exist!
       
    79 ##     pgpassfile = expanduser('~/.pgpass')
       
    80 ##     pgpass = open(pgpassfile, 'w')
       
    81 ##     if dbpasswd:
       
    82 ##         pgpass.write('%s:*:%s:%s:%s\n' % (dbhost or applhost or 'localhost',
       
    83 ##                                           dbname, dbuser, dbpasswd))
       
    84 ##     if tmpdbpasswd:
       
    85 ##         pgpass.write('%s:*:%s:%s:%s\n' % (tmpdbhost or 'localhost', dbname,
       
    86 ##                                           tmpdbuser, tmpdbpasswd))
       
    87 ##     pgpass.close()
       
    88 ##     chmod(pgpassfile, 0600)
       
    89     # dump db
       
    90 ##     dumpcmd = 'pg_dump -Fc -U %s -f /tmp/%s.dump %s' % (
       
    91 ##         dbuser, dbname, dbname)
       
    92 ##     if dbhost:
       
    93 ##         dumpcmd += ' -h %s' % dbhost
       
    94     dumpfile = '/tmp/%s.dump' % applid
       
    95     dumpcmd = 'cubicweb-ctl db-dump --output=%s %s' % (dumpfile, applid)
       
    96     if sudo:
       
    97         dumpcmd = 'sudo %s' % dumpcmd
       
    98     if applhost:
       
    99         dumpcmd = 'ssh %s "%s"' % (applhost, dumpcmd)
       
   100     if system(dumpcmd):
       
   101         raise Exception('error while dumping the database')
       
   102 ##     if not dbhost and applhost:
       
   103     if applhost:
       
   104         # retrieve the dump
       
   105         if system('scp %s:%s %s' % (applhost, dumpfile, dumpfile)):
       
   106             raise Exception('error while retreiving the dump')
       
   107     # move the dump into the chroot
       
   108     system('mv %s %s%s' % (dumpfile, chrootpath, dumpfile))
       
   109     # locate installed versions
       
   110     vcconf = read_config(join(testapplhome, 'vc.conf'))
       
   111     template = vcconf['TEMPLATE']
       
   112     cubicwebversion = vcconf['CW']
       
   113     templversion = vcconf['TEMPLATE_VERSION']
       
   114     # install the same versions cubicweb and template versions into the chroot
       
   115     system('sudo chroot %s apt-get update' % chrootpath)
       
   116     system('sudo chroot %s apt-get install cubicweb-server=%s cubicweb-client=%s'
       
   117            % (chrootpath, cubicwebversion, cubicwebversion))
       
   118     system('sudo chroot %s apt-get install cubicweb-%s-appl-server=%s cubicweb-%s-appl-client=%s'
       
   119            % (chrootpath, template, templversion, template, templversion))
       
   120     # update and upgrade to the latest version
       
   121     system('sudo chroot %s apt-get install cubicweb-server cubicweb-client' % chrootpath)
       
   122     system('sudo chroot %s apt-get install cubicweb-%s-appl-server cubicweb-%s-appl-client'
       
   123            % (chrootpath, template, template))
       
   124     # create and fill the database
       
   125     system('sudo chroot cubicweb-ctl db-restore %s %s' % (applid, dumpfile))
       
   126 ##     if not tmpdbhost:
       
   127 ##         system('createdb -U %s -T template0 -E UTF8 %s' % (tmpdbuser, dbname))
       
   128 ##         system('pg_restore -U %s -O -Fc -d %s /tmp/%s.dump'
       
   129 ##                % (tmpdbuser, dbname, dbname))
       
   130 ##     else:
       
   131 ##         system('createdb -h %s -U %s -T template0 -E UTF8 %s'
       
   132 ##                % (tmpdbhost, tmpdbuser, dbname))
       
   133 ##         system('pg_restore -h %s -U %s -O -Fc -d %s /tmp/%s.dump'
       
   134 ##                % (tmpdbhost, tmpdbuser, dbname, dbname))
       
   135     # launch upgrade
       
   136     system('sudo chroot %s cubicweb-ctl upgrade %s' % (chrootpath, applid))
       
   137 
       
   138     # cleanup
       
   139     rm(testapplhome)
       
   140 ##     rm(pgpassfile)
       
   141 ##     if tmpdbhost:
       
   142 ##         system('dropdb -h %s -U %s %s' % (tmpdbuser, tmpdbhost, dbname))
       
   143 ##     else:
       
   144 ##         system('dropdb -U %s %s' % (tmpdbuser, dbname))
       
   145 ##     if not dbhost and applhost:
       
   146     if applhost:
       
   147         system('ssh %s rm %s' % (applhost, dumpfile))
       
   148     rm('%s%s' % (chrootpath, dumpfile))
       
   149 
       
   150 
       
   151 if __name__ == '__main__':
       
   152     play_migration('/etc/cubicweb.d/jpl', 'lepus')