[server] set PGPASSWORD when running pg commands for backup and restore
For db-dump (implied by upgrade) and db-restore with postgres with non-trusted
authentication, the db password is asked by pg tools (pg_dump, pg_restore,
createdb, dropdb).
Avoid this behavior by setting the PGPASSWORD environment variable when
database driver is postgres and db-password is set in sources file.
--- a/cubicweb/server/sqlutils.py Thu Mar 22 14:17:40 2018 +0100
+++ b/cubicweb/server/sqlutils.py Thu Mar 22 14:45:11 2018 +0100
@@ -19,6 +19,7 @@
from __future__ import print_function
+import os
import sys
import re
import subprocess
@@ -48,13 +49,16 @@
SQL_PREFIX = 'cw_'
-def _run_command(cmd):
+def _run_command(cmd, extra_env=None):
+ env = os.environ.copy()
+ for key, value in (extra_env or {}).items():
+ env.setdefault(key, value)
if isinstance(cmd, string_types):
print(cmd)
- return subprocess.call(cmd, shell=True)
+ return subprocess.call(cmd, shell=True, env=env)
else:
print(' '.join(cmd))
- return subprocess.call(cmd)
+ return subprocess.call(cmd, env=env)
def sqlexec(sqlstmts, cursor_or_execute, withpb=True,
@@ -342,18 +346,25 @@
"""open and return a connection to the database"""
return self.dbhelper.get_connection()
+ def _backup_restore_env(self):
+ if (self.config['db-driver'] == 'postgres'
+ and self.config['db-password'] is not None):
+ return {'PGPASSWORD': self.config['db-password']}
+
def backup_to_file(self, backupfile, confirm):
+ extra_env = self._backup_restore_env()
for cmd in self.dbhelper.backup_commands(backupfile,
keepownership=False):
- if _run_command(cmd):
+ if _run_command(cmd, extra_env=extra_env):
if not confirm(' [Failed] Continue anyway?', default='n'):
raise Exception('Failed command: %s' % cmd)
def restore_from_file(self, backupfile, confirm, drop=True):
+ extra_env = self._backup_restore_env()
for cmd in self.dbhelper.restore_commands(backupfile,
keepownership=False,
drop=drop):
- if _run_command(cmd):
+ if _run_command(cmd, extra_env=extra_env):
if not confirm(' [Failed] Continue anyway?', default='n'):
raise Exception('Failed command: %s' % cmd)