mirror of
https://github.com/odoo/runbot.git
synced 2025-03-27 13:25:47 +07:00
[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.
This commit is contained in:
parent
be34ac72c9
commit
93dd0f5ee0
@ -1,5 +1,6 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import datetime
|
import datetime
|
||||||
import fcntl
|
import fcntl
|
||||||
import glob
|
import glob
|
||||||
@ -8,6 +9,7 @@ import itertools
|
|||||||
import logging
|
import logging
|
||||||
import operator
|
import operator
|
||||||
import os
|
import os
|
||||||
|
import psycopg2
|
||||||
import re
|
import re
|
||||||
import resource
|
import resource
|
||||||
import shutil
|
import shutil
|
||||||
@ -152,6 +154,16 @@ def uniq_list(l):
|
|||||||
def fqdn():
|
def fqdn():
|
||||||
return socket.getfqdn()
|
return socket.getfqdn()
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def local_pgadmin_cursor():
|
||||||
|
cnx = None
|
||||||
|
try:
|
||||||
|
cnx = psycopg2.connect("dbname=postgres")
|
||||||
|
cnx.autocommit = True # required for admin commands
|
||||||
|
yield cnx.cursor()
|
||||||
|
finally:
|
||||||
|
if cnx: cnx.close()
|
||||||
|
|
||||||
#----------------------------------------------------------
|
#----------------------------------------------------------
|
||||||
# RunBot Models
|
# RunBot Models
|
||||||
#----------------------------------------------------------
|
#----------------------------------------------------------
|
||||||
@ -797,17 +809,19 @@ class runbot_build(osv.osv):
|
|||||||
build.write({'server_match': server_match,
|
build.write({'server_match': server_match,
|
||||||
'modules': ','.join(modules_to_test)})
|
'modules': ','.join(modules_to_test)})
|
||||||
|
|
||||||
def pg_dropdb(self, cr, uid, dbname):
|
def _local_pg_dropdb(self, cr, uid, dbname):
|
||||||
run(['dropdb', dbname])
|
with local_pgadmin_cursor() as local_cr:
|
||||||
|
local_cr.execute('DROP DATABASE IF EXISTS "%s"' % dbname)
|
||||||
# cleanup filestore
|
# cleanup filestore
|
||||||
datadir = appdirs.user_data_dir()
|
datadir = appdirs.user_data_dir()
|
||||||
paths = [os.path.join(datadir, pn, 'filestore', dbname) for pn in 'OpenERP Odoo'.split()]
|
paths = [os.path.join(datadir, pn, 'filestore', dbname) for pn in 'OpenERP Odoo'.split()]
|
||||||
run(['rm', '-rf'] + paths)
|
run(['rm', '-rf'] + paths)
|
||||||
|
|
||||||
def pg_createdb(self, cr, uid, dbname):
|
def _local_pg_createdb(self, cr, uid, dbname):
|
||||||
self.pg_dropdb(cr, uid, dbname)
|
self._local_pg_dropdb(cr, uid, dbname)
|
||||||
_logger.debug("createdb %s", dbname)
|
_logger.debug("createdb %s", dbname)
|
||||||
run(['createdb', '--encoding=unicode', '--lc-collate=C', '--template=template0', dbname])
|
with local_pgadmin_cursor() as local_cr:
|
||||||
|
local_cr.execute("""CREATE DATABASE "%s" TEMPLATE template0 LC_COLLATE 'C' ENCODING 'unicode'""" % dbname)
|
||||||
|
|
||||||
def cmd(self, cr, uid, ids, context=None):
|
def cmd(self, cr, uid, ids, context=None):
|
||||||
"""Return a list describing the command to start the build"""
|
"""Return a list describing the command to start the build"""
|
||||||
@ -906,7 +920,7 @@ class runbot_build(osv.osv):
|
|||||||
def job_10_test_base(self, cr, uid, build, lock_path, log_path):
|
def job_10_test_base(self, cr, uid, build, lock_path, log_path):
|
||||||
build._log('test_base', 'Start test base module')
|
build._log('test_base', 'Start test base module')
|
||||||
# run base test
|
# run base test
|
||||||
self.pg_createdb(cr, uid, "%s-base" % build.dest)
|
self._local_pg_createdb(cr, uid, "%s-base" % build.dest)
|
||||||
cmd, mods = build.cmd()
|
cmd, mods = build.cmd()
|
||||||
if grep(build.server("tools/config.py"), "test-enable"):
|
if grep(build.server("tools/config.py"), "test-enable"):
|
||||||
cmd.append("--test-enable")
|
cmd.append("--test-enable")
|
||||||
@ -915,7 +929,7 @@ class runbot_build(osv.osv):
|
|||||||
|
|
||||||
def job_20_test_all(self, cr, uid, build, lock_path, log_path):
|
def job_20_test_all(self, cr, uid, build, lock_path, log_path):
|
||||||
build._log('test_all', 'Start test all modules')
|
build._log('test_all', 'Start test all modules')
|
||||||
self.pg_createdb(cr, uid, "%s-all" % build.dest)
|
self._local_pg_createdb(cr, uid, "%s-all" % build.dest)
|
||||||
cmd, mods = build.cmd()
|
cmd, mods = build.cmd()
|
||||||
if grep(build.server("tools/config.py"), "test-enable"):
|
if grep(build.server("tools/config.py"), "test-enable"):
|
||||||
cmd.append("--test-enable")
|
cmd.append("--test-enable")
|
||||||
@ -1075,7 +1089,7 @@ class runbot_build(osv.osv):
|
|||||||
|
|
||||||
# cleanup only needed if it was not killed
|
# cleanup only needed if it was not killed
|
||||||
if build.state == 'done':
|
if build.state == 'done':
|
||||||
build.cleanup()
|
build._local_cleanup()
|
||||||
|
|
||||||
def skip(self, cr, uid, ids, context=None):
|
def skip(self, cr, uid, ids, context=None):
|
||||||
self.write(cr, uid, ids, {'state': 'done', 'result': 'skipped'}, context=context)
|
self.write(cr, uid, ids, {'state': 'done', 'result': 'skipped'}, context=context)
|
||||||
@ -1083,16 +1097,19 @@ class runbot_build(osv.osv):
|
|||||||
if len(to_unduplicate):
|
if len(to_unduplicate):
|
||||||
self.force(cr, uid, to_unduplicate, context=context)
|
self.force(cr, uid, to_unduplicate, context=context)
|
||||||
|
|
||||||
def cleanup(self, cr, uid, ids, context=None):
|
def _local_cleanup(self, cr, uid, ids, context=None):
|
||||||
for build in self.browse(cr, uid, ids, context=context):
|
for build in self.browse(cr, uid, ids, context=context):
|
||||||
cr.execute("""
|
# Cleanup the *local* cluster
|
||||||
SELECT datname
|
with local_pgadmin_cursor() as local_cr:
|
||||||
FROM pg_database
|
local_cr.execute("""
|
||||||
WHERE pg_get_userbyid(datdba) = current_user
|
SELECT datname
|
||||||
AND datname LIKE %s
|
FROM pg_database
|
||||||
""", [build.dest + '%'])
|
WHERE pg_get_userbyid(datdba) = current_user
|
||||||
for db, in cr.fetchall():
|
AND datname LIKE %s
|
||||||
self.pg_dropdb(cr, uid, db)
|
""", [build.dest + '%'])
|
||||||
|
to_delete = local_cr.fetchall()
|
||||||
|
for db, in to_delete:
|
||||||
|
self._local_pg_dropdb(cr, uid, db)
|
||||||
|
|
||||||
if os.path.isdir(build.path()) and build.result != 'killed':
|
if os.path.isdir(build.path()) and build.result != 'killed':
|
||||||
shutil.rmtree(build.path())
|
shutil.rmtree(build.path())
|
||||||
@ -1111,7 +1128,7 @@ class runbot_build(osv.osv):
|
|||||||
build.write(v)
|
build.write(v)
|
||||||
cr.commit()
|
cr.commit()
|
||||||
build.github_status()
|
build.github_status()
|
||||||
build.cleanup()
|
build._local_cleanup()
|
||||||
|
|
||||||
def reap(self, cr, uid, ids):
|
def reap(self, cr, uid, ids):
|
||||||
while True:
|
while True:
|
||||||
|
Loading…
Reference in New Issue
Block a user