diff --git a/update-gitea.pl b/update-gitea.pl index ab995eb..a42a725 100755 --- a/update-gitea.pl +++ b/update-gitea.pl @@ -18,6 +18,15 @@ use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ; use Data::Dumper; use JSON; use Sort::Versions; +use Getopt::Long qw(GetOptions); +use Term::ANSIColor qw(:constants); + +my ($verbose, $info); + +GetOptions( + 'info!' => \$info, + 'verbose!' => \$verbose, +); ########################################## ### The user must set these variables ### @@ -26,6 +35,7 @@ my $email='tclark77@tlcnet.info'; my $filetype='-linux-amd64'; # https://api.github.com/repos/go-gitea/gitea/releases/latest +# This URL will only have one version listed my $gitea_bin_dir = '/home/gitea/bin/'; my $gitea_bin = $gitea_bin_dir . '/gitea'; my $gitea_current_version_string = `$gitea_bin --version`; @@ -35,7 +45,7 @@ chomp $gitea_current_version_string; print("System is running \"$gitea_current_version_string\"\n"); unless ( defined $gitea_current_version_string ) { - die "ERROR: Unable to get the version of the gitea binary on the system: $!"; + die "[DIE]: Unable to get the version of the gitea binary on the system: $!"; } my %binary_file = get_current_version($gitea_current_version_string); @@ -51,9 +61,10 @@ my $ua = LWP::UserAgent->new( my $resp = $ua->get($latestURL); my $latest_release_hash = decode_json($resp->content) - or die "ERROR: Unable to parse the version data! Output is:\n $resp"; + or die "[DIE]: Unable to parse the version data! Output is:\n $resp"; -my $tag = $latest_release_hash->{tag_name}; +my $orig_tag = $latest_release_hash->{tag_name}; +my $tag = $orig_tag; substr($tag, 0, 1, ''); print("Latest upstream version is $tag\n"); my $release_id = $latest_release_hash->{id}; # Works @@ -76,7 +87,10 @@ if ( versioncmp($gitea_current_version, $tag) == -1 ) { print("Download URL we got from the sub is $download_url\n"); `wget -P $gitea_bin_dir $download_url`; my $xz_file = $gitea_bin_dir . $download_filename; - print("!!! xz file is $xz_file\n"); + + if (defined $info) { + print YELLOW "[INFO] xz file is $xz_file\n", RESET; + } install_bin($download_filename); @@ -92,13 +106,12 @@ if ( versioncmp($gitea_current_version, $tag) == -1 ) { my $status = check_gitea_status(); if( $status =~ /active/) { - print("[INFO] Gitea service is active\n"); + print(GREEN "[INFO] Gitea service is active\n", RESET); } unless( $status =~ /active/) { - die"[WARN] Status of gitea is no good!! OMGURD\n"; + die RED "[DIE] Status of gitea service is no good! OMGURD\n", RESET; } $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'}; @@ -128,7 +141,6 @@ sub get_current_version { my $current_version_string=shift; my $current_version; 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"); } @@ -142,7 +154,7 @@ sub get_current_version { version => $current_version, ); unless ( defined $current_version ) { - die "ERROR: Unable to get the current gitea version! Value is $current_version" + die RED "[DIE]: Unable to get the current gitea version! Value is $current_version", RESET } return %binary_file; } @@ -153,15 +165,12 @@ sub backup_bin { my ( %opts ) = %{shift()}; my $input = $opts{input}; 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"); -# printf("!! Input is $gitea_bin\n and output is $gitea_bin_backup\n"); - - printf("Backing up the current binary\n"); - printf("Input in backup_bin sub is $input\n and version is $version\n"); + print GREEN "Backing up the current binary\n", RESET; + if (defined $info) { + print YELLOW "[INFO] Input in backup_bin sub is:\n Directory $input\n Version $version\n", RESET; + } my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2'; - printf("Gitea bin backup will be $gitea_bin_backup\n"); + print GREEN "Gitea bin backup will be $gitea_bin_backup\n", RESET; system("cp $input $input.bak"); # TODO @@ -169,11 +178,11 @@ sub backup_bin { # # bzip2 -k file.txt # # my $status = system("vi", "fred.txt"); # # if (($status >>=8) != 0) { - # # die "Failed to run vi"; + # # die "[DIE] Failed to run vi"; # # } # my $bzip_status = system("bzip2 -k", "$input"); # if (($status >>=8) != 0) { - # die "Failed to run bzip2"; + # die "[DIE] Failed to run bzip2"; # } return; @@ -182,38 +191,63 @@ sub backup_bin { sub get_download_url { my ($dl_filename, $rel_id) = @_; my $dl_url; - print("Download filename in the sub is $dl_filename\n"); # Get the download_url from the array my $assetsURL = 'https://api.github.com/repos/go-gitea/gitea/releases/' . $release_id . '/assets'; - # For debugging - # printf("!!! Dump of assets URL"); - # printf("!!! $assetsURL\n"); + if (defined $info) { + print YELLOW "[INFO] Assets URL\n"; + print "$assetsURL\n", RESET; + } my $asset_resp = $ua->get($assetsURL); my $asset_resp_array_ref = decode_json($asset_resp->content); - # printf("!! Print the json response for the assets:\n"); - # printf Dumper $asset_resp_array_ref; - # Array of hashes + if (defined $verbose) { + printf(RED "[DEBUG] Array of hashes of the json response for the assets:\n"); + printf Dumper $asset_resp_array_ref, RESET; + } foreach my $asset_ref (@{$asset_resp_array_ref}) { + if (defined $verbose) { + print(RED "[DEBUG] The asset ref in get_download_url is $asset_ref\n"); + print("The asset ref name in get_download_url is $asset_ref->{name}\n", RESET); + } if ( $asset_ref->{name} eq $dl_filename ) { print ("Yay we have a match for our desired file :D and it is \n"); print $asset_ref->{name} . "\n"; $dl_url = $asset_ref->{browser_download_url}; - print("!! The download url is $dl_url \n"); + if (defined $info) { + print YELLOW "[INFO] The download url from the response array is $dl_url\n", RESET; + } return $dl_url; } } unless ( defined $dl_url ) { - print("ONOES we have no download URL!"); + print( RED "ONOES we have no download URL from the github API! Trying to build one manually\n", RESET); + # die RED "[DIE] Can't continue without a download URL", RESET; + print BLUE "Tag we are passing *into* the build_download_url from get_download_url is $orig_tag\n", RESET; + my $dl_url = build_download_url($orig_tag, $download_filename); + return $dl_url; } - return; + return 0; +} + +sub build_download_url { + my ($url_tag, $url_file) = @_; + print BLUE "Tag passed into build_download_url is $url_tag\n", RESET; + # If getting the download URL fails through the API, use this to build it manually + # URL is in the form of + # https://github.com/go-gitea/gitea/releases/download/v1.15.2/gitea-1.15.2-linux-amd64.xz + my $built_url = 'https://github.com/go-gitea/gitea/releases/download/' . $url_tag . '/' . $url_file; + if (defined $info) { + print YELLOW "The URL built for the download is $built_url\n", RESET; + } + return $built_url; + } sub install_bin { my $filename=shift; - #my $unz_binary = + #my $unz_binary = print("Unpacking the downloaded file, setting owner and permissions\n"); # Copy the downloaded binary to gitea and chmod it 750