add upd_pending to server table
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/790fa838 Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/790fa838 Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/790fa838 Branch: refs/heads/master Commit: 790fa8380f334d6c3a08d301cf9eef2d022833c8 Parents: b23a923 Author: Derek Gelinas <derek_geli...@cable.comcast.com> Authored: Tue Feb 21 19:38:30 2017 -0500 Committer: Jeremy Mitchell <mitchell...@gmail.com> Committed: Thu Mar 16 15:08:23 2017 -0600 ---------------------------------------------------------------------- .../20170221190747_add_reval_pending.sql | 18 +- traffic_ops/app/lib/Schema/Result/Server.pm | 24 ++- traffic_ops/bin/traffic_ops_ort.pl | 198 +++++++++++++++++-- 3 files changed, 210 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/790fa838/traffic_ops/app/db/migrations/20170221190747_add_reval_pending.sql ---------------------------------------------------------------------- diff --git a/traffic_ops/app/db/migrations/20170221190747_add_reval_pending.sql b/traffic_ops/app/db/migrations/20170221190747_add_reval_pending.sql index a6341b2..29957cf 100644 --- a/traffic_ops/app/db/migrations/20170221190747_add_reval_pending.sql +++ b/traffic_ops/app/db/migrations/20170221190747_add_reval_pending.sql @@ -1,8 +1,22 @@ +/* + + 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. +*/ -- +goose Up -- SQL in section 'Up' is executed when this migration is applied - +ALTER TABLE server ADD COLUMN reval_pending boolean NOT NULL DEFAULT false; -- +goose Down -- SQL section 'Down' is executed when this migration is rolled back - +ALTER TABLE server DROP COLUMN reval_pending; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/790fa838/traffic_ops/app/lib/Schema/Result/Server.pm ---------------------------------------------------------------------- diff --git a/traffic_ops/app/lib/Schema/Result/Server.pm b/traffic_ops/app/lib/Schema/Result/Server.pm index 33c2217..2bbb973 100644 --- a/traffic_ops/app/lib/Schema/Result/Server.pm +++ b/traffic_ops/app/lib/Schema/Result/Server.pm @@ -211,6 +211,12 @@ __PACKAGE__->table("server"); data_type: 'bigint' is_nullable: 1 +=head2 reval_pending + + data_type: 'boolean' + default_value: false + is_nullable: 0 + =cut __PACKAGE__->add_columns( @@ -299,6 +305,8 @@ __PACKAGE__->add_columns( }, "https_port", { data_type => "bigint", is_nullable => 1 }, + "reval_pending", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, ); =head1 PRIMARY KEY @@ -323,7 +331,7 @@ __PACKAGE__->set_primary_key("id", "cachegroup", "type", "status", "profile"); =head1 UNIQUE CONSTRAINTS -=head2 C<idx_54485_ip6_profile> +=head2 C<idx_16629_ip6_profile> =over 4 @@ -335,9 +343,9 @@ __PACKAGE__->set_primary_key("id", "cachegroup", "type", "status", "profile"); =cut -__PACKAGE__->add_unique_constraint("idx_54485_ip6_profile", ["ip6_address", "profile"]); +__PACKAGE__->add_unique_constraint("idx_16629_ip6_profile", ["ip6_address", "profile"]); -=head2 C<idx_54485_ip_profile> +=head2 C<idx_16629_ip_profile> =over 4 @@ -349,9 +357,9 @@ __PACKAGE__->add_unique_constraint("idx_54485_ip6_profile", ["ip6_address", "pro =cut -__PACKAGE__->add_unique_constraint("idx_54485_ip_profile", ["ip_address", "profile"]); +__PACKAGE__->add_unique_constraint("idx_16629_ip_profile", ["ip_address", "profile"]); -=head2 C<idx_54485_se_id_unique> +=head2 C<idx_16629_se_id_unique> =over 4 @@ -361,7 +369,7 @@ __PACKAGE__->add_unique_constraint("idx_54485_ip_profile", ["ip_address", "profi =cut -__PACKAGE__->add_unique_constraint("idx_54485_se_id_unique", ["id"]); +__PACKAGE__->add_unique_constraint("idx_16629_se_id_unique", ["id"]); =head1 RELATIONS @@ -501,8 +509,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2016-11-18 22:45:19 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3f6+Y6k32Vn3CDG7R7uToQ +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-21 19:34:06 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:KJKD6BEj4wc8uPGqonz13g # # Licensed under the Apache License, Version 2.0 (the "License"); http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/790fa838/traffic_ops/bin/traffic_ops_ort.pl ---------------------------------------------------------------------- diff --git a/traffic_ops/bin/traffic_ops_ort.pl b/traffic_ops/bin/traffic_ops_ort.pl index 19cfacf..772da98 100755 --- a/traffic_ops/bin/traffic_ops_ort.pl +++ b/traffic_ops/bin/traffic_ops_ort.pl @@ -92,6 +92,7 @@ my $INTERACTIVE = 0; my $REPORT = 1; my $BADASS = 2; my $SYNCDS = 3; +my $REVALIDATE = 4; #### Logging constants for bit shifting #### my $ALL = 7; my $TRACE = 6; @@ -115,6 +116,9 @@ my $UPDATE_TROPS_NOTNEEDED = 0; my $UPDATE_TROPS_NEEDED = 1; my $UPDATE_TROPS_SUCCESSFUL = 2; my $UPDATE_TROPS_FAILED = 3; +my $UPDATE_REVAL_NEEDED = 4; +my $UPDATE_REVAL_SUCCESSFUL = 5; +my $UPDATE_REVAL_FAILED = 6; #### Other constants ##### my $START_FAILED = 0; my $START_SUCCESSFUL = 1; @@ -132,7 +136,6 @@ my $CFG_FILE_ALREADY_PROCESSED = 4; #### LWP globals my $lwp_conn = &setup_lwp(); - my $unixtime = time(); my $hostname_short = `/bin/hostname -s`; chomp($hostname_short); @@ -178,31 +181,55 @@ my $ssl_tracker = undef; ####-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#### #### First and foremost, if this is a syncds run, check to see if we can bail. +my $reval_update = undef; my $syncds_update = undef; -if ( defined $traffic_ops_host ) { - ($syncds_update) = &check_syncds_state(); +if ( $script_mode == $REVALIDATE ) { + if ( defined $traffic_ops_host ) { + ($syncds_update) = &check_revalidate_state(); + } + else { + print "FATAL Could not resolve Traffic Ops host!\n"; + exit 1; + } } else { - print "FATAL Could not resolve Traffic Ops host!\n"; - exit 1; + if ( defined $traffic_ops_host ) { + ($syncds_update) = &check_syncds_state(); + } + else { + print "FATAL Could not resolve Traffic Ops host!\n"; + exit 1; + } } + #### Delete /tmp dirs older than one week -if ( $script_mode == $BADASS || $script_mode == $INTERACTIVE || $script_mode == $SYNCDS ) { +if ( $script_mode == $BADASS || $script_mode == $INTERACTIVE || $script_mode == $SYNCDS || $script_mode == $REVALIDATE ) { &smart_mkdir($TMP_BASE); &clean_tmp_dirs(); } -( my $my_profile_name, $cfg_file_tracker, my $my_cdn_name ) = &get_cfg_file_list( $hostname_short, $traffic_ops_host ); +( my $my_profile_name, $cfg_file_tracker, my $my_cdn_name ) = &get_cfg_file_list( $hostname_short, $traffic_ops_host, $script_mode ); my $header_comment = &get_header_comment($traffic_ops_host); -&process_packages( $hostname_short, $traffic_ops_host ); -# get the ats user's UID after package installation in case this is the initial badass + my $ats_uid = getpwnam("ats"); -&process_chkconfig( $hostname_short, $traffic_ops_host ); +if ( $script_mode == $REVALIDATE ) { + ( $log_level >> $INFO ) && print "\nINFO: ======== Doing the revalidate thing========\n"; +} +else { + ( $log_level >> $INFO ) && print "\nINFO: ======== Start processing packages========\n"; + &process_packages( $hostname_short, $traffic_ops_host ); + # get the ats user's UID after package installation in case this is the initial badass + ( $log_level >> $INFO ) && print "\nINFO: ======== Start second package processing run========\n"; + &process_chkconfig( $hostname_short, $traffic_ops_host ); +} + + #### First time &process_config_files(); + #### Second time, in case there were new files added to the registry &process_config_files(); @@ -213,7 +240,7 @@ foreach my $file ( keys ( %{$cfg_file_tracker} ) ) { &touch_file('remap.config'); last; } - } + } } if ( ($installed_new_ssl_keys) && !$cfg_file_tracker->{'ssl_multicert.config'}->{'change_applied'} ) { @@ -226,6 +253,7 @@ if ( ($installed_new_ssl_keys) && !$cfg_file_tracker->{'ssl_multicert.config'}-> } } + &start_restart_services(); if ( $sysctl_p_needed && $script_mode != $SYNCDS ) { @@ -573,7 +601,7 @@ sub smart_mkdir { my $dir = shift; if ( !-d ($dir) ) { - if ( $script_mode == $BADASS || $script_mode == $INTERACTIVE || $script_mode == $SYNCDS ) { + if ( $script_mode == $BADASS || $script_mode == $INTERACTIVE || $script_mode == $SYNCDS || $script_mode == $REVALIDATE ) { ( $log_level >> $TRACE ) && print "TRACE Directory to create if needed: $dir\n"; system("/bin/mkdir -p $dir"); if ( $dir =~ m/config_trops/ ) { @@ -671,6 +699,119 @@ sub get_print_current_client_connections { ( $log_level >> $DEBUG ) && print "DEBUG There are currently $current_connections connections.\n"; } +sub check_revalidate_state { + + my $revalidate_update = 0; + return $revalidate_update; + + ( $log_level >> $DEBUG ) && print "DEBUG Checking revalidate state.\n"; + if ( $script_mode == $REVALIDATE ) { + ## The herd is about to get /update/<hostname> + + my $url = "$traffic_ops_host\/update/$hostname_short"; + my $upd_ref = &lwp_get($url); + if ( $upd_ref =~ m/^\d{3}$/ ) { + ( $log_level >> $ERROR ) && print "ERROR Update URL: $url returned $upd_ref. Exiting, not sure what else to do.\n"; + exit 1; + } + + my $upd_json = decode_json($upd_ref); + my $reval_pending = ( defined( $upd_json->[0]->{'reval_pending'} ) ) ? $upd_json->[0]->{'upd_pending'} : undef; + if ( !defined($reval_pending) ) { + ( $log_level >> $ERROR ) && print "ERROR Update URL: $url did not have an reval_pending key. This mode is not compatible with the installed Traffic Ops version.\n"; + exit 1; + } + + if ( $reval_pending == 1 ) { + ( $log_level >> $ERROR ) && print "ERROR Traffic Ops is signaling that a revalidation is waiting to be applied.\n"; + $reval_update = $UPDATE_REVAL_NEEDED; + + my $parent_reval_pending = ( defined( $upd_json->[0]->{'parent_reval_pending'} ) ) ? $upd_json->[0]->{'parent_pending'} : undef; + if ( !defined($parent_reval_pending) ) { + ( $log_level >> $ERROR ) && print "ERROR Update URL: $url did not have an parent_reval_pending key. Unable to continue!\n"; + exit 1; + } + if ( $parent_reval_pending == 1 ) { + ( $log_level >> $ERROR ) && print "ERROR Traffic Ops is signaling that my parents need to revalidate.\n"; + if ( $dispersion > 0 ) { + ( $log_level >> $WARN ) && print "WARN In revalidate mode, sleeping for " . $dispersion . "s to see if the update my parents need is cleared.\n"; + for ( my $i = $dispersion; $i > 0; $i-- ) { + ( $log_level >> $WARN ) && print "."; + sleep 1; + } + } + ( $log_level >> $WARN ) && print "\n"; + $upd_ref = &lwp_get($url); + if ( $upd_ref =~ m/^\d{3}$/ ) { + ( $log_level >> $ERROR ) && print "ERROR Update URL: $url returned $upd_ref. Exiting, not sure what else to do.\n"; + exit 1; + } + $upd_json = decode_json($upd_ref); + $parent_reval_pending = ( defined( $upd_json->[0]->{'parent_reval_pending'} ) ) ? $upd_json->[0]->{'parent_reval_pending'} : undef; + if ( !defined($parent_reval_pending) ) { + ( $log_level >> $ERROR ) && print "ERROR Invalid JSON for $url. Exiting, not sure what else to do.\n"; + exit 1; + } + if ( $parent_reval_pending == 1 ) { + ( $log_level >> $ERROR ) && print "ERROR My parents still need an update, bailing.\n"; + exit 1; + } + else { + ( $log_level >> $DEBUG ) && print "DEBUG The update on my parents cleared; continuing.\n"; + } + } + else { + ( $log_level >> $DEBUG ) && print "DEBUG Traffic Ops is signaling that my parents do not need an update, or wait_for_parents == 0.\n"; + } + } + elsif ( $script_mode == $REVALIDATE && $reval_pending != 1 ) { + ( $log_level >> $ERROR ) && print "ERROR In revalidate mode, but no update needs to be applied. I'm outta here.\n"; + exit 0; + } + else { + ( $log_level >> $ERROR ) && print "ERROR Traffic Ops is signaling that no update is waiting to be applied.\n"; + } + + my $stj = &lwp_get("$traffic_ops_host\/datastatus"); + if ( $stj =~ m/^\d{3}$/ ) { + ( $log_level >> $ERROR ) && print "Statuses URL: $url returned $stj! Skipping creation of status file.\n"; + } + + my $statuses = decode_json($stj); + my $my_status = ( defined( $upd_json->[0]->{'status'} ) ) ? $upd_json->[0]->{'status'} : undef; + + if ( defined($my_status) ) { + ( $log_level >> $DEBUG ) && print "DEBUG Found $my_status status from Traffic Ops.\n"; + } + else { + ( $log_level >> $ERROR ) && print "ERROR Returning; did not find status from Traffic Ops!\n"; + return ($revalidate_update); + } + + my $status_dir = dirname($0) . "/status"; + my $status_file = $status_dir . "/" . $my_status; + + if ( !-f $status_file ) { + ( $log_level >> $ERROR ) && print "ERROR status file $status_file does not exist.\n"; + } + + for my $status ( @{$statuses} ) { + next if ( $status->{name} eq $my_status ); + my $other_status = $status_dir . "/" . $status->{name}; + + if ( -f $other_status && $status->{name} ne $my_status ) { + ( $log_level >> $ERROR ) && print "ERROR Other status file $other_status exists.\n"; + if ( $script_mode != $REPORT ) { + ( $log_level >> $DEBUG ) && print "DEBUG Removing $other_status\n"; + unlink($other_status); + } + } + } + } + return ($revalidate_update); +} + + sub check_syncds_state { my $syncds_update = 0; @@ -1219,7 +1360,7 @@ sub replace_cfg_file { $select = $input; } } - if ( $select == 1 || $script_mode == $BADASS || $script_mode == $SYNCDS ) { + if ( $select == 1 || $script_mode == $BADASS || $script_mode == $SYNCDS || $script_mode == $REVALIDATE ) { ( $log_level >> $ERROR ) && print "ERROR Copying " . $cfg_file_tracker->{$cfg_file}->{'backup_from_trops'} . " to " @@ -1310,7 +1451,6 @@ sub get_cookie { my $to_login = shift; my ( $u, $p ) = split( /:/, $to_login ); my %headers; - my $url = $to_host . "/login"; my $response = $lwp_conn->post( $url, [ 'u' => $u, 'p' => $p ], %headers ); @@ -1395,6 +1535,10 @@ sub check_script_mode { ( $log_level >> $DEBUG ) && print "DEBUG Script running in syncds mode.\n"; $script_mode = 3; } + elsif ( $ARGV[0] eq "revalidate" ) { + ( $log_level >> $DEBUG ) && print "DEBUG Script running in revalidate mode.\n"; + $script_mode = 4; + } else { ( $log_level >> $FATAL ) && print "FATAL You did not specify a valid mode. Exiting.\n"; &usage(); @@ -1449,6 +1593,7 @@ sub set_domainname { sub get_cfg_file_list { my $host_name = shift; my $tm_host = shift; + my $script_mode = shift; my $cfg_files; my $profile_name; my $cdn_name; @@ -1461,12 +1606,25 @@ sub get_cfg_file_list { ( $log_level >> $INFO ) && printf("INFO Found profile from Traffic Ops: $profile_name\n"); $cdn_name = $ort_ref->{'other'}->{'CDN_name'}; ( $log_level >> $INFO ) && printf("INFO Found CDN_name from Traffic Ops: $cdn_name\n"); - foreach my $cfg_file ( keys %{ $ort_ref->{'config_files'} } ) { - my $fname_on_disk = &get_filename_on_disk($cfg_file); - ( $log_level >> $INFO ) - && printf( "INFO Found config file (on disk: %-41s): %-41s with location: %-50s\n", $fname_on_disk, $cfg_file, $ort_ref->{'config_files'}->{$cfg_file}->{'location'} ); - $cfg_files->{$fname_on_disk}->{'location'} = $ort_ref->{'config_files'}->{$cfg_file}->{'location'}; - $cfg_files->{$fname_on_disk}->{'fname-in-TO'} = $cfg_file; + if ( $script_mode == $REVALIDATE ) { + foreach my $cfg_file ( keys %{ $ort_ref->{'config_files'} } ) { + if ( $cfg_file eq "regex_revalidate.config" ) { + my $fname_on_disk = &get_filename_on_disk($cfg_file); + ( $log_level >> $INFO ) + && printf( "INFO Found config file (on disk: %-41s): %-41s with location: %-50s\n", $fname_on_disk, $cfg_file, $ort_ref->{'config_files'}->{$cfg_file}->{'location'} ); + $cfg_files->{$fname_on_disk}->{'location'} = $ort_ref->{'config_files'}->{$cfg_file}->{'location'}; + $cfg_files->{$fname_on_disk}->{'fname-in-TO'} = $cfg_file; + } + } + } + else { + foreach my $cfg_file ( keys %{ $ort_ref->{'config_files'} } ) { + my $fname_on_disk = &get_filename_on_disk($cfg_file); + ( $log_level >> $INFO ) + && printf( "INFO Found config file (on disk: %-41s): %-41s with location: %-50s\n", $fname_on_disk, $cfg_file, $ort_ref->{'config_files'}->{$cfg_file}->{'location'} ); + $cfg_files->{$fname_on_disk}->{'location'} = $ort_ref->{'config_files'}->{$cfg_file}->{'location'}; + $cfg_files->{$fname_on_disk}->{'fname-in-TO'} = $cfg_file; + } } return ( $profile_name, $cfg_files, $cdn_name ); }