Changed command line argument -i to -a and interactive mode is now default mode of operation
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/7046baa6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/7046baa6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/7046baa6 Branch: refs/heads/master Commit: 7046baa6c104f2e6b5d7f9d102d4842a2464ffa5 Parents: 9edf9d7 Author: peryder <pery...@cisco.com> Authored: Tue Nov 15 13:56:52 2016 -0500 Committer: Dan Kirkwood <dang...@gmail.com> Committed: Fri Jan 27 09:52:53 2017 -0700 ---------------------------------------------------------------------- traffic_ops/install/bin/postinstall-new | 130 +++++++++++++++++---------- 1 file changed, 84 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/7046baa6/traffic_ops/install/bin/postinstall-new ---------------------------------------------------------------------- diff --git a/traffic_ops/install/bin/postinstall-new b/traffic_ops/install/bin/postinstall-new index abd2170..ce5fe9c 100755 --- a/traffic_ops/install/bin/postinstall-new +++ b/traffic_ops/install/bin/postinstall-new @@ -1,8 +1,10 @@ #!/usr/bin/perl -use lib qw(/opt/traffic_ops/install/lib /opt/traffic_ops/install/lib/perl5 /opt/traffic_ops/app/local/lib/perl5 /opt/traffic_ops/app/lib); -$ENV{PATH} = "/opt/traffic_ops/install/bin:$ENV{PATH}"; -$ENV{PERL5LIB} = "/opt/traffic_ops/install/lib:/opt/traffic_ops/install/lib/perl5:/opt/traffic_ops/app/local/lib/perl5:/opt/traffic_ops/app/lib"; +use lib + qw(/opt/traffic_ops/install/lib /opt/traffic_ops/install/lib/perl5 /opt/traffic_ops/app/local/lib/perl5 /opt/traffic_ops/app/lib); +$ENV{PATH} = "/opt/traffic_ops/install/bin:$ENV{PATH}"; +$ENV{PERL5LIB} = +"/opt/traffic_ops/install/lib:/opt/traffic_ops/install/lib/perl5:/opt/traffic_ops/app/local/lib/perl5:/opt/traffic_ops/app/lib"; use strict; use warnings; @@ -36,7 +38,7 @@ sub getInstallPath { # fileName: The name of the output config file given by the input config file # # Determines an answer to the questions asked. If an input question and answer pair is given, will return the -# answer. If a question is given but no answer, it will prompt the user if interactive mode is enabled, but if +# answer. If a question is given but no answer, it will prompt the user if interactive mode is enabled, but if # interactive mode is not enabled it will return the default answer to the question. If there is no default answer # to the question and interactive mode is not enabled it will return an error and quit. @@ -45,8 +47,8 @@ sub getField { my $config_answer = shift; my $fileName = shift; - # if there is no config file and interactive mode prompt for all questions with default answers - if ( $::inputFile eq "" && $::interactive ) { +# if there is no config file and not in automatic mode prompt for all questions with default answers + if ( $::inputFile eq "" && !$::automatic ) { return promptUser( $question, $config_answer ); } @@ -55,8 +57,8 @@ sub getField { return $config_answer; } else { - # if no config value and in interactive mode prompt user - if ($::interactive) { + # if no config value and not in automatic mode prompt user + if ( !$::automatic ) { return promptUser($question); } @@ -95,9 +97,13 @@ sub getConfig { } foreach my $var ( @{ $userInput->{$fileName} } ) { - my $question = ( ( keys $var )[0] eq "config_var" ? ( keys $var )[1] : ( keys $var )[0] ); + my $question = + ( ( keys $var )[0] eq "config_var" + ? ( keys $var )[1] + : ( keys $var )[0] ); - my $answer = $config{ $var->{"config_var"} } = getField( $question, $var->{$question}, $fileName ); + my $answer = $config{ $var->{"config_var"} } = + getField( $question, $var->{$question}, $fileName ); $config{ $var->{"config_var"} } = $answer; if ($::debug) { @@ -129,12 +135,14 @@ sub generateDbConf { my %todbconf = getConfig( $userInput, $toDBFileName ); # No YAML library installed, but this is a simple file.. - open( my $fh, '>', $dbAccessFileName ) or errorOut("Can't write to $dbAccessFileName $!"); + open( my $fh, '>', $dbAccessFileName ) + or errorOut("Can't write to $dbAccessFileName $!"); print $fh "version: 1.0\n"; print $fh "name: dbconf.yml\n\n"; print $fh "production:\n"; print $fh " driver: ", getDbDriver() . "\n"; - print $fh " open: tcp:$dbconf{hostname}:$dbconf{port}*$dbconf{dbname}/$dbconf{root_user}/$dbconf{root_passwd}\n"; + print $fh +" open: tcp:$dbconf{hostname}:$dbconf{port}*$dbconf{dbname}/$dbconf{root_user}/$dbconf{root_passwd}\n"; close $fh; return \%todbconf; @@ -150,7 +158,8 @@ sub generateCdnConf { my $fileName = shift; # First, read existing one -- already loaded with a bunch of stuff - my $cdnConf = Safe->new->rdo($fileName) or errorOut("Error loading $fileName: $@"); + my $cdnConf = Safe->new->rdo($fileName) + or errorOut("Error loading $fileName: $@"); my %cdnconf = getConfig( $userInput, $fileName ); @@ -162,7 +171,9 @@ sub generateCdnConf { my @secrets = @{ $cdnConf->{secrets} }; my $newSecret = randomWord(); unshift @secrets, randomWord(); - if ( $cdnconf{keepSecrets} > 0 && $#secrets > $cdnconf{keepSecrets} - 1 ) { + if ( $cdnconf{keepSecrets} > 0 + && $#secrets > $cdnconf{keepSecrets} - 1 ) + { # Shorten the array to requested length $#secrets = $cdnconf{keepSecrets} - 1; @@ -230,13 +241,24 @@ sub generateProfilesDir { sub sanityCheckDefaults { foreach my $file ( ( keys $::defaultInputs ) ) { foreach my $defaultValue ( @{ $::defaultInputs->{$file} } ) { - my $question = ( ( keys $defaultValue )[0] eq "config_var" ? ( keys $defaultValue )[1] : ( keys $defaultValue )[0] ); - if ( !defined $defaultValue->{$question} || $defaultValue->{$question} eq "" ) { - errorOut("Error: question \'$question\' in file \'$file\' has no default answer\n"); + my $question = + ( ( keys $defaultValue )[0] eq "config_var" + ? ( keys $defaultValue )[1] + : ( keys $defaultValue )[0] ); + if ( !defined $defaultValue->{$question} + || $defaultValue->{$question} eq "" ) + { + errorOut( +"Error: question \'$question\' in file \'$file\' has no default answer\n" + ); } - if ( !defined $defaultValue->{"config_var"} || $defaultValue->{"config_var"} eq "" ) { - errorOut("Error: question \'$question\' in file \'$file\' has no config_var"); + if ( !defined $defaultValue->{"config_var"} + || $defaultValue->{"config_var"} eq "" ) + { + errorOut( +"Error: question \'$question\' in file \'$file\' has no config_var" + ); } } } @@ -256,7 +278,8 @@ sub sanityCheckConfig { foreach my $file ( ( keys $::defaultInputs ) ) { if ( !defined $userInput->{$file} ) { - print "Warning: File \'$file\' found in defaults but not config file\n"; + print + "Warning: File \'$file\' found in defaults but not config file\n"; next; } @@ -264,27 +287,40 @@ sub sanityCheckConfig { my $found = 0; foreach my $configValue ( @{ $userInput->{$file} } ) { - if ( $defaultValue->{"config_var"} eq $configValue->{"config_var"} ) { + if ( $defaultValue->{"config_var"} eq + $configValue->{"config_var"} ) + { $found = 1; } } - # if the question is not found in the config file add it from defaults + # if the question is not found in the config file add it from defaults if ( !$found ) { - print "Warning: Value " . Dumper($defaultValue) . "found in defaults but not in \'$file\'\n"; + print "Warning: Value " + . Dumper($defaultValue) + . "found in defaults but not in \'$file\'\n"; - my $question = ( ( keys $defaultValue )[0] eq "config_var" ? ( keys $defaultValue )[1] : ( keys $defaultValue )[0] ); + my $question = + ( ( keys $defaultValue )[0] eq "config_var" + ? ( keys $defaultValue )[1] + : ( keys $defaultValue )[0] ); my %temp; - # if interactive add the question without default answer - if ($::interactive) { - %temp = ( "config_var" => $defaultValue->{"config_var"}, $question => "" ); + # if not in automatic mode add the question without default answer + if ( !$::automatic ) { + %temp = ( + "config_var" => $defaultValue->{"config_var"}, + $question => "" + ); } - # if not interactive add question with default answer + # if in automatic mode add question with default answer else { - %temp = ( "config_var" => $defaultValue->{"config_var"}, $question => $defaultValue->{$question} ); + %temp = ( + "config_var" => $defaultValue->{"config_var"}, + $question => $defaultValue->{$question} + ); } push $userInput->{$file}, \%temp; @@ -392,26 +428,22 @@ sub getDefaults { } # -d - Debug Mode: More output to the terminal -# -i - Interactive mode: Any questions which do not have answersin config file will result in blocking prompts going -# to the user +# -a - Automatic mode: If there are questions in the config file which do not have answers, the script +# will look to the defaults for the answer. If the answer is not in the defaults +# the script will exit # -h - Help: Basic command line help menu # -cfile - Input File: The input config file used to ask and answer questions -# In interactive mode: prompt if no value in cfile - -# Not in interactive mode: if answer in cfile, use if. If no answer in cfile, use answer in default. -# if no answer in default die - sub main { our $inputFile = ""; + our $automatic = 0; + our $debug = 0; my $help = 0; - our $interactive = 0; - our $debug = 0; GetOptions( "cfile=s" => \$inputFile, "c=s" => \$inputFile, - "i" => \$interactive, + "a" => \$automatic, "d" => \$debug, "h" => \$help, "help" => \$help @@ -421,7 +453,7 @@ sub main { our $defaultInputs = getDefaults(); if ($help) { - print "Usage: postinstall [-i] [-d] -cfile=[config_file]\n"; + print "Usage: postinstall [-a] [-d] -cfile=[config_file]\n"; exit(0); } @@ -429,8 +461,8 @@ sub main { print "Debug is on\n"; } - if ( $::debug && $::interactive ) { - print "Running in interactive mode\n"; + if ( $::debug && $::automatic ) { + print "Running in automatic mode\n"; } # used to store the questions and answers - @@ -443,11 +475,16 @@ sub main { } else { print "Using input file $::inputFile\n"; + + if ( !-e $::inputFile ) { + errorOut("Error: file \'$::inputFile\' not found\n"); + } + $userInput = readJson($::inputFile); } - # check the defaults - if ( $::inputFile eq "" && !$::interactive ) { + # check the defaults if running them automatically + if ( $::inputFile eq "" && $::automatic ) { sanityCheckDefaults(); } @@ -457,10 +494,11 @@ sub main { sanityCheckConfig($userInput); } - # The generator functions handle checking input/default/interactive mode + # The generator functions handle checking input/default/automatic mode # todbconf will be used later when setting up the database - my $todbconf = generateDbConf( $userInput, 'testdb.conf', 'testtodb.conf', 'testdbconf.yml' ); + my $todbconf = generateDbConf( $userInput, 'testdb.conf', 'testtodb.conf', + 'testdbconf.yml' ); generateCdnConf( $userInput, 'testcdn.conf' ); generateLdapConf( $userInput, 'testldap.conf' ); generatePostInstallConf( $userInput, 'testpost_install.json' );