From fb2ed99dcf976e88e29c098b6c3685bd485a19e0 Mon Sep 17 00:00:00 2001 From: Dan Kirkwood Date: Fri, 14 Apr 2017 11:23:54 -0600 Subject: [PATCH 1/4] reset and seed instead of setup --- traffic_ops/install/bin/_postinstall | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/traffic_ops/install/bin/_postinstall b/traffic_ops/install/bin/_postinstall index 7a8f50a50c..751f78e186 100755 --- a/traffic_ops/install/bin/_postinstall +++ b/traffic_ops/install/bin/_postinstall @@ -588,7 +588,11 @@ sub setupDatabase { $ENV{PGUSER} = $todbconf->{"dbAdminUser"}; $ENV{PGPASSWORD} = $todbconf->{"dbAdminPw"}; chdir("/opt/traffic_ops/app"); - my $result = InstallUtils::execCommand( "/usr/bin/perl", "db/admin.pl", "--env=production", "setup" ); + my $result = InstallUtils::execCommand( "/usr/bin/perl", "db/admin.pl", "--env=production", "reset" ); + + if ( $result == 0 ) { + $result = InstallUtils::execCommand( "/usr/bin/perl", "db/admin.pl", "--env=production", "seed" ); + } if ( $result != 0 ) { errorOut("Database initialization failed"); From df4e4e3b007f170a118ac7bc973d7d78fce3273a Mon Sep 17 00:00:00 2001 From: Dan Kirkwood Date: Mon, 17 Apr 2017 11:21:06 -0600 Subject: [PATCH 2/4] revert execCommand -- need return status --- traffic_ops/install/lib/InstallUtils.pm | 42 +++---------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/traffic_ops/install/lib/InstallUtils.pm b/traffic_ops/install/lib/InstallUtils.pm index 1b9cf725e4..040a8e9d11 100644 --- a/traffic_ops/install/lib/InstallUtils.pm +++ b/traffic_ops/install/lib/InstallUtils.pm @@ -50,44 +50,10 @@ sub initLogger { } sub execCommand { - my ( $command, @args ) = @_; - - my $pipe = IO::Pipe->new; - my $pid; - my $result = 0; - my $customLog = ""; - - # find log file in args and remove if found - # if there is a string in the list of args which starts with 'pi_custom_log=' then remove it from the parameters and use - # it as the log file for the exec - foreach my $var (@args) { - if ( index( $var, "pi_custom_log=" ) != -1 ) { - $customLog = ( split( /=/, $var ) )[1]; - splice( @args, index( $var, "pi_custom_log=" ), 1 ); - logger( "Using custom log '$customLog'", "info" ); - } - } - - # create pipe between child and parent and redirect output from child to parent for logging - my $child = open READER, '-|'; - defined $child or die "pipe/fork: $!\n"; - if ($child) { #parent - while ( $line = ) { - - # log all output from child pipe - if ( $customLog ne "" ) { - logger( $line, "info", $customLog ); - } - else { - logger( $line, "info" ); - } - } - } - else { #child - # redirect stderr to stdout so parent can read - open STDERR, '>&STDOUT'; - exec( $command, @args ) or exit(1); - } + my ( $cmd, @args ) = @_; + system( $cmd, @args ); + my $result = $? >> 8; + return $result; } # log the error and then kill the process From 17b6e07b7540dd6ede74c6fafeb7759c5200d79d Mon Sep 17 00:00:00 2001 From: Dan Kirkwood Date: Mon, 17 Apr 2017 12:57:41 -0600 Subject: [PATCH 3/4] fix psql options --- traffic_ops/app/db/admin.pl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/traffic_ops/app/db/admin.pl b/traffic_ops/app/db/admin.pl index baf9b9bfb3..de383940e1 100755 --- a/traffic_ops/app/db/admin.pl +++ b/traffic_ops/app/db/admin.pl @@ -195,7 +195,7 @@ sub load_schema { sub dropdb { print "Dropping database: $db_name\n"; - if ( system("dropdb -h $host_ip -p $host_port -U $db_user -e --if-exists $db_name;") != 0 ) { + if ( system("dropdb -h $host_ip -p $host_port -U $db_super_user -e --if-exists $db_name;") != 0 ) { die "Can't drop db $db_name\n"; } } @@ -206,7 +206,7 @@ sub createdb { print "Database $db_name already exists\n"; return; } - my $cmd = "createdb -h $host_ip -p $host_port -U $db_super_user --owner $db_user $db_name;"; + my $cmd = "createdb -h $host_ip -p $host_port -U $db_super_user -e --owner $db_user $db_name;"; if ( system($cmd) != 0 ) { die "Can't create db $db_name\n"; } @@ -219,20 +219,20 @@ sub create_user { if (!$user_exists) { my $cmd = "CREATE USER $db_user WITH LOGIN ENCRYPTED PASSWORD '$db_password'"; - if ( system(qq{psql -h $host_ip -p $host_port -U $db_super_user -tAc "$cmd"}) != 0 ) { + if ( system(qq{psql -h $host_ip -p $host_port -U $db_super_user -etAc "$cmd"}) != 0 ) { die "Can't create user $db_user\n"; } } } sub drop_user { - if ( system("dropuser -h $host_ip -p $host_port -i -e $db_user;") != 0 ) { + if ( system("dropuser -h $host_ip -p $host_port -U $db_super_user-i -e $db_user;") != 0 ) { die "Can't drop user $db_user\n"; } } sub show_users { - if ( system("psql -h $host_ip -p $host_port -ec '\\du';") != 0 ) { + if ( system("psql -h $host_ip -p $host_port -U $db_super_user -ec '\\du';") != 0 ) { die "Can't show users"; } } From 8e2df91687bce78607537da9a61822d9705f4074 Mon Sep 17 00:00:00 2001 From: Dan Kirkwood Date: Mon, 17 Apr 2017 15:05:17 -0600 Subject: [PATCH 4/4] always run psql as superuser -- root doesn't have access to .pgpass --- traffic_ops/app/db/admin.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traffic_ops/app/db/admin.pl b/traffic_ops/app/db/admin.pl index de383940e1..aa181e86da 100755 --- a/traffic_ops/app/db/admin.pl +++ b/traffic_ops/app/db/admin.pl @@ -181,14 +181,14 @@ sub migrate { sub seed { print "Seeding database.\n"; - if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_user -e < db/seeds.sql") != 0 ) { + if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_super_user -e < db/seeds.sql") != 0 ) { die "Can't seed database\n"; } } sub load_schema { print "Creating database tables.\n"; - if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_user -e < db/create_tables.sql") != 0 ) { + if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_super_user -e < db/create_tables.sql") != 0 ) { die "Can't create database tables\n"; } }