diff --git a/update-gitea.pl b/update-gitea.pl index a2be88e..9d82f59 100755 --- a/update-gitea.pl +++ b/update-gitea.pl @@ -24,6 +24,10 @@ use Sort::Versions; use Getopt::Long qw(GetOptions); use Term::ANSIColor qw(:constants); use feature qw(say); +use MIME::Lite; +use utf8; +use open ':std', ':encoding(UTF-8)'; +use Encode qw(encode encode_utf8 ); my ( $verbose, $info ); @@ -34,9 +38,9 @@ GetOptions( ########################################## ### The user must set these variables ### -# SET email address -my $email = 'tclark77@tlcnet.info'; +my $email = 'gitea-update@tlcnet.info'; my $filetype = '-linux-amd64'; +########################################## # https://api.github.com/repos/go-gitea/gitea/releases/latest # This URL will only have one version listed @@ -47,8 +51,9 @@ my $alphanum_ver = 0; chomp $gitea_current_version_string; unless ( defined $gitea_current_version_string ) { - die - "[DIE]: Unable to get the version of the gitea binary on the system: $!"; + 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_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 $tag = $orig_tag; 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 $unz_filename = 'gitea-' . $tag . $filetype; #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" ); } - - -#ex: gitea-1.7.6-linux-amd64.xz - if ( versioncmp( $gitea_current_version, $tag ) == -1 ) { 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 ); @@ -100,17 +101,23 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) { if ( defined $info ) { print YELLOW "[INFO] xz file is $xz_file\n", RESET; } - 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(); -if ( $status =~ /active/ ) { +my $gitea_status = check_gitea_status(); +if ( $gitea_status =~ /active/ ) { print( GREEN "[INFO] Gitea service is active\n", RESET ); } -unless ( $status =~ /active/ ) { - die RED "[DIE] Status of gitea service is no good! OMGURD\n", RESET; +unless ( $gitea_status =~ /active/ ) { + 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`; chomp $gitea_current_version_string; @@ -118,22 +125,19 @@ chomp $gitea_current_version_string; $gitea_current_version = $binary_file{'version'}; if ( versioncmp( $gitea_current_version, $tag ) == -1 ) { - printf( -"Upstream version tag is STILL greater than the current system version, something went wrong\n" - ); + my $status = RED "Upstream version tag is STILL greater than the current system version, something went wrong\n", RESET; + notification('failed ❌', $status); + printf( $status ); } else { - say "Cleanup and remove the old xz file and uncompressed file"; - say "Unzipped filename in cleanup is " . $download_dir . $unz_filename; + my $status = GREEN "✅ [INFO] Gitea version is current and service is running\n", RESET; + say "Removing the downloaded xz file and uncompressed file"; system("rm -vf " . $download_dir . $unz_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. } -#TODO Add check if running binary is the same version as what we downloaded - -# #notification( $email ); - # ##### Subroutines ##### # sub check_deps { @@ -273,9 +277,6 @@ sub build_download_url { sub install_bin { my $filename = shift; - - #my $unz_binary = - print("Unpacking the downloaded file, setting owner and permissions\n"); # Copy the downloaded binary to gitea and chmod it 750 @@ -288,9 +289,8 @@ sub install_bin { system("chmod 750 $gitea_bin"); # Restart service - print("Restarting the gitea service\n"); - system("systemctl restart gitea"); - return; + print("[INFO] Restarting the gitea service\n"); + return system("systemctl restart gitea"); } sub check_gitea_status { @@ -298,14 +298,24 @@ sub check_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 { - 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 . '
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
+ ' ) + ); + + $msg->send(); + + return 1; }