User:AnomieBOT/source/tools-startbot.pl
Appearance
< User:AnomieBOT | source
#!/usr/bin/perl -w
use strict;
# binmodes
$|=1;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
my $quiet = 0;
if ( $ARGV[0] eq '--quiet' ) {
$quiet = 1;
shift;
}
die "Cannot run as root\n" if $<==0;
die "USAGE: $0 taskdir\n" if @ARGV!=1;
use Cwd;
use File::Basename;
my $base;
BEGIN {
$base = File::Basename::dirname( Cwd::realpath( __FILE__ ) );
chdir( $base );
}
use lib $base;
use AnomieBOT::API;
use POSIX ':sys_wait_h';
use sort 'stable';
my %instances=();
chdir($AnomieBOT::API::basedir);
my $dir=shift or die "USAGE: $0 taskdir\n";
$dir.='/' unless substr($dir,-1) eq '/';
opendir(D, $dir) or die "Could not open task directory: $!\n";
while(my $file=readdir(D)){
next if -d $dir.$file;
next if substr($file,0,1) eq '.';
my $task='';
if(!open(X, '<:utf8', $dir.$file)){
warn "Could not open task file $file: $!\n";
next;
}
while(<X>){
$task=$1 if /^package (.*);$/;
}
close(X);
if($task eq ''){
warn "Invalid task file $file\n";
next;
}
AnomieBOT::API::load($dir.$file);
my $t=$task->new();
my $a=$t->approved;
$instances{$a}=1 if $a>0;
}
closedir(D);
die "No instances found" unless %instances;
# Check permissions
die "Bad file permissions on conf.ini" if (stat 'conf.ini')[2]&7;
# List running instances
my %running=();
open X, q( kubectl get pods -o json | jq -r '.items[] | [ .metadata.labels["job-name"], .metadata.name, .status.phase ] | @tsv' | ) or die "kubectl: $!";
while(<X>) {
chomp;
my ( $name, $host, $status ) = split /\t/;
$running{$name} = $host if $status eq 'Running';
}
my $home = $ENV{'HOME'};
# Start the instances
for my $botnum (sort { $a <=> $b } keys %instances) {
my $jname = "anomiebot-$botnum";
if(exists($running{$jname})){
my $jhost = $running{$jname};
warn "Job $jname is already running on $jhost\n" unless $quiet;
} else {
my $api = AnomieBOT::API->new("conf.ini", $botnum, { db => 0 });
my $memlimit = $api->{'memlimit'};
my $cpulimit = $api->{'cpulimit'};
$api->DESTROY;
$api = undef;
system( '/usr/bin/toolforge-jobs', 'run', '--command', "$home/bot/bot-wrapper.sh $botnum $dir >> $home/botlogs/$jname.job 2>&1", '--image=perl5.36', '--no-filelog', "--mem=$memlimit", "--cpu=$cpulimit", "--emails=onfailure", $jname );
}
}