# Batch EXecutor 2.0 -- Configuration
# (c) 2011 Martin Mares <mj@ucw.cz>

use strict;
use warnings;

package BEX::Config;

# This file specifies default values, which can be overridden in BEX.cf

# A hash of all known machines and groups
# 'name' => { Option => ... }	for a machine; options:
#	Host => 'name'			Host name to use in ssh, ping, ...
# 'name' => ['a','b','c']	for a group containing specified machines/subgroups
our %machines = (
);


# A file whose contents should be prepended before the job. Should start with the "#!" line.
our $job_prolog = 'prolog';

# A file whose contents should be appended to the job
our $job_epilog = 'epilog';

# Keep history of successfully completed jobs
our $keep_history = 1;

# Before we try to connect to a host, ping it to check if it's alive
our $ping_hosts = 1;

# Whenever we want to run a job on a machine, we must obtain a lock.
# Available locking schemes are:
#	none - no locking takes place (dangerous!)
#	job - obtain exclusive access to the job, other jobs on the same
#	      host can run in parallel
#	host - obtain exclusive access to the host, other hosts can run
#	queue - obtain exclusive access to the whole queue
our $locking_scheme = 'host';

# Maximum number of simultaneously running jobs in `bprun'
our $max_parallel_jobs = 5;

# When a job fails, skip all other jobs on the same host
# (however, when locking_scheme is set to `job', another instance of `brun'
# still could run such jobs in parallel)
our $skip_on_fail = 0;

# How we run ssh (including options)
our $ssh_command = "ssh";

# Various utility functions

sub parse_machine_list(@);

sub parse_machine_list(@) {
	my %set = ();
	for my $m (@_) {
		if ($m eq '*') {
			for my $mm (keys %machines) {
				if (ref($machines{$mm}) eq 'HASH') {
					$set{$mm} = 1;
				}
			}
			next;
		}
		my $op = 1;
		if ($m =~ s{^!}{}) { $op = 0; }
		my $v = $machines{$m};
		if (!defined $v) {
			die "Unknown machine or class: $m\n";
		} elsif (ref($v) eq 'HASH') {
			$set{$m} = $op;
		} elsif (ref($v) eq 'ARRAY') {
			for my $mm (parse_machine_list(@$v)) {
				$set{$mm} = $op;
			}
		}
	}
	return sort grep { $set{$_} } keys %set;
}

sub host_name($) {
	my ($mach) = @_;
	return $machines{$mach}->{'Host'} // $mach;
}

require 'BEX.cf';

42;
