226 lines
8.0 KiB
Perl
Executable File
226 lines
8.0 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
#
|
|
# Copyright (c) 2019-2022 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 notify script for OpenBSD Amsterdam
|
|
# 2020/05/17 initial release
|
|
# 2021/05/09 complete restructure and KISS
|
|
# 2022/11/13 added price structure for notifications
|
|
#
|
|
use 5.024;
|
|
use strict;
|
|
use warnings;
|
|
use autodie;
|
|
use POSIX qw(strftime);
|
|
use HTTP::Tiny;
|
|
use File::Basename;
|
|
|
|
# get function and function_variable (vmid) from arguments
|
|
my $function = $ARGV[0] || "empty";
|
|
my $function_variable = $ARGV[1] || "empty";
|
|
|
|
# define the prices for the different components of the VM
|
|
my $base_price = '64';
|
|
my %memory_prices = ('2G' => '10', '4G' => '30', '8G' => '70');
|
|
my %hdd_prices = ('50G' => '50', '100G' => '100', '150G' => '150', '200G' => '200');
|
|
my %payment_links = (
|
|
'44' => 'https://buy.stripe.com/28odR0aWK0jq0aAeV0',
|
|
'64' => 'https://buy.stripe.com/8wMaEO0i67LS1eE288',
|
|
'74' => 'https://buy.stripe.com/aEU5kuc0Ofek4qQ001',
|
|
'84' => 'https://buy.stripe.com/9AQ8wG8OCfek9LaaEI',
|
|
'94' => 'https://buy.stripe.com/3cs28i6GuaY4f5ueUZ',
|
|
'114' => 'https://buy.stripe.com/14k00ae8W2ryg9y28a',
|
|
'124' => 'https://buy.stripe.com/cN2fZ81mac28g9ydQT',
|
|
'134' => 'https://buy.stripe.com/28o4gq0i67LSe1q8wD',
|
|
'144' => 'https://buy.stripe.com/cN214ec0O1nuf5u6ow',
|
|
'184' => 'https://buy.stripe.com/9AQ8wGe8W0jq1eEdQZ',
|
|
'194' => 'https://buy.stripe.com/bIY5kugh4gioe1qeV6',
|
|
'334' => 'https://buy.stripe.com/9AQfZ80i6gio8H64gq',
|
|
);
|
|
|
|
# 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;
|
|
}
|
|
|
|
sub mailout {
|
|
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 $_ip4netmask = $conf{'conf'}{'NETMASK'};
|
|
my $_ip4gw = $conf{'conf'}{'ROUTER'};
|
|
|
|
my $template = "$_tmpl/email-$function.txt";
|
|
my $server_number = $1 if $_server =~ /([0-9]+)/;
|
|
my $evenodd = $server_number % 2;
|
|
my $year = strftime("%Y", localtime);
|
|
my $month = strftime("%m", localtime);
|
|
|
|
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'} || '0';
|
|
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 $_username = $vms{$vm_name}{'username'};
|
|
my $_memory = $vms{$vm_name}{'memory'} || '';
|
|
my $_disk2 = $vms{$vm_name}{'disk2'} || '';
|
|
|
|
if ($_payment == 0) {
|
|
my $memory_price = $memory_prices{$_memory} || '0';
|
|
my $hdd_price = $hdd_prices{$_disk2} || '0';
|
|
$_payment = $base_price + $memory_price + $hdd_price;
|
|
}
|
|
my $stripe = $payment_links{$_payment} || '';
|
|
|
|
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
|
|
(my $_year, my $_month, my $_day) = split(/\//, $_date, 3);
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
|
my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n');
|
|
(my $ip6gw = $ip6address) =~ s/::.*/::1/;
|
|
|
|
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/FIRSTNAME/$_firstname/g;
|
|
$row =~ s/IPV4$/$ip4address/g;
|
|
$row =~ s/IPV4NETMASK$/$_ip4netmask/g;
|
|
$row =~ s/IPV4GW$/$_ip4gw/g;
|
|
$row =~ s/IPV6$/$ip6address/g;
|
|
$row =~ s/IPV6GW$/$ip6gw/g;
|
|
$row =~ s/VMID/$vm_name/g;
|
|
$row =~ s/SERVER/$_server/g;
|
|
$row =~ s/HOSTNAME/$_hostname/g;
|
|
$row =~ s/USERNAME/$_username/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;
|
|
$row =~ s/STRIPE/$stripe/g;
|
|
|
|
if ($row =~ /TIME\((.*)\)/) {
|
|
my @TIMES = split(/,/, $1);
|
|
$row =~ s/TIME\(.*\)/$TIMES[$evenodd]/g;
|
|
}
|
|
print $fh_email "$row\n";
|
|
}
|
|
close $fh_email;
|
|
print "$function: $_date, $_payment ($_payment), $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\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;
|
|
%conf = get_variables('conf', "$dir/_deploy.conf");
|
|
} elsif (-d "$prod") {
|
|
$dir = $prod;
|
|
%conf = get_variables('conf', "$dir/_deploy.conf");
|
|
} else {
|
|
printf "Unable to find config file\n";
|
|
exit 1;
|
|
}
|
|
|
|
# parse all vm*.txt files in the VMS directory
|
|
my @files = glob "$conf{'conf'}{'VMS'}/*.txt";
|
|
%vms = get_variables('vms', @files);
|
|
|
|
if ($function =~ /notify/) {
|
|
mailout(\%conf, \%vms);
|
|
} elsif ($function =~ /(deployed|redeployed|cpu|msg|thanx)/ and $function_variable !~ /empty/) {
|
|
my %slice = %vms{$function_variable};
|
|
mailout(\%conf, \%slice);
|
|
} elsif ($function =~ /(renewal|subscription|deprovision)/) {
|
|
my $year = strftime("%Y", localtime);
|
|
my $month = strftime("%m", localtime);
|
|
for my $vm_name (sort keys %vms) {
|
|
if ($vms{$vm_name}{'donated'} =~ /(done|expire|sponsor|renewal)/) { delete $vms{$vm_name}; next; }
|
|
|
|
my ($_vm_year, $_vm_month, $_vm_day) = split('/', $vms{$vm_name}{'date'});
|
|
if ($_vm_year >= $year) { delete $vms{$vm_name}; next; }
|
|
if ($_vm_month != $month) { delete $vms{$vm_name}; next; }
|
|
|
|
if ($function =~ /(renewal|deprovision)/) {
|
|
if (defined $vms{$vm_name}{'subscription'} and $vms{$vm_name}{'subscription'} ne "") { delete $vms{$vm_name}; next; }
|
|
}
|
|
if ($function =~ /subscription/) {
|
|
if (!defined $vms{$vm_name}{'subscription'} or $vms{$vm_name}{'subscription'} eq "" or $vms{$vm_name}{'subscription'} =~ /no/) { delete $vms{$vm_name}; next; }
|
|
}
|
|
}
|
|
mailout(\%conf, \%vms);
|
|
} elsif ($function =~ /stopped/) {
|
|
my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}');
|
|
for my $vm_name (sort keys %vms) {
|
|
if (!grep(/$vm_name/, @stopped_vms)) { delete $vms{$vm_name}; next; }
|
|
if ($vms{$vm_name}{'donated'} =~ /expire/) { delete $vms{$vm_name}; next; }
|
|
}
|
|
mailout(\%conf, \%vms);
|
|
} else {
|
|
print "usage: " . basename($0) . " [stopped | renewal | notify | deprovision] | [deployed | redeployed | cpu | msg | thanx] <vmid>\n";
|
|
}
|