[pmg-devel] [PATCH pmg-api v2 1/3] move postgres_admin_command into PMG::Utils

Stoiko Ivanov s.ivanov at proxmox.com
Thu Aug 8 15:36:09 CEST 2019


Take postgres_admin_command from PMG::DBTools and put it in PMG::Utils.
This avoids a cyclic dependency (PMG::DBTools calls
PMG::Config::rewrite_postfix_whitelist when reloading the RuleDB, and
PMG::Config needs access to the Postgres major version for rendering
the postgresql.conf)

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PMG/Backup.pm  |  3 ++-
 src/PMG/DBTools.pm | 18 +-----------------
 src/PMG/Utils.pm   | 23 +++++++++++++++++++++++
 3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
index 9114d2b..11eff5f 100644
--- a/src/PMG/Backup.pm
+++ b/src/PMG/Backup.pm
@@ -10,6 +10,7 @@ use PVE::Tools;
 
 use PMG::pmgcfg;
 use PMG::AtomicFile;
+use PMG::Utils qw(postgres_admin_cmd);
 
 my $sa_custom_config_fn = "/etc/mail/spamassassin/custom.cf";
 
@@ -286,7 +287,7 @@ sub pmg_restore {
 	    }
 
 	    print STDERR "run analyze to speed up database queries\n";
-	    PMG::DBTools::postgres_admin_cmd('psql', { input => 'analyze;' }, $dbname);
+	    postgres_admin_cmd('psql', { input => 'analyze;' }, $dbname);
 
 	    print "Analyzing/Upgrading existing Databases...";
 	    my $ruledb = PMG::RuleDB->new($dbh);
diff --git a/src/PMG/DBTools.pm b/src/PMG/DBTools.pm
index a01f07c..c675e2f 100644
--- a/src/PMG/DBTools.pm
+++ b/src/PMG/DBTools.pm
@@ -15,6 +15,7 @@ use PMG::Utils;
 use PMG::RuleDB;
 use PMG::MailQueue;
 use PMG::Config;
+use PMG::Utils qw(postgres_admin_cmd);
 
 our $default_db_name = "Proxmox_ruledb";
 
@@ -76,23 +77,6 @@ sub open_ruledb {
     }
 }
 
-sub postgres_admin_cmd {
-    my ($cmd, $options, @params) = @_;
-
-    $cmd = ref($cmd) ? $cmd : [ $cmd ];
-
-    my $save_uid = POSIX::getuid();
-    my $pg_uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
-
-    PVE::Tools::setresuid(-1, $pg_uid, -1) ||
-	die "setresuid postgres ($pg_uid) failed - $!\n";
-
-    PVE::Tools::run_command([@$cmd, '-U', 'postgres', @params], %$options);
-
-    PVE::Tools::setresuid(-1, $save_uid, -1) ||
-	die "setresuid back failed - $!\n";
-}
-
 sub delete_ruledb {
     my ($dbname) = @_;
 
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index aa6aac7..7606a89 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -37,6 +37,12 @@ use PMG::AtomicFile;
 use PMG::MailQueue;
 use PMG::SMTPPrinter;
 
+use base 'Exporter';
+
+our @EXPORT_OK = qw(
+postgres_admin_cmd
+);
+
 my $valid_pmg_realms = ['pam', 'pmg', 'quarantine'];
 
 PVE::JSONSchema::register_standard_option('realm', {
@@ -1378,4 +1384,21 @@ sub cond_add_default_locale {
     system("dpkg-reconfigure locales -f noninteractive");
 }
 
+sub postgres_admin_cmd {
+    my ($cmd, $options, @params) = @_;
+
+    $cmd = ref($cmd) ? $cmd : [ $cmd ];
+
+    my $save_uid = POSIX::getuid();
+    my $pg_uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
+
+    PVE::Tools::setresuid(-1, $pg_uid, -1) ||
+	die "setresuid postgres ($pg_uid) failed - $!\n";
+
+    PVE::Tools::run_command([@$cmd, '-U', 'postgres', @params], %$options);
+
+    PVE::Tools::setresuid(-1, $save_uid, -1) ||
+	die "setresuid back failed - $!\n";
+}
+
 1;
-- 
2.20.1




More information about the pmg-devel mailing list