141 lines
3.2 KiB
Perl
141 lines
3.2 KiB
Perl
#!/usr/bin/perl
|
|
|
|
# Update gitea based on version provided
|
|
# Author: Tracey Clark
|
|
# Created: 2019-03-04
|
|
# You must have a mail transport agent installed to run this script
|
|
|
|
# Note: Daily backups are also being run from cron
|
|
# /home/gitea/gitea_backup.sh
|
|
|
|
use strict;
|
|
use warnings;
|
|
#use LWP::Simple;
|
|
use LWP::UserAgent;
|
|
use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
|
|
use Data::Dumper;
|
|
use JSON;
|
|
|
|
##########################################
|
|
### The user must set these variables ###
|
|
# SET email address
|
|
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_current_version_string = `$gitea_bin --version`;
|
|
my $gitea_current_version;
|
|
if ( $gitea_current_version_string =~ m/ion\ (\d+\.\d+\.\d*)\s+b/ ) {
|
|
$gitea_current_version = $1;
|
|
}
|
|
my %binary_file = (
|
|
input => $gitea_bin,
|
|
version => $gitea_current_version,
|
|
);
|
|
|
|
printf("The version of gitea that is running is \"$gitea_current_version\"\n");
|
|
|
|
# Backup the current binary
|
|
backup_bin( \%binary_file );
|
|
|
|
my $baseURL = q{https://github.com/go-gitea/gitea/};
|
|
my $latestURL = q{https://api.github.com/repos/go-gitea/gitea/releases/latest};
|
|
|
|
# https://hubpages.com/technology/Use-Perl-to-access-REST-API
|
|
# Spin up the browser object (via LWP::UserAgent)
|
|
my $ua = LWP::UserAgent->new(
|
|
cookie_jar => {}, # keep cookies in RAM but not persistent between sessions
|
|
);
|
|
|
|
my $resp = $ua->get($latestURL);
|
|
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};
|
|
printf("The latest gitea version is $tag\n");
|
|
my $assetsURL = $latest_release_hash->{tag_name};
|
|
|
|
if ( $tag gt $gitea_current_version ) {
|
|
printf("Proceeding with update\n");
|
|
download_bin($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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");
|
|
|
|
|
|
my $status = bzip2 $input => $gitea_bin_backup
|
|
or die "bzip2 failed: $Bzip2Error\n";
|
|
#$bz->close();
|
|
return;
|
|
}
|
|
|
|
sub download_bin {
|
|
my $dl_tag = shift;
|
|
my $dl_filename='gitea-' . $dl_tag . $filetype;
|
|
printf("Downloaded filename will be $dl_filename\n");
|
|
# Unzip downloaded gitea
|
|
|
|
# Get the download_url from the array
|
|
|
|
# Download version specified\
|
|
# $gitea_bin
|
|
# wget
|
|
|
|
# unxz $GITEA_XZ
|
|
return;
|
|
}
|
|
|
|
sub install_bin{
|
|
|
|
# Stop service
|
|
|
|
# Copy the downloaded binary to gitea and chmod it 750
|
|
|
|
# Change owner unless we run this as user gitea
|
|
|
|
# Start service
|
|
|
|
# verify systemctld status is good
|
|
|
|
# return the status
|
|
return;
|
|
}
|
|
|
|
sub cleanup {
|
|
# 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;
|
|
}
|