Skip to content

Commit ab648fe

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 ab648fe

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

runbot/runbot.py

+36-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,17 @@ 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+
cr = cnx.cursor()
164+
yield cr
165+
finally:
166+
if cnx: cnx.close()
167+
155168
#----------------------------------------------------------
156169
# RunBot Models
157170
#----------------------------------------------------------
@@ -797,17 +810,19 @@ def checkout(self, cr, uid, ids, context=None):
797810
build.write({'server_match': server_match,
798811
'modules': ','.join(modules_to_test)})
799812

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

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

812827
def cmd(self, cr, uid, ids, context=None):
813828
"""Return a list describing the command to start the build"""
@@ -906,7 +921,7 @@ def job_00_init(self, cr, uid, build, lock_path, log_path):
906921
def job_10_test_base(self, cr, uid, build, lock_path, log_path):
907922
build._log('test_base', 'Start test base module')
908923
# run base test
909-
self.pg_createdb(cr, uid, "%s-base" % build.dest)
924+
self._local_pg_createdb(cr, uid, "%s-base" % build.dest)
910925
cmd, mods = build.cmd()
911926
if grep(build.server("tools/config.py"), "test-enable"):
912927
cmd.append("--test-enable")
@@ -915,7 +930,7 @@ def job_10_test_base(self, cr, uid, build, lock_path, log_path):
915930

916931
def job_20_test_all(self, cr, uid, build, lock_path, log_path):
917932
build._log('test_all', 'Start test all modules')
918-
self.pg_createdb(cr, uid, "%s-all" % build.dest)
933+
self._local_pg_createdb(cr, uid, "%s-all" % build.dest)
919934
cmd, mods = build.cmd()
920935
if grep(build.server("tools/config.py"), "test-enable"):
921936
cmd.append("--test-enable")
@@ -1075,24 +1090,27 @@ def schedule(self, cr, uid, ids, context=None):
10751090

10761091
# cleanup only needed if it was not killed
10771092
if build.state == 'done':
1078-
build.cleanup()
1093+
build._local_cleanup()
10791094

10801095
def skip(self, cr, uid, ids, context=None):
10811096
self.write(cr, uid, ids, {'state': 'done', 'result': 'skipped'}, context=context)
10821097
to_unduplicate = self.search(cr, uid, [('id', 'in', ids), ('duplicate_id', '!=', False)])
10831098
if len(to_unduplicate):
10841099
self.force(cr, uid, to_unduplicate, context=context)
10851100

1086-
def cleanup(self, cr, uid, ids, context=None):
1101+
def _local_cleanup(self, cr, uid, ids, context=None):
10871102
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)
1103+
# Cleanup the *local* cluster
1104+
with local_pgadmin_cursor() as local_cr:
1105+
local_cr.execute("""
1106+
SELECT datname
1107+
FROM pg_database
1108+
WHERE pg_get_userbyid(datdba) = current_user
1109+
AND datname LIKE %s
1110+
""", [build.dest + '%'])
1111+
to_delete = local_cr.fetchall()
1112+
for db, in to_delete:
1113+
self._local_pg_dropdb(cr, uid, db)
10961114

10971115
if os.path.isdir(build.path()) and build.result != 'killed':
10981116
shutil.rmtree(build.path())
@@ -1111,7 +1129,7 @@ def kill(self, cr, uid, ids, result=None, context=None):
11111129
build.write(v)
11121130
cr.commit()
11131131
build.github_status()
1114-
build.cleanup()
1132+
build._local_cleanup()
11151133

11161134
def reap(self, cr, uid, ids):
11171135
while True:

0 commit comments

Comments
 (0)