Updated bin directory to be a variable, updated for Ubuntu. Added cleanup of downloaded files

This commit is contained in:
Tracey Clark 2022-01-16 12:41:14 -06:00
commit 1aeff36818

View file

@ -10,6 +10,9 @@
# Re-write to use this dir according to gitea docs # Re-write to use this dir according to gitea docs
# https://docs.gitea.io/en-us/install-from-binary/ # https://docs.gitea.io/en-us/install-from-binary/
# Get binary from system rather than hard code
# Stop gitea with systemctl before file copy
# Set right mod & perms?
use strict; use strict;
use warnings; use warnings;
@ -20,6 +23,7 @@ use JSON;
use Sort::Versions; 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);
my ( $verbose, $info ); my ( $verbose, $info );
@ -36,14 +40,12 @@ 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
my $gitea_bin_dir = '/home/gitea/bin/'; my $gitea_bin_dir = '/usr/local/bin/';
my $gitea_bin = $gitea_bin_dir . '/gitea'; 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; 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 die
"[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: $!";
@ -69,15 +71,17 @@ my $tag = $orig_tag;
substr( $tag, 0, 1, '' ); substr( $tag, 0, 1, '' );
print("Latest upstream version is $tag\n"); print("Latest upstream version is $tag\n");
my $release_id = $latest_release_hash->{id}; # Works my $release_id = $latest_release_hash->{id}; # Works
printf("Release id: $release_id\n");
my $unz_filename = 'gitea-' . $tag . $filetype; my $unz_filename = 'gitea-' . $tag . $filetype;
printf("Uncompressed filename: $unz_filename\n");
#ex: gitea-1.7.6-linux-amd64 #ex: gitea-1.7.6-linux-amd64
my $download_dir = '/home/gitea/bin/';
my $download_filename = $unz_filename . '.xz'; my $download_filename = $unz_filename . '.xz';
printf("Download file target: $download_filename\n");
if ( defined $verbose ) {
printf( YELLOW
"[INFO]\nRelease id: $release_id\nUncompressed filename: $unz_filename\nDownload file target: $download_filename\n"
);
}
#ex: gitea-1.7.6-linux-amd64.xz #ex: gitea-1.7.6-linux-amd64.xz
@ -99,15 +103,6 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
install_bin($download_filename); install_bin($download_filename);
=devel
# TODO
# If can start then
# Rename new binary to gitea
# Remove .xz file
# Notify success notify(1)
# If no start notify and end & notify fail notify(0)
=cut
} }
my $status = check_gitea_status(); my $status = check_gitea_status();
@ -128,15 +123,15 @@ if ( versioncmp( $gitea_current_version, $tag ) == -1 ) {
); );
} }
else { else {
print( say "Cleanup and remove the old xz file and uncompressed file";
"Write something here to call cleanup and remove the old xz file and uncompressed file\n" say "Unzipped filename in cleanup is " . $download_dir . $unz_filename;
); system("rm -vf " . $download_dir . $unz_filename);
system("rm -vf " . $download_dir . $download_filename);
# 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 #TODO Add check if running binary is the same version as what we downloaded
# # If install_bin good then cleanup()
# #notification( $email ); # #notification( $email );
# ##### Subroutines ##### # ##### Subroutines #####
@ -164,7 +159,7 @@ sub get_current_version {
); );
unless ( defined $current_version ) { unless ( defined $current_version ) {
die RED die RED
"[DIE]: Unable to get the current gitea version! Value is $current_version", "[DIE]: Unable to get the current gitea version! Value is $current_version",
RESET; RESET;
} }
return %binary_file; return %binary_file;
@ -180,7 +175,7 @@ sub backup_bin {
print GREEN "Backing up the current binary\n", RESET; print GREEN "Backing up the current binary\n", RESET;
if ( defined $info ) { if ( defined $info ) {
print YELLOW print YELLOW
"[INFO] Input in backup_bin sub is:\n Directory $input\n Version $version\n", "[INFO] Input in backup_bin sub is:\n Directory $input\n Version $version\n",
RESET; RESET;
} }
my $gitea_bin_backup = my $gitea_bin_backup =
@ -232,7 +227,7 @@ sub get_download_url {
print( RED print( RED
"[DEBUG] The asset ref in get_download_url is $asset_ref\n" ); "[DEBUG] The asset ref in get_download_url is $asset_ref\n" );
print( print(
"The asset ref name in get_download_url is $asset_ref->{name}\n", "The asset ref name in get_download_url is $asset_ref->{name}\n",
RESET RESET
); );
} }
@ -242,7 +237,7 @@ sub get_download_url {
$dl_url = $asset_ref->{browser_download_url}; $dl_url = $asset_ref->{browser_download_url};
if ( defined $info ) { if ( defined $info ) {
print YELLOW print YELLOW
"[INFO] The download url from the response array is $dl_url\n", "[INFO] The download url from the response array is $dl_url\n",
RESET; RESET;
} }
return $dl_url; return $dl_url;
@ -250,7 +245,7 @@ sub get_download_url {
} }
unless ( defined $dl_url ) { unless ( defined $dl_url ) {
print( RED print( RED
"ONOES we have no download URL from the github API! Trying to build one manually\n", "ONOES we have no download URL from the github API! Trying to build one manually\n",
RESET RESET
); );
my $dl_url = build_download_url( $orig_tag, $download_filename ); my $dl_url = build_download_url( $orig_tag, $download_filename );
@ -286,9 +281,9 @@ sub install_bin {
# Copy the downloaded binary to gitea and chmod it 750 # Copy the downloaded binary to gitea and chmod it 750
# Doing this with a filthy system call because the perl library # Doing this with a filthy system call because the perl library
# for xz manipulation is a memory hog and buggy besides # for xz manipulation is a memory hog and buggy besides
system("xz -d --keep /home/gitea/bin/$filename"); system("xz -d --keep $download_dir . $filename");
system("systemctl stop gitea"); system("systemctl stop gitea");
system("cp $gitea_bin_dir/$unz_filename $gitea_bin"); system("cp $download_dir/$unz_filename $gitea_bin");
system("chown gitea.gitea $gitea_bin"); system("chown gitea.gitea $gitea_bin");
system("chmod 750 $gitea_bin"); system("chmod 750 $gitea_bin");