TT#15857 Fix DBI errors output

The DBI connect call can be specified the PrintError option, which will
make the function print the contents of the $DBI:errstr, but then we do
that again, which results in redudant output, so we disable this option.

The $DBI::errstr variable contains an error string that is *not*
terminated with a newline. The perl die built-in has magic behavior
when the string it gets does not end in a newline character, it will
then emit the source line, file name and a newline where the error
happened, which is in most cases not what we want. We just avoid
this by adding an explicit newline.

Replace a print + exit with a die, and a printf without a terminating
newline which would mess up further output with a warn call with an
explicit newline, which will also make it go to stderr.

And remove the local PrintError overrides which are now unnecessary.

Change-Id: I348b8cc676da7f255c6c823de2a774694dbca826
changes/20/34620/2
Guillem Jover 7 years ago
parent 1054cb4d74
commit f8f3d9e8b2

@ -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(<<SQL);
SELECT name FROM ngcp.timezone
@ -178,7 +175,6 @@ SQL
print $@;
$ok = 0;
}
$dbh->{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(<<SQL)
SELECT version
FROM ngcp.tzinfo_version
@ -240,7 +234,6 @@ SQL
};
local $RS = $p_rs;
$err = $EVAL_ERROR;
$dbh->{PrintError} = $pe;
if ($err) {
print $err,"sql: $sql\n";
$dbh->rollback();

@ -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 {

@ -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(<<SQL)
SELECT Host FROM mysql.user WHERE User = ? AND Host LIKE ?
SQL
or die "Cannot prepare: ".$DBI::errstr;
or die "Cannot prepare: $DBI::errstr\n";
my $rc = 0;
@ -262,10 +262,10 @@ SQL
foreach my $host_rx (@$ref) {
$host_rx =~ s/\*/%/g;
$ch_sel->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) {

Loading…
Cancel
Save