1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#!/usr/bin/perl -w
use strict;
# this is a wrapper around rt-setup-database to
# get it to run as the regular RT user (from RT_SiteConfig.pm)
# without exposing the password on the command line
# we feed the password into the rt-setup-database stdin
# when it looks like it's asking for it
# everything rt-setup-database says is tee'd to the RT logger
# so that the administrator can find the real output in the
# syslog when this is run from the RTFM maintainer scripts
use Getopt::Std;
use IPC::Open3;
use IO::Select;
use Symbol;
use POSIX ":sys_wait_h";
my %opts;
getopts('v:', \%opts) or usage();
# the version selection parameter is never actually used by the
# rtfm maintainer scripts, but I'm keeping it for possible reuse
# when 3.8 (or whatever) comes out
sub usage {
print STDERR <<EOF;
Usage: $0 [ -v ( 3.4 | 3.6 | 3.8 )] [-- <options for rt-setup-database>]
EOF
exit 1;
}
my $rt_version = "3.8";
if (exists $opts{v}) {
if ($opts{v} =~ /^(3\.[468])$/) {
$rt_version = $1;
} else {
usage();
}
}
unshift @INC, "/usr/local/share/request-tracker${rt_version}/lib";
unshift @INC, "/usr/share/request-tracker${rt_version}/lib";
require RT;
RT::LoadConfig();
my $config = RT->Config;
$config->Set(LogToScreen => 'error');
RT::InitLogging();
my ($child_out, $child_in, $child_err) = ("","",gensym);
my @cmd = (
"/usr/sbin/rt-setup-database-${rt_version}",
"--skip-create",
@ARGV,
);
my $cmd = join(" ", @cmd);
$RT::Logger->notice("running $cmd\n");
my $pid = open3($child_in, $child_out, $child_err, @cmd);
my $s = IO::Select->new;
$s->add($child_out);
$s->add($child_err);
my $password_sent=0;
while ($s->handles and my @ready = $s->can_read(10)) {
for my $fh (@ready) {
my $ret = sysread($fh, my $buf, 1024);
if (not defined $ret) {
die("sysread failed: $!");
}
if ($ret == 0) {
$s->remove($fh);
next;
}
print STDERR $buf if $fh eq $child_err;
$RT::Logger->notice("rt-setup-database-${rt_version}: $buf")
if $fh eq $child_err or $password_sent;
if ($fh == $child_out) {
if ($buf =~ /Password:/ && !$password_sent++) {
print $child_in RT->Config->Get('DatabasePassword') ."\n"
}
}
}
}
my $i = 0;
do {
if (waitpid($pid, WNOHANG) > 0) {
my $ret = $?;
exit $ret >> 8;
}
sleep 1;
} while ($i++ < 5);
|