Cleanup of custom mysqladmin command code

Fixes #30.
This commit is contained in:
Major Hayden 2014-02-21 11:32:49 -06:00
parent 7b0664bbbf
commit 18eed21ecd

View file

@ -101,9 +101,7 @@ sub usage {
" --port <port> Port to use for connection (default: 3306)\n". " --port <port> Port to use for connection (default: 3306)\n".
" --user <username> Username to use for authentication\n". " --user <username> Username to use for authentication\n".
" --pass <password> Password to use for authentication\n". " --pass <password> Password to use for authentication\n".
" Database type\n". " --mysqladmin <path> Path to a custom mysqladmin executable\n".
" --mysql Run with mysql database (default)\n".
" --maria Run with mariadb database\n".
"\n". "\n".
" Performance and Reporting Options\n". " Performance and Reporting Options\n".
" --skipsize Don't enumerate tables and their types/sizes (default: on)\n". " --skipsize Don't enumerate tables and their types/sizes (default: on)\n".
@ -253,13 +251,18 @@ my ($mysqllogin,$doremote,$remotestring);
sub mysql_setup { sub mysql_setup {
$doremote = 0; $doremote = 0;
$remotestring = ''; $remotestring = '';
my $command = `which mysqladmin`; my $mysqladmincmd;
chomp($command); if ($opt{mysqladmin}) {
if ( $opt{mysqladmin} ) { $mysqladmincmd = $opt{mysqladmin};
$command = $opt{mysqladmin}; } else {
$mysqladmincmd = `which mysqladmin`;
} }
elsif (! -e $command ) { chomp($mysqladmincmd);
badprint "Unable to find mysqladmin in your \$PATH. Is MySQL installed?\n"; if (! -e $mysqladmincmd && $opt{mysqladmin}) {
badprint "Unable to find the mysqladmin command you specified: ".$mysqladmincmd."\n";
exit;
} elsif (! -e $mysqladmincmd) {
badprint "Couldn't find mysqladmin in your \$PATH. Is MySQL installed?\n";
exit; exit;
} }
@ -284,7 +287,7 @@ sub mysql_setup {
# Did we already get a username and password passed on the command line? # Did we already get a username and password passed on the command line?
if ($opt{user} ne 0 and $opt{pass} ne 0) { if ($opt{user} ne 0 and $opt{pass} ne 0) {
$mysqllogin = "-u $opt{user} -p'$opt{pass}'".$remotestring; $mysqllogin = "-u $opt{user} -p'$opt{pass}'".$remotestring;
my $loginstatus = `$command ping $mysqllogin 2>&1`; my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
if ($loginstatus =~ /mysqld is alive/) { if ($loginstatus =~ /mysqld is alive/) {
goodprint "Logged in using credentials passed on the command line\n"; goodprint "Logged in using credentials passed on the command line\n";
return 1; return 1;
@ -296,7 +299,7 @@ sub mysql_setup {
if ( -r "/etc/psa/.psa.shadow" and $doremote == 0 ) { if ( -r "/etc/psa/.psa.shadow" and $doremote == 0 ) {
# It's a Plesk box, use the available credentials # It's a Plesk box, use the available credentials
$mysqllogin = "-u admin -p`cat /etc/psa/.psa.shadow`"; $mysqllogin = "-u admin -p`cat /etc/psa/.psa.shadow`";
my $loginstatus = `$command ping $mysqllogin 2>&1`; my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
unless ($loginstatus =~ /mysqld is alive/) { unless ($loginstatus =~ /mysqld is alive/) {
badprint "Attempted to use login credentials from Plesk, but they failed.\n"; badprint "Attempted to use login credentials from Plesk, but they failed.\n";
exit 0; exit 0;
@ -304,7 +307,7 @@ sub mysql_setup {
} elsif ( -r "/etc/mysql/debian.cnf" and $doremote == 0 ){ } elsif ( -r "/etc/mysql/debian.cnf" and $doremote == 0 ){
# We have a debian maintenance account, use it # We have a debian maintenance account, use it
$mysqllogin = "--defaults-file=/etc/mysql/debian.cnf"; $mysqllogin = "--defaults-file=/etc/mysql/debian.cnf";
my $loginstatus = `$command $mysqllogin ping 2>&1`; my $loginstatus = `$mysqladmincmd $mysqllogin ping 2>&1`;
if ($loginstatus =~ /mysqld is alive/) { if ($loginstatus =~ /mysqld is alive/) {
goodprint "Logged in using credentials from debian maintenance account.\n"; goodprint "Logged in using credentials from debian maintenance account.\n";
return 1; return 1;
@ -314,7 +317,7 @@ sub mysql_setup {
} }
} else { } else {
# It's not Plesk or debian, we should try a login # It's not Plesk or debian, we should try a login
my $loginstatus = `$command $remotestring ping 2>&1`; my $loginstatus = `$mysqladmincmd $remotestring ping 2>&1`;
if ($loginstatus =~ /mysqld is alive/) { if ($loginstatus =~ /mysqld is alive/) {
# Login went just fine # Login went just fine
$mysqllogin = " $remotestring "; $mysqllogin = " $remotestring ";
@ -341,7 +344,7 @@ sub mysql_setup {
$mysqllogin .= " -p'$password'"; $mysqllogin .= " -p'$password'";
} }
$mysqllogin .= $remotestring; $mysqllogin .= $remotestring;
my $loginstatus = `$command ping $mysqllogin 2>&1`; my $loginstatus = `$mysqladmincmd ping $mysqllogin 2>&1`;
if ($loginstatus =~ /mysqld is alive/) { if ($loginstatus =~ /mysqld is alive/) {
print STDERR "\n"; print STDERR "\n";
if (! length($password)) { if (! length($password)) {