You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by da...@apache.org on 2017/05/09 18:30:40 UTC
[05/27] incubator-trafficcontrol git commit: made changes to support
seeds cleanup
made changes to support seeds cleanup
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/d3bbaec4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/d3bbaec4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/d3bbaec4
Branch: refs/heads/master
Commit: d3bbaec410536737ab7d3e6d3ed6bfa09444806a
Parents: bd603e8
Author: Dewayne Richardson <de...@apache.org>
Authored: Tue May 2 10:58:58 2017 -0600
Committer: Dan Kirkwood <da...@gmail.com>
Committed: Tue May 9 18:28:58 2017 +0000
----------------------------------------------------------------------
traffic_ops/install/bin/_postinstall | 357 ++++++++++++++++---------
traffic_ops/install/bin/postinstall | 4 +-
traffic_ops/install/lib/InstallUtils.pm | 6 +-
traffic_ops/install/lib/ProfileCleanup.pm | 254 ------------------
4 files changed, 232 insertions(+), 389 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d3bbaec4/traffic_ops/install/bin/_postinstall
----------------------------------------------------------------------
diff --git a/traffic_ops/install/bin/_postinstall b/traffic_ops/install/bin/_postinstall
index 66ecf7a..3c1b8c0 100755
--- a/traffic_ops/install/bin/_postinstall
+++ b/traffic_ops/install/bin/_postinstall
@@ -17,7 +17,8 @@
use lib qw(/opt/traffic_ops/install/lib /opt/traffic_ops/app/lib /opt/traffic_ops/app/local/lib/perl5);
$ENV{PERL5LIB} = "/opt/traffic_ops/install/lib:/opt/traffic_ops/app/lib:/opt/traffic_ops/app/local/lib/perl5:$ENV{PERL5LIB}";
-$ENV{PATH} = "/usr/local/bin:/opt/traffic_ops/install/bin:$ENV{PATH}";
+$ENV{PATH} = "/usr/bin:/opt/traffic_ops/go/bin:/usr/local/go/bin:/opt/traffic_ops/install/bin:$ENV{PATH}";
+$ENV{GOPATH} = "/opt/traffic_ops/go";
use strict;
use warnings;
@@ -34,7 +35,6 @@ use Getopt::Long;
use InstallUtils qw{ :all };
use GenerateCert qw{ :all };
-use ProfileCleanup qw{ :all };
# paths of the output configuration files
my $databaseConfFile = "/opt/traffic_ops/app/conf/production/database.conf";
@@ -64,15 +64,9 @@ my $debug = 1;
# log file for cpan output
my $cpanLogFile = "/var/log/traffic_ops/cpan.log";
-# whether or not to reconfigure traffic ops
-my $reconfigure = 1;
-
# used to check for .reconfigure_defaults file for backwards compatability
my $reconfigure_defaults = "/opt/traffic_ops/.reconfigure_defaults";
-# old way of reconfiguring postinstall - only here to check for file and let user know it is deprecated
-my $reconfigure_file = "/opt/traffic_ops/.reconfigure";
-
# maximum size the uncompressed log file should be before rotating it - rotating it copies the current log
# file to the same name appended with .bkp replacing the old backup if any is there
my $maxLogSize = 10000000; #bytes
@@ -405,11 +399,11 @@ sub getDefaults {
$dbConfFile => [
{
"Database server root (admin) user" => "postgres",
- "config_var" => "dbAdminUser"
+ "config_var" => "pgUser"
},
{
"Password for database server admin" => "",
- "config_var" => "dbAdminPw",
+ "config_var" => "pgPassword",
"hidden" => "true"
},
{
@@ -508,20 +502,20 @@ sub getDefaults {
"config_var" => "tm.url"
},
{
- "Human-readable CDN Name. (No whitespace, please)" => "kabletown_cdn",
- "config_var" => "cdn_name"
+ "Traffic Ops Instance Name" => "Traffic Ops CDN",
+ "config_var" => "tm.instance_name"
},
{
- "Health Polling Interval (milliseconds)" => "8000",
- "config_var" => "health_polling_int"
+ "Traffic Ops Tool Name" => "Traffic Ops",
+ "config_var" => "tm.toolname"
},
{
- "DNS sub-domain for which your CDN is authoritative" => "cdn1.kabletown.net",
- "config_var" => "dns_subdomain"
+ "Human-readable CDN Name. (No whitespace, please)" => "kabletown_cdn",
+ "config_var" => "cdn_name"
},
{
- "TLD SOA admin" => "traffic_ops",
- "config_var" => "soa_admin"
+ "DNS sub-domain for which your CDN is authoritative" => "cdn1.kabletown.net",
+ "config_var" => "dns_subdomain"
},
{
"TrafficServer Drive Prefix" => "/dev/sd",
@@ -534,20 +528,7 @@ sub getDefaults {
{
"TrafficServer RAM Drive Letters (comma separated)" => "0,1,2,3,4,5,6,7",
"config_var" => "ram_drive_letters"
- },
- {
- "Health Threshold Load Average" => "25",
- "config_var" => "health_thresh_load_avg"
- },
- {
- "Health Threshold Available Bandwidth in Kbps" => "1750000",
- "config_var" => "health_thresh_kbps"
- },
- {
- "Traffic Server Health Connection Timeout (milliseconds)" => "2000",
- "config_var" => "health_connect_timeout"
}
-
]
};
}
@@ -557,50 +538,74 @@ sub getDefaults {
# todbconf: The database configuration to be used
# opensslconf: The openssl configuration if any
-sub setupDatabase {
- my $todbconf = shift;
- my $opensslconf = shift;
- my $setupMaxmind = shift;
- my $databaseConfFile = shift;
+sub setupDatabaseData {
+ my $dbh = shift;
+ my $adminconf = shift;
+ my $paramconf = shift;
+ InstallUtils::logger( "paramconf " . Dumper($paramconf), "info" );
- my $dbconf = InstallUtils::readJson($databaseConfFile);
+ my $result;
- # Check if the Postgres db is used and set the admin database to be "postgres"
- my $dbName = $dbconf->{type};
- if ( $dbconf->{type} eq "Pg" ) {
- $dbName = "postgres";
- }
+ my $q = <<"QUERY";
+ select exists(select 1 from pg_tables where schemaname = 'public' and tablename = 'tm_user')
+QUERY
- my $dsn = sprintf( "DBI:%s:db=%s;host=%s;port=%d", $dbconf->{type}, $dbName, $dbconf->{hostname}, $dbconf->{port} );
- my $dbh = DBI->connect( $dsn, $todbconf->{"dbAdminUser"}, $todbconf->{"dbAdminPw"} );
- if ($dbh) {
- InstallUtils::logger( "Database connection succeeded", "info" );
+ my $stmt = $dbh->prepare($q);
+ $stmt->execute();
- # Success!
- $dbh->disconnect();
+ InstallUtils::logger( "Setting up the database data", "info" );
+ my $tables_found;
+ while ( my $row = $stmt->fetch() ) {
+ $tables_found = $row->[0];
}
- else {
- InstallUtils::logger( "Error connecting to database", "error" );
- exit(-1);
+ if ($tables_found) {
+ InstallUtils::logger( "Found existing tables skipping table creation", "info" );
+ } else {
+ invoke_db_admin_pl("load_schema");
}
+ invoke_db_admin_pl("migrate");
+ invoke_db_admin_pl("seed");
- InstallUtils::logger( "Setting up database", "info" );
- $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", "reset" );
+ # Skip the insert if the admin 'username' is already there.
+ my $sha1_passwd = sha1_hex( $adminconf->{"password"} );
- if ( $result == 0 ) {
- $result = InstallUtils::execCommand( "/usr/bin/perl", "db/admin.pl", "--env=production", "seed" );
- }
+ my $insert_admin = <<"ADMIN";
+ insert into tm_user (username, role, local_passwd)
+ values ("$adminconf->{"username"}",
+ (select id from role where name = 'admin'),
+ "$sha1_passwd",
+ "$sha1_passwd" ))
+ ON CONFLICT (profile, parameter) DO NOTHING ON CONFLICT (username) DO NOTHING;"
+ADMIN
+ $dbh->do($insert_admin);
+
+ insert_parameters($dbh, $paramconf);
+ insert_profiles($dbh, $paramconf);
+
+
+}
+
+sub invoke_db_admin_pl {
+ my $action = shift;
+
+ chdir("/opt/traffic_ops/app");
+ my $result = InstallUtils::execCommand( "/usr/bin/perl", "db/admin.pl", "--env=production", $action );
if ( $result != 0 ) {
- errorOut("Database initialization failed");
+ errorOut("Database $action failed");
}
else {
- InstallUtils::logger( "Database initialization succeeded", "info" );
+ InstallUtils::logger( "Database $action succeeded", "info" );
}
+ return $result;
+}
+
+sub setupMaxMind {
+ my $setupMaxmind = shift;
+
+ my $result;
+
if ( $setupMaxmind =~ /^y(?:es)?/ ) {
InstallUtils::logger( "Downloading Maxmind data", "info" );
chdir("/opt/traffic_ops/app/public/routing");
@@ -608,17 +613,22 @@ sub setupDatabase {
if ( $result != 0 ) {
InstallUtils::logger( "Failed to download MaxMind data", "error" );
}
+
+ $result = InstallUtils::execCommand("/usr/bin/wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz");
+ if ( $result != 0 ) {
+ InstallUtils::logger( "Failed to download MaxMind data", "error" );
+ }
}
else {
InstallUtils::logger("Not downloading Maxmind data");
}
- chdir("/opt/traffic_ops/app/public/routing");
- InstallUtils::logger( "Copying coverage zone file to public dir", "info" );
- $result = InstallUtils::execCommand("/bin/mv /opt/traffic_ops/app/public/coverage-zone.json .");
- if ( $result != 0 ) {
- InstallUtils::logger( "Failed to copy coverage zone file", "error" );
- }
+}
+
+sub setupCertificates {
+ my $opensslconf = shift;
+
+ my $result;
if ( lc $opensslconf->{"genCert"} =~ /^y(?:es)?/ ) {
if ( -x "/usr/bin/openssl" ) {
@@ -643,11 +653,144 @@ sub setupDatabase {
}
}
+#------------------------------------
+sub connect_to_database {
+ my $databaseConfFile = shift;
+ my $todbconf = shift;
+
+ my $dbconf = InstallUtils::readJson($databaseConfFile);
+
+ # Check if the Postgres db is used and set the admin database to be "postgres"
+ my $dbName = $dbconf->{type};
+ if ( $dbconf->{type} eq "Pg" ) {
+ $dbName = "traffic_ops";
+ }
+
+ $ENV{PGUSER} = $todbconf->{"pgUser"};
+ $ENV{PGPASSWORD} = $todbconf->{"pgPassword"};
+
+ my $dsn = sprintf( "DBI:%s:db=%s;host=%s;port=%d", $dbconf->{type}, $dbName, $dbconf->{hostname}, $dbconf->{port} );
+ my $dbh = DBI->connect( $dsn, $todbconf->{"pgUser"}, $todbconf->{"pgPassword"} );
+ if ($dbh) {
+ InstallUtils::logger( "Database connection succeeded", "info" );
+ }
+ else {
+ InstallUtils::logger( "Error connecting to database", "error" );
+ exit(-1);
+ }
+ return $dbh;
+
+}
+
+#------------------------------------
+sub insert_parameters {
+ my $dbh = shift;
+ my $paramconf = shift;
+
+ InstallUtils::logger( "=========== Setting up parameters", "info" );
+
+ # Enable multiple inserts into one commit
+ $dbh->{pg_server_prepare} = 0;
+
+ my $insert_stmt = <<"INSERTS";
+
+ -- global parameters
+ insert into parameter (name, config_file, value)
+ values ('tm.url', 'global', '$paramconf->{"tm.url"}')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ insert into parameter (name, config_file, value)
+ values ('tm.instance_name', 'global', '$paramconf->{"tm.instance_name"}')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ insert into parameter (name, config_file, value)
+ values ('tm.toolname', 'global', '$paramconf->{"tm.toolname"}')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ insert into parameter (name, config_file, value)
+ values ('tm.infourl', 'global', '$paramconf->{"tm.url"}/doc')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ -- CRConfig.json parameters
+ insert into parameter (name, config_file, value)
+ values ('geolocation.polling.url', 'CRConfig.json', '$paramconf->{"tm.url"}/routing/GeoLite2-City.mmdb.gz')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ insert into parameter (name, config_file, value)
+ values ('geolocation6.polling.url', 'CRConfig.json', '$paramconf->{"tm.url"}/routing/GeoLiteCityv6.dat.gz')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ -- RAM Prefix
+ insert into parameter (name, config_file, value)
+ values ('RAM_Drive_Prefix', 'storage.config', '$paramconf->{"ram_drive_prefix"}')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+ insert into parameter (name, config_file, value)
+ values ('RAM_Drive_Letters', 'storage.config', '$paramconf->{"ram_drive_letters"}')
+ ON CONFLICT (name, config_file, value) DO NOTHING;
+
+
+INSERTS
+ doInsert($dbh, $insert_stmt);
+}
+
+#------------------------------------
+sub insert_profiles {
+ my $dbh = shift;
+ my $paramconf = shift;
+
+ InstallUtils::logger( "\n=========== Setting up profiles", "info" );
+ my $insert_stmt = <<"INSERTS";
+
+ -- global parameters
+ insert into profile (name, description, type)
+ values ('GLOBAL', 'Global Traffic Ops profile, DO NOT DELETE', 'UNK_PROFILE')
+ ON CONFLICT (name) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'tm.url' and config_file = 'global' and value = '$paramconf->{"tm.url"}') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'tm.instance_name' and config_file = 'global' and value = '$paramconf->{"tm.instance_name"}') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'tm.toolname' and config_file = 'global' and value = '$paramconf->{"tm.toolname"}') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'tm.infourl' and config_file = 'global' and value = '$paramconf->{"tm.url"}/doc') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'geolocation.polling.url' and config_file = 'CRConfig.json' and value = 'https://$paramconf->{"dns_subdomain"}/routing/GeoLite2-City.mmdb.gz') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+ insert into profile_parameter (profile, parameter)
+ values ( (select id from profile where name = 'GLOBAL'), (select id from parameter where name = 'geolocation6.polling.url' and config_file = 'CRConfig.json' and value = 'https://$paramconf->{"dns_subdomain"}/routing/GeoLiteCityv6.dat.gz') )
+ ON CONFLICT (profile, parameter) DO NOTHING;
+
+INSERTS
+ doInsert($dbh, $insert_stmt);
+}
+
+#------------------------------------
+sub doInsert {
+ my $dbh = shift;
+ my $insert_stmt = shift;
+
+ InstallUtils::logger( "\n" . $insert_stmt, "info" );
+ my $stmt = $dbh->prepare($insert_stmt);
+ $stmt->execute();
+}
+
+
+
# -cfile - Input File: The input config file used to ask and answer questions
# -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
-# -r - Reconfigure: Whether or not to reconfigure the database and check perl dependencies - This will recreate the database
# -defaults - Defaults: Writes out a configuration file with defaults which can be used as input
# -debug - Debug Mode: More output to the terminal
# -h - Help: Basic command line help menu
@@ -661,7 +804,6 @@ sub main {
GetOptions(
"cfile=s" => \$inputFile,
"automatic" => \$automatic,
- "reconfigure" => \$reconfigure,
"defaults" => \$dumpDefaults,
"debug" => \$debug,
"help" => \$help
@@ -695,21 +837,12 @@ sub main {
InstallUtils::logger( "Running in automatic mode", "info" );
}
- # check if the reconfigure_file is present on the system - if it is let the user know its deprecated
- # and exit with an error
- if ( -f $reconfigure_file ) {
- InstallUtils::logger( "$reconfigure_file file is depreprecated - please remove and rerun postinstall", "error" );
- return;
- }
-
if ($dumpDefaults) {
InstallUtils::logger( "Writing default configuration file to $outputConfigFile", "info" );
InstallUtils::writeJson( $outputConfigFile, $defaultInputs );
return;
}
- InstallUtils::logger( "Postinstall " . ( $reconfigure ? "" : "not " ) . "in reconfigure mode", "info" );
-
InstallUtils::rotateLog($cpanLogFile);
if ( -s $logFile > $maxLogSize ) {
@@ -741,24 +874,13 @@ sub main {
# check the input config file against the defaults to check for missing questions
sanityCheckConfig($userInput) if ( $inputFile ne "" );
- # if reconfigure is set then rebuild the perl deps
- if ($reconfigure) {
- chdir("/opt/traffic_ops/install/bin");
- my $rc = InstallUtils::execCommand( "./download_web_deps", "-i" );
- if ( $rc != 0 ) {
- errorOut("Failed to install Traffic Ops Web dependencies, check the console output and rerun postinstall once you've resolved the error");
- }
- }
- else {
- chdir("/opt/traffic_ops/install/bin");
- my $rc = InstallUtils::execCommand( "./download_web_deps", "-i" );
- if ( $rc != 0 ) {
- errorOut("Failed to install Traffic Ops Web dependencies, check the console output and rerun postinstall once you've resolved the error");
- }
+ chdir("/opt/traffic_ops/install/bin");
+ my $rc = InstallUtils::execCommand( "./download_web_deps", "-i" );
+ if ( $rc != 0 ) {
+ errorOut("Failed to install Traffic Ops Web dependencies, check the console output and rerun postinstall once you've resolved the error");
}
# The generator functions handle checking input/default/automatic mode
-
# todbconf will be used later when setting up the database
my $todbconf = generateDbConf( $userInput, $databaseConfFile, $dbConfFile );
generateCdnConf( $userInput, $cdnConfFile );
@@ -772,49 +894,24 @@ sub main {
InstallUtils::writeJson( $post_install_cfg, {} );
}
- # if reconfigure is set then setup the database
- if ($reconfigure) {
- setupDatabase( $todbconf, $opensslconf, $todbconf->{"maxmind"}, $databaseConfFile );
+ setupMaxMind( $todbconf->{"maxmind"} );
+ setupCertificates( $opensslconf );
- ## Create the tm_user
- my $dbconf = InstallUtils::readJson($databaseConfFile);
- my $dsn = sprintf( "DBI:%s:db=%s;host=%s;port=%d", $dbconf->{type}, $dbconf->{dbname}, $dbconf->{hostname}, $dbconf->{port} );
- my $dbh = DBI->connect( $dsn, $todbconf->{"dbAdminUser"}, $todbconf->{"dbAdminPw"} );
- my $sha1_passwd = sha1_hex( $adminconf->{"password"} );
- my $sub_string = sprintf( "('%s', 4, '%s')", $adminconf->{"username"}, $sha1_passwd, $sha1_passwd );
+ my $dbh = connect_to_database($databaseConfFile, $todbconf);
+ setupDatabaseData( $dbh, $adminconf, $paramconf );
- $dbh->do("insert into tm_user (username, role, local_passwd) values $sub_string");
+ InstallUtils::logger("Starting Traffic Ops", "info" );
+ InstallUtils::execCommand("/sbin/service traffic_ops restart");
- }
-
- InstallUtils::logger( "Starting Traffic Ops", "info" );
- InstallUtils::execCommand("/sbin/service traffic_ops start");
+ InstallUtils::logger("Waiting for Traffic Ops to restart", "info" );
- InstallUtils::logger( "Waiting for Traffic Ops to start", "info" );
-
- if ( -f $post_install_cfg ) {
- $parameters = InstallUtils::readJson($post_install_cfg);
- }
-
- if ( !ProfileCleanup::profiles_exist( $adminconf, $paramconf->{"tm.url"}, $parameters, $reconfigure_defaults, $reconfigure ) ) {
- InstallUtils::logger( "Creating default profiles...", "info" );
- ProfileCleanup::replace_profile_templates( $paramconf, $adminconf, $post_install_cfg, $parameters, $profileDir );
- ProfileCleanup::import_profiles( $paramconf->{"tm.url"}, $adminconf, $profileDir );
-
- # call again to create $reconfigure_defaults file if import was successful
- ProfileCleanup::profiles_exist( $adminconf, $paramconf->{"tm.url"}, $parameters, $reconfigure_defaults, $reconfigure );
- }
- else {
- InstallUtils::logger( "Not creating default profiles", "info" );
- }
-
- if ( $custom_profile =~ /^y(?:es)?/ ) {
- ProfileCleanup::add_custom_profiles( $custom_profile_dir, $adminconf, $parameters->{"tm.url"} );
- }
+ InstallUtils::logger("Success! Postinstall complete.");
- InstallUtils::logger("Postinstall complete");
+ #InstallUtils::logger("Zipping up $logFile to $logFile.gz");
+ #InstallUtils::execCommand( "/bin/gzip", "$logFile" );
- InstallUtils::execCommand( "/bin/gzip", "$logFile" );
+ # Success!
+ $dbh->disconnect();
}
main;
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d3bbaec4/traffic_ops/install/bin/postinstall
----------------------------------------------------------------------
diff --git a/traffic_ops/install/bin/postinstall b/traffic_ops/install/bin/postinstall
index 4c1b035..aee1f78 100755
--- a/traffic_ops/install/bin/postinstall
+++ b/traffic_ops/install/bin/postinstall
@@ -18,7 +18,7 @@
umask 022
# go and git are required, but don't want to stomp on those if already installed
-for p in git go; do
+for p in git /usr/local/go/bin/go /opt/traffic_ops/go/bin/goose; do
type $p >/dev/null 2>&1 || { echo >&2 "postinstall requires $p but it's not installed. Aborting."; exit 1; }
done
@@ -36,7 +36,7 @@ cd /opt/traffic_ops/app
export POSTGRES_HOME=${POSTGRES_HOME:-/usr/pgsql-9.6}
/usr/local/bin/carton
-export GOPATH=${GOPATH:-/usr/local}
+export GOPATH=${GOPATH:-/usr/local/go/bin}
go get bitbucket.org/liamstask/goose/cmd/goose
export PERL5LIB=/opt/traffic_ops/app/lib:/opt/traffic_ops/app/local/lib/perl5
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d3bbaec4/traffic_ops/install/lib/InstallUtils.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/install/lib/InstallUtils.pm b/traffic_ops/install/lib/InstallUtils.pm
index 040a8e9..22f53b8 100644
--- a/traffic_ops/install/lib/InstallUtils.pm
+++ b/traffic_ops/install/lib/InstallUtils.pm
@@ -199,7 +199,7 @@ sub trim {
sub readJson {
my $file = shift;
- open( my $fh, '<', $file ) or die("open(): $!");
+ open( my $fh, '<', $file ) or die("open($file): $!");
local $/; # slurp mode
my $text = <$fh>;
undef $fh;
@@ -208,7 +208,7 @@ sub readJson {
sub writeJson {
my $file = shift;
- open( my $fh, '>', $file ) or die("open(): $!");
+ open( my $fh, '>', $file ) or die("open($file): $!");
logger( "Writing json to $file", "info" );
foreach my $data (@_) {
my $json_text = JSON->new->utf8->pretty->encode($data);
@@ -221,7 +221,7 @@ sub writePerl {
my $file = shift;
my $data = shift;
- open( my $fh, '>', $file ) or die("open(): $!");
+ open( my $fh, '>', $file ) or die("open($file): $!");
my $dumper = Data::Dumper->new( [$data] );
# print without var names and with simple indentation
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d3bbaec4/traffic_ops/install/lib/ProfileCleanup.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/install/lib/ProfileCleanup.pm b/traffic_ops/install/lib/ProfileCleanup.pm
deleted file mode 100644
index f9ffa01..0000000
--- a/traffic_ops/install/lib/ProfileCleanup.pm
+++ /dev/null
@@ -1,254 +0,0 @@
-#!/usr/bin/perl
-
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-package ProfileCleanup;
-
-use warnings;
-use strict;
-
-use InstallUtils qw{ :all };
-use WWW::Curl::Easy;
-use LWP::UserAgent;
-
-use base qw{ Exporter };
-our @EXPORT_OK = qw{ replace_profile_templates import_profiles profiles_exist add_custom_profiles };
-our %EXPORT_TAGS = ( all => \@EXPORT_OK );
-
-sub profile_replace {
- my $profile = shift;
- my $adminconf = shift;
- my $parameters = shift;
-
- my $profile_bak = $profile . ".bak";
- InstallUtils::logger( "Replacing parameters in profile: $profile", "info" );
- rename( $profile, $profile_bak ) or die("rename(): $!");
- open( my $fh, '<', $profile_bak ) or die("open(): $!");
- open( my $ofh, '>', $profile ) or die("open(): $!");
- while (<$fh>) {
- s/{{.TmUrl}}/$parameters->{'tm.url'}/g;
- s/{{.TmInfoUrl}}/$parameters->{"tminfo.url"}/g;
- s/{{.TmInstanceName}}/$parameters->{"cdnname"}/g;
- s/{{.GeolocationPollingUrl}}/$parameters->{"geolocation.polling.url"}/g;
- s/{{.Geolocation6PollingUrl}}/$parameters->{"geolocation6.polling.url"}/g;
- s/{{.TmUrl}}/$parameters->{'tm.url'}/g;
- s/{{.TmToolName}}/Traffic Ops/g;
- s/{{.HealthPollingInterval}}/$parameters->{"health.polling.interval"}/g;
- s/{{.CoveragezonePollingUrl}}/$parameters->{"coveragezone.polling.url"}/g;
- s/{{.DomainName}}/$parameters->{"domainname"}/g;
- s/{{.TldSoaAdmin}}/$parameters->{"tld.soa.admin"}/g;
- s/{{.DrivePrefix}}/$parameters->{"Drive_Prefix"}/g;
- s/{{.HealthThresholdLoadavg}}/$parameters->{"health.threshold.loadavg"}/g;
- s/{{.HealthThresholdAvailableBandwidthInKbps}}/$parameters->{"health.threshold.availableBandwidthInKbps"}/g;
- s/{{.RAMDrivePrefix}}/$parameters->{"RAM_Drive_Prefix"}/g;
- s/{{.RAMDriveLetters}}/$parameters->{"RAM_Drive_Letters"}/g;
- s/{{.HealthConnectionTimeout}}/$parameters->{"health.connection.timeout"}/g;
- s#{{.CronOrtSyncds}}#*/15 * * * * root /opt/ort/traffic_ops_ort.pl syncds warn $parameters->{'tm.url'} $adminconf->{tmAdminUser}:$adminconf->{tmAdminPw} > /tmp/ort/syncds.log 2>&1#g;
- print $ofh $_;
- }
- close $fh;
- close $ofh;
- unlink $profile_bak;
-}
-
-sub replace_profile_templates {
- my $conf = shift;
- my $adminconf = shift;
- my $post_install_cfg = shift;
- my $parameters = shift;
- my $profileDir = shift;
-
- $parameters->{'tm.url'} = $conf->{"tm.url"};
- $parameters->{"tminfo.url"} = "$parameters->{'tm.url'}/info";
- $parameters->{"cdnname"} = $conf->{"cdn_name"};
- $parameters->{"geolocation.polling.url"} = "$parameters->{'tm.url'}/routing/GeoIP2-City.mmdb.gz";
- $parameters->{"geolocation6.polling.url"} = "$parameters->{'tm.url'}/routing/GeoIP2-Cityv6.mmdb.gz";
- $parameters->{"health.polling.interval"} = $conf->{"health_polling_int"};
- $parameters->{"coveragezone.polling.url"} = "$parameters->{'tm.url'}/routing/coverage-zone.json";
- $parameters->{"domainname"} = $conf->{"dns_subdomain"};
- $parameters->{"tld.soa.admin"} = $conf->{"soa_admin"};
- $parameters->{"Drive_Prefix"} = $conf->{"driver_prefix"};
- $parameters->{"RAM_Drive_Prefix"} = $conf->{"ram_drive_prefix"};
- $parameters->{"RAM_Drive_Letters"} = $conf->{"ram_drive_letters"};
- $parameters->{"health.threshold.loadavg"} = $conf->{"health_thresh_load_avg"};
- $parameters->{"health.threshold.availableBandwidthInKbps"} = substr( $conf->{"health_thresh_kbps"}, 0, 1 ) eq ">" ? "" : ">" . $conf->{"health_thresh_kbps"};
- $parameters->{"health.connection.timeout"} = $conf->{"health_connect_timeout"};
-
- profile_replace( $profileDir . "profile.global.traffic_ops", $adminconf, $parameters );
- profile_replace( $profileDir . "profile.traffic_monitor.traffic_ops", $adminconf, $parameters );
- profile_replace( $profileDir . "profile.traffic_router.traffic_ops", $adminconf, $parameters );
- profile_replace( $profileDir . "profile.trafficserver_edge.traffic_ops", $adminconf, $parameters );
- profile_replace( $profileDir . "profile.trafficserver_mid.traffic_ops", $adminconf, $parameters );
- writeJson( $post_install_cfg, $parameters );
-}
-
-# Takes the Traffic Ops URI, user, and password.
-# Returns the cookie, or the empty string on error
-sub get_traffic_ops_cookie {
- my ( $uri, $user, $pass ) = @_;
-
- my $loginUri = "/api/1.2/user/login";
-
- my $curl = WWW::Curl::Easy->new;
- my $response_body = "";
- open( my $fileb, ">", \$response_body );
- my $loginData = JSON::encode_json( { u => $user, p => $pass } );
- $curl->setopt( WWW::Curl::Easy::CURLOPT_URL, $uri . $loginUri );
- $curl->setopt( WWW::Curl::Easy::CURLOPT_SSL_VERIFYPEER, 0 );
- $curl->setopt( WWW::Curl::Easy::CURLOPT_HEADER, 1 ); # include header in response
- $curl->setopt( WWW::Curl::Easy::CURLOPT_NOBODY, 1 ); # disclude body in response
- $curl->setopt( WWW::Curl::Easy::CURLOPT_POST, 1 );
- $curl->setopt( WWW::Curl::Easy::CURLOPT_POSTFIELDS, $loginData );
- $curl->setopt( WWW::Curl::Easy::CURLOPT_WRITEDATA, $fileb ); # put response in this var
- $curl->perform();
-
- my $cookie = $response_body;
- if ( $cookie =~ /mojolicious=(.*); expires/ ) {
- $cookie = $1;
- }
- else {
- $cookie = "";
- }
- return $cookie;
-}
-
-# Takes the filename of a Traffic Ops (TO) profile to import, the TO URI, and the TO login cookie
-sub profile_import_single {
- my ( $profileFilename, $uri, $trafficOpsCookie ) = @_;
- InstallUtils::logger( "Importing Profiles with: " . "curl -v -k -X POST -H \"Cookie: mojolicious=$trafficOpsCookie\" -F \"filename=$profileFilename\" -F \"profile_to_import=\@$profileFilename\" $uri/profile/doImport", "info" );
- my $rc = InstallUtils::execCommand("curl -v -k -X POST -H \"Cookie: mojolicious=$trafficOpsCookie\" -F \"filename=$profileFilename\" -F \"profile_to_import=\@$profileFilename\" $uri/profile/doImport");
- if ( $rc != 0 ) {
- InstallUtils::logger( "Failed to import Traffic Ops profile, check the console output and rerun postinstall once you've resolved the error", "error" );
- }
-}
-
-sub import_profiles {
- my $toUri = shift;
- my $adminconf = shift;
- my $profileDir = shift;
-
- InstallUtils::logger( "Importing profiles...", "info" );
-
- my $toUser = $adminconf->{"username"};
- my $toPass = $adminconf->{"password"};
-
- my $toCookie = get_traffic_ops_cookie( $toUri, $toUser, $toPass );
-
- InstallUtils::logger( "Got cookie: " . $toCookie, "info" );
-
- # \todo use an array?
- InstallUtils::logger( "Importing Global profile...", "info" );
- profile_import_single( $profileDir . "profile.global.traffic_ops", $toUri, $toCookie );
- InstallUtils::logger( "Importing Traffic Monitor profile...", "info" );
- profile_import_single( $profileDir . "profile.traffic_monitor.traffic_ops", $toUri, $toCookie );
- InstallUtils::logger( "Importing Traffic Router profile...", "info" );
- profile_import_single( $profileDir . "profile.traffic_router.traffic_ops", $toUri, $toCookie );
- InstallUtils::logger( "Importing TrafficServer Edge profile...", "info" );
- profile_import_single( $profileDir . "profile.trafficserver_edge.traffic_ops", $toUri, $toCookie );
- InstallUtils::logger( "Importing TrafficServer Mid profile...", "info" );
- profile_import_single( $profileDir . "profile.trafficserver_mid.traffic_ops", $toUri, $toCookie );
- InstallUtils::logger( "Finished Importing Profiles.", "info" );
-}
-
-sub profiles_exist {
- my $config = shift;
- my $tmurl = shift;
- my $parameters = shift;
- my $reconfigure_defaults = shift;
- my $reconfigure = shift;
-
- if ( -f $reconfigure_defaults ) {
- InstallUtils::logger( "Default profiles were previously created. Remove " . $reconfigure_defaults . " to create again", "warn" );
- return 1;
- }
-
- $parameters->{'tm.url'} = $tmurl;
-
- InstallUtils::logger( "Checking profiles at $tmurl using username " . $config->{"username"}, "info" );
-
- my $uri = $parameters->{'tm.url'};
-
- # This module gets installed during BuildPerlDeps portion, so require here when it's available.
- require 'LWP/Protocol/https.pm';
- my $toCookie = get_traffic_ops_cookie( $parameters->{'tm.url'}, $config->{"username"}, $config->{"password"} );
-
- my $profileEndpoint = "/api/1.2/profiles.json";
-
- my $ua = LWP::UserAgent->new;
- $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00 );
- my $req = HTTP::Request->new( GET => $uri . $profileEndpoint );
- $req->header( 'Cookie' => "mojolicious=" . $toCookie );
- my $resp = $ua->request($req);
-
- if ( !$resp->is_success ) {
- InstallUtils::logger( "Error checking if profiles exist: " . $resp->status_line, "error" );
- return 1; # return true, so we don't attempt to create profiles
- }
- my $message = $resp->decoded_content;
-
- my $profiles = JSON->new->utf8->decode($message);
- if ( ( !defined $profiles->{"response"} )
- || ( ref $profiles->{"response"} ne 'ARRAY' ) )
- {
- InstallUtils::logger( "Error checking if profiles exist: invalid JSON: $message", "error" );
- return 1; # return true, so we don't attempt to create profiles
- }
-
- my $num_profiles = scalar( @{ $profiles->{"response"} } );
- InstallUtils::logger( "Existing Profile Count: $num_profiles", "info" );
-
- my %initial_profiles = (
- "INFLUXDB" => 1,
- "RIAK_ALL" => 1,
- "TRAFFIC_STATS" => 1
- );
-
- my $profiles_response = $profiles->{"response"};
- foreach my $profile (@$profiles_response) {
- if ( !exists $initial_profiles{ $profile->{"name"} } ) {
- InstallUtils::logger( "Found existing profile (" . $profile->{"name"} . ")", "info" );
- open( my $reconfigure_defaults, '>', $reconfigure ) or die("Failed to open() $reconfigure_defaults: $!");
- close($reconfigure_defaults);
- return 1;
- }
- }
- return 0;
-}
-
-sub add_custom_profiles {
- my $custom_profile_dir = shift;
- my $adminconf = shift;
- my $toUri = shift;
-
- return if ( !-e $custom_profile_dir );
-
- opendir( DH, $custom_profile_dir ) || return;
- my @profiles = readdir(DH);
- closedir(DH);
- @profiles = grep( /^profile\..*\.traffic_ops$/, @profiles );
-
- return if ( scalar @profiles == 0 );
-
- my $toUser = $adminconf->{tmAdminUser};
- my $toPass = $adminconf->{tmAdminPw};
- my $toCookie = get_traffic_ops_cookie( $toUri, $toUser, $toPass );
-
- foreach my $profile (@profiles) {
- print "\nimport profile " . $custom_profile_dir . $profile . "\n\n";
- profile_import_single( $custom_profile_dir . $profile, $toUri, $toCookie );
- }
-}
-
-1;