Skip to content

Commit

Permalink
Split Logging support out of WebAPI
Browse files Browse the repository at this point in the history
and use it for webapi and scheduler
  • Loading branch information
coolo committed Sep 12, 2016
1 parent 7ea7b0d commit 016409e
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 129 deletions.
6 changes: 5 additions & 1 deletion lib/OpenQA/IPC.pm
Expand Up @@ -28,6 +28,7 @@ use Try::Tiny;
use Carp;

use Scalar::Util qw/weaken/;
use OpenQA::Utils qw/log_debug/;

my $openqa_prefix = 'org.opensuse.openqa';
my %services = (
Expand Down Expand Up @@ -208,7 +209,10 @@ sub _dispatch {
confess "error getting ipc service: $_";
};
my $object = $service->get_object('/' . $services{$target}, join('.', $openqa_prefix, $services{$target}));
return $object->$command(@data);
log_debug("dispatching IPC $command to $target: " . pp(\@data));
my $ret = $object->$command(@data);
log_debug("IPC retrieved " . pp($ret));
return $ret;
}

## Helpers
Expand Down
22 changes: 21 additions & 1 deletion lib/OpenQA/Scheduler.pm
@@ -1,4 +1,4 @@
# Copyright (C) 2015 SUSE Linux GmbH
# Copyright (C) 2015-2016 SUSE LLC
#
# 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
Expand All @@ -23,11 +23,31 @@ use Net::DBus::Reactor;
use Data::Dump qw/pp/;

use OpenQA::IPC;
use OpenQA::Scheduler::FakeApp;
use OpenQA::Scheduler::Scheduler qw//;
use OpenQA::Scheduler::Locks qw//;
use OpenQA::Utils qw/log_debug/;
use OpenQA::ServerStartup;

# monkey patching for debugging IPC
sub _is_method_allowed {
my ($self, $method) = @_;

my $ret = $self->SUPER::_is_method_allowed($method);
if ($ret) {
log_debug "scheduler IPC calling $method";
}
return $ret;
}

our $fakeapp;
sub run {
$fakeapp = OpenQA::Scheduler::FakeApp->new;
OpenQA::ServerStartup::read_config($fakeapp);
OpenQA::ServerStartup::setup_logging($fakeapp);

OpenQA::Scheduler->new();
log_debug("Scheduler started");
Net::DBus::Reactor->main->run;
}

Expand Down
55 changes: 55 additions & 0 deletions lib/OpenQA/Scheduler/FakeApp.pm
@@ -0,0 +1,55 @@
# Copyright (C) 2016 SUSE LLC
#
# 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/>.

package OpenQA::Scheduler::FakeApp;
use Mojo::Log;
use strict;
use warnings;

# implementing the interface of Mojolicious we need to get the rest doing debug
sub new {
my ($class) = @_;
my $self = bless {}, $class;
$self->{config} = {};
$self->{log} = Mojo::Log->new;
return $self;
}

sub mode {
my ($self) = @_;
return 'production';
}

sub config {
my ($self) = @_;
return $self->{config};
}

sub log {
my ($self) = @_;
return $self->{log};
}

sub schema {
my ($self) = @_;
return OpenQA::Schema::connect_db();
}

sub log_name {
my ($self) = @_;
return 'scheduler';
}

1;
2 changes: 1 addition & 1 deletion lib/OpenQA/Schema.pm
Expand Up @@ -37,9 +37,9 @@ sub _get_schema {
}

sub connect_db {
my $mode = shift || $ENV{OPENQA_DATABASE} || 'production';
my $schema = _get_schema;
unless ($$schema) {
my $mode = shift || $ENV{OPENQA_DATABASE} || 'production';
my %ini;
my $cfgpath = $ENV{OPENQA_CONFIG} || "$Bin/../etc/openqa";
my $database_file = $cfgpath . '/database.ini';
Expand Down
128 changes: 128 additions & 0 deletions lib/OpenQA/ServerStartup.pm
@@ -0,0 +1,128 @@
# Copyright (C) 2015-2016 SUSE LLC
#
# 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/>.

# This package contains shared functions between WebAPI and WebSockets
package OpenQA::ServerStartup;

use strict;
use warnings;
use Config::IniFiles;
use db_profiler;
use db_helpers;
use OpenQA::Utils;

sub read_config {
my $app = shift;

my %defaults = (
global => {
appname => 'openQA',
base_url => undef,
branding => 'openSUSE',
allowed_hosts => undef,
download_domains => undef,
suse_mirror => undef,
scm => undef,
hsts => 365,
audit_enabled => 1,
plugins => undef,
hide_asset_types => 'repo',
},
auth => {
method => 'OpenID',
},
'scm git' => {
do_push => 'no',
},
logging => {
level => undef,
file => undef,
sql_debug => undef,
},
openid => {
provider => 'https://www.opensuse.org/openid/user/',
httpsonly => 1,
},
hypnotoad => {
listen => ['http://localhost:9526/'],
proxy => 1,
},
audit => {
blacklist => '',
},
);

# in development mode we use fake auth and log to stderr
my %mode_defaults = (
development => {
auth => {
method => 'Fake',
},
logging => {
file => undef,
level => 'debug',
},
},
test => {
auth => {
method => 'Fake',
},
logging => {
file => undef,
level => 'debug',
},
});

# Mojo's built in config plugins suck. JSON for example does not
# support comments
my $cfgpath = $ENV{OPENQA_CONFIG} || $app->home . '/etc/openqa';
my $cfg = Config::IniFiles->new(-file => $cfgpath . '/openqa.ini') || undef;

for my $section (sort keys %defaults) {
for my $k (sort keys %{$defaults{$section}}) {
my $v = $cfg && $cfg->val($section, $k);
$v //= exists $mode_defaults{$app->mode}{$section}->{$k} ? $mode_defaults{$app->mode}{$section}->{$k} : $defaults{$section}->{$k};
$app->config->{$section}->{$k} = $v if defined $v;
}
}
$app->config->{_openid_secret} = db_helpers::rndstr(16);
}

sub _log_format {
my ($app, $time, $level, @lines) = @_;
return '[' . localtime($time) . "] [" . $app->log_name . ":$level] " . join "\n", @lines, '';
}

sub setup_logging {
my ($app) = @_;

my $config = $app->config;
my $logfile = $ENV{OPENQA_LOGFILE} || $config->{logging}->{file};
$app->log->path($logfile);
$app->log->format(sub { _log_format($app, @_); });

if ($logfile && $config->{logging}->{level}) {
$app->log->level($config->{logging}->{level});
}
if ($ENV{OPENQA_SQL_DEBUG} // $config->{logging}->{sql_debug} // 'false' eq 'true') {
# avoid enabling the SQL debug unless we really want to see it
# it's rather expensive
db_profiler::enable_sql_debugging($app);
}

$OpenQA::Utils::app = $app;
}

1;
2 changes: 0 additions & 2 deletions lib/OpenQA/Utils.pm
Expand Up @@ -152,8 +152,6 @@ sub file_content($;$) {
}

# logging helpers
# Withing Mojo openQA $app is defined and Mojo::Log is used
# Scheduler prints to stdout&stderr -> journal
sub log_debug {
my ($msg) = @_;
if ($app && $app->log) {
Expand Down

0 comments on commit 016409e

Please sign in to comment.