deploy.pl/_archive/notify-renewal.pl
2024-05-20 11:26:09 +00:00

184 lines
6.6 KiB
Perl
Executable File

#!/usr/bin/env perl
#
# Copyright (c) 2018-2020 Mischa Peters <mischa @ openbsd.amsterdam>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# vmm(4)/vmd(8) VM renewal notify script for OpenBSD Amsterdam
# 2019/04/13 initial release
# 2019/11/29 changed: merged notify.pl and reminder.pl, match on month, calculate months automatically
# 2019/12/03 added: vm_name and _server to make it easier to identify which vm.
# 2020/05/11 added: use local relay to send out email
#
use 5.024;
use strict;
use warnings;
use autodie;
use POSIX qw(strftime);
my $week = $ARGV[0] || 3;
my $total_donated = qx(ftp -Vo- https://obsda.ms/index.html | grep "donated to OpenBSD" | awk -F';' '{print \$4}' | awk '{printf \$1}');
my $total_vms = qx(ftp -Vo- https://obsda.ms/index.html | grep "deployed" | awk -F '>' '{print \$3}' | awk '{printf \$1}');
my $total_hosts = qx(ftp -Vo- https://obsda.ms/servers.html | grep ">Server " | wc -l | tr -d ' ' | tr -d '\n');
# fuction to parse _deploy.conf and vm*.txt files
# all variables are stripped and added to either %vms or %conf
sub get_variables {
my ($hash_name, @files) = @_;
my %hash;
my $filename;
my $vm_name;
my $vm_number;
for my $file (@files) {
# When hash is 'vms' use the vm_name as key
# Otherwise use 'conf' as key
if ($hash_name eq "vms") {
($filename = $file) =~ s/.*\///;
($vm_name = $filename) =~ s/\.txt//;
($vm_number = $vm_name) =~ s/^vm//;
$hash{$vm_name}{'vm_number'} = $vm_number;
}
open my $fh, "<", "$file";
while (my $row = <$fh>) {
next if ($row =~ /^\s*($|#)/);
chomp ($row);
(my $key, my $val) = split(/=/, $row, 2);
if ($hash_name eq "vms") {
($hash{$vm_name}{$key} .= $val) =~ s/^"+|"+$//g;
} else {
($hash{$hash_name}{$key} .= $val) =~ s/^"+|"+$//g;
}
}
close $fh;
}
return %hash;
}
# function to notify people
sub notify {
my %conf = %{$_[0]};
my %vms = %{$_[1]};
my $_server = $conf{'conf'}{'SERVER'};
my $_vms = $conf{'conf'}{'VMS'};
my $year = strftime ("%Y", localtime);
my $month = strftime ("%m", localtime);
my $day = strftime ("%d", localtime);
my $timestamp = strftime "%a, %d %b %Y %H:%M:%S %z", localtime;
for my $vm_name (sort keys %vms) {
my $_date = $vms{$vm_name}{'date'};
my $_payment = $vms{$vm_name}{'payment'};
my $_donated = $vms{$vm_name}{'donated'};
my $_name = $vms{$vm_name}{'name'};
my $_email = $vms{$vm_name}{'email'};
my $_hostname = $vms{$vm_name}{'hostname'};
if ($_donated =~ /done/) { next; }
if ($_donated =~ /expire/) { next; }
if ($_donated =~ /sponsor/) { next; }
if ($_donated =~ /renewal/) { next; }
if ($_date =~ /$year\//) { next; }
if ($_date =~ /\/$month\//) {
(my $_year, my $_month, my $_day) = split(/\//, $_date, 3);
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
my $membership = ($year - $_year) * 12;
print "RENEW: $_date ($membership), $_payment, $_name, $_email, $_hostname, $_server ($vm_name)\n";
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
printf $fh_email "To: <%s>\n", $_email;
printf $fh_email "From: Mischa <mischa\@openbsd.amsterdam>\n";
printf $fh_email "Date: %s\n", $timestamp;
if ($week == 3) {
printf $fh_email "Subject: OpenBSD Amsterdam Renewal $year\n\n";
} else {
printf $fh_email "Subject: OpenBSD Amsterdam Renewal $year Reminder\n\n";
}
printf $fh_email "Hi %s,\n\n", $_firstname;
printf $fh_email "First and foremost, we thank you for your continued support!\n\n";
printf $fh_email "Can you believe that it's been $membership months since you started with\n";
printf $fh_email "your VM?\n\n";
printf $fh_email "Since that time, your support has enabled us to reach the following\n";
printf $fh_email "milestones:\n\n";
printf $fh_email "1) Donate €$total_donated to the OpenBSD Foundation\n";
printf $fh_email "2) Deploy $total_vms VMs\n";
printf $fh_email "3) Deploy $total_hosts hosts\n\n";
printf $fh_email "We really hope you have been happy with the service provided, if so,\n";
printf $fh_email "please consider renewing now. You can find all the options to renew at:\n\n";
printf $fh_email "https://openbsd.amsterdam/pay.html\n\n";
printf $fh_email "Your renewal fee for %s on %s this year is €%d.\n", $vm_name, $_server, $_payment;
printf $fh_email "For every renewal we will donate €15 to the OpenBSD Foundation.\n\n";
printf $fh_email "When you want to cancel your VM please let us know, when we don't\n";
if ($week == 1) {
printf $fh_email "hear from you within the next week we will remove your VM.\n\n";
} else {
printf $fh_email "hear from you within the next $week weeks we will remove your VM.\n\n";
}
printf $fh_email "Mischa\n\n";
printf $fh_email "--\n";
printf $fh_email "OpenBSD Amsterdam\n";
printf $fh_email "https://obsda.ms\n";
close $fh_email;
}
}
}
# function to print all keys & values for debug purposes
sub debug_parse {
my %conf = %{$_[0]};
my %vms = %{$_[1]};
print "All VMs\n##\n";
for my $vm_name (sort keys %vms) {
my $_date = $vms{$vm_name}{'date'};
my $_payment = $vms{$vm_name}{'payment'};
my $_donated = $vms{$vm_name}{'donated'};
my $_name = $vms{$vm_name}{'name'};
my $_email = $vms{$vm_name}{'email'};
my $_hostname = $vms{$vm_name}{'hostname'};
print "$_date, $_payment, $_name, $_email, $_hostname, ($vm_name)\n";
}
print "##\n";
}
# check if _deploy.conf exists
my $dev = $ENV{'HOME'} . "/openbsd.amsterdam/deploy.pl";
my $prod = $ENV{'HOME'};
my $dir;
my $debug;
my %conf;
my %vms;
if (-d "$dev") {
$dir = $dev;
$debug = 1;
} else {
$dir = $prod;
}
if (-e "$dir/_deploy.conf") {
%conf = get_variables('conf', "$dir/_deploy.conf");
} else {
printf "Unable to find config file in current directory (%s).\n", $dir;
printf "Create the config file _deploy.conf in %s.\n", $dir;
exit 1;
}
# parse all vm*.txt files in the VMS directory
my @files = glob "$conf{'conf'}{'VMS'}/*.txt";
%vms = get_variables('vms', @files);
# run all functions
if ($debug) { debug_parse(\%conf, \%vms); }
notify(\%conf, \%vms);