diff --git a/update-gitea.pl b/update-gitea.pl index d7ebd53..0bf29dc 100755 --- a/update-gitea.pl +++ b/update-gitea.pl @@ -12,7 +12,7 @@ use strict; use warnings; #use LWP::Simple; use LWP::UserAgent; -use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ; +#use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ; use Data::Dumper; use JSON; @@ -23,8 +23,10 @@ my $email='tclark77@tlcnet.info'; my $filetype='-linux-amd64.xz'; # https://api.github.com/repos/go-gitea/gitea/releases/latest -my $gitea_bin = '/home/gitea/bin/gitea'; +my $gitea_bin_dir = '/home/gitea/bin/'; +my $gitea_bin = $gitea_bin_dir . '/gitea'; my $gitea_current_version_string = `$gitea_bin --version`; +my $alphanum_ver = 0; unless ( defined $gitea_current_version_string ) { die "ERROR: Unable to get the version of the gitea binary: $!"; @@ -38,6 +40,8 @@ if ( $gitea_current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s/ ) { 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; + print("!! Alpha num is true\n"); } my %binary_file = ( input => $gitea_bin, @@ -65,30 +69,35 @@ my $latest_release_hash = decode_json($resp->content); # printf("!! Print the json response if we have one:\n"); # printf Dumper $latest_release_hash; -# In case we need to deref later -#my %latest_release_data = %$latest_release_hash; - my $tag = $latest_release_hash->{tag_name}; # Works printf("The latest gitea version is $tag\n"); +substr($tag, 0, 1, ''); +print("!! Tag is now $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 printf("The release id for the latest gitea version is $release_id\n"); -# Assets URL is -# GET /repos/:owner/:repo/releases/:release_id/assets -# Single asset URL is -# GET /repos/:owner/:repo/releases/assets/:asset_id -# https://api.github.com/repos/go-gitea/gitea/releases/16739869/assets -my $assetsURL = 'https://api.github.com/repos/go-gitea/gitea/releases/' . $release_id . '/assets'; -# printf("!!! Dump of assets URL"); -# printf("!!! $assetsURL\n"); - - -if ( $tag gt $gitea_current_version ) { +if ( $tag gt $gitea_current_version || $alphanum_ver == 1 ) { printf("Proceeding with update\n"); - download_bin($tag); - + my $download_url = get_download_url( $download_filename, $release_id ); + print("Downloading the compressed binary\n"); + 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"); +=devel + # Because doing this in perl would be a memory hog + `xz -d --keep /home/gitea/bin/$download_filename` + # rename unpacked binary to gitea or symlink?? + # Check if we can start gitea + # If can start then end + # If no start restore the backup + # Notifications +=cut } @@ -114,27 +123,42 @@ sub backup_bin { my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2'; printf("Gitea bin backup will be $gitea_bin_backup\n"); -=devel - my $status = bzip2 $input => $gitea_bin_backup - or die "bzip2 failed: $Bzip2Error\n"; - printf("!! Status of backup operation is $status\n"); - #$bz->close(); -=cut return; } -sub download_bin { - my $dl_tag = shift; - my $dl_filename='gitea-' . $dl_tag . $filetype; - printf("Downloaded filename will be $dl_filename\n"); +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'; + # printf("!!! Dump of assets URL"); + # printf("!!! $assetsURL\n"); -# Get the download_url from the array + my $asset_resp = $ua->get($assetsURL); + my $asset_resp_array_ref = decode_json($asset_resp->content); -# Download version specified\ - # $gitea_bin - # wget + # printf("!! Print the json response for the assets:\n"); + # printf Dumper $asset_resp_array_ref; + # Array of hashes - # unxz $GITEA_XZ + 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 ) { + print ("Yay we have a match :D and it is \n"); + print $asset_ref->{name} . "\n"; + $dl_url = $asset_ref->{browser_download_url}; + print("!! The download url is\n $dl_url \n"); + 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"); return; }