#!/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 Getopt::Long;
Getopt::Long::Configure("no_ignore_case");

use OpenQA::Worker;
use OpenQA::Worker::Common;

my %options;

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|debug|d", "help|h",
) or usage(1);

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

# count workers from 1 if not set - if tap devices are used worker would try to use tap -1
$options{instance} ||= 1;

my ($worker_settings, $host_settings) = OpenQA::Worker::Common::read_worker_config($options{instance}, $options{host});
$worker_settings->{LOG_LEVEL} = 'debug' if $options{verbose};
$OpenQA::Worker::Common::worker_settings = $worker_settings;
# 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;

OpenQA::Worker::init($host_settings, \%options);
exit OpenQA::Worker::main($host_settings);

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