|
1 |
| -# Copyright (C) 2015-2016 SUSE LLC |
| 1 | +# Copyright (C) 2017 SUSE LLC |
2 | 2 | #
|
3 | 3 | # This program is free software; you can redistribute it and/or modify
|
4 | 4 | # it under the terms of the GNU General Public License as published by
|
|
13 | 13 | # You should have received a copy of the GNU General Public License along
|
14 | 14 | # with this program; if not, see <http://www.gnu.org/licenses/>.
|
15 | 15 |
|
16 |
| -# This package contains shared functions between WebAPI and WebSockets |
17 |
| -package OpenQA::ServerStartup; |
18 |
| - |
| 16 | +package OpenQA::Setup; |
| 17 | +use Mojo::Log; |
| 18 | +use Mojo::Home; |
19 | 19 | use strict;
|
20 | 20 | use warnings;
|
| 21 | +use Mojo::Base -base; |
| 22 | +use Sys::Hostname; |
| 23 | +use File::Spec::Functions 'catfile'; |
| 24 | +use Mojo::File 'path'; |
21 | 25 | use Config::IniFiles;
|
22 | 26 | use db_profiler;
|
23 | 27 | use db_helpers;
|
24 | 28 | use OpenQA::Utils;
|
25 |
| -use Mojo::File 'path'; |
| 29 | +use File::Path 'make_path'; |
26 | 30 |
|
27 |
| -sub read_config { |
28 |
| - my $app = shift; |
| 31 | +has config => sub { {} }; |
| 32 | + |
| 33 | +has log => sub { Mojo::Log->new(handle => \*STDOUT, level => "info"); }; |
| 34 | + |
| 35 | +has home => sub { Mojo::Home->new($ENV{MOJO_HOME} || '/') }; |
| 36 | + |
| 37 | +has mode => 'production'; |
| 38 | + |
| 39 | +has 'log_name'; |
| 40 | + |
| 41 | +has level => 'info'; |
| 42 | + |
| 43 | +has 'instance'; |
| 44 | + |
| 45 | +has 'log_dir'; |
| 46 | + |
| 47 | +has schema => sub { OpenQA::Schema::connect_db() }; |
| 48 | + |
| 49 | +sub setup_log { |
| 50 | + my ($self) = @_; |
| 51 | + my ($logfile, $logdir, $level, $log); |
| 52 | + |
| 53 | + if ($self->isa('OpenQA::Setup')) { |
| 54 | + $logdir = $self->log_dir; |
| 55 | + $level = $self->level; |
| 56 | + if ($logdir && !-e $logdir) { |
| 57 | + make_path($logdir); |
| 58 | + } |
| 59 | + elsif ($logdir && !-d $logdir) { |
| 60 | + die "Please point the logs to a valid folder!"; |
| 61 | + } |
| 62 | + } |
| 63 | + else { |
| 64 | + $log = $self->log; |
| 65 | + $level = $self->config->{logging}->{level} || 'debug'; |
| 66 | + } |
| 67 | + $logfile = $ENV{OPENQA_LOGFILE} || $self->config->{logging}->{file}; |
| 68 | + |
| 69 | + if ($logfile && $logdir) { |
| 70 | + $logfile = catfile($logdir, $logfile); |
| 71 | + $log = Mojo::Log->new( |
| 72 | + handle => path($logfile)->open('>>'), |
| 73 | + level => $self->level, |
| 74 | + format => sub { return log_format($self->log_name, @_); }); |
| 75 | + } |
| 76 | + elsif ($logfile) { |
| 77 | + $log = Mojo::Log->new( |
| 78 | + handle => path($logfile)->open('>>'), |
| 79 | + level => $level, |
| 80 | + format => sub { return log_format($self->log_name, @_); }); |
| 81 | + } |
| 82 | + elsif ($logdir) { |
| 83 | + # So each worker from each host get it's own log (as the folder can be shared). Hopefully the machine hostname |
| 84 | + # is already sanitized. Otherwise we need to check |
| 85 | + $logfile |
| 86 | + = catfile($logdir, hostname() . (defined $self->instance ? "-${\$self->instance}" : '') . ".log"); |
| 87 | + $log = Mojo::Log->new( |
| 88 | + handle => path($logfile)->open('>>'), |
| 89 | + level => $self->level, |
| 90 | + format => sub { return log_format($self->log_name, @_); }); |
| 91 | + } |
| 92 | + else { |
| 93 | + $log = Mojo::Log->new( |
| 94 | + handle => \*STDOUT, |
| 95 | + level => $level, |
| 96 | + format => sub { |
| 97 | + my ($time, $level, @lines) = @_; |
| 98 | + return "[${\$self->log_name}:$level] " . join "\n", @lines, ''; |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + $self->log($log); |
| 103 | + unless ($self->isa('OpenQA::Setup')) { |
| 104 | + if ($ENV{OPENQA_SQL_DEBUG} // $self->config->{logging}->{sql_debug} // 'false' eq 'true') { |
| 105 | + # avoid enabling the SQL debug unless we really want to see it |
| 106 | + # it's rather expensive |
| 107 | + db_profiler::enable_sql_debugging($self); |
| 108 | + } |
| 109 | + } |
29 | 110 |
|
| 111 | + $OpenQA::Utils::app = $self; |
| 112 | + return $log; |
| 113 | +} |
| 114 | + |
| 115 | +sub log_format { |
| 116 | + my ($logname, $time, $level, @lines) = @_; |
| 117 | + return '[' . localtime($time) . "] [$logname:$level] " . join "\n", @lines, ''; |
| 118 | +} |
| 119 | + |
| 120 | +sub emit_event { |
| 121 | + my ($self, $event, $data) = @_; |
| 122 | + # nothing to see here, move along |
| 123 | +} |
| 124 | + |
| 125 | +sub read_config { |
| 126 | + my $app = shift; |
30 | 127 | my %defaults = (
|
31 | 128 | global => {
|
32 | 129 | appname => 'openQA',
|
@@ -123,41 +220,6 @@ sub read_config {
|
123 | 220 | $app->config->{auth}->{method} =~ s/\s//g;
|
124 | 221 | }
|
125 | 222 |
|
126 |
| -sub setup_logging { |
127 |
| - my ($app) = @_; |
128 |
| - |
129 |
| - my $config = $app->config; |
130 |
| - my $logfile = $ENV{OPENQA_LOGFILE} || $config->{logging}->{file}; |
131 |
| - |
132 |
| - if ($logfile) { |
133 |
| - $app->log->handle(path($logfile)->open('>>')); |
134 |
| - $app->log->path($logfile); |
135 |
| - $app->log->format( |
136 |
| - sub { |
137 |
| - my ($time, $level, @lines) = @_; |
138 |
| - return '[' . localtime($time) . "] [" . $app->log_name . ":$level] " . join "\n", @lines, ''; |
139 |
| - }); |
140 |
| - } |
141 |
| - else { |
142 |
| - $app->log->format( |
143 |
| - sub { |
144 |
| - my (undef, $level, @lines) = @_; |
145 |
| - return '[' . $app->log_name . ":$level] " . join "\n", @lines, ''; |
146 |
| - }); |
147 |
| - } |
148 |
| - |
149 |
| - if ($config->{logging}->{level}) { |
150 |
| - $app->log->level($config->{logging}->{level}); |
151 |
| - } |
152 |
| - if ($ENV{OPENQA_SQL_DEBUG} // $config->{logging}->{sql_debug} // 'false' eq 'true') { |
153 |
| - # avoid enabling the SQL debug unless we really want to see it |
154 |
| - # it's rather expensive |
155 |
| - db_profiler::enable_sql_debugging($app); |
156 |
| - } |
157 |
| - |
158 |
| - $OpenQA::Utils::app = $app; |
159 |
| -} |
160 |
| - |
161 | 223 | # Update config definition from plugin requests
|
162 | 224 | sub update_config {
|
163 | 225 | my ($config, @namespaces) = @_;
|
|
0 commit comments