Update 'update-gitea.pl' - Finished download sub and code to extract binary from xz file

This commit is contained in:
Tracey Clark 2019-04-18 21:22:05 -05:00
commit 6098f6edd7

View file

@ -12,8 +12,7 @@ use strict;
use warnings; use warnings;
#use LWP::Simple; #use LWP::Simple;
use LWP::UserAgent; use LWP::UserAgent;
use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ; #use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
# use Compress::Bzip2 qw(:all :constant :utilities :gzip); # cant get output
use Data::Dumper; use Data::Dumper;
use JSON; use JSON;
@ -23,53 +22,37 @@ use JSON;
my $email='tclark77@tlcnet.info'; my $email='tclark77@tlcnet.info';
my $filetype='-linux-amd64.xz'; my $filetype='-linux-amd64.xz';
# gitea is hosted on github
# GET /repos/go-gitea/gitea/releases/latest
# 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/';
=json my $gitea_bin = $gitea_bin_dir . '/gitea';
$VAR1 = {
'upload_url' => 'https://uploads.github.com/repos/go-gitea/gitea/releases/15817548/assets{?name,label}',
'assets_url' => 'https://api.github.com/repos/go-gitea/gitea/releases/15817548/assets',
'tag_name' => 'v1.7.3',
'author' => {
'gravatar_id' => '',
etc
},
=cut
my $gitea_bin = '/home/gitea/bin/gitea';
my $gitea_current_version_string = `$gitea_bin --version`; my $gitea_current_version_string = `$gitea_bin --version`;
my $gitea_current_version; my $alphanum_ver = 0;
if ( $gitea_current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) {
$gitea_current_version = $1; unless ( defined $gitea_current_version_string ) {
die "ERROR: Unable to get the version of the gitea binary: $!";
}
my $gitea_current_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;
print("!! Alpha num is true\n");
}
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"
} }
printf("The version of gitea that is running is \"$gitea_current_version\"\n");
# Backup the current binary # Backup the current binary
my $gitea_bin_backup = '/home/gitea/bin/gitea.' . $gitea_current_version . '.bz2'; backup_bin( \%binary_file );
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
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";
# bzip2 $input => "$gitea_bin_backup"
# or die "bzip2 failed: $Bzip2Error\n"; # This works, but writes garbage to end of filename
# need to do in OO way
# https://metacpan.org/pod/IO::Compress::Bzip2
#print $bz $gitea_bin; # This writes the filename as string to the bz
#$bz->close();
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};
@ -86,28 +69,109 @@ my $latest_release_hash = decode_json($resp->content);
# printf("!! Print the json response if we have one:\n"); # printf("!! Print the json response if we have one:\n");
# printf Dumper $latest_release_hash; # printf Dumper $latest_release_hash;
# In case we need to deref later my $tag = $latest_release_hash->{tag_name}; # Works
#my %latest_release_data = %$latest_release_hash;
my $tag = $latest_release_hash->{tag_name};
printf("The latest gitea version is $tag\n"); 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
if ( $tag gt $gitea_current_version ) { my $release_id = $latest_release_hash->{id}; # Works
printf("The release id for the latest gitea version is $release_id\n");
if ( $tag gt $gitea_current_version || $alphanum_ver == 1 ) {
printf("Proceeding with update\n"); printf("Proceeding with update\n");
my $dl_filename='gitea-' . $tag . $filetype; my $download_url = get_download_url( $download_filename, $release_id );
printf("Downloaded filename will be $dl_filename\n"); 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`
# Stop gitea service
# rename unpacked binary to gitea or symlink??
# chown gitea.gitea binary
# chmod 754 gitea
# Check if we can start gitea
# If can start then
# Remove old binary (backup done at beginning)
# Rename new binary to gitea
# Remove .xz file
# Notify success notify(1)
# If no start notify and end & notify fail notify(0)
=cut
} }
# Get the download_url from the array
# Download version specified\
# $gitea_bin
# wget
# Unzip downloaded gitea
# unxz $GITEA_XZ
# Stop service
# If install_bin good then cleanup()
#notification( $email );
##### Subroutines #####
sub backup_bin {
my ( %opts ) = %{shift()};
my $input = $opts{input};
my $version = $opts{version};
printf("Backing up the current binary\n");
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';
printf("Gitea bin backup will be $gitea_bin_backup\n");
return;
}
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");
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
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;
}
sub install_bin{
# Stop service
# Copy the downloaded binary to gitea and chmod it 750 # Copy the downloaded binary to gitea and chmod it 750
@ -117,7 +181,17 @@ if ( $tag gt $gitea_current_version ) {
# verify systemctld status is good # verify systemctld status is good
# If good then remove downloaded binary and old backup set install_status=success ELSE remove "gitea" and restore from backup and set install_status=fail # return the status
return;
}
# Notification sub cleanup {
# $install_status message # remove downloaded binary and old backup set install_status=success ELSE remove "gitea" and restore from backup and set install_status=fail
return;
}
sub notification {
my $to = shift;
return;
}