From 47dde83d944571df61d9c72e9e5b93f105b54a0c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 15 Oct 2012 18:00:10 +0100 Subject: [PATCH] wip reorg ts-host-install --- Osstest.pm | 3 + Osstest/Executive.pm | 104 +---------------------------------- Osstest/HostDB/Executive.pm | 84 ++++++++++++++++++++++++++++ Osstest/HostDB/Static.pm | 59 ++++++++++++++++++++ Osstest/JobDB/Executive.pm | 30 ++++++++-- Osstest/JobDB/Standalone.pm | 4 ++ Osstest/TestSupport.pm | 60 ++++++++++++++++++++ README | 11 +++- standalone-config-example | 1 + ts-host-install | 1 - ts-hosts-allocate-Standalone | 4 +- 11 files changed, 247 insertions(+), 114 deletions(-) create mode 100644 Osstest/HostDB/Executive.pm diff --git a/Osstest.pm b/Osstest.pm index 0cecb2a4..f0105eec 100644 --- a/Osstest.pm +++ b/Osstest.pm @@ -18,6 +18,7 @@ BEGIN { getmethod $dbh_tests db_retry db_begin_work get_filecontents ensuredir get_filecontents_core_quiet system_checked + ); %EXPORT_TAGS = ( ); @@ -93,6 +94,8 @@ sub readglobalconfig () { $mjobdb = getmethod("Osstest::JobDB::$c{JobDb}"); $mhostdb = getmethod("Osstest::HostDB::$c{HostDb}"); + + $c{TestHostDomain} ||= $c{DnsDomain}; } sub augmentconfigdefaults { diff --git a/Osstest/Executive.pm b/Osstest/Executive.pm index 13f798d1..dadba138 100644 --- a/Osstest/Executive.pm +++ b/Osstest/Executive.pm @@ -81,8 +81,7 @@ BEGIN { resource_check_allocated resource_shared_mark_ready built_stash duration_estimator csreadconfig ts_get_host_guest - opendb_state selecthost get_hostflags - get_host_property get_timeout + opendb_state get_timeout host_involves_pcipassthrough host_get_pcipassthrough_devs postfork link_file_contents create_webfile @@ -1040,13 +1039,6 @@ sub get_hostflags ($) { return grep /./, split /\,/, $flags; } -sub get_host_property ($$;$) { - my ($ho, $prop, $defval) = @_; - my $row= $ho->{Properties}{$prop}; - return $defval unless $row && defined $row->{val}; - return $row->{val}; -} - sub host_involves_pcipassthrough ($) { my ($ho) = @_; return !!grep m/^pcipassthrough\-/, get_hostflags($ho->{Ident}); @@ -1070,100 +1062,6 @@ sub host_get_pcipassthrough_devs ($) { return @devs; } -sub selecthost ($) { - my ($ident) = @_; - # must be run outside transaction - my $name; - if ($ident =~ m/=/) { - $ident= $`; - $name= $'; #' - $r{$ident}= $name; - } else { - $name= $r{$ident}; - die "no specified $ident" unless defined $name; - } - - my $ho= { - Ident => $ident, - Name => $name, - TcpCheckPort => 22, - Fqdn => "$name.$c{TestHostDomain}", - Info => [], - Suite => get_runvar_default("${ident}_suite",$job,$c{Suite}), - }; - - $ho->{Properties}= $dbh_tests->selectall_hashref(<{Properties}{$r}; - return unless $row; - $ho->{$k}= $row->{val}; - }; - $ho->{Ether}= get_host_property($ho,'ether'); - $ho->{Power}= get_host_property($ho,'power-method'); - $ho->{DiskDevice}= get_host_property($ho,'disk-device'); - $ho->{DhcpLeases}= get_host_property($ho,'dhcp-leases',$c{Dhcp3Leases}); - - if (!$ho->{Ether} || !$ho->{Power}) { - my $dbh_config= opendb('configdb'); - my $selname= $ho->{Fqdn}; - my $sth= $dbh_config->prepare(<execute($selname); - my $row= $sth->fetchrow_hashref(); - die "$ident $name $selname ?" unless $row; - die if $sth->fetchrow_hashref(); - $sth->finish(); - my $get= sub { - my ($k,$nowarn) = @_; - my $v= $row->{$k}; - defined $v or $nowarn or - warn "host $name: undefined $k in configdb::ips\n"; - return $v; - }; - $ho->{Asset}= $get->('asset',1); - $ho->{Ether} ||= $get->('hardware'); - $ho->{Power} ||= "statedb $ho->{Asset}"; - push @{ $ho->{Info} }, "(asset=$ho->{Asset})" if defined $ho->{Asset}; - $dbh_config->disconnect(); - } - - my $ip_packed= gethostbyname($ho->{Fqdn}); - die "$ho->{Fqdn} ?" unless $ip_packed; - $ho->{Ip}= inet_ntoa($ip_packed); - die "$ho->{Fqdn} ?" unless defined $ho->{Ip}; - - $ho->{Flags}= { }; - my $flagsq= $dbh_tests->prepare(<execute($name); - while (my ($flag) = $flagsq->fetchrow_array()) { - $ho->{Flags}{$flag}= 1; - } - $flagsq->finish(); - - $ho->{Shared}= resource_check_allocated('host', $name); - $ho->{SharedReady}= - $ho->{Shared} && - $ho->{Shared}{State} eq 'ready' && - !! grep { $_ eq "share-".$ho->{Shared}{Type} } get_hostflags($ident); - $ho->{SharedOthers}= - $ho->{Shared} ? $ho->{Shared}{Others} : 0; - - logm("host: selected $ho->{Name} $ho->{Ether} $ho->{Ip}". - (!$ho->{Shared} ? '' : - sprintf(" - shared %s %s %d", $ho->{Shared}{Type}, - $ho->{Shared}{State}, $ho->{Shared}{Others}+1))); - - return $ho; -} - sub get_timeout ($$$) { my ($ho,$which,$default) = @_; return $default + get_host_property($ho, "$which-time-extra", 0); diff --git a/Osstest/HostDB/Executive.pm b/Osstest/HostDB/Executive.pm new file mode 100644 index 00000000..e7d94287 --- /dev/null +++ b/Osstest/HostDB/Executive.pm @@ -0,0 +1,84 @@ + +package Osstest::HostDB::Executive; + +use strict; +use warnings; + +use Osstest; +use Osstest::Executive; + +BEGIN { + use Exporter (); + our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); + $VERSION = 1.00; + @ISA = qw(Exporter); + @EXPORT = qw(); + %EXPORT_TAGS = ( ); + + @EXPORT_OK = qw(); +} + +sub new { return bless {}, $_[0]; } + +sub get_properties ($$) { + my ($hd, $name) = @_; + + return $dbh_tests->selectall_hashref(<prepare(<execute($ho->{Name}); + + while (my ($flag) = $flagsq->fetchrow_array()) { + $flags->{$flag}= 1; + } + $flagsq->finish(); + return $flags; +} + +sub get_property ($$$;$) { + my ($hd, $ho, $prop, $defval) = @_; + my $row= $ho->{Properties}{$prop}; + return $defval unless $row && defined $row->{val}; + return $row->{val}; +} + +sub default_methods ($$) { + my ($hd, $ho) = @_; + + return if $ho->{Ether} && $ho->{Power}; + + my $dbh_config= opendb('configdb'); + my $selname= $ho->{Fqdn}; + my $sth= $dbh_config->prepare(<execute($selname); + my $row= $sth->fetchrow_hashref(); + die "$ident $name $selname ?" unless $row; + die if $sth->fetchrow_hashref(); + $sth->finish(); + my $get= sub { + my ($k,$nowarn) = @_; + my $v= $row->{$k}; + defined $v or $nowarn or + warn "host $name: undefined $k in configdb::ips\n"; + return $v; + }; + $ho->{Asset}= $get->('asset',1); + $ho->{Ether} ||= $get->('hardware'); + $ho->{Power} ||= "statedb $ho->{Asset}"; + push @{ $ho->{Info} }, "(asset=$ho->{Asset})" if defined $ho->{Asset}; + $dbh_config->disconnect(); +} + +1; diff --git a/Osstest/HostDB/Static.pm b/Osstest/HostDB/Static.pm index e8828548..53456f40 100644 --- a/Osstest/HostDB/Static.pm +++ b/Osstest/HostDB/Static.pm @@ -20,4 +20,63 @@ BEGIN { sub new { return bless {}, $_[0]; } +sub get_properties ($$) { #method + my ($hd, $name) = @_; + my $hp = { }; + my $k; + foreach $k (keys %c) { + next unless $k =~ m/^HostProp_([A-Z].*)$/; + $hp->{$1} = $c{$k}; + } + foreach $k (keys %c) { + next unless $k =~ m/^HostProp_([a-z0-9]+)_(.*)$/; + next unless $1 eq $name; + $hp->{$2} = $c{$k}; + } + return $hp; +} + +sub get_property ($$$;$) { #method + my ($hd, $ho, $prop, $defval) = @_; + + $prop = ucfirst $prop; + while ($prop =~ m/-/) { + $prop = $`.ucfirst $'; #'; + } + + my $val = $ho->{Properties}{$prop}; + return $defval unless defined $val; + return $val; +} + +sub get_flags ($$) { #method + my ($hd, $ho) = @_; + + my $flags = { }; + my $process = sub { + my $str = $c{$_[0]}; + return unless defined $str; + foreach my $fl (split /[ \t,;]+/, $str) { + next unless length $fl; + if ($fl =~ s/^\!//) { + delete $flags->{$fl}; + } else { + $flags->{$fl} = 1; + } + } + }; + + $process->('HostFlags'); + $process->("HostFlags_$ho->{Name}"); + + return $flags; +} + +sub default_methods ($$) { #method + my ($hd, $ho) = @_; + + die "need ethernet address for $ho->{Name}" unless $ho->{Ether}; + $ho->{Power} ||= "manual $ho->{Name}"; +} + 1; diff --git a/Osstest/JobDB/Executive.pm b/Osstest/JobDB/Executive.pm index 1ccb2d08..ee9a7826 100644 --- a/Osstest/JobDB/Executive.pm +++ b/Osstest/JobDB/Executive.pm @@ -19,8 +19,8 @@ BEGIN { sub new { return bless {}, Osstest::JobDB::Standalone }; -sub begin_work ($$) { - my ($dbh,$tables) = @_; +sub begin_work ($$$) { #method + my ($jd, $dbh,$tables) = @_; return if $ENV{'OSSTEST_DEBUG_NOSQLLOCK'}; foreach my $tab (@$tables) { @@ -28,11 +28,11 @@ sub begin_work ($$) { } } -sub current_flight ($) { +sub current_flight ($) { #method return $ENV{'OSSTEST_FLIGHT'}; } -sub open () { +sub open ($) { #method return opendb('osstestdb'); } @@ -68,7 +68,8 @@ END } } -sub flight_create () { +sub flight_create ($) { #method + my ($jd) = @_; $dbh_tests->do(<selectrow_array(<0; } + +sub host_check_allocated ($$) { #method + my ($jd, $ho) = @_; + $ho->{Shared}= resource_check_allocated('host', $name); + $ho->{SharedReady}= + $ho->{Shared} && + $ho->{Shared}{State} eq 'ready' && + !! grep { $_ eq "share-".$ho->{Shared}{Type} } get_hostflags($ident); + $ho->{SharedOthers}= + $ho->{Shared} ? $ho->{Shared}{Others} : 0; + + die if $ho->{SharedOthers} && !$ho->{SharedReady}; +} + +1; diff --git a/Osstest/JobDB/Standalone.pm b/Osstest/JobDB/Standalone.pm index 0e8c196f..9a115220 100644 --- a/Osstest/JobDB/Standalone.pm +++ b/Osstest/JobDB/Standalone.pm @@ -61,4 +61,8 @@ sub current_flight ($) { sub job_ensure_started ($) { } +sub host_check_allocated ($$) { #method + my ($jd, $ho) = @_; +} + 1; diff --git a/Osstest/TestSupport.pm b/Osstest/TestSupport.pm index c60aca5d..213b2015 100644 --- a/Osstest/TestSupport.pm +++ b/Osstest/TestSupport.pm @@ -7,6 +7,7 @@ use warnings; use POSIX; use DBI; use IO::File; +use IO::Socket::INET; use Osstest; @@ -33,6 +34,9 @@ store_runvar get_runvar get_runvar_maybe get_runvar_default need_runvars target_install_packages target_install_packages_norec target_extract_jobdistpath poll_loop + +selecthost get_hostflags + get_host_property ); %EXPORT_TAGS = ( ); @@ -516,4 +520,60 @@ sub poll_loop ($$$&) { logm("$what: ok. (${waited}s)"); } +#---------- host selection and properties ---------- + +sub selecthost ($) { + my ($ident) = @_; + # must be run outside transaction + my $name; + if ($ident =~ m/=/) { + $ident= $`; + $name= $'; #' + $r{$ident}= $name; + } else { + $name= $r{$ident}; + die "no specified $ident" unless defined $name; + } + + my $fqdn = $name; + $fqdn .= ".$c{TestHostDomain}" unless $fqdn =~ m/\./; + my $ho= { + Ident => $ident, + Name => $name, + TcpCheckPort => 22, + Fqdn => $fqdn, + Info => [], + Suite => get_runvar_default("${ident}_suite",$job,$c{Suite}), + }; + + $ho->{Properties} = $mhostdb->get_properties($name); + + $ho->{Ether}= get_host_property($ho,'ether'); + $ho->{Power}= get_host_property($ho,'power-method'); + $ho->{DiskDevice}= get_host_property($ho,'disk-device'); + $ho->{DhcpLeases}= get_host_property($ho,'dhcp-leases',$c{Dhcp3Leases}); + + $mhostdb->default_methods($ho); + + my $ip_packed= gethostbyname($ho->{Fqdn}); + die "$ho->{Fqdn} ?" unless $ip_packed; + $ho->{Ip}= inet_ntoa($ip_packed); + die "$ho->{Fqdn} ?" unless defined $ho->{Ip}; + + $ho->{Flags} = $mhostdb->get_flags($ho); + + $mjobdb->host_check_allocated($ho); + + logm("host: selected $ho->{Name} $ho->{Ether} $ho->{Ip}". + (!$ho->{Shared} ? '' : + sprintf(" - shared %s %s %d", $ho->{Shared}{Type}, + $ho->{Shared}{State}, $ho->{Shared}{Others}+1))); + + return $ho; +} + +sub get_host_property ($$;$) { + return $mhostdb->get_property(@_); +} + 1; diff --git a/README b/README index e1753525..392ca098 100644 --- a/README +++ b/README @@ -18,8 +18,15 @@ JobDbStandaloneFilename Database file to use to record the "jobs" and their run variables. Default: ./standalone.db (sqlite3) -TargetHost_ where eg = "host" -TargetHost +TestHostDomain defaults to DnsDomain + +TestHost_ where eg = "host" +TestHost + +HostProp_ +HostProp__ +HostFlags flag,flag,flag,... +HostFlags_ flag,!flag,!flag,flag... ======================================== diff --git a/standalone-config-example b/standalone-config-example index 08bb2cf1..10406fd1 100644 --- a/standalone-config-example +++ b/standalone-config-example @@ -1,3 +1,4 @@ DnsDomain cam.xci-test.com NetNameservers 10.80.248.2 10.80.16.28 10.80.16.67 TargetHost bedbug +HostProp_bedbug_Ether 00:13:72:14:c0:51 diff --git a/ts-host-install b/ts-host-install index 21a73918..a1ae3203 100755 --- a/ts-host-install +++ b/ts-host-install @@ -23,7 +23,6 @@ our ($whhost) = @ARGV; $whhost ||= 'host'; our $ho= selecthost($whhost); exit 0 if $ho->{SharedReady}; -die if $ho->{SharedOthers}; our %timeout= qw(ReadPreseed 350 Sshd 2400); diff --git a/ts-hosts-allocate-Standalone b/ts-hosts-allocate-Standalone index cbdb725c..bed3afb3 100755 --- a/ts-hosts-allocate-Standalone +++ b/ts-hosts-allocate-Standalone @@ -13,8 +13,8 @@ foreach my $ident (@ARGV) { } $host ||= $r{$ident};; $host ||= $ENV{'OSSTEST_HOST_'.uc $ident}; - $host ||= $c{"TargetHost_$ident"}; - $host ||= $c{TargetHost}; + $host ||= $c{"TestHost_$ident"}; + $host ||= $c{TestHost}; $host || die "need host setting for $ident"; store_runvar($ident, $host); } -- 2.39.5