[FIX] correct usage of pg var env & use Odoo config in priority
1. remove code that forces the usage of database user to current user
2. remove code that forces database host to localhost on Windows
3. remove unused code that generate pgpass file
4. when using pg_dump or pg_restore, force pg environment variables to
Odoo database configuration
Closes #5911
This commit is contained in:
@@ -75,10 +75,10 @@ def report_configuration():
|
||||
config = openerp.tools.config
|
||||
_logger.info("OpenERP version %s", __version__)
|
||||
_logger.info('addons paths: %s', openerp.modules.module.ad_paths)
|
||||
if config['db_host']:
|
||||
_logger.info('database: %s@%s:%s', config['db_user'], config['db_host'], config['db_port'] or '5432')
|
||||
else:
|
||||
_logger.info('database: %s@unix-socket', config['db_user'])
|
||||
host = config['db_host'] or os.environ.get('PGHOST', 'default')
|
||||
port = config['db_port'] or os.environ.get('PGPORT', 'default')
|
||||
user = config['db_user'] or os.environ.get('PGUSER', 'default')
|
||||
_logger.info('database: %s@%s:%s', user, host, port)
|
||||
|
||||
def rm_pid_file():
|
||||
config = openerp.tools.config
|
||||
|
||||
@@ -174,12 +174,6 @@ def dump_db(db_name, stream, backup_format='zip'):
|
||||
_logger.info('DUMP DB: %s format %s', db_name, backup_format)
|
||||
|
||||
cmd = ['pg_dump', '--no-owner']
|
||||
if openerp.tools.config['db_user']:
|
||||
cmd.append('--username=' + openerp.tools.config['db_user'])
|
||||
if openerp.tools.config['db_host']:
|
||||
cmd.append('--host=' + openerp.tools.config['db_host'])
|
||||
if openerp.tools.config['db_port']:
|
||||
cmd.append('--port=' + str(openerp.tools.config['db_port']))
|
||||
cmd.append(db_name)
|
||||
|
||||
if backup_format == 'zip':
|
||||
@@ -248,12 +242,6 @@ def restore_db(db, dump_file, copy=False):
|
||||
pg_args = ['--no-owner', dump_file]
|
||||
|
||||
args = []
|
||||
if openerp.tools.config['db_user']:
|
||||
args.append('--username=' + openerp.tools.config['db_user'])
|
||||
if openerp.tools.config['db_host']:
|
||||
args.append('--host=' + openerp.tools.config['db_host'])
|
||||
if openerp.tools.config['db_port']:
|
||||
args.append('--port=' + str(openerp.tools.config['db_port']))
|
||||
args.append('--dbname=' + db)
|
||||
pg_args = args + pg_args
|
||||
|
||||
|
||||
@@ -457,21 +457,6 @@ class configmanager(object):
|
||||
if len(self.options['language']) > 5:
|
||||
raise Exception('ERROR: The Lang name must take max 5 chars, Eg: -lfr_BE')
|
||||
|
||||
if not self.options['db_user']:
|
||||
try:
|
||||
import getpass
|
||||
self.options['db_user'] = getpass.getuser()
|
||||
except:
|
||||
self.options['db_user'] = None
|
||||
|
||||
die(not self.options['db_user'], 'ERROR: No user specified for the connection to the database')
|
||||
|
||||
if self.options['db_password']:
|
||||
if sys.platform == 'win32' and not self.options['db_host']:
|
||||
self.options['db_host'] = 'localhost'
|
||||
#if self.options['db_host']:
|
||||
# self._generate_pgpassfile()
|
||||
|
||||
if opt.save:
|
||||
self.save()
|
||||
|
||||
@@ -481,44 +466,6 @@ class configmanager(object):
|
||||
else:
|
||||
openerp.conf.server_wide_modules = ['web','web_kanban']
|
||||
|
||||
def _generate_pgpassfile(self):
|
||||
"""
|
||||
Generate the pgpass file with the parameters from the command line (db_host, db_user,
|
||||
db_password)
|
||||
|
||||
Used because pg_dump and pg_restore can not accept the password on the command line.
|
||||
"""
|
||||
is_win32 = sys.platform == 'win32'
|
||||
if is_win32:
|
||||
filename = os.path.join(os.environ['APPDATA'], 'pgpass.conf')
|
||||
else:
|
||||
filename = os.path.join(os.environ['HOME'], '.pgpass')
|
||||
|
||||
text_to_add = "%(db_host)s:*:*:%(db_user)s:%(db_password)s" % self.options
|
||||
|
||||
if os.path.exists(filename):
|
||||
content = [x.strip() for x in file(filename, 'r').readlines()]
|
||||
if text_to_add in content:
|
||||
return
|
||||
|
||||
fp = file(filename, 'a+')
|
||||
fp.write(text_to_add + "\n")
|
||||
fp.close()
|
||||
|
||||
if is_win32:
|
||||
try:
|
||||
import _winreg
|
||||
except ImportError:
|
||||
_winreg = None
|
||||
x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
|
||||
y = _winreg.OpenKey(x, r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", 0,_winreg.KEY_ALL_ACCESS)
|
||||
_winreg.SetValueEx(y,"PGPASSFILE", 0, _winreg.REG_EXPAND_SZ, filename )
|
||||
_winreg.CloseKey(y)
|
||||
_winreg.CloseKey(x)
|
||||
else:
|
||||
import stat
|
||||
os.chmod(filename, stat.S_IRUSR + stat.S_IWUSR)
|
||||
|
||||
def _is_addons_path(self, path):
|
||||
for f in os.listdir(path):
|
||||
modpath = os.path.join(path, f)
|
||||
|
||||
+13
-3
@@ -103,15 +103,25 @@ def find_pg_tool(name):
|
||||
raise Exception('Command `%s` not found.' % name)
|
||||
|
||||
def exec_pg_environ():
|
||||
""" On systems where pg_restore/pg_dump require an explicit password (i.e.
|
||||
on Windows where TCP sockets are used), it is necessary to pass the
|
||||
"""
|
||||
Force the database PostgreSQL environment variables to the database
|
||||
configuration of Odoo.
|
||||
|
||||
Note: On systems where pg_restore/pg_dump require an explicit password
|
||||
(i.e. on Windows where TCP sockets are used), it is necessary to pass the
|
||||
postgres user password in the PGPASSWORD environment variable or in a
|
||||
special .pgpass file.
|
||||
|
||||
See also http://www.postgresql.org/docs/8.4/static/libpq-envars.html
|
||||
"""
|
||||
env = os.environ.copy()
|
||||
if not env.get('PGPASSWORD') and openerp.tools.config['db_password']:
|
||||
if openerp.tools.config['db_host']:
|
||||
env['PGHOST'] = openerp.tools.config['db_host']
|
||||
if openerp.tools.config['db_port']:
|
||||
env['PGPORT'] = str(openerp.tools.config['db_port'])
|
||||
if openerp.tools.config['db_user']:
|
||||
env['PGUSER'] = openerp.tools.config['db_user']
|
||||
if openerp.tools.config['db_password']:
|
||||
env['PGPASSWORD'] = openerp.tools.config['db_password']
|
||||
return env
|
||||
|
||||
|
||||
Reference in New Issue
Block a user