Wrote modules for install and checking status. Refactored file decompression

This commit is contained in:
Tracey Clark 2021-03-20 02:00:03 -05:00
commit 573a91ca8a

View file

@ -13,17 +13,17 @@
use strict; use strict;
use warnings; use warnings;
#use LWP::Simple;
use LWP::UserAgent; use LWP::UserAgent;
#use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ; use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
use Data::Dumper; use Data::Dumper;
use JSON; use JSON;
use Sort::Versions;
########################################## ##########################################
### The user must set these variables ### ### The user must set these variables ###
# SET email address # SET email address
my $email='tclark77@tlcnet.info'; my $email='tclark77@tlcnet.info';
my $filetype='-linux-amd64.xz'; my $filetype='-linux-amd64';
# https://api.github.com/repos/go-gitea/gitea/releases/latest # https://api.github.com/repos/go-gitea/gitea/releases/latest
my $gitea_bin_dir = '/home/gitea/bin/'; my $gitea_bin_dir = '/home/gitea/bin/';
@ -31,97 +31,58 @@ my $gitea_bin = $gitea_bin_dir . '/gitea';
my $gitea_current_version_string = `$gitea_bin --version`; my $gitea_current_version_string = `$gitea_bin --version`;
my $alphanum_ver = 0; my $alphanum_ver = 0;
chomp $gitea_current_version_string;
print("System is running \"$gitea_current_version_string\"\n");
unless ( defined $gitea_current_version_string ) { unless ( defined $gitea_current_version_string ) {
die "ERROR: Unable to get the version of the gitea binary: $!"; die "ERROR: Unable to get the version of the gitea binary on the system: $!";
} }
my $gitea_current_version; my %binary_file = get_current_version($gitea_current_version_string);
if ( $gitea_current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) { my $gitea_current_version = $binary_file{'version'};
#if ( $gitea_current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s/ ) {
$gitea_current_version = $1;
printf("Current version of gitea that is installed is $1\n");
}
elsif ( $gitea_current_version_string =~ m/ion\ (\d+\d+\w*)\s/ ) {
$gitea_current_version = $1;
printf("Current version of gitea that is installed is $1\n");
$alphanum_ver = 1;
}
my %binary_file = (
input => $gitea_bin,
version => $gitea_current_version,
);
unless ( defined $gitea_current_version ) {
die "ERROR: Unable to get the current gitea version! Value is $gitea_current_version"
}
# Backup the current binary
my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2';
printf("Gitea bin backup will be $gitea_bin_backup\n");
printf("!! Input is $gitea_bin\n and output is $gitea_bin_backup\n");
=example
# IO::File mode
print($z $string);
printf($z $format, $string);
close($z);
=cut
# This method may not work. Check the module. Ken said doing this through system call was more reliable & performant
my $input = $gitea_bin;
my $temp_out = '/home/gitea/tempout.bz';
my $bz = new IO::Compress::Bzip2 $temp_out
or die "bzip2 failed: $Bzip2Error\n";
#backup_bin( \%binary_file );
backup_gitea_bin.pl( \%binary_file );
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};
# https://hubpages.com/technology/Use-Perl-to-access-REST-API # https://hubpages.com/technology/Use-Perl-to-access-REST-API
# Spin up the browser object (via LWP::UserAgent) # Spin up the browser object
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
cookie_jar => {}, # keep cookies in RAM but not persistent between sessions cookie_jar => {}, # keep cookies in RAM but not persistent between sessions
); );
my $resp = $ua->get($latestURL); my $resp = $ua->get($latestURL);
my $latest_release_hash = decode_json($resp->content); my $latest_release_hash = decode_json($resp->content)
or die "ERROR: Unable to parse the version data! Output is:\n $resp";
# printf("!! Print the json response if we have one:\n"); my $tag = $latest_release_hash->{tag_name};
# printf Dumper $latest_release_hash;
my $tag = $latest_release_hash->{tag_name}; # Works
printf("The latest gitea version is $tag\n");
substr($tag, 0, 1, ''); substr($tag, 0, 1, '');
print("!! Tag is now $tag\n"); print("Latest upstream version is $tag\n");
my $download_filename='gitea-' . $tag . $filetype;
printf("Downloaded filename will be $download_filename\n");
# gitea-1.7.6-linux-amd64.xz
my $release_id = $latest_release_hash->{id}; # Works my $release_id = $latest_release_hash->{id}; # Works
printf("The release id for the latest gitea version is $release_id\n"); printf("Release id: $release_id\n");
my $unz_filename = 'gitea-' . $tag . $filetype;
printf("Uncompressed filename: $unz_filename\n");
#ex: gitea-1.7.6-linux-amd64
my $download_filename = $unz_filename . '.xz';
printf("Download 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");
backup_bin( \%binary_file );
if ( $tag gt $gitea_current_version || $alphanum_ver == 1 ) {
printf("Proceeding with update\n");
my $download_url = get_download_url( $download_filename, $release_id ); my $download_url = get_download_url( $download_filename, $release_id );
print("Downloading the compressed binary\n"); print("Downloading the compressed binary\n");
print("Download URL we got from the sub is $download_url\n"); print("Download URL we got from the sub is $download_url\n");
`wget -P $gitea_bin_dir $download_url`; `wget -P $gitea_bin_dir $download_url`;
my $xz_file = $gitea_bin_dir . $download_filename; my $xz_file = $gitea_bin_dir . $download_filename;
print("!!! xz file is $xz_file\n"); print("!!! xz file is $xz_file\n");
=devel
# Because doing this in perl would be a memory hog
`xz -d --keep /home/gitea/bin/$download_filename`
# Stop gitea service install_bin($download_filename);
# rename unpacked binary to gitea or symlink??
# chown gitea.gitea binary =devel
# chmod 754 gitea # TODO
# Check if we can start gitea
# If can start then # If can start then
# Remove old binary (backup done at beginning)
# Rename new binary to gitea # Rename new binary to gitea
# Remove .xz file # Remove .xz file
# Notify success notify(1) # Notify success notify(1)
@ -129,46 +90,94 @@ if ( $tag gt $gitea_current_version || $alphanum_ver == 1 ) {
=cut =cut
} }
my $status = check_gitea_status();
if( $status =~ /active/) {
print("[INFO] Gitea service is active\n");
}
unless( $status =~ /active/) {
die"[WARN] Status of gitea is no good!! OMGURD\n";
}
$gitea_current_version_string = `$gitea_bin --version`;
print("Running version of gitea is now $gitea_current_version_string");
chomp $gitea_current_version_string;
%binary_file = get_current_version($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");
}
else {
print("Write something here to call cleanup and remove the old xz file and uncompressed file\n");
}
#TODO Add check if running binary is the same version as what we downloaded
# # If install_bin good then cleanup()
# #notification( $email );
# ##### Subroutines #####
# sub check_deps {
# # Check to make sure dependencies installed on system - unit test??
# # bzip2 perl mods
# }
sub get_current_version {
# If install_bin good then cleanup() my $current_version_string=shift;
my $current_version;
#notification( $email ); if ( $current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) {
#if ( $current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s/ ) {
$current_version = $1;
printf("Version number of the installed gitea binary: $1\n");
##### Subroutines ##### }
elsif ( $current_version_string =~ m/ion\ (\d+\d+\w*)\s/ ) {
sub check_deps { $current_version = $1;
# Check to make sure dependencies installed on system - unit test?? printf("Current version number the installed gitea binary: $1\n");
# bzip2 $alphanum_ver = 1;
}
my %binary_file = (
input => $gitea_bin,
version => $current_version,
);
unless ( defined $current_version ) {
die "ERROR: Unable to get the current gitea version! Value is $current_version"
}
return %binary_file;
} }
# sub backup_bin { sub backup_bin {
# my ( %opts ) = %{shift()}; # Ghetto to get it working for now
# my $input = $opts{input};
# my $version = $opts{version}; my ( %opts ) = %{shift()};
# printf("Backing up the current binary\n"); my $input = $opts{input};
# printf("Input in backup_bin sub is $input\n and version is $version\n"); my $version = $opts{version};
# my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2';
# printf("Gitea bin backup will be $gitea_bin_backup\n"); # my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2';
# # Do this with a system call to make a compressed copy of the current binary # printf("Gitea bin backup will be $gitea_bin_backup\n");
# # bzip2 -k file.txt # printf("!! Input is $gitea_bin\n and output is $gitea_bin_backup\n");
# # my $status = system("vi", "fred.txt");
# # if (($status >>=8) != 0) { printf("Backing up the current binary\n");
# # die "Failed to run vi"; printf("Input in backup_bin sub is $input\n and version is $version\n");
# # } my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2';
# my $bzip_status = system("bzip2 -k", "$input"); printf("Gitea bin backup will be $gitea_bin_backup\n");
# if (($status >>=8) != 0) { system("cp $input $input.bak");
# die "Failed to run bzip2";
# } # TODO
# # # Do this with a system call to make a compressed copy of the current binary
# return; # # bzip2 -k file.txt
# } # # my $status = system("vi", "fred.txt");
# # if (($status >>=8) != 0) {
# # die "Failed to run vi";
# # }
# my $bzip_status = system("bzip2 -k", "$input");
# if (($status >>=8) != 0) {
# die "Failed to run bzip2";
# }
return;
}
sub get_download_url { sub get_download_url {
my ($dl_filename, $rel_id) = @_; my ($dl_filename, $rel_id) = @_;
@ -176,6 +185,7 @@ sub get_download_url {
print("Download filename in the sub is $dl_filename\n"); print("Download filename in the sub is $dl_filename\n");
# Get the download_url from the array # Get the download_url from the array
my $assetsURL = 'https://api.github.com/repos/go-gitea/gitea/releases/' . $release_id . '/assets'; my $assetsURL = 'https://api.github.com/repos/go-gitea/gitea/releases/' . $release_id . '/assets';
# For debugging
# printf("!!! Dump of assets URL"); # printf("!!! Dump of assets URL");
# printf("!!! $assetsURL\n"); # printf("!!! $assetsURL\n");
@ -187,41 +197,45 @@ sub get_download_url {
# Array of hashes # Array of hashes
foreach my $asset_ref (@{$asset_resp_array_ref}) { foreach my $asset_ref (@{$asset_resp_array_ref}) {
printf("!! Print the name\n");
# printf Dumper $asset_ref;
#print $asset_ref->{name} . "\n";
if ( $asset_ref->{name} eq $dl_filename ) { if ( $asset_ref->{name} eq $dl_filename ) {
print ("Yay we have a match :D and it is \n"); print ("Yay we have a match for our desired file :D and it is \n");
print $asset_ref->{name} . "\n"; print $asset_ref->{name} . "\n";
$dl_url = $asset_ref->{browser_download_url}; $dl_url = $asset_ref->{browser_download_url};
print("!! The download url is\n $dl_url \n"); print("!! The download url is $dl_url \n");
return $dl_url; return $dl_url;
} }
unless ( defined $dl_url ) {
print("ONOES we have no download URL!");
}
} }
print("The download URL returning from sub is $dl_url\n"); unless ( defined $dl_url ) {
print("ONOES we have no download URL!");
}
return; return;
} }
sub install_bin{ sub install_bin {
my $filename=shift;
#my $unz_binary =
# Stop service print("Unpacking the downloaded file, setting owner and permissions\n");
# Copy the downloaded binary to gitea and chmod it 750
# Doing this with a filthy system call because the perl library
# for xz manipulation is a memory hog and buggy besides
system("xz -d --keep /home/gitea/bin/$filename");
system("systemctl stop gitea");
system("cp $gitea_bin_dir/$unz_filename $gitea_bin");
system("chown gitea.gitea $gitea_bin");
system("chmod 750 $gitea_bin");
# Copy the downloaded binary to gitea and chmod it 750 # Restart service
print("Restarting the gitea service\n");
# Change owner unless we run this as user gitea system("systemctl restart gitea");
# Start service
# verify systemctld status is good
# return the status
return; return;
} }
sub check_gitea_status {
my $gitea_status=`systemctl is-active gitea`;
return $gitea_status;
}
sub cleanup { sub cleanup {
# remove downloaded binary and old backup set install_status=success ELSE remove "gitea" and restore from backup and set install_status=fail # remove downloaded binary and old backup set install_status=success ELSE remove "gitea" and restore from backup and set install_status=fail
return; return;