Added notification sub. Added UTF8 support for more notification clarity

This commit is contained in:
Tracey Clark 2022-01-16 20:16:51 -06:00
commit 37196bd060

View file

@ -24,6 +24,10 @@ use Sort::Versions;
use Getopt::Long qw(GetOptions); use Getopt::Long qw(GetOptions);
use Term::ANSIColor qw(:constants); use Term::ANSIColor qw(:constants);
use feature qw(say); use feature qw(say);
use MIME::Lite;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Encode qw(encode encode_utf8 );
my ( $verbose, $info ); my ( $verbose, $info );
@ -34,9 +38,9 @@ GetOptions(
########################################## ##########################################
### The user must set these variables ### ### The user must set these variables ###
# SET email address my $email = 'gitea-update@tlcnet.info';
my $email = 'tclark77@tlcnet.info';
my $filetype = '-linux-amd64'; my $filetype = '-linux-amd64';
##########################################
# https://api.github.com/repos/go-gitea/gitea/releases/latest # https://api.github.com/repos/go-gitea/gitea/releases/latest
# This URL will only have one version listed # This URL will only have one version listed
@ -47,8 +51,9 @@ my $alphanum_ver = 0;
chomp $gitea_current_version_string; chomp $gitea_current_version_string;
unless ( defined $gitea_current_version_string ) { unless ( defined $gitea_current_version_string ) {
die my $status = "❌ [DIE]: Unable to get the version of the gitea binary on the system: $!";
"[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_version($gitea_current_version_string); my %binary_file = get_current_version($gitea_current_version_string);
@ -69,7 +74,7 @@ my $latest_release_hash = decode_json( $resp->content )
my $orig_tag = $latest_release_hash->{tag_name}; my $orig_tag = $latest_release_hash->{tag_name};
my $tag = $orig_tag; my $tag = $orig_tag;
substr( $tag, 0, 1, '' ); substr( $tag, 0, 1, '' );
print("Latest upstream version is $tag\n"); print("Latest upstream version: $tag\n");
my $release_id = $latest_release_hash->{id}; # Works my $release_id = $latest_release_hash->{id}; # Works
my $unz_filename = 'gitea-' . $tag . $filetype; my $unz_filename = 'gitea-' . $tag . $filetype;
#ex: gitea-1.7.6-linux-amd64 #ex: gitea-1.7.6-linux-amd64
@ -81,13 +86,9 @@ if ( defined $verbose ) {
"[INFO]\nRelease id: $release_id\nUncompressed filename: $unz_filename\nDownload file target: $download_filename\n" "[INFO]\nRelease id: $release_id\nUncompressed filename: $unz_filename\nDownload file target: $download_filename\n"
); );
} }
#ex: gitea-1.7.6-linux-amd64.xz
if ( versioncmp( $gitea_current_version, $tag ) == -1 ) { if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
printf( printf(
"Version tag is greater than the system version, proceeding with update\n" "[INFO] Version tag is greater than the system version, proceeding with update\n"
); );
backup_bin( \%binary_file ); backup_bin( \%binary_file );
@ -100,17 +101,23 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
if ( defined $info ) { if ( defined $info ) {
print YELLOW "[INFO] xz file is $xz_file\n", RESET; print YELLOW "[INFO] xz file is $xz_file\n", RESET;
} }
install_bin($download_filename); install_bin($download_filename);
}
else {
my $status = '✅ Version tag upstream matches the system version, no update needed';
print( YELLOW "[INFO] $status\n", RESET );
notification('not needed ✅', $status);
exit 1;
} }
my $status = check_gitea_status(); my $gitea_status = check_gitea_status();
if ( $status =~ /active/ ) { if ( $gitea_status =~ /active/ ) {
print( GREEN "[INFO] Gitea service is active\n", RESET ); print( GREEN "[INFO] Gitea service is active\n", RESET );
} }
unless ( $status =~ /active/ ) { unless ( $gitea_status =~ /active/ ) {
die RED "[DIE] Status of gitea service is no good! OMGURD\n", RESET; my $status = RED "❌ [DIE] Status of gitea service is no good!\n", RESET;
notification('failed ❌', $status);
die $status;
} }
$gitea_current_version_string = `$gitea_bin --version`; $gitea_current_version_string = `$gitea_bin --version`;
chomp $gitea_current_version_string; chomp $gitea_current_version_string;
@ -118,22 +125,19 @@ chomp $gitea_current_version_string;
$gitea_current_version = $binary_file{'version'}; $gitea_current_version = $binary_file{'version'};
if ( versioncmp( $gitea_current_version, $tag ) == -1 ) { if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
printf( my $status = RED "Upstream version tag is STILL greater than the current system version, something went wrong\n", RESET;
"Upstream version tag is STILL greater than the current system version, something went wrong\n" notification('failed ❌', $status);
); printf( $status );
} }
else { else {
say "Cleanup and remove the old xz file and uncompressed file"; my $status = GREEN "✅ [INFO] Gitea version is current and service is running\n", RESET;
say "Unzipped filename in cleanup is " . $download_dir . $unz_filename; say "Removing the downloaded xz file and uncompressed file";
system("rm -vf " . $download_dir . $unz_filename); system("rm -vf " . $download_dir . $unz_filename);
system("rm -vf " . $download_dir . $download_filename); system("rm -vf " . $download_dir . $download_filename);
notification('succeeded ✅', $status);
# Intentionally *not* removing the 'gitea' binary in case something goes wrong running the current binary. # Intentionally *not* removing the 'gitea' binary in case something goes wrong running the current binary.
} }
#TODO Add check if running binary is the same version as what we downloaded
# #notification( $email );
# ##### Subroutines ##### # ##### Subroutines #####
# sub check_deps { # sub check_deps {
@ -273,9 +277,6 @@ sub build_download_url {
sub install_bin { sub install_bin {
my $filename = shift; my $filename = shift;
#my $unz_binary =
print("Unpacking the downloaded file, setting owner and permissions\n"); print("Unpacking the downloaded file, setting owner and permissions\n");
# Copy the downloaded binary to gitea and chmod it 750 # Copy the downloaded binary to gitea and chmod it 750
@ -288,9 +289,8 @@ sub install_bin {
system("chmod 750 $gitea_bin"); system("chmod 750 $gitea_bin");
# Restart service # Restart service
print("Restarting the gitea service\n"); print("[INFO] Restarting the gitea service\n");
system("systemctl restart gitea"); return system("systemctl restart gitea");
return;
} }
sub check_gitea_status { sub check_gitea_status {
@ -298,14 +298,24 @@ sub check_gitea_status {
return $gitea_status; return $gitea_status;
} }
sub cleanup {
# remove downloaded binary and old backup set install_status=success ELSE remove "gitea" and restore from backup and set install_status=fail
return;
}
sub notification { sub notification {
my $to = shift; my ($result, $body) = @_;
my $subject = "Gitea update check: update " . $result;
my $from = 'system@host.tlcnet.info';
return; my $msg = MIME::Lite->new(
Subject => encode( 'MIME-Header', $subject ),
From => $from,
To => $email,
Type => 'text/html',
Data => encode_utf8( '<body>
<H1>Gitea update result</H1>
<p>' . $body . '</p><br>
<p>&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;&#9135;</p>
</body>' )
);
$msg->send();
return 1;
} }