From f73f10dd395b9c39ba1a0352e7e5a57d7e270f19 Mon Sep 17 00:00:00 2001 From: Cris Dywan Date: Wed, 26 Oct 2022 15:00:01 +0200 Subject: [PATCH] Move backend process logic into new class See: https://progress.opensuse.org/issues/81899 --- CMakeLists.txt | 1 + OpenQA/Isotovideo/Backend.pm | 33 ++++++++++++++++++++++ OpenQA/Isotovideo/NeedleDownloader.pm | 2 +- isotovideo | 40 +++++++-------------------- 4 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 OpenQA/Isotovideo/Backend.pm diff --git a/CMakeLists.txt b/CMakeLists.txt index aabd07afeda..18099616a2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ set(PERL_FILES OpenQA/Benchmark/Stopwatch.pm OpenQA/Commands.pm OpenQA/Exceptions.pm + OpenQA/Isotovideo/Backend.pm OpenQA/Isotovideo/CommandHandler.pm OpenQA/Isotovideo/Interface.pm OpenQA/Isotovideo/NeedleDownloader.pm diff --git a/OpenQA/Isotovideo/Backend.pm b/OpenQA/Isotovideo/Backend.pm new file mode 100644 index 00000000000..93c4b397b0a --- /dev/null +++ b/OpenQA/Isotovideo/Backend.pm @@ -0,0 +1,33 @@ +# Copyright SUSE LLC +# SPDX-License-Identifier: GPL-2.0-or-later + +package OpenQA::Isotovideo::Backend; +use Mojo::Base -base, -signatures; + +use Mojo::File qw(path); +use backend::driver; +use bmwqemu; +use log qw(diag); + +sub new ($class, @args) { + $bmwqemu::vars{BACKEND} ||= "qemu"; + $bmwqemu::backend = backend::driver->new($bmwqemu::vars{BACKEND}); + + path('os-autoinst.pid')->spurt("$$"); + # might throw an exception + $bmwqemu::backend->start_vm; + $class->SUPER::new(@args); +} + +sub process ($self) { + $bmwqemu::backend->{backend_process}; +} + +sub stop ($self) { + return undef unless my $process = $self->process; + diag('stopping backend process ' . $process->pid); + $process->stop if $process->is_running; + diag('done with backend process'); +} + +1; diff --git a/OpenQA/Isotovideo/NeedleDownloader.pm b/OpenQA/Isotovideo/NeedleDownloader.pm index 52da03c5e2d..8626b3336e6 100644 --- a/OpenQA/Isotovideo/NeedleDownloader.pm +++ b/OpenQA/Isotovideo/NeedleDownloader.pm @@ -1,4 +1,4 @@ -# Copyright 2018 SUSE LLC +# Copyright SUSE LLC # SPDX-License-Identifier: GPL-2.0-or-later package OpenQA::Isotovideo::NeedleDownloader; diff --git a/isotovideo b/isotovideo index c5110a59007..76e8e5061b1 100755 --- a/isotovideo +++ b/isotovideo @@ -54,6 +54,7 @@ use JSON::PP; my $installprefix; # $bmwqemu::scriptdir my $fatal_error; # the last error message caught by the die handler +my $backend; BEGIN { # the following line is modified during make install @@ -65,7 +66,6 @@ BEGIN { unshift @INC, "$installprefix"; } -use backend::driver; use log qw(diag fctwarn); use needle; use autotest (); @@ -78,11 +78,12 @@ use POSIX qw(:sys_wait_h _exit); use Time::HiRes qw(gettimeofday tv_interval sleep time); use Try::Tiny; use IO::Select; -use Mojo::File qw(curfile path); +use Mojo::File qw(curfile); use Mojo::UserAgent; use Mojo::IOLoop::ReadWriteProcess 'process'; use Mojo::IOLoop::ReadWriteProcess::Session 'session'; Getopt::Long::Configure("no_ignore_case"); +use OpenQA::Isotovideo::Backend; use OpenQA::Isotovideo::CommandHandler; use OpenQA::Isotovideo::Interface; use OpenQA::Isotovideo::Utils qw(git_rev_parse checkout_git_repo_and_branch @@ -154,7 +155,6 @@ my $command_handler; my $testprocess; my $cmd_srv_fd; my $cmd_srv_port; -my $backend_process; my $loop = 1; # note: The subsequently defined stop_* functions are used to tear down the process tree. @@ -199,30 +199,15 @@ sub stop_autotest () { diag('done with autotest process'); } -sub stop_backend () { - return unless defined $bmwqemu::backend && $backend_process; - - diag('stopping backend process ' . $backend_process->pid); - $backend_process->stop if $backend_process->is_running; - $backend_process = undef; - diag('done with backend process'); -} - sub signalhandler ($sig) { bmwqemu::serialize_state(component => 'isotovideo', msg => "isotovideo received signal $sig", log => 1); return $loop = 0 if $loop; - stop_backend; + $backend->stop if defined $backend; stop_commands("received signal $sig"); stop_autotest; _exit(1); } -sub init_backend () { - $bmwqemu::vars{BACKEND} ||= "qemu"; - $bmwqemu::backend = backend::driver->new($bmwqemu::vars{BACKEND}); - return $bmwqemu::backend; -} - $SIG{TERM} = \&signalhandler; $SIG{INT} = \&signalhandler; $SIG{HUP} = \&signalhandler; @@ -272,11 +257,7 @@ bmwqemu::save_vars(); my $testfd; ($testprocess, $testfd) = autotest::start_process(); -init_backend(); -path('os-autoinst.pid')->spurt("$$"); - -# might throw an exception -$bmwqemu::backend->start_vm; +$backend = OpenQA::Isotovideo::Backend->new; # launch debugging tools my %debugging_tools; @@ -289,16 +270,15 @@ for my $tool (keys %debugging_tools) { exit -1; # don't continue in any case (exec returns if it fails to spawn the process printing an error message on its own) } -$backend_process = $bmwqemu::backend->{backend_process}; my $io_select = IO::Select->new(); $io_select->add($testfd); $io_select->add($cmd_srv_fd); -$io_select->add($backend_process->channel_out); +$io_select->add($backend->process->channel_out); # stop main loop as soon as one of the child processes terminates my $stop_loop = sub (@) { $loop = 0 if $loop; }; $testprocess->once(collected => $stop_loop); -$backend_process->once(collected => $stop_loop); +$backend->process->once(collected => $stop_loop); $cmd_srv_process->once(collected => $stop_loop); # now we have everything, give the tests a go @@ -306,7 +286,7 @@ $testfd->write("GO\n"); $command_handler = OpenQA::Isotovideo::CommandHandler->new( cmd_srv_fd => $cmd_srv_fd, - backend_fd => $backend_process->channel_in, + backend_fd => $backend->process->channel_in, ); $command_handler->on(tests_done => sub (@) { CORE::close($testfd); @@ -361,7 +341,7 @@ while ($loop) { $loop = 0; last; } - if ($readable == $backend_process->channel_out) { + if ($readable == $backend->process->channel_out) { $command_handler->send_to_backend_requester({ret => $rsp->{rsp}}); next; } @@ -412,7 +392,7 @@ $return_code = handle_generated_assets($command_handler, $clean_shutdown) unless $fatal_error = undef; END { - stop_backend; + $backend->stop if defined $backend; stop_commands('test execution ended through exception'); stop_autotest;