diff --git a/helper/sync-db b/helper/sync-db index 95766be9..438cc2e7 100755 --- a/helper/sync-db +++ b/helper/sync-db @@ -64,10 +64,9 @@ sub db_connect { my $dbh = DBI->connect("DBI:mysql:database=${dbname};host=${dbhost};port=${dbport};" . "mysql_read_default_file=${dbcredentials}", - "", "", { PrintError => 1 }); + "", "", { PrintError => 0 }); unless (defined $dbh) { - print "Error: Could not connect to database '$dbname' at '$dbhost:$dbport' using '${dbcredentials}': $DBI::errstr\n"; - exit 1; + die "Error: Could not connect to database '$dbname' at '$dbhost:$dbport' using '${dbcredentials}': $DBI::errstr\n"; } return $dbh; } @@ -148,14 +147,12 @@ sub sync_general_timezone { my $dbh = shift; my $tz = shift; my $ok = 1; - my $pe = $dbh->{PrintError}; my ($sql_log_bin) = $dbh->selectrow_array('SELECT @@sql_log_bin'); eval { die "Error: general.timezone value is not set\n" unless $tz; $dbh->do("SET sql_log_bin=0"); - $dbh->{PrintError} = 0; my ($current_tz) = $dbh->selectrow_array(<{PrintError} = $pe; $dbh->do("SET sql_log_bin=$sql_log_bin"); return $ok; @@ -206,12 +202,10 @@ sub sync_db_timezones { } chomp $tzinfo_version; - my $pe = $dbh->{PrintError}; - $dbh->{PrintError} = 0; my $sql = ''; my $p_rs = $RS; eval { - $dbh->begin_work() or die "Cannot start tx: ".$DBI::errstr; + $dbh->begin_work() or die "Cannot start tx: $DBI::errstr\n"; my ($cur_tzinfo_version) = $dbh->selectrow_array(<{PrintError} = $pe; if ($err) { print $err,"sql: $sql\n"; $dbh->rollback(); diff --git a/sbin/ngcp-sync-constants b/sbin/ngcp-sync-constants index 3db587c8..5b4bee71 100755 --- a/sbin/ngcp-sync-constants +++ b/sbin/ngcp-sync-constants @@ -99,16 +99,16 @@ sub connect_db { $dsn .= ";mysql_read_default_file=$SIPWISE_EXTRA_CNF"; } - if ( $dbh = DBI->connect($dsn, $dbuser, $dbpass, { PrintError => 1 }) ) { + if ($dbh = DBI->connect($dsn, $dbuser, $dbpass, { PrintError => 0 })) { log_debug("connected to $dbhost:$dbport using $dbauthmsg"); } else { - printf "Can't connect to MySQL database 'mysql' using $dbauthmsg.\n". $DBI::errstr; + warn "Cannot connect to MySQL database 'mysql' using $dbauthmsg: $DBI::errstr\n"; if ($mysql_root) { printf "\nPlease provide valid password for MariaDB user '$dbuser': "; $dbpass = prompt('', -echo=>'*', -v, -hNONE); - $dbh = DBI->connect($dsn, $dbuser, $dbpass, { PrintError => 1 }) - or die "Can't connect to MySQL database 'mysql' using $dbauthmsg with provided password.\n". $DBI::errstr; + $dbh = DBI->connect($dsn, $dbuser, $dbpass, { PrintError => 0 }) + or die "Cannot connect to MySQL database 'mysql' using $dbauthmsg with provided password: $DBI::errstr\n"; log_debug("connected to $dbhost:$dbport using $dbauthmsg with provided password."); } else { exit 1; @@ -116,7 +116,7 @@ sub connect_db { } $dbh->do("SET sql_log_bin=0") - or die "Cannot set sql_log_bin=0: ".$DBI::errstr; + or die "Cannot set sql_log_bin=0: $DBI::errstr\n"; return; } @@ -148,9 +148,9 @@ sub adjust_replication_master_info { my ($user, $pass) = @_; my $ch = $dbh->prepare("show slave status") - or die "Cannot prepare: ".$DBI::errstr; + or die "Cannot prepare: $DBI::errstr\n"; - $ch->execute() or die "Cannot execute: ".$DBI::errstr; + $ch->execute() or die "Cannot execute: $DBI::errstr\n"; my $fields = $ch->{NAME}; my $vals = $ch->fetchall_arrayref(); @@ -164,14 +164,14 @@ sub adjust_replication_master_info { if (defined $data{Master_Server_Id}) { $dbh->do("STOP SLAVE"); - die "Cannot stop slave: ".$DBI::errstr if $DBI::err; + die "Cannot stop slave: $DBI::errstr\n" if $DBI::err; log_info("* updating replication password for user $user"); $dbh->do("CHANGE MASTER TO MASTER_PASSWORD=?", undef, $pass); $dbh->do("START SLAVE"); - die "Cannot start slave: ".$DBI::errstr if $DBI::err; + die "Cannot start slave: $DBI::errstr\n" if $DBI::err; } return; @@ -197,7 +197,7 @@ UPDATE user SQL $sth_sel->execute($pass, $user) - or die "Cannot execute: ".$DBI::errstr; + or die "Cannot execute: $DBI::errstr\n"; my ($count, $match) = $sth_sel->fetchrow_array(); $count //= 0; @@ -209,7 +209,7 @@ SQL } unless ($test_mode) { my $rows = $sth_upd->execute($pass, $user, $pass) - or die "Cannot update: ".$DBI::errstr; + or die "Cannot update: $DBI::errstr\n"; if ($rows != $count) { log_warn(sprintf "User update was supposed to affect %d rows but changed %d", @@ -258,7 +258,7 @@ sub flush_privs { return if $test_mode; log_debug("flush priveleges"); $dbh->do('FLUSH PRIVILEGES') - or die "Can't flush MySQL privileges: ". $DBI::errstr; + or die "Cannot flush MySQL privileges: $DBI::errstr\n"; return; } @@ -288,7 +288,7 @@ sub main { connect_db($dbhost, $dbport); - $dbh->begin_work or die "Cannot start transaction: ".$DBI::errstr; + $dbh->begin_work or die "Cannot start transaction: $DBI::errstr\n"; eval { diff --git a/sbin/ngcp-sync-grants b/sbin/ngcp-sync-grants index 9acad7b8..b4a31ca1 100755 --- a/sbin/ngcp-sync-grants +++ b/sbin/ngcp-sync-grants @@ -67,10 +67,10 @@ sub connect_db { . "mysql_read_default_file=${dbcredentials}", "", "", { PrintError => 0 }) - or die "Can't connect to MySQL database 'mysql': ". $DBI::errstr; + or die "Can't connect to MySQL database 'mysql': $DBI::errstr\n"; log_debug("connected to $dbhost:$dbport using '${dbcredentials}'"); $dbh->do("SET sql_log_bin=0") - or die "Cannot set sql_log_bin=0: ".$DBI::errstr; + or die "Cannot set sql_log_bin=0: $DBI::errstr\n"; return; } @@ -167,7 +167,7 @@ sub apply_grants { $new_user = 1; } elsif ($DBI::errstr !~ /There is no such grant defined/) { print "USER: $user HOST: $host\n"; - die sprintf "Cannot revoke privileges from %s\@%s: %s", + die sprintf "Cannot revoke privileges from %s\@%s: %s\n", $user, $host, $DBI::errstr; } } @@ -181,7 +181,7 @@ sub apply_grants { $dbh->do("GRANT $s_grant TO '$user'\@'$host' $s_suffix"); if ($DBI::errstr && $DBI::errstr !~ /Table\s+'\S+\.\S+'\s+doesn't\s+exist/) { - die "Cannot grant privileges: ".$DBI::errstr; + die "Cannot grant privileges: $DBI::errstr\n"; } elsif ($DBI::errstr) { $log_offset = 0; log_warn("Cannot apply grant: ".$DBI::errstr); @@ -247,7 +247,7 @@ sub apply_drop_users { my $ch_sel = $dbh->prepare(<execute($user, $host_rx) - or die "Cannot select user $user -- $host_rx: ".$DBI::errstr; + or die "Cannot select user $user -- $host_rx: $DBI::errstr\n"; while (my ($host) = $ch_sel->fetchrow_array) { $dbh->do("DROP USER '$user'\@'$host'") - or die "Cannot drop user $user -- $host: ".$DBI::errstr; + or die "Cannot drop user $user -- $host: $DBI::errstr\n"; log_info(sprintf "drop: %s\@%s", $user, $host); $rc++; } @@ -281,7 +281,7 @@ sub set_user_protected_password { my ($user, $host) = @_; my ($random_pass) = $dbh->selectrow_array("SELECT PASSWORD(?)", undef, pwgen()); - die "Cannot generate password: ".$DBI::errstr if $DBI::err; + die "Cannot generate password: $DBI::errstr\n" if $DBI::err; unless ($random_pass =~ /^\*(\S+)\s*$/) { die "Cannot parse generated password: $random_pass"; } @@ -293,16 +293,16 @@ SELECT COUNT(User) WHERE User = ? AND Host = ? SQL - die "Cannot select grant temp user: ".$DBI::errstr if $DBI::err; + die "Cannot select grant temp user: $DBI::errstr\n" if $DBI::err; unless ($temp_user_count) { $dbh->do("CREATE USER '$user'\@'$host'"); - die "Cannot create grant temp user: ".$DBI::errstr if $DBI::err; + die "Cannot create grant temp user: $DBI::errstr\n" if $DBI::err; } $dbh->do("UPDATE user SET Password = ? WHERE User = ? AND Host = ?", undef, $random_pass, $user, $host); - die sprintf "Cannot update %s@%s with generated password, %s", + die sprintf "Cannot update %s@%s with generated password, %s\n", $user, $host, $DBI::errstr if $DBI::err; return; @@ -334,7 +334,7 @@ sub grants_helper { $dbh->do("GRANT $s_grant TO '$user'\@'$host' $s_suffix"); if ($DBI::errstr && $DBI::errstr !~ /Table\s+'\S+\.\S+'\s+doesn't\s+exist/) { - die "Cannot grant privileges: ".$DBI::errstr; + die "Cannot grant privileges: $DBI::errstr\n"; } elsif ($DBI::errstr) { $log_offset = 0; log_warn("Cannot apply grant: ".$DBI::errstr); @@ -355,7 +355,7 @@ sub grants_helper { die "Error in checking grants" if $#$temp_grants < 0; $dbh->do("DROP USER '$user'\@'$host'"); - die "Cannot drop grant temp user: ".$DBI::errstr if $DBI::err; + die "Cannot drop grant temp user: $DBI::errstr\n" if $DBI::err; flush_privs(); @$grants = ( map { $_->[0] } @$temp_grants ); @@ -404,7 +404,7 @@ sub check_grants { if ($DBI::errstr && ($DBI::errstr !~ /There is no such grant defined/ && $DBI::errstr !~ /fetch[()]+ without execute[()]+/)) { - die sprintf "Cannot select grants for %s\@%s: %s", + die sprintf "Cannot select grants for %s\@%s: %s\n", $user, $host, $DBI::errstr; } @@ -440,7 +440,7 @@ sub check_grants { sub flush_privs { log_debug("flush privileges"); $dbh->do("FLUSH PRIVILEGES") - or die "Cannot flush privileges: ".$DBI::errstr; + or die "Cannot flush privileges: $DBI::errstr\n"; return; } @@ -459,7 +459,7 @@ sub main { connect_db($dbhost, $dbport); - $dbh->begin_work or die "Cannot start transaction: ".$DBI::errstr; + $dbh->begin_work or die "Cannot start transaction: $DBI::errstr\n"; eval { my $rc = 0; @@ -489,7 +489,7 @@ sub main { die "Error: $@"; } - $dbh->commit or die "Cannot commit transaction: ".$DBI::errstr; + $dbh->commit or die "Cannot commit transaction: $DBI::errstr\n"; $dbh->disconnect; if ($recreate_user) {