2020-05-17 17:40:46 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
#
|
2021-01-03 22:02:58 +01:00
|
|
|
# Copyright (c) 2019-2021 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
|
|
|
|
#
|
|
|
|
# This is WIP, currently there is a lot of overlap in the functions.
|
|
|
|
#
|
|
|
|
use 5.024;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use autodie;
|
|
|
|
use POSIX qw(strftime);
|
2020-06-15 17:11:52 +02:00
|
|
|
use HTTP::Tiny;
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
# Get function and function_variable (vmid) from arguments
|
|
|
|
my $function = $ARGV[0] || "empty";
|
|
|
|
my $function_variable = $ARGV[1] || "empty";
|
|
|
|
|
|
|
|
# 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 deployed {
|
|
|
|
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-deployed.txt";
|
2020-10-24 12:48:36 +02:00
|
|
|
my $_ip4netmask = $conf{'conf'}{'NETMASK'};
|
|
|
|
my $_ip4gw = $conf{'conf'}{'ROUTER'};
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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'};
|
|
|
|
my $_boot = $vms{$vm_name}{'boot'} || "yes";
|
|
|
|
|
|
|
|
if ($function_variable =~ /empty/) { print "No VMID provided\n"; last; }
|
|
|
|
if ($function_variable =~ /$vm_name/) {
|
|
|
|
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
|
2020-10-24 12:48:36 +02:00
|
|
|
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/;
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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;
|
2020-10-24 12:48:36 +02:00
|
|
|
$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;
|
2020-05-17 17:40:46 +02:00
|
|
|
$row =~ s/VMID/$vm_name/g;
|
|
|
|
$row =~ s/SERVER/$_server/g;
|
|
|
|
print $fh_email "$row\n";
|
|
|
|
}
|
|
|
|
close $fh_email;
|
2020-11-25 11:10:29 +01:00
|
|
|
print "DEPLOYED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2020-05-17 17:40:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# function to collect stopped vms
|
|
|
|
sub stopped {
|
|
|
|
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-stopped.txt";
|
|
|
|
|
|
|
|
my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}');
|
|
|
|
|
|
|
|
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'};
|
|
|
|
my $_boot = $vms{$vm_name}{'boot'} || "yes";
|
|
|
|
|
|
|
|
if ($_boot =~ /no/) {
|
|
|
|
print "NOT NOTIFIED: $_name, $_email, $_hostname, $_server ($vm_name)\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (grep(/$vm_name/, @stopped_vms)) {
|
|
|
|
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
|
2021-04-13 21:24:44 +02:00
|
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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;
|
2021-04-13 21:24:44 +02:00
|
|
|
$row =~ s/IPV4/$ip4address/g;
|
2020-05-17 17:40:46 +02:00
|
|
|
$row =~ s/VMID/$vm_name/g;
|
|
|
|
$row =~ s/SERVER/$_server/g;
|
|
|
|
print $fh_email "$row\n";
|
|
|
|
}
|
|
|
|
close $fh_email;
|
2021-04-13 21:24:44 +02:00
|
|
|
print "STOPPED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2020-05-17 17:40:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub renewal {
|
|
|
|
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-renewal.txt";
|
|
|
|
|
|
|
|
my $year = strftime("%Y", localtime);
|
|
|
|
my $month = strftime("%m", localtime);
|
|
|
|
|
2020-10-24 12:48:36 +02:00
|
|
|
#my $total_donated = qx(ftp -Vo- https://obsda.ms/index.html | grep "donated to the OpenBSD" | awk -F';' '{print \$4}' | awk '{printf \$1}');
|
2020-06-15 17:11:52 +02:00
|
|
|
#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');
|
2020-10-24 12:48:36 +02:00
|
|
|
my $total_donated = $1 if $response->{'content'} =~ /([0-9,]+) donated to the OpenBSD/;
|
2021-01-11 22:20:29 +01:00
|
|
|
my $total_vms = $1 if $response->{'content'} =~ /([0-9]+) VMs deployed/;
|
2020-06-15 17:11:52 +02:00
|
|
|
$response = HTTP::Tiny->new->get('https://openbsd.amsterdam/servers.html');
|
|
|
|
my $total_hosts = () = $response->{'content'} =~ /(\>Server )/g;
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
for my $vm_name (sort keys %vms) {
|
|
|
|
my $_date = $vms{$vm_name}{'date'};
|
|
|
|
my $_payment = $vms{$vm_name}{'payment'};
|
2021-02-17 09:25:20 +01:00
|
|
|
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
|
2020-05-17 17:40:46 +02:00
|
|
|
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";
|
|
|
|
|
2021-02-17 09:25:20 +01:00
|
|
|
if ($_subscription =~ /yes/) { next; }
|
2020-05-17 17:40:46 +02:00
|
|
|
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);
|
2021-04-13 21:24:44 +02:00
|
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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;
|
2021-04-13 21:24:44 +02:00
|
|
|
$row =~ s/IPV4/$ip4address/g;
|
2020-05-17 17:40:46 +02:00
|
|
|
$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;
|
2021-04-13 21:24:44 +02:00
|
|
|
print "RENEWAL: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2020-05-17 17:40:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:25:20 +01:00
|
|
|
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);
|
2021-04-13 21:24:44 +02:00
|
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
2021-02-17 09:25:20 +01:00
|
|
|
|
|
|
|
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;
|
2021-04-13 21:24:44 +02:00
|
|
|
$row =~ s/IPV4/$ip4address/g;
|
2021-02-17 09:25:20 +01:00
|
|
|
$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;
|
2021-04-13 21:24:44 +02:00
|
|
|
print "SUBSCRIPTION: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2021-02-17 09:25:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:40:46 +02:00
|
|
|
# function to notify people
|
|
|
|
sub notify {
|
|
|
|
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-notify.txt";
|
2021-01-11 22:15:01 +01:00
|
|
|
my $server_number = $1 if $_server =~ /([0-9]+)/;
|
|
|
|
my $oddeven = $server_number % 2;
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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'};
|
|
|
|
my $_boot = $vms{$vm_name}{'boot'} || "yes";
|
|
|
|
|
|
|
|
if (-e "$template") {
|
|
|
|
#if ($_boot =~ /yes/) {
|
|
|
|
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
|
2021-04-13 21:24:44 +02:00
|
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
2020-05-17 17:40:46 +02:00
|
|
|
|
|
|
|
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;
|
2021-04-13 21:24:44 +02:00
|
|
|
$row =~ s/IPV4/$ip4address/g;
|
2020-05-17 17:40:46 +02:00
|
|
|
$row =~ s/VMID/$vm_name/g;
|
|
|
|
$row =~ s/SERVER/$_server/g;
|
2021-01-11 22:15:01 +01:00
|
|
|
if ($row =~ /TIME\((.*)\)/) {
|
|
|
|
my @TIMES = split(/,/, $1);
|
|
|
|
$row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g;
|
|
|
|
}
|
2020-05-17 17:40:46 +02:00
|
|
|
print $fh_email "$row\n";
|
|
|
|
}
|
|
|
|
close $fh_email;
|
2021-04-13 21:24:44 +02:00
|
|
|
print "NOTIFIED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub msg {
|
|
|
|
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-msg.txt";
|
|
|
|
my $server_number = $1 if $_server =~ /([0-9]+)/;
|
|
|
|
my $oddeven = $server_number % 2;
|
|
|
|
|
|
|
|
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'};
|
|
|
|
my $_boot = $vms{$vm_name}{'boot'} || "yes";
|
|
|
|
|
|
|
|
if ($function_variable =~ /empty/) { print "No VMID provided\n"; last; }
|
|
|
|
if ($function_variable =~ /$vm_name/) {
|
|
|
|
(my $_firstname, my $_lastname) = split(/ /, $_name, 2);
|
|
|
|
my $ip4address = 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/IPV4/$ip4address/g;
|
|
|
|
$row =~ s/VMID/$vm_name/g;
|
|
|
|
$row =~ s/SERVER/$_server/g;
|
|
|
|
if ($row =~ /TIME\((.*)\)/) {
|
|
|
|
my @TIMES = split(/,/, $1);
|
|
|
|
$row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g;
|
|
|
|
}
|
|
|
|
print $fh_email "$row\n";
|
|
|
|
}
|
|
|
|
close $fh_email;
|
2021-04-13 21:29:17 +02:00
|
|
|
print "MSG: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2020-05-17 17:40:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:10:29 +01:00
|
|
|
sub deprovision {
|
|
|
|
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-deprovision.txt";
|
|
|
|
|
|
|
|
my $year = strftime("%Y", localtime);
|
|
|
|
my $month = strftime("%m", localtime);
|
|
|
|
|
|
|
|
for my $vm_name (sort keys %vms) {
|
|
|
|
my $_date = $vms{$vm_name}{'date'};
|
|
|
|
my $_payment = $vms{$vm_name}{'payment'};
|
2021-02-17 09:25:20 +01:00
|
|
|
my $_subscription = $vms{$vm_name}{'subscription'} || "no";
|
2020-11-25 11:10:29 +01:00
|
|
|
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";
|
|
|
|
|
2021-02-17 09:25:20 +01:00
|
|
|
if ($_subscription =~ /yes/) { next; }
|
2020-11-25 11:10:29 +01:00
|
|
|
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);
|
2021-04-13 21:24:44 +02:00
|
|
|
my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n');
|
2020-11-25 11:10:29 +01:00
|
|
|
|
|
|
|
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;
|
2021-04-13 21:24:44 +02:00
|
|
|
$row =~ s/IPV4/$ip4address/g;
|
2020-11-25 11:10:29 +01:00
|
|
|
$row =~ s/VMID/$vm_name/g;
|
|
|
|
$row =~ s/SERVER/$_server/g;
|
|
|
|
print $fh_email "$row\n";
|
|
|
|
}
|
|
|
|
close $fh_email;
|
2021-04-13 21:24:44 +02:00
|
|
|
print "DEPROVISION: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n";
|
2020-11-25 11:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:40:46 +02:00
|
|
|
# function to print all keys & values for debug purposes
|
|
|
|
sub debug_parse {
|
|
|
|
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-debug.txt";
|
|
|
|
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'};
|
|
|
|
my $_boot = $vms{$vm_name}{'boot'} || "yes";
|
|
|
|
print "$_date, $_payment, $_name, $_email, $_hostname, $_server ($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;
|
|
|
|
%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);
|
|
|
|
|
|
|
|
# run functions
|
|
|
|
if ($debug) { debug_parse(\%conf, \%vms); }
|
|
|
|
if ($function =~ /deployed/) { deployed(\%conf, \%vms); }
|
|
|
|
elsif ($function =~ /stopped/) { stopped(\%conf, \%vms); }
|
|
|
|
elsif ($function =~ /renewal/) { renewal(\%conf, \%vms); }
|
2021-02-17 09:25:20 +01:00
|
|
|
elsif ($function =~ /subscription/) { subscription(\%conf, \%vms); }
|
2020-05-17 17:40:46 +02:00
|
|
|
elsif ($function =~ /notify/) { notify(\%conf, \%vms); }
|
2021-04-13 21:24:44 +02:00
|
|
|
elsif ($function =~ /msg/) { msg(\%conf, \%vms); }
|
2020-11-25 11:10:29 +01:00
|
|
|
elsif ($function =~ /deprovision/) { deprovision(\%conf, \%vms); }
|
|
|
|
else { print "Specify function: deployed, stopped, renewal, notify, deprovision\n"; }
|