Skip to content

Instantly share code, notes, and snippets.

@wilpig
Created May 20, 2024 20:24
Show Gist options
  • Save wilpig/bf1d6086c528359560d51ebd7c2a1c80 to your computer and use it in GitHub Desktop.
Save wilpig/bf1d6086c528359560d51ebd7c2a1c80 to your computer and use it in GitHub Desktop.
Check for updates and self-disable problematic repos
#!/usr/bin/perl
use strict;
use warnings;
my $basedir = $ENV{'CFWORKDIR'}; # base agent directory
my $failurefile = "$basedir/outputs/software_update_check_failure"; # flag file to indicate a failure
my $badrepofile = "$basedir/outputs/repo_error_detected"; # flag file to indicate a failure
my $yum = "/usr/bin/yum";
my $yum_options = '';
# store the date and time
my ($SS,$MM,$HH,$dd,$mm,$yy,$wday,$jjj,$isdst) = localtime(time);
++$mm;
my $date = sprintf "%04.4d%02.2d%02.2d", 1900+$yy,$mm,$dd;
my $logdate = sprintf "%04.4d-%02.2d-%02.2d", 1900+$yy,$mm,$dd;
my $time = sprintf "%02.2d:%02.2d:%02.2d", $HH,$MM,$SS;
sub write_error_tag {
my ($tag) = @_;
open (my $fh, '>', $tag);
print $fh "$time $date\n";
close ($fh);
}
# reset any previous failures
unlink($failurefile);
unlink($badrepofile);
# Self-referencing function to run the command and handle errors
sub run_yum_check_update {
my ($yum, $yum_options) = @_;
while (1) {
# Construct the command
my $command = "$yum $yum_options -d 0 check-update 2>&1";
# Run the command and capture the output
my $output = `$command`;
my $exit_code = $? >> 8; # Get the exit code
# reset any previous failures
unlink($failurefile);
unlink($badrepofile);
# Self-referencing function to run the command and handle errors
sub run_yum_check_update {
my ($yum, $yum_options) = @_;
while (1) {
# Construct the command
my $command = "$yum $yum_options -d 0 check-update 2>&1";
# Run the command and capture the output
my $output = `$command`;
my $exit_code = $? >> 8; # Get the exit code
# • 0 : Operation was successful.
# • 1 : An error occurred, which was handled by dnf.
# • 3 : An unknown unhandled error occurred during operation.
# • 100: See check-update
# • 200: There was a problem with acquiring or releasing of locks.
if ($exit_code == 1) {
# yum error, write out tag
write_error_tag($failurefile);
# Check for specific error and update yum options if necessary
if ($output =~ /Error: Failed to download metadata for repo '([^']+)':/) {
my $repo = $1;
$yum_options .= "--disablerepo=$repo ";
print "Disabling repo: $repo\n";
write_error_tag($badrepofile);
}
} else {
return ($exit_code, $output);
}
}
}
# Call the function
my ($exit_code, $output) = run_yum_check_update($yum, $yum_options);
# Print the results
print "Exit code: $exit_code\n";
#print "Output:\n$output";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment