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-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
|
34 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
35 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
|
36 """ |
|
37 __docformat__ = "restructuredtext en" |
|
38 |
|
39 from os import system |
|
40 from os.path import join, basename |
|
41 |
|
42 from logilab.common.shellutils import cp, rm |
|
43 |
|
44 from cubicweb.toolsutils import read_config |
|
45 from cubicweb.server.utils import generate_sources_file |
|
46 |
|
47 # XXXX use db-copy instead |
|
48 |
|
49 # test environment configuration |
|
50 chrootpath = '/sandbox/cubicwebtest' |
|
51 tmpdbhost = 'crater' |
|
52 tmpdbuser = 'syt' |
|
53 tmpdbpasswd = 'syt' |
|
54 |
|
55 def play_migration(applhome, applhost='', sudo=False): |
|
56 applid = dbname = basename(applhome) |
|
57 testapplhome = join(chrootpath, applhome) |
|
58 # copy instance into the chroot |
|
59 if applhost: |
|
60 system('scp -r %s:%s %s' % (applhost, applhome, testapplhome)) |
|
61 else: |
|
62 cp(applhome, testapplhome) |
|
63 ## # extract db parameters |
|
64 ## sources = read_config(join(testapplhome, 'sources')) |
|
65 ## dbname = sources['system']['db-name'] |
|
66 ## dbhost = sources['system'].get('db-host') or '' |
|
67 ## dbuser = sources['system'].get('db-user') or '' |
|
68 ## dbpasswd = sources['system'].get('db-password') or '' |
|
69 # generate sources file |
|
70 # XXX multisources |
|
71 sources = {'system': {}} |
|
72 sources['system']['db-encoding'] = 'UTF8' # XXX |
|
73 sources['system']['db-name'] = dbname |
|
74 sources['system']['db-host'] = None |
|
75 sources['system']['db-user'] = tmpdbuser |
|
76 sources['system']['db-password'] = None |
|
77 generate_sources_file(join(testapplhome, 'sources'), sources) |
|
78 ## # create postgres password file so we won't need anymore passwords |
|
79 ## # XXX may exist! |
|
80 ## pgpassfile = expanduser('~/.pgpass') |
|
81 ## pgpass = open(pgpassfile, 'w') |
|
82 ## if dbpasswd: |
|
83 ## pgpass.write('%s:*:%s:%s:%s\n' % (dbhost or applhost or 'localhost', |
|
84 ## dbname, dbuser, dbpasswd)) |
|
85 ## if tmpdbpasswd: |
|
86 ## pgpass.write('%s:*:%s:%s:%s\n' % (tmpdbhost or 'localhost', dbname, |
|
87 ## tmpdbuser, tmpdbpasswd)) |
|
88 ## pgpass.close() |
|
89 ## chmod(pgpassfile, 0600) |
|
90 # dump db |
|
91 ## dumpcmd = 'pg_dump -Fc -U %s -f /tmp/%s.dump %s' % ( |
|
92 ## dbuser, dbname, dbname) |
|
93 ## if dbhost: |
|
94 ## dumpcmd += ' -h %s' % dbhost |
|
95 dumpfile = '/tmp/%s.dump' % applid |
|
96 dumpcmd = 'cubicweb-ctl db-dump --output=%s %s' % (dumpfile, applid) |
|
97 if sudo: |
|
98 dumpcmd = 'sudo %s' % dumpcmd |
|
99 if applhost: |
|
100 dumpcmd = 'ssh %s "%s"' % (applhost, dumpcmd) |
|
101 if system(dumpcmd): |
|
102 raise Exception('error while dumping the database') |
|
103 ## if not dbhost and applhost: |
|
104 if applhost: |
|
105 # retrieve the dump |
|
106 if system('scp %s:%s %s' % (applhost, dumpfile, dumpfile)): |
|
107 raise Exception('error while retreiving the dump') |
|
108 # move the dump into the chroot |
|
109 system('mv %s %s%s' % (dumpfile, chrootpath, dumpfile)) |
|
110 # locate installed versions |
|
111 vcconf = read_config(join(testapplhome, 'vc.conf')) |
|
112 template = vcconf['TEMPLATE'] |
|
113 cubicwebversion = vcconf['CW'] |
|
114 templversion = vcconf['TEMPLATE_VERSION'] |
|
115 # install the same versions cubicweb and template versions into the chroot |
|
116 system('sudo chroot %s apt-get update' % chrootpath) |
|
117 system('sudo chroot %s apt-get install cubicweb-server=%s cubicweb-client=%s' |
|
118 % (chrootpath, cubicwebversion, cubicwebversion)) |
|
119 system('sudo chroot %s apt-get install cubicweb-%s-appl-server=%s cubicweb-%s-appl-client=%s' |
|
120 % (chrootpath, template, templversion, template, templversion)) |
|
121 # update and upgrade to the latest version |
|
122 system('sudo chroot %s apt-get install cubicweb-server cubicweb-client' % chrootpath) |
|
123 system('sudo chroot %s apt-get install cubicweb-%s-appl-server cubicweb-%s-appl-client' |
|
124 % (chrootpath, template, template)) |
|
125 # create and fill the database |
|
126 system('sudo chroot cubicweb-ctl db-restore %s %s' % (applid, dumpfile)) |
|
127 ## if not tmpdbhost: |
|
128 ## system('createdb -U %s -T template0 -E UTF8 %s' % (tmpdbuser, dbname)) |
|
129 ## system('pg_restore -U %s -O -Fc -d %s /tmp/%s.dump' |
|
130 ## % (tmpdbuser, dbname, dbname)) |
|
131 ## else: |
|
132 ## system('createdb -h %s -U %s -T template0 -E UTF8 %s' |
|
133 ## % (tmpdbhost, tmpdbuser, dbname)) |
|
134 ## system('pg_restore -h %s -U %s -O -Fc -d %s /tmp/%s.dump' |
|
135 ## % (tmpdbhost, tmpdbuser, dbname, dbname)) |
|
136 # launch upgrade |
|
137 system('sudo chroot %s cubicweb-ctl upgrade %s' % (chrootpath, applid)) |
|
138 |
|
139 # cleanup |
|
140 rm(testapplhome) |
|
141 ## rm(pgpassfile) |
|
142 ## if tmpdbhost: |
|
143 ## system('dropdb -h %s -U %s %s' % (tmpdbuser, tmpdbhost, dbname)) |
|
144 ## else: |
|
145 ## system('dropdb -U %s %s' % (tmpdbuser, dbname)) |
|
146 ## if not dbhost and applhost: |
|
147 if applhost: |
|
148 system('ssh %s rm %s' % (applhost, dumpfile)) |
|
149 rm('%s%s' % (chrootpath, dumpfile)) |
|
150 |
|
151 |
|
152 if __name__ == '__main__': |
|
153 play_migration('/etc/cubicweb.d/jpl', 'lepus') |
|