#!/usr/bin/perl # -*- mode: cperl; -*- use 5.40.2; use strict; use warnings; use Getopt::Long; use Pod::Usage; use File::Basename; use File::Path qw(make_path); use File::Copy; use POSIX qw(strftime); use Data::Dumper; $Data::Dumper::Terse = 1; =head1 NAME deploy - deploy the MyFitnessTracker web app to my vps =head1 SYNOPSIS deploy [--help] IMAGE HOST =head1 AUTHOR Marius Peter =head1 COPYRIGHT Copyright 2024 Marius Peter =cut GetOptions( test => \my $test ); my ( $source, $destination ) = @ARGV; if ( not defined $source ) { pod2usage "No development image specified."; } elsif ( not -e $source ) { pod2usage "$source does not exist."; } elsif ( not defined $destination ) { pod2usage "No destination host specified."; } my $iso8601 = strftime '%Y-%m-%dT%H:%M:%SZ', localtime; # Identify resources my ( $image_devel, $path_devel ) = fileparse $source; my ( $image_staging, $path_staging ) = ( "mft.image", "/tmp/mft-${iso8601}" ); open my $fh, '<', "${path_devel}/pharo.version"; chomp( my $pharo_version = <$fh> ); close $fh; say "Pharo version for $image_devel is $pharo_version."; my $pharo_vm = $ENV{HOME} . "/Pharo/vms/${pharo_version}-x64/pharo"; if ( -x $pharo_vm ) { say "Found Pharo VM $pharo_vm."; } else { die "$pharo_vm is not a valid Pharo VM executable path"; } # Staging image make_path $path_staging or die $!; say "Copying development image $image_devel to staging image..."; copy "${path_devel}/${image_devel}", "${path_staging}/${image_staging}" or die $!; my $script = "MFTUtils createDeploymentImage"; say "Building staging image at $path_staging..."; system "$pharo_vm --headless ${path_staging}/${image_staging} eval $script"; if ($?) { die "Failed to build staging image"; } else { say "Staging image built at ${path_staging}/${image_staging}."; } # Production image my $image_prod = "${destination}:/var/www/myfitnesstracker.io/${image_staging}"; say "Deploying production image at $image_prod..."; system 'rsync', qw( --verbose --progress --human-readable --compress ), "${path_staging}/${image_staging}", $image_prod; warn "You must manually log into $destination in order to restart mft.service."; # system 'ssh root@vps systemctl restart mft'; # if ($?) { # die "Couldn't restart mft.service on $destination" # } # else { # print "Successfully restarted mft.service on $destination.\n" # } say "$image_devel successfully deployed to $image_prod."