89 lines
2.2 KiB
Perl
89 lines
2.2 KiB
Perl
package Util;
|
|
|
|
use strict;
|
|
use warnings;
|
|
use diagnostics;
|
|
|
|
use feature 'say';
|
|
|
|
my $ntfy_url = 'https://ntfy.vps.chriphost.de';
|
|
my $ntfy_token = 'tk_rx8fd6hojuz4ekcb72j7juugkbmga'; # May be public
|
|
my $ntfy_topic = 'fail-alerts';
|
|
|
|
sub notify {
|
|
my ($msg) = @_;
|
|
|
|
system( 'curl', '-H', "Authorization: Bearer $ntfy_token",
|
|
'-d', $msg, "$ntfy_url/$ntfy_topic" );
|
|
}
|
|
|
|
sub notify_file {
|
|
my ($file) = @_;
|
|
|
|
system(
|
|
'curl', '-H', "Authorization: Bearer $ntfy_token",
|
|
'-T', $file, '-H', "Filename: $file",
|
|
"$ntfy_url/$ntfy_topic"
|
|
);
|
|
}
|
|
|
|
sub find_files {
|
|
my ($dir) = @_;
|
|
|
|
opendir( my $dhandle, $dir ) or die "opendir($dir): $!";
|
|
my @files = grep { -f "$dir/$_" } readdir($dhandle);
|
|
closedir($dhandle);
|
|
|
|
return @files;
|
|
}
|
|
|
|
sub find_subdirs {
|
|
my ($dir) = @_;
|
|
|
|
opendir( my $dhandle, $dir ) or die "opendir($dir): $!";
|
|
my @subdirs =
|
|
grep { $_ ne '.' && $_ ne '..' && -d "$dir/$_" } readdir($dhandle);
|
|
closedir($dhandle);
|
|
|
|
return @subdirs;
|
|
}
|
|
|
|
sub execute_query {
|
|
my ( $experiment, $queryname, $db_conf, $builds_dir, $do_notify_file ) = @_;
|
|
|
|
my $module = "Queries::$queryname";
|
|
my $file = "$module.pm";
|
|
$file =~ s/::/\//g;
|
|
|
|
require $file;
|
|
|
|
my $query = $module->can('query') or die "$module can't query()";
|
|
my $args = $module->can('args') or die "$module can't args()";
|
|
my $filename = $module->can('filename') or die "$module can't filanem()";
|
|
my $postprocess = $module->can('postprocess')
|
|
or die "$module can't postprocess()";
|
|
|
|
my $querystring = $query->($experiment);
|
|
my $argsstring = $args->();
|
|
my $filenamestring = $filename->();
|
|
|
|
my $result =
|
|
qx{mariadb --defaults-file=$db_conf $argsstring -e "$querystring"};
|
|
die "Query failed: $?" if $? != 0;
|
|
|
|
$postprocess->($result);
|
|
|
|
open( my $results_handle, '>', "$builds_dir/$experiment/$filenamestring" )
|
|
or die "failed to open file: $!";
|
|
print $results_handle $result;
|
|
close($results_handle) or die "failed to close file: $!";
|
|
|
|
if ( defined $do_notify_file and $do_notify_file == 1 ) {
|
|
notify_file("$builds_dir/$experiment/$filenamestring");
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
1;
|