#!/usr/bin/perl
# Copyright (C) 2015 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.

=head1 worker

worker - openQA worker daemon

=head1 SYNOPSIS

worker [OPTIONS]

=head1 OPTIONS

=over 4

=item B<--host> HOST

specify dispatcher/scheduler host to connect to

=item B<--instance> NR

specify instance number, ie pool directory to use

=item B<--apikey> <value>

specify the public key needed for API authentication

=item B<--apisecret> <value>

specify the secret key needed for API authentication

=item B<--isotovideo> PATH

path to isotovideo script, useful for running from git

=item B<--no-cleanup>

don't clean pool directory after job

=item B<--verbose>

verbose output

=item B<--help, -h>

print help

=back

=head1 DESCRIPTION

(no content)

=head1 CONFIG FILE

L<worker> relies on credentials provided by L<OpenQA::Client>, i.e. tries to
find a config file C<client.conf> resolving C<$OPENQA_CONFIG> or
C<~/.config/openqa> or C</etc/openqa/> in this order of preference.
Additionally L<worker> uses a config file C<workers.ini> to configure worker
settings.

Example:
  [global]
  BACKEND = qemu
  HOST = http://openqa.example.com


=head1 SEE ALSO
L<OpenQA::Client>

=cut

use strict;
use warnings;


BEGIN {
    #prepare for large files
    $ENV{MOJO_MAX_MESSAGE_SIZE}   = 1024 * 1024 * 1024 * 20;
    $ENV{MOJO_INACTIVITY_TIMEOUT} = 300;
    $ENV{MOJO_CONNECT_TIMEOUT}    = 300;
    # the default is EV, and this heavily screws with our children handling
    $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
    #$ENV{MOJO_LOG_LEVEL} = 'debug';
    #$ENV{MOJO_USERAGENT_DEBUG} = 1;
    #$ENV{MOJO_IOLOOP_DEBUG} = 1;
}

use FindBin;
use lib "$FindBin::Bin/../lib";
use Config::IniFiles;
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");

use OpenQA::Worker;

my %options;
my $worker_settings;

sub usage($) {
    my $r = shift;
    eval "use Pod::Usage; pod2usage($r);";
    if ($@) {
        die "cannot display help, install perl(Pod::Usage)\n";
    }
}

GetOptions(\%options, "no-cleanup", "instance=i", "isotovideo=s", "host=s", "apikey:s", "apisecret:s", "verbose|v", "help|h",) or usage(1);

usage(0) if ($options{help});

sub read_worker_config($) {
    my $instance   = shift;
    my $worker_dir = $ENV{OPENQA_CONFIG} || '/etc/openqa';
    my $cfg        = Config::IniFiles->new(-file => $worker_dir . '/workers.ini');

    my $sets = {};
    foreach my $section ('global', $instance) {
        if ($cfg && $cfg->SectionExists($section)) {
            foreach my $set ($cfg->Parameters($section)) {
                $sets->{uc $set} = $cfg->val($section, $set);
            }
        }
    }
    $sets->{'HOST'} ||= "localhost";

    return $sets;
}

$worker_settings = read_worker_config($options{instance});

$options{instance} ||= 0;
$options{host} ||= $worker_settings->{'HOST'};

# XXX: this should be sent to the scheduler to be included in the worker's table
$ENV{QEMUPORT} = ($options{instance}) * 10 + 20002;
$ENV{VNC}      = ($options{instance}) + 90;
($ENV{OPENQA_HOSTNAME}) = $options{host} =~ m|([^/]+:?\d*)/?$|;

OpenQA::Worker::init($worker_settings, %options);
OpenQA::Worker::main();

# vim: set sw=4 sts=4 et:
