Added status check to condition where update not needed. Added SSH health check. Improve logic flow of update and notification
This commit is contained in:
parent
327abea47e
commit
7781fedfd0
1 changed files with 105 additions and 44 deletions
|
|
@ -28,6 +28,8 @@ use MIME::Lite;
|
|||
use utf8;
|
||||
use open ':std', ':encoding(UTF-8)';
|
||||
use Encode qw(encode encode_utf8 );
|
||||
# use Net::SSH qw(sshopen2);
|
||||
use Capture::Tiny qw(capture);
|
||||
|
||||
my ( $verbose, $info );
|
||||
|
||||
|
|
@ -40,24 +42,32 @@ GetOptions(
|
|||
### The user must set these variables ###
|
||||
my $email = 'gitea-update@tlcnet.info';
|
||||
my $filetype = '-linux-amd64';
|
||||
my $download_dir = '/home/gitea/bin/';
|
||||
##########################################
|
||||
|
||||
my $update_needed = 0;
|
||||
|
||||
###################################
|
||||
# Get version currently installed #
|
||||
###################################
|
||||
# https://api.github.com/repos/go-gitea/gitea/releases/latest
|
||||
# This URL will only have one version listed
|
||||
my $gitea_bin_dir = '/usr/local/bin/';
|
||||
my $gitea_bin = $gitea_bin_dir . '/gitea';
|
||||
my $gitea_current_version_string = `$gitea_bin --version`;
|
||||
my $alphanum_ver = 0;
|
||||
|
||||
chomp $gitea_current_version_string;
|
||||
unless ( defined $gitea_current_version_string ) {
|
||||
my $status = "❌ [DIE]: Unable to get the version of the gitea binary on the system: $!";
|
||||
notification('failed ❌', $status);
|
||||
die RED "$status", RESET;
|
||||
}
|
||||
|
||||
my %binary_file = get_current_binary($gitea_current_version_string);
|
||||
my $gitea_current_version = $binary_file{'version'};
|
||||
|
||||
######################################
|
||||
# Check upstream version and get tag #
|
||||
######################################
|
||||
my $baseURL = q{https://github.com/go-gitea/gitea/};
|
||||
my $latestURL = q{https://api.github.com/repos/go-gitea/gitea/releases/latest};
|
||||
|
||||
|
|
@ -72,23 +82,31 @@ my $latest_release_hash = decode_json( $resp->content )
|
|||
or die "[DIE]: Unable to parse the version data! Output is:\n $resp";
|
||||
|
||||
my $orig_tag = $latest_release_hash->{tag_name};
|
||||
# print "[TLC DEBUG]: ORIG TAG is $orig_tag\n";
|
||||
print "[TLC DEBUG]: ORIG TAG is $orig_tag\n" if defined $verbose;
|
||||
my $tag = $orig_tag;
|
||||
substr( $tag, 0, 1, '' );
|
||||
my $release_id = $latest_release_hash->{id}; # Works
|
||||
printf( YELLOW
|
||||
"[INFO] Latest upstream version: $tag\n Release ID $release_id\n", RESET);
|
||||
|
||||
################
|
||||
# Set filename #
|
||||
################
|
||||
my $unz_filename = 'gitea-' . $tag . $filetype;
|
||||
#ex: gitea-1.7.6-linux-amd64
|
||||
my $download_dir = '/home/gitea/bin/';
|
||||
my $download_filename = $unz_filename . '.xz';
|
||||
|
||||
if ( defined $verbose ) {
|
||||
printf( YELLOW
|
||||
"[INFO]\nRelease id: $release_id\nUncompressed filename: $unz_filename\nDownload file target: $download_filename\n"
|
||||
"[INFO] Latest upstream version: $tag\n Release ID $release_id\n", RESET);
|
||||
printf( YELLOW
|
||||
"[INFO]\nRelease id: $release_id\nUncompressed filename: $unz_filename\nDownload file target: $download_filename\n", RESET
|
||||
);
|
||||
}
|
||||
|
||||
#############################################
|
||||
# Check if upstream is newer than installed #
|
||||
#############################################
|
||||
if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
|
||||
$update_needed = 1;
|
||||
printf(
|
||||
"[INFO] Version tag is greater than the system version, proceeding with update\n"
|
||||
);
|
||||
|
|
@ -105,36 +123,39 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
|
|||
}
|
||||
install_bin($download_filename);
|
||||
}
|
||||
else {
|
||||
my $status = '✅ Version tag upstream matches the system version, no update needed';
|
||||
print "[TLC DEBUG] TAG is $tag\n";
|
||||
print( YELLOW "[INFO] $status\n", RESET );
|
||||
notification('not needed ✅', $status, $gitea_current_version, $tag);
|
||||
exit 1;
|
||||
}
|
||||
|
||||
my $gitea_status = check_gitea_status();
|
||||
if ( $gitea_status =~ /active/ ) {
|
||||
print( GREEN "[INFO] Gitea service is active\n", RESET );
|
||||
}
|
||||
unless ( $gitea_status =~ /active/ ) {
|
||||
my $status = '❌ [DIE] Status of gitea service is no good!';
|
||||
print( RED "[INFO] $status\n", RESET );
|
||||
notification('failed ❌', $status, $gitea_current_version, $tag);
|
||||
die $status;
|
||||
}
|
||||
##################################
|
||||
# Get service status and version #
|
||||
##################################
|
||||
my ($gitea_status, $gitea_proc_status, $gitea_ssh_status) = check_gitea_status();
|
||||
$gitea_current_version_string = `$gitea_bin --version`;
|
||||
chomp $gitea_current_version_string;
|
||||
%binary_file = get_current_binary($gitea_current_version_string);
|
||||
$gitea_current_version = $binary_file{'version'};
|
||||
|
||||
if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
|
||||
my $status = RED "Upstream version tag is STILL greater than the current system version, something went wrong\n", RESET;
|
||||
notification('failed ❌', $status, $gitea_current_version, $tag);
|
||||
printf( $status );
|
||||
if ( $gitea_status ) {
|
||||
print( GREEN "[INFO] ✅ Gitea service is active\n", RESET );
|
||||
}
|
||||
else {
|
||||
my $status = '✅ [INFO] Gitea successfuly update. Installed version is current and service is running';
|
||||
print( RED "[ERROR] ❌ Gitea service is NOT active\nProcess: $gitea_proc_status\nSSH: $gitea_ssh_status\n", RESET );
|
||||
}
|
||||
|
||||
########################
|
||||
# Notify appropriately #
|
||||
########################
|
||||
if ( !$update_needed && $gitea_status ) {
|
||||
my $status = '✅ Version tag upstream matches the system version, no update needed. Service is running.';
|
||||
print( YELLOW "[INFO] $status\nInstalled version: $gitea_current_version\nUpstream version: $tag\n", RESET );
|
||||
notification('not needed ✅', $status, $gitea_current_version, $tag);
|
||||
exit 1;
|
||||
}
|
||||
elsif ( $update_needed && versioncmp( $gitea_current_version, $tag ) == -1 ) {
|
||||
my $status = '❌ Upstream version tag is STILL greater than the current system version after attempting to update, something went wrong';
|
||||
notification('failed ❌', $status, $gitea_current_version, $tag);
|
||||
print( RED $status, RESET );
|
||||
}
|
||||
elsif ( $update_needed && $gitea_status ) {
|
||||
my $status = '✅ [INFO] Gitea successfuly updated. Installed version is current and service is running';
|
||||
print( GREEN "[INFO] $status\n", RESET );
|
||||
say "Removing the downloaded xz file and uncompressed file";
|
||||
system("rm -vf " . $download_dir . $unz_filename);
|
||||
|
|
@ -142,6 +163,19 @@ else {
|
|||
notification('succeeded ✅', $status, $gitea_current_version, $tag);
|
||||
# Intentionally *not* removing the 'gitea' binary in case something goes wrong running the current binary.
|
||||
}
|
||||
elsif ( !$gitea_status ) {
|
||||
my $status = q{❌ [DIE] The gitea service is no good!<br><br>
|
||||
<b>Process:</b> } . $gitea_proc_status . q{<br><br>
|
||||
<b>SSH Output:</b><br>} . $gitea_ssh_status . q{<br>};
|
||||
notification('failed ❌', $status, $gitea_current_version, $tag);
|
||||
die '❌ [DIE] The gitea service is no good!';
|
||||
}
|
||||
else {
|
||||
my $status = '❌ [DIE] Unknown update status!';
|
||||
print( RED $status, RESET );
|
||||
notification('failed ❌', $status, $gitea_current_version, $tag);
|
||||
die $status;
|
||||
}
|
||||
|
||||
# ##### Subroutines #####
|
||||
|
||||
|
|
@ -155,7 +189,7 @@ sub get_current_binary {
|
|||
my $current_version;
|
||||
if ( $current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) {
|
||||
$current_version = $1;
|
||||
printf("Version number of the installed gitea binary: $1\n");
|
||||
printf("Version number of the installed gitea binary: $1\n") if defined $verbose;
|
||||
}
|
||||
elsif ( $current_version_string =~ m/ion\ (\d+\d+\w*)\s/ ) {
|
||||
$current_version = $1;
|
||||
|
|
@ -305,8 +339,35 @@ sub install_bin {
|
|||
}
|
||||
|
||||
sub check_gitea_status {
|
||||
my $gitea_status = `systemctl is-active gitea`;
|
||||
return $gitea_status;
|
||||
my $gitea_status;
|
||||
my $gitea_proc_status = `systemctl is-active gitea`;
|
||||
sleep 1; # Otherwise the SSH regex doesnt work;
|
||||
|
||||
my $command = 'ssh -i /home/tracey/.ssh/id_tlc_gitea gitea';
|
||||
# DEBUG - induce failure
|
||||
# my $command = 'ssh -i /home/tracey/.ssh/blerg gitea';
|
||||
my ($out, $err, $exit) = capture { system $command };
|
||||
|
||||
# print "\n---- STDERR: --------------\n";
|
||||
# print $err;
|
||||
# print "\n---- STDOUT: --------------\n";
|
||||
# print $out;
|
||||
# print "\n---- EXIT CODE: -----------\n";
|
||||
# print $exit / 256;
|
||||
# print "\n---------------------------\n";
|
||||
my $gitea_ssh_status = $err;
|
||||
if ($gitea_ssh_status =~ /(successfully)/gm) {
|
||||
print "Gitea SSH status: $1\n";
|
||||
}
|
||||
|
||||
|
||||
unless ( ($gitea_proc_status =~ /active/) && ($gitea_ssh_status =~ /successfully/gm) ) {
|
||||
$gitea_status = 0;
|
||||
}
|
||||
if ( ($gitea_proc_status =~ /active/) && ($gitea_ssh_status =~ /successfully/gm) ) {
|
||||
$gitea_status = 1;
|
||||
}
|
||||
return $gitea_status, $gitea_proc_status, $gitea_ssh_status;
|
||||
}
|
||||
|
||||
sub notification {
|
||||
|
|
@ -315,7 +376,7 @@ sub notification {
|
|||
my $from = 'system@host.tlcnet.info';
|
||||
my $ver_body;
|
||||
|
||||
# print "[TLC DEBUG] Body received is\n$body\n";
|
||||
print "[TLC DEBUG] Body received is\n$body\n" if defined $verbose;
|
||||
|
||||
if ( defined $running_version ) {
|
||||
$ver_body = q{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue