2020-05-17 17:40:46 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
#
|
2023-05-05 23:04:44 +02:00
|
|
|
# Copyright (c) 2019-2023 Mischa Peters <mischa @ openbsd.amsterdam>
|
2020-05-17 17:40:46 +02:00
|
|
|
#
|
|
|
|
# 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 09:55:58 +02:00
|
|
|
# 2021/05/09 complete restructure and KISS
|
2022-11-13 21:14:11 +01:00
|
|
|
# 2022/11/13 added price structure for notifications
|
2023-05-05 23:04:44 +02:00
|
|
|
# 2023/05/05 variable clean up, rework IP logic
|
2024-01-02 12:28:47 +01:00
|
|
|
# 2023/12/03 price increase, +3
|
2020-05-17 17:40:46 +02:00
|
|
|
#
|
|
|
|
use 5.024;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use autodie;
|
2021-08-04 18:08:29 +02:00
|
|
|
use File::Basename;
|
2023-05-05 23:04:44 +02:00
|
|
|
use HTTP::Tiny;
|
|
|
|
use POSIX qw(strftime);
|
2020-05-17 17:40:46 +02:00
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
# get function and function_variable (vmid) from arguments
|
2020-05-17 17:40:46 +02:00
|
|
|
my $function = $ARGV[0] || "empty";
|
|
|
|
my $function_variable = $ARGV[1] || "empty";
|
2023-05-06 14:15:50 +02:00
|
|
|
my $message = $ARGV[2] || "empty";
|
2020-05-17 17:40:46 +02:00
|
|
|
|
2024-05-09 20:33:27 +02:00
|
|
|
if ($function_variable =~ m/.txt/) {
|
|
|
|
$function_variable = substr $function_variable, 0, -4;
|
|
|
|
$function_variable = substr $function_variable, 4;
|
|
|
|
say $function_variable;
|
|
|
|
}
|
2024-05-09 20:26:27 +02:00
|
|
|
|
2022-11-13 21:14:11 +01:00
|
|
|
# define the prices for the different components of the VM
|
2024-01-02 12:28:47 +01:00
|
|
|
my $base_price = '67';
|
2022-11-13 21:14:11 +01:00
|
|
|
my %memory_prices = ('2G' => '10', '4G' => '30', '8G' => '70');
|
|
|
|
my %hdd_prices = ('50G' => '50', '100G' => '100', '150G' => '150', '200G' => '200');
|
2023-05-05 23:04:44 +02:00
|
|
|
my $ideal_url;
|
|
|
|
my $paypal_url;
|
|
|
|
my %stripe_urls = (
|
|
|
|
'sponsor' => 'SPONSORED',
|
|
|
|
'sponsored' => 'SPONSORED',
|
2024-01-02 12:28:47 +01:00
|
|
|
'47' => 'https://buy.stripe.com/28odR0aWK0jq0aAeV0',
|
|
|
|
'67' => 'https://buy.stripe.com/8wMaEO0i67LS1eE288',
|
|
|
|
'77' => 'https://buy.stripe.com/aEU5kuc0Ofek4qQ001',
|
|
|
|
'97' => 'https://buy.stripe.com/3cs28i6GuaY4f5ueUZ',
|
|
|
|
'117' => 'https://buy.stripe.com/14k00ae8W2ryg9y28a',
|
|
|
|
'127' => 'https://buy.stripe.com/cN2fZ81mac28g9ydQT',
|
|
|
|
'147' => 'https://buy.stripe.com/cN214ec0O1nuf5u6ow',
|
|
|
|
'277' => 'https://buy.stripe.com/28o5kuaWK6HO3mMeV8',
|
|
|
|
'337' => 'https://buy.stripe.com/9AQfZ80i6gio8H64gq',
|
2023-04-23 09:27:57 +02:00
|
|
|
);
|
2022-11-13 21:14:11 +01:00
|
|
|
|
2020-05-17 17:40:46 +02:00
|
|
|
# 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;
|
|
|
|
}
|
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
sub mailout {
|
2020-05-17 17:40:46 +02:00
|
|
|
my %conf = %{$_[0]};
|
|
|
|
my %vms = %{$_[1]};
|
|
|
|
|
2023-05-05 23:04:44 +02:00
|
|
|
my $template = "$conf{'conf'}{'TEMPLATES'}/email-$function.txt";
|
|
|
|
my $server_number = $1 if $conf{'conf'}{'SERVER'} =~ /([0-9]+)/;
|
|
|
|
my $evenodd = $server_number % 2 if $server_number;
|
2020-05-17 17:40:46 +02:00
|
|
|
my $year = strftime("%Y", localtime);
|
|
|
|
my $month = strftime("%m", localtime);
|
2021-02-17 09:25:20 +01:00
|
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get('https://openbsd.amsterdam/index.html');
|
2022-12-05 17:30:01 +01:00
|
|
|
my $total_donated = $1 if $response->{'content'} =~ /([0-9,\.]+) donated to the OpenBSD/;
|
2021-02-17 09:25:20 +01:00
|
|
|
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'};
|
2023-05-05 23:04:44 +02:00
|
|
|
my $_payment = $vms{$vm_name}{'payment'} || 0;
|
2021-02-17 09:25:20 +01:00
|
|
|
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
|
|
|
|
my $_donated = $vms{$vm_name}{'donated'};
|
|
|
|
my $_name = $vms{$vm_name}{'name'};
|
2023-05-05 23:04:44 +02:00
|
|
|
my ($_firstname, $_lastname) = split(/ /, $_name, 2);
|
2021-02-17 09:25:20 +01:00
|
|
|
my $_email = $vms{$vm_name}{'email'};
|
|
|
|
my $_hostname = $vms{$vm_name}{'hostname'};
|
2021-05-09 09:55:58 +02:00
|
|
|
my $_username = $vms{$vm_name}{'username'};
|
2022-11-13 21:14:11 +01:00
|
|
|
my $_memory = $vms{$vm_name}{'memory'} || '';
|
|
|
|
my $_disk2 = $vms{$vm_name}{'disk2'} || '';
|
2023-05-05 23:04:44 +02:00
|
|
|
my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
|
2022-11-13 21:14:11 +01:00
|
|
|
|
2023-05-05 23:04:44 +02:00
|
|
|
my ($_ipv4_address, $_ipv4_subnet) = $vms{$vm_name}{'ipv4'} =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d{2})/ if $vms{$vm_name}{'ipv4'};
|
|
|
|
my $_ipv4 = $_ipv4_address || $conf{'conf'}{'IP_PREFIX'} . "." . ($conf{'conf'}{'IP_START'} + $vms{$vm_name}{'vm_number'});
|
|
|
|
my $_ipv4_netmask = $_ipv4_subnet || $conf{'conf'}{'NETMASK'};
|
|
|
|
my $_ipv4_gateway = $vms{$vm_name}{'ipv4_gw'} || $conf{'conf'}{'ROUTER'};
|
|
|
|
|
|
|
|
my $_ipv6 = $vms{$vm_name}{'ipv6'} || $conf{'conf'}{'IPV6_PREFIX'} . ":" . ($conf{'conf'}{'IPV6_START'} + $vms{$vm_name}{'vm_number'}) . "::" . ($conf{'conf'}{'IP_START'} + $vms{$vm_name}{'vm_number'});
|
|
|
|
my $_ipv6_gateway = $vms{$vm_name}{'ipv6_gw'} || $conf{'conf'}{'IPV6_PREFIX'} . ":" . ($conf{'conf'}{'IPV6_START'} + $vms{$vm_name}{'vm_number'}) . "::1";
|
|
|
|
|
|
|
|
if (! $_payment) {
|
2023-04-23 09:27:57 +02:00
|
|
|
my $memory_price = $memory_prices{$_memory} || '0';
|
|
|
|
my $hdd_price = $hdd_prices{$_disk2} || '0';
|
|
|
|
$_payment = $base_price + $memory_price + $hdd_price;
|
2023-05-05 23:04:44 +02:00
|
|
|
} elsif ($_payment =~ m/sponsor/) {
|
|
|
|
$ideal_url = "SPONSORED";
|
|
|
|
$paypal_url = "SPONSORED";
|
2023-04-23 09:27:57 +02:00
|
|
|
}
|
2021-02-17 09:25:20 +01:00
|
|
|
|
2024-05-14 18:19:35 +02:00
|
|
|
#if ($_donated !~ m/renewal/) {
|
|
|
|
#print "renewal not set\n";
|
|
|
|
#next;
|
|
|
|
#}
|
2024-05-12 20:06:58 +02:00
|
|
|
|
2023-05-05 23:04:44 +02:00
|
|
|
my $stripe = $stripe_urls{$_payment} || '';
|
|
|
|
my $ideal = $ideal_url || "https://bunq.me/openbsdams/${_payment}/${_instance}%20$conf{'conf'}{'SERVER'}";
|
|
|
|
my $paypal = $paypal_url || "https://paypal.me/runbsd/${_payment}eur";
|
2021-05-02 19:39:49 +02:00
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
open(my $fh, '<', $template);
|
|
|
|
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
|
|
|
|
printf $fh_email "To: %s\n", $_email;
|
2023-05-06 14:15:50 +02:00
|
|
|
TEMPLATE: while (my $row = <$fh>) {
|
2021-05-09 09:55:58 +02:00
|
|
|
chomp $row;
|
2023-05-06 14:15:50 +02:00
|
|
|
if ($row =~ m/MESSAGE/) {
|
|
|
|
if ($message ne "empty") {
|
|
|
|
$row =~ s/MESSAGE/$message\n/g;
|
|
|
|
} else {
|
|
|
|
next TEMPLATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 10:51:48 +02:00
|
|
|
$row =~ s/FIRSTNAME/$_firstname/g;
|
2023-05-05 23:04:44 +02:00
|
|
|
$row =~ s/VMID/$_instance/g;
|
|
|
|
$row =~ s/SERVER/$conf{'conf'}{'SERVER'}/g;
|
|
|
|
$row =~ s/DOMAIN/$conf{'conf'}{'DOMAIN'}/g;
|
2021-05-09 09:55:58 +02:00
|
|
|
$row =~ s/HOSTNAME/$_hostname/g;
|
|
|
|
$row =~ s/USERNAME/$_username/g;
|
|
|
|
|
2023-05-05 23:04:44 +02:00
|
|
|
$row =~ s/IPV4$/$_ipv4/g;
|
|
|
|
$row =~ s/IPV4NETMASK$/$_ipv4_netmask/g;
|
|
|
|
$row =~ s/IPV4GW$/$_ipv4_gateway/g;
|
|
|
|
$row =~ s/IPV6$/$_ipv6/g;
|
|
|
|
$row =~ s/IPV6GW$/$_ipv6_gateway/g;
|
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
$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;
|
2023-05-05 23:04:44 +02:00
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
$row =~ s/PAYMENT/$_payment/g;
|
2023-04-23 09:27:57 +02:00
|
|
|
$row =~ s/STRIPE/$stripe/g;
|
2023-05-05 23:04:44 +02:00
|
|
|
$row =~ s/IDEAL/$ideal/g;
|
|
|
|
$row =~ s/PAYPAL/$paypal/g;
|
2021-05-09 09:55:58 +02:00
|
|
|
|
|
|
|
if ($row =~ /TIME\((.*)\)/) {
|
|
|
|
my @TIMES = split(/,/, $1);
|
2021-05-18 12:47:43 +02:00
|
|
|
$row =~ s/TIME\(.*\)/$TIMES[$evenodd]/g;
|
2020-11-25 11:10:29 +01:00
|
|
|
}
|
2021-05-09 09:55:58 +02:00
|
|
|
print $fh_email "$row\n";
|
2020-11-25 11:10:29 +01:00
|
|
|
}
|
2021-05-09 09:55:58 +02:00
|
|
|
close $fh_email;
|
2023-05-05 23:04:44 +02:00
|
|
|
print "$function: $_date, $_payment, $_name, $_email, $_hostname, $conf{'conf'}{'SERVER'} ($_instance), $_ipv4\n";
|
2020-11-25 11:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:40:46 +02:00
|
|
|
# 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);
|
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
if ($function =~ /notify/) {
|
|
|
|
mailout(\%conf, \%vms);
|
2022-04-09 10:57:44 +02:00
|
|
|
} elsif ($function =~ /(deployed|redeployed|cpu|msg|thanx)/ and $function_variable !~ /empty/) {
|
2021-05-09 09:55:58 +02:00
|
|
|
my %slice = %vms{$function_variable};
|
|
|
|
mailout(\%conf, \%slice);
|
2021-05-09 16:28:05 +02:00
|
|
|
} elsif ($function =~ /(renewal|subscription|deprovision)/) {
|
2021-05-09 09:55:58 +02:00
|
|
|
my $year = strftime("%Y", localtime);
|
|
|
|
my $month = strftime("%m", localtime);
|
|
|
|
for my $vm_name (sort keys %vms) {
|
2023-05-05 23:04:44 +02:00
|
|
|
if ($vms{$vm_name}{'donated'} =~ /(done|expire|sponsor|sponsored|renewal)/) { delete $vms{$vm_name}; next; }
|
2023-01-03 13:46:44 +01:00
|
|
|
|
2023-05-05 23:04:44 +02:00
|
|
|
my ($_vm_year, $_vm_month, $_vm_day) = split('/', $vms{$vm_name}{'date'}, 3);
|
2023-01-03 13:46:44 +01:00
|
|
|
if ($_vm_year >= $year) { delete $vms{$vm_name}; next; }
|
|
|
|
if ($_vm_month != $month) { delete $vms{$vm_name}; next; }
|
|
|
|
|
2021-05-09 09:55:58 +02:00
|
|
|
if ($function =~ /(renewal|deprovision)/) {
|
2023-01-03 13:46:44 +01:00
|
|
|
if (defined $vms{$vm_name}{'subscription'} and $vms{$vm_name}{'subscription'} ne "") { delete $vms{$vm_name}; next; }
|
2021-05-09 09:55:58 +02:00
|
|
|
}
|
|
|
|
if ($function =~ /subscription/) {
|
2021-07-05 16:27:16 +02:00
|
|
|
if (!defined $vms{$vm_name}{'subscription'} or $vms{$vm_name}{'subscription'} eq "" or $vms{$vm_name}{'subscription'} =~ /no/) { delete $vms{$vm_name}; next; }
|
2021-05-09 09:55:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mailout(\%conf, \%vms);
|
2021-05-09 16:28:05 +02:00
|
|
|
} elsif ($function =~ /stopped/) {
|
2021-05-09 09:55:58 +02:00
|
|
|
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; }
|
2023-03-13 11:44:46 +01:00
|
|
|
if ($vms{$vm_name}{'donated'} =~ /expire/) { delete $vms{$vm_name}; next; }
|
2021-05-09 09:55:58 +02:00
|
|
|
}
|
|
|
|
mailout(\%conf, \%vms);
|
2021-05-09 16:28:05 +02:00
|
|
|
} else {
|
2022-04-09 10:57:44 +02:00
|
|
|
print "usage: " . basename($0) . " [stopped | renewal | notify | deprovision] | [deployed | redeployed | cpu | msg | thanx] <vmid>\n";
|
2021-05-09 09:55:58 +02:00
|
|
|
}
|