MCP/Net.pm

filter: Statementstmt 56.9% 78 / 137 Branchbran 23.3% 7 / 30 Conditioncond 9.0% 1 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.2% 17 / 22 Podpod 100% 9 / 9 totaltotal 52.0% 112 / 215 CC 20 SCAR 20.1
CC
20
Cov
48%
start_minion
20.0
write_grub_file
20.0
start_simnow
12.0
CRAP 7.5
« MCP/Daemon.pm ↑ Summary MCP/Net/TAP.pm »
1 package Tapper::MCP::Net;
2 31 our $AUTHORITY = 'cpan:TAPPER';
3 31 $Tapper::MCP::Net::VERSION = '5.0.9';
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 31 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 31 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Net::SSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use Net::SCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 31 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 31 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 31 extends 'Tapper::MCP';
20  
21 31 ▶use Tapper::Model qw(model get_hardware_overview);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24  
25 sub start_simnow
26 0 ▶{
Subroutine start_simnow: not called CC=3 SCAR=24.8
Pod documented
27 0 my ($self, $hostname) = @_;
28  
29 0 my $simnow_installer = $self->cfg->{files}{simnow_installer};
30 0 ▶ my $server = Sys::Hostname::hostname() || $self->cfg->{mcp_host};
Condition0%0/3
Sys::Hostname::hostname() || $self->cfg->{'mcp_host'}
l!l&&r!l&&!r
000
MC/DC0%0/2
Sys::Hostname::hostname() || $self->cfg->{'mcp_host'}
Sys::Hostname::hostname() $self->cfg->{'mcp_host'}
Truth table0%0/3
A || B
A= Sys::Hostname::hostname()B= $self->cfg->{'mcp_host'}
ABresult
1X1
011
000
31 0 my $retval = Net::SSH::ssh("root\@$hostname",$simnow_installer, "--host=$server");
32 0 ▶ return "Can not start simnow installer: $!" if $retval;
Branch: 1 branch
BranchTrueFalseTotal
if $retval 0 0 0
33  
34  
35 0 $self->log->info("Simnow installation started on $hostname.");
36 0 return 0;
37  
38 }
39  
40  
41  
42 sub start_ssh
43 1 ▶{
Subroutine start_ssh: called CC=2 SCAR=7.0
Pod documented
44 1 my ($self, $hostname) = @_;
45  
46 1 my $tapper_script = $self->cfg->{files}{tapper_prc};
47 1 my $tftp_host = $self->cfg->{mcp_host};
48 1 my $error = Net::SSH::ssh("$hostname","TAPPER_TEST_TYPE=ssh $tapper_script --host $tftp_host");
49 1 ▶ return "Can not start PRC with ssh: $error" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
50 1 return 0;
51 }
52  
53  
54 sub start_local
55 1 ▶{
Subroutine start_local: called CC=2 SCAR=7.0
Pod documented
56 1 my ($self, $path_to_config) = @_;
57  
58 1 my $tapper_script = $self->cfg->{files}{tapper_prc};
59 1 my $error = qx($tapper_script --config $path_to_config);
60 1 ▶ return "Can not start PRC locally: $error" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
61 1 return 0;
62 }
63  
64  
65  
66 0 ▶sub wait_for_minion_job {
Subroutine wait_for_minion_job: not called CC=3 SCAR=24.8
Pod documented
67 0 my ($self, $testrun_id, $hostname) = @_;
68  
69 0 my $state = '';
70 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$state ne "finished" and do { do { my $minion_cfg = $self->cfg->{"minion"}{"frontend"}{"Minion"}; my $minion = "Minion"->new(%$minion_cfg); my $backend = $minion->backend; my $job = $backend->list_jobs(0, 1, {"tasks", ["tapper_testrun"], "queues", [$hostname]})->{"jobs"}[0]; return if $job->{"notes"}{"testrun_id"} != $testrun_id; my $job_id = $job->{"id"}; $state = $job->{"state"}; $self->log->debug(q[minion: wait for 'finished' ] . "job:$job_id testrun:$testrun_id host:$hostname state:$state"); sleep 5 } } 0 0 0
71 0 my $minion_cfg = $self->cfg->{minion}{frontend}{Minion};
72 0 my $minion = Minion->new(%$minion_cfg);
73 0 my $backend = $minion->backend;
74 my $job = $backend->list_jobs
75 (0, 1,
76 {
77 tasks => ['tapper_testrun'],
78 queues => [$hostname],
79 0 })->{jobs}[0];
80  
81 # don't wait if already running different testrun?
82 0 ▶ return if $job->{notes}{testrun_id} != $testrun_id;
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"notes"}{"testrun_id"} != $testrun_id 0 0 0
83  
84 0 my $job_id = $job->{id};
85 0 $state = $job->{state};
86 0 $self->log->debug("minion: wait for 'finished' ".
87 "job:$job_id testrun:$testrun_id host:$hostname state:$state");
88 0 sleep 5;
89 } while ($state ne 'finished');
90 }
91  
92  
93  
94 sub start_minion
95 0 ▶{
Subroutine start_minion: not called CC=4 SCAR=30.0
Pod documented
96 0 my ($self, $path_to_config, $revive) = @_;
97  
98 0 require Minion;
99  
100 # Tapper details
101 0 my $prc_cfg = YAML::LoadFile($path_to_config);
102 0 my $testrun_id = $prc_cfg->{testrun_id};
103 0 my $hostname = $prc_cfg->{hostname};
104  
105 # Minion details
106 0 my $minion_cfg = $self->cfg->{minion}{frontend}{Minion};
107 0 my $minion = Minion->new(%$minion_cfg);
108 0 my $backend = $minion->backend;
109 0 my $queue_name = $hostname; # Mapping Tapper <---> Minion
110  
111 # Potentially already running job
112 my $last_job = $backend->list_jobs
113 (0, 1, {
114 tasks => ['tapper_testrun'],
115 queues => [$queue_name],
116 0 })->{jobs}[0];
117 0 my $last_job_id =$last_job->{id};
118  
119 # Revive and corresponding job already running? Wait for it.
120 0 ▶ if ($revive
Branch: 1 branch
BranchTrueFalseTotal
if ($revive and $last_job->{"notes"}{"testrun_id"} == $testrun_id and $last_job->{"state"} ne "finished") 0 0 0
Condition0%0/6
$revive and $last_job->{"notes"}{"testrun_id"} == $testrun_id and $last_job->{"state"} ne "finished"
!ll&&!rl&&r
000
$revive and $last_job->{"notes"}{"testrun_id"} == $testrun_id
!ll&&!rl&&r
000
MC/DC0%0/3
$revive and $last_job->{"notes"}{"testrun_id"} == $testrun_id and $last_job->{"state"} ne "finished"
$revive $last_job->{"notes"}{"testrun_id"} == $testrun_id $last_job->{"state"} ne "finished"
Truth table0%0/4
A and B and C
A= $reviveB= $last_job->{"notes"}{"testrun_id"} == $testrun_idC= $last_job->{"state"} ne "finished"
ABCresult
0XX0
10X0
1100
1111
121 and $last_job->{notes}{testrun_id} == $testrun_id
122 and $last_job->{state} ne 'finished' # or better eq 'active'?
123 )
124 {
125 0 $self->log->debug("minion: revive - skip enqueue of testrun $testrun_id");
126 0 return 0;
127 }
128  
129 # Start job
130 0 my @job_args = (prc_cfg => $prc_cfg); # a "sorted hash"
131 0 my $job_id = $minion->enqueue('tapper_testrun'
132 => [@job_args]
133 => { queue => $queue_name,
134 notes => { testrun_id => $testrun_id },
135 },
136 );
137 0 $self->log->debug("minion: started job:$job_id testrun:$testrun_id host:$hostname");
138  
139 # TODO: error handling - what could go wrong?
140  
141 # success
142 0 return 0;
143 }
144  
145  
146  
147 sub stop_minion
148 0 ▶{
Subroutine stop_minion: not called CC=2 SCAR=17.9
Pod documented
149 0 my ($self, $testrun_id, $hostname) = @_;
150  
151 0 require Minion;
152  
153 # Minion details
154 0 my $minion_cfg = $self->cfg->{minion}{frontend}{Minion};
155 0 my $minion = Minion->new(%$minion_cfg);
156 0 my $backend = $minion->backend;
157 0 my $queue_name = $hostname; # Mapping Tapper <---> Minion
158 0 my $jobs = $backend->list_jobs(0, 1, {
159 tasks => ['tapper_testrun'],
160 queues => [$queue_name],
161 });
162 0 my $job_state = $jobs->{jobs}[0]{state};
163 my $job_id
164 0 = $jobs->{jobs}[0]{id};
165 0 my $job_testrun = $jobs->{jobs}[0]{notes}{testrun_id};
166  
167 # Send Minion the 'stop' command;
168 0 ▶ if ($job_testrun == $testrun_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job_testrun == $testrun_id) { } 0 0 0
169 0 $self->log->debug(
170 "minion: CANCEL/STOP job:$job_id testrun:$job_testrun");
171 0 $minion->broadcast('kill', ['INT', $job_id]);
172 } else {
173 0 $self->log->debug(
174 "minion: NO CANCEL/STOP of job:$job_id testrun:$job_testrun != stopped testrun:$testrun_id");
175 }
176  
177 }
178  
179  
180 sub install_client_package
181 1 ▶{
Subroutine install_client_package: called CC=6 SCAR=18.5
Pod documented
182 1 my ($self, $hostname, $package) = @_;
183  
184 1 ▶ my $dest_path = $package->{dest_path} || '/tmp';
Condition50%1/2
$package->{'dest_path'} || '/tmp'
l!l
10
MC/DC0%0/1
$package->{'dest_path'} || '/tmp'
$package->{'dest_path'}
Truth table50%1/2
A
A= $package->{'dest_path'}
Aresult
11
00
185 1 $dest_path .= "/tapper-clientpkg.tgz";
186  
187 1 my $arch = $package->{arch};
188 1 ▶ return "No architecture defined. Can not install client package" if not $arch;
Branch: 1 branch
BranchTrueFalseTotal
unless $arch 0 1 1
189 1 my $clientpkg = $self->cfg->{files}{tapper_package}{$arch};
190  
191 1 ▶ $clientpkg = $self->cfg->{paths}{package_dir}.$clientpkg
Branch: 1 branch
BranchTrueFalseTotal
unless $clientpkg =~ m[^/] 1 0 1
192 if not $clientpkg =~ m,^/,;
193  
194 1 my $scp = Net::SCP->new($hostname);
195 1 my $success = $scp->put(
196 $clientpkg,
197 $dest_path,
198 );
199 1 ▶ return "Can not copy client package '$clientpkg' to $hostname:/$dest_path: ".$scp->{errstr} if not $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 1 1
200  
201 1 my $error = Net::SSH::ssh("$hostname","tar -xzf $dest_path -C /");
202 1 ▶ return "Can not unpack client package on $hostname: $!" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
203 1 return 0;
204 }
205  
206  
207  
208 sub write_grub_file
209 0 ▶{
Subroutine write_grub_file: not called CC=4 SCAR=30.0
Pod documented
210 0 my ($self, $system, $text) = @_;
211 0 ▶ return "No grub text given" unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
212  
213 0 my $grub_file = $self->cfg->{paths}{grubpath}."/$system.lst";
214 0 $self->log->debug("writing grub file $grub_file");
215  
216 # create the initial grub file for installation of the test system,
217 0 ▶ open (my $GRUBFILE, ">", $grub_file) or return "Can open ".$self->cfg->{paths}{grubpath}."/$system.lst for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $GRUBFILE, ">", $grub_file or return 'Can open ' . $self->cfg->{"paths"}{"grubpath"} . "/$system.lst for writing: $!" 0 0 0
218 0 print $GRUBFILE $text;
219 0 ▶ close $GRUBFILE or return "Can't save grub file for $system:$!";
Branch: 1 branch
BranchTrueFalseTotal
close $GRUBFILE or return "Can't save grub file for ${system}:$!" 0 0 0
220 0 return(0);
221 }
222  
223  
224  
225 sub hw_report_create
226 5 ▶{
Subroutine hw_report_create: called CC=2 SCAR=6.9
Pod documented
227 5 my ($self, $testrun_id) = @_;
228 5 my $testrun = model->resultset('Testrun')->find($testrun_id);
229 5 my $host;
230 5 eval {
231 # parts of this chain may be undefined
232  
233 5 $host = $testrun->testrun_scheduling->host;
234 };
235 5 ▶ return (1, qq(testrun '$testrun_id' has no host associated)) unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 5 5
236  
237 5 my $data = get_hardware_overview($host->id);
238 5 my $yaml = Dump($data);
239 5 $yaml .= "...\n";
240 5 $yaml =~ s/^(.*)$/ $1/mg; # indent
241 5 my $report = sprintf("
242 TAP Version 13
243 1..2
244 # Tapper-Reportgroup-Testrun: %s
245 # Tapper-Suite-Name: Hardwaredb Overview
246 # Tapper-Suite-Version: %s
247 # Tapper-Machine-Name: %s
248 ok 1 - Getting hardware information
249 %s
250 ok 2 - Sending
251 ", $testrun_id, $Tapper::MCP::VERSION, $host->name, $yaml);
252  
253 5 return (0, $report);
254 }
255  
256 31 1;
257  
258 __END__
« MCP/Daemon.pm ↑ Summary MCP/Net/TAP.pm »