add subscription option to notify.pl

This commit is contained in:
mischa 2021-02-17 09:25:20 +01:00
parent 6dc48ff8b8
commit 64e24d553a
5 changed files with 96 additions and 3 deletions

View File

@ -184,12 +184,14 @@ sub renewal {
for my $vm_name (sort keys %vms) {
my $_date = $vms{$vm_name}{'date'};
my $_payment = $vms{$vm_name}{'payment'};
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
my $_donated = $vms{$vm_name}{'donated'};
my $_name = $vms{$vm_name}{'name'};
my $_email = $vms{$vm_name}{'email'};
my $_hostname = $vms{$vm_name}{'hostname'};
my $_boot = $vms{$vm_name}{'boot'} || "yes";
if ($_subscription =~ /yes/) { next; }
if ($_donated =~ /done/) { next; }
if ($_donated =~ /expire/) { next; }
if ($_donated =~ /sponsor/) { next; }
@ -224,6 +226,73 @@ sub renewal {
}
}
sub subscription {
my %conf = %{$_[0]};
my %vms = %{$_[1]};
my $_etc = $conf{'conf'}{'ETC'};
my $_vms = $conf{'conf'}{'VMS'};
my $_tmpl = $conf{'conf'}{'TEMPLATES'};
my $_server = $conf{'conf'}{'SERVER'};
my $template = "$_tmpl/email-subscription.txt";
my $year = strftime("%Y", localtime);
my $month = strftime("%m", localtime);
#my $total_donated = qx(ftp -Vo- https://obsda.ms/index.html | grep "donated to the 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 -c ">Server " | tr -d '\n');
my $response = HTTP::Tiny->new->get('https://openbsd.amsterdam/index.html');
my $total_donated = $1 if $response->{'content'} =~ /([0-9,]+) donated to the OpenBSD/;
my $total_vms = $1 if $response->{'content'} =~ /([0-9]+) VMs deployed/;
$response = HTTP::Tiny->new->get('https://openbsd.amsterdam/servers.html');
my $total_hosts = () = $response->{'content'} =~ /(\>Server )/g;
for my $vm_name (sort keys %vms) {
my $_date = $vms{$vm_name}{'date'};
my $_payment = $vms{$vm_name}{'payment'};
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
my $_donated = $vms{$vm_name}{'donated'};
my $_name = $vms{$vm_name}{'name'};
my $_email = $vms{$vm_name}{'email'};
my $_hostname = $vms{$vm_name}{'hostname'};
my $_boot = $vms{$vm_name}{'boot'} || "yes";
if ($_subscription =~ /no/) { next; }
if ($_donated =~ /done/) { next; }
if ($_donated =~ /expire/) { next; }
if ($_donated =~ /sponsor/) { next; }
if ($_donated =~ /renewal/) { next; }
if ($_date =~ /$year\//) { next; }
if ($_date =~ /\/$month\//) {
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
(my $_year, my $_month, my $_day) = split(/\//, $_date, 3);
my $ipaddress = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
open(my $fh, '<', $template);
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
printf $fh_email "To: %s\n", $_email;
while (my $row = <$fh>) {
chomp $row;
$row =~ s/NAME/$_firstname/g;
$row =~ s/IP$/$ipaddress/g;
$row =~ s/VMID/$vm_name/g;
$row =~ s/SERVER/$_server/g;
$row =~ s/YEAR/$year/g;
$row =~ s/TOTAL_DONATED/$total_donated/g;
$row =~ s/TOTAL_VMS/$total_vms/g;
$row =~ s/TOTAL_HOSTS/$total_hosts/g;
$row =~ s/PAYMENT/$_payment/g;
print $fh_email "$row\n";
}
close $fh_email;
print "SUBSCRIPTION: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ipaddress\n";
}
}
}
# function to notify people
sub notify {
my %conf = %{$_[0]};
@ -286,12 +355,14 @@ sub deprovision {
for my $vm_name (sort keys %vms) {
my $_date = $vms{$vm_name}{'date'};
my $_payment = $vms{$vm_name}{'payment'};
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
my $_donated = $vms{$vm_name}{'donated'};
my $_name = $vms{$vm_name}{'name'};
my $_email = $vms{$vm_name}{'email'};
my $_hostname = $vms{$vm_name}{'hostname'};
my $_boot = $vms{$vm_name}{'boot'} || "yes";
if ($_subscription =~ /yes/) { next; }
if ($_donated =~ /done/) { next; }
if ($_donated =~ /expire/) { next; }
if ($_donated =~ /sponsor/) { next; }
@ -370,6 +441,7 @@ if ($debug) { debug_parse(\%conf, \%vms); }
if ($function =~ /deployed/) { deployed(\%conf, \%vms); }
elsif ($function =~ /stopped/) { stopped(\%conf, \%vms); }
elsif ($function =~ /renewal/) { renewal(\%conf, \%vms); }
elsif ($function =~ /subscription/) { subscription(\%conf, \%vms); }
elsif ($function =~ /notify/) { notify(\%conf, \%vms); }
elsif ($function =~ /deprovision/) { deprovision(\%conf, \%vms); }
else { print "Specify function: deployed, stopped, renewal, notify, deprovision\n"; }

View File

@ -0,0 +1,19 @@
From: admin
Subject: OpenBSD Amsterdam Renewal YEAR
Content-Type: text/plain; charset=utf-8
Hi NAME,
Your renewal fee for VMID on SERVER this year is €PAYMENT.
Total donated: TOTAL_DONATED
Total VMs deployed: TOTAL_VMS
Total hosts deployed: TOTAL_HOSTS
Payment will be done automatically.
Mischa
--
OpenBSD Amsterdam
https://obsda.ms

View File

@ -1,7 +1,7 @@
mac="fe:e1:ab:dd:73:a4"
ip="192.168.1.1"
owner="testuser1"
date="2018/01/02"
date="2018/02/02"
payment="70"
donated=""
name="Test1 User"

View File

@ -1,5 +1,6 @@
date="2018/07/02"
date="2018/02/02"
payment="70"
subscription=""
donated=""
name="Test2 User"
email="vm02@high5.nl"

View File

@ -1,5 +1,6 @@
date="2020/07/21"
date="2020/02/21"
payment="60"
subscription="yes"
donated=""
name="Test3"
email="vm03@high5.nl"