Improved notification text with current and upstream versions. Fixed notification formatting bug

This commit is contained in:
Tracey Clark 2022-07-31 15:37:10 -05:00
commit 8df33dc5f5

View file

@ -56,7 +56,7 @@ unless ( defined $gitea_current_version_string ) {
die RED "$status", RESET; die RED "$status", RESET;
} }
my %binary_file = get_current_version($gitea_current_version_string); my %binary_file = get_current_binary($gitea_current_version_string);
my $gitea_current_version = $binary_file{'version'}; my $gitea_current_version = $binary_file{'version'};
my $baseURL = q{https://github.com/go-gitea/gitea/}; my $baseURL = q{https://github.com/go-gitea/gitea/};
my $latestURL = q{https://api.github.com/repos/go-gitea/gitea/releases/latest}; my $latestURL = q{https://api.github.com/repos/go-gitea/gitea/releases/latest};
@ -72,7 +72,7 @@ my $latest_release_hash = decode_json( $resp->content )
or die "[DIE]: Unable to parse the version data! Output is:\n $resp"; or die "[DIE]: Unable to parse the version data! Output is:\n $resp";
my $orig_tag = $latest_release_hash->{tag_name}; my $orig_tag = $latest_release_hash->{tag_name};
print "!!! ORIG TAG is $orig_tag\n"; # print "[TLC DEBUG]: ORIG TAG is $orig_tag\n";
my $tag = $orig_tag; my $tag = $orig_tag;
substr( $tag, 0, 1, '' ); substr( $tag, 0, 1, '' );
my $release_id = $latest_release_hash->{id}; # Works my $release_id = $latest_release_hash->{id}; # Works
@ -107,8 +107,9 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
} }
else { else {
my $status = '✅ Version tag upstream matches the system version, no update needed'; 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 ); print( YELLOW "[INFO] $status\n", RESET );
notification('not needed ✅', $status); notification('not needed ✅', $status, $gitea_current_version, $tag);
exit 1; exit 1;
} }
@ -117,26 +118,28 @@ if ( $gitea_status =~ /active/ ) {
print( GREEN "[INFO] Gitea service is active\n", RESET ); print( GREEN "[INFO] Gitea service is active\n", RESET );
} }
unless ( $gitea_status =~ /active/ ) { unless ( $gitea_status =~ /active/ ) {
my $status = RED "❌ [DIE] Status of gitea service is no good!\n", RESET; my $status = '❌ [DIE] Status of gitea service is no good!';
notification('failed ❌', $status); print( RED "[INFO] $status\n", RESET );
notification('failed ❌', $status, $gitea_current_version, $tag);
die $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;
%binary_file = get_current_version($gitea_current_version_string); %binary_file = get_current_binary($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 ) {
my $status = RED "Upstream version tag is STILL greater than the current system version, something went wrong\n", RESET; my $status = RED "Upstream version tag is STILL greater than the current system version, something went wrong\n", RESET;
notification('failed ❌', $status); notification('failed ❌', $status, $gitea_current_version, $tag);
printf( $status ); printf( $status );
} }
else { else {
my $status = GREEN "✅ [INFO] Gitea version is current and service is running\n", RESET; my $status = '✅ [INFO] Gitea successfuly update. Installed version is current and service is running';
print( GREEN "[INFO] $status\n", RESET );
say "Removing the downloaded xz file and uncompressed file"; 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); notification('succeeded ✅', $status, $gitea_current_version, $tag);
# 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.
} }
@ -147,7 +150,7 @@ else {
# # bzip2 perl mods # # bzip2 perl mods
# } # }
sub get_current_version { sub get_current_binary {
my $current_version_string = shift; my $current_version_string = shift;
my $current_version; my $current_version;
if ( $current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) { if ( $current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) {
@ -171,6 +174,10 @@ sub get_current_version {
return %binary_file; return %binary_file;
} }
sub get_binary_file {
my $version = shift;
}
sub backup_bin { sub backup_bin {
# Ghetto to get it working for now # Ghetto to get it working for now
@ -303,20 +310,43 @@ sub check_gitea_status {
} }
sub notification { sub notification {
my ($result, $body) = @_; my ($result, $body, $running_version, $upstream_version) = @_;
my $subject = "Gitea update check: update " . $result; my $subject = "Gitea update check: update " . $result;
my $from = 'system@host.tlcnet.info'; my $from = 'system@host.tlcnet.info';
my $ver_body;
# print "[TLC DEBUG] Body received is\n$body\n";
if ( defined $running_version ) {
$ver_body = q{
<table>
<tbody>
<tr>
<td>Running version:</td>
<td>} . $running_version . q{</td>
</tr>
<tr>
<td>Upstream version:</td>
<td>} . $upstream_version . q{</td>
</tr>
</tbody>
</table>}
}
my $msg = MIME::Lite->new( my $msg = MIME::Lite->new(
Subject => encode( 'MIME-Header', $subject ), Subject => encode( 'MIME-Header', $subject ),
From => $from, From => $from,
To => $email, To => $email,
Type => 'text/html', Type => 'text/html',
Data => encode_utf8( '<body> Data => encode_utf8(
'<body>
<H1>Gitea update result</H1> <H1>Gitea update result</H1>
<p>' . $body . '</p><br> <p>' . $body . '</p>' . $ver_body .
'<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> <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>' ) <p>Sincerely, <br>Your Linode VM</p>
</body>'
)
); );
$msg->send(); $msg->send();