Skip to content

Commit 9bd4838

Browse files
committed
[FIX] runbot: do not use remote cursor for dropping local dbs
To find out the list of manually created dbs, rev. 4d94f45 used the cursor that is connected to the master `runbot` database, which may reside on a different cluster/host. - Add a helper to run commands on the local PG cluster instead ("postgres" database). - Modify other commands for local cluster (create/drop db) to use the same cursor. Rename those methods to _local_* to better indicate their local effect.
1 parent 8067425 commit 9bd4838

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

runbot/runbot.py

+35-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- encoding: utf-8 -*-
22

3+
import contextlib
34
import datetime
45
import fcntl
56
import glob
@@ -8,6 +9,7 @@
89
import logging
910
import operator
1011
import os
12+
import psycopg2
1113
import re
1214
import resource
1315
import shutil
@@ -152,6 +154,16 @@ def uniq_list(l):
152154
def fqdn():
153155
return socket.getfqdn()
154156

157+
@contextlib.contextmanager
158+
def local_pgadmin_cursor():
159+
cnx = None
160+
try:
161+
cnx = psycopg2.connect("dbname=postgres")
162+
cnx.autocommit = True # required for admin commands
163+
yield cnx.cursor()
164+
finally:
165+
if cnx: cnx.close()
166+
155167
#----------------------------------------------------------
156168
# RunBot Models
157169
#----------------------------------------------------------
@@ -797,17 +809,19 @@ def checkout(self, cr, uid, ids, context=None):
797809
build.write({'server_match': server_match,
798810
'modules': ','.join(modules_to_test)})
799811

800-
def pg_dropdb(self, cr, uid, dbname):
801-
run(['dropdb', dbname])
812+
def _local_pg_dropdb(self, cr, uid, dbname):
813+
with local_pgadmin_cursor() as local_cr:
814+
local_cr.execute('DROP DATABASE IF EXISTS "%s"' % dbname)
802815
# cleanup filestore
803816
datadir = appdirs.user_data_dir()
804817
paths = [os.path.join(datadir, pn, 'filestore', dbname) for pn in 'OpenERP Odoo'.split()]
805818
run(['rm', '-rf'] + paths)
806819

807-
def pg_createdb(self, cr, uid, dbname):
808-
self.pg_dropdb(cr, uid, dbname)
820+
def _local_pg_createdb(self, cr, uid, dbname):
821+
self._local_pg_dropdb(cr, uid, dbname)
809822
_logger.debug("createdb %s", dbname)
810-
run(['createdb', '--encoding=unicode', '--lc-collate=C', '--template=template0', dbname])
823+
with local_pgadmin_cursor() as local_cr:
824+
local_cr.execute("""CREATE DATABASE "%s" TEMPLATE template0 LC_COLLATE 'C' ENCODING 'unicode'""" % dbname)
811825

812826
def cmd(self, cr, uid, ids, context=None):
813827
"""Return a list describing the command to start the build"""
@@ -906,7 +920,7 @@ def job_00_init(self, cr, uid, build, lock_path, log_path):
906920
def job_10_test_base(self, cr, uid, build, lock_path, log_path):
907921
build._log('test_base', 'Start test base module')
908922
# run base test
909-
self.pg_createdb(cr, uid, "%s-base" % build.dest)
923+
self._local_pg_createdb(cr, uid, "%s-base" % build.dest)
910924
cmd, mods = build.cmd()
911925
if grep(build.server("tools/config.py"), "test-enable"):
912926
cmd.append("--test-enable")
@@ -915,7 +929,7 @@ def job_10_test_base(self, cr, uid, build, lock_path, log_path):
915929

916930
def job_20_test_all(self, cr, uid, build, lock_path, log_path):
917931
build._log('test_all', 'Start test all modules')
918-
self.pg_createdb(cr, uid, "%s-all" % build.dest)
932+
self._local_pg_createdb(cr, uid, "%s-all" % build.dest)
919933
cmd, mods = build.cmd()
920934
if grep(build.server("tools/config.py"), "test-enable"):
921935
cmd.append("--test-enable")
@@ -1075,24 +1089,27 @@ def schedule(self, cr, uid, ids, context=None):
10751089

10761090
# cleanup only needed if it was not killed
10771091
if build.state == 'done':
1078-
build.cleanup()
1092+
build._local_cleanup()
10791093

10801094
def skip(self, cr, uid, ids, context=None):
10811095
self.write(cr, uid, ids, {'state': 'done', 'result': 'skipped'}, context=context)
10821096
to_unduplicate = self.search(cr, uid, [('id', 'in', ids), ('duplicate_id', '!=', False)])
10831097
if len(to_unduplicate):
10841098
self.force(cr, uid, to_unduplicate, context=context)
10851099

1086-
def cleanup(self, cr, uid, ids, context=None):
1100+
def _local_cleanup(self, cr, uid, ids, context=None):
10871101
for build in self.browse(cr, uid, ids, context=context):
1088-
cr.execute("""
1089-
SELECT datname
1090-
FROM pg_database
1091-
WHERE pg_get_userbyid(datdba) = current_user
1092-
AND datname LIKE %s
1093-
""", [build.dest + '%'])
1094-
for db, in cr.fetchall():
1095-
self.pg_dropdb(cr, uid, db)
1102+
# Cleanup the *local* cluster
1103+
with local_pgadmin_cursor() as local_cr:
1104+
local_cr.execute("""
1105+
SELECT datname
1106+
FROM pg_database
1107+
WHERE pg_get_userbyid(datdba) = current_user
1108+
AND datname LIKE %s
1109+
""", [build.dest + '%'])
1110+
to_delete = local_cr.fetchall()
1111+
for db, in to_delete:
1112+
self._local_pg_dropdb(cr, uid, db)
10961113

10971114
if os.path.isdir(build.path()) and build.result != 'killed':
10981115
shutil.rmtree(build.path())
@@ -1111,7 +1128,7 @@ def kill(self, cr, uid, ids, result=None, context=None):
11111128
build.write(v)
11121129
cr.commit()
11131130
build.github_status()
1114-
build.cleanup()
1131+
build._local_cleanup()
11151132

11161133
def reap(self, cr, uid, ids):
11171134
while True:

0 commit comments

Comments
 (0)