Rather than having mg-hosts mkpxedir hardcode the strange thing done
in the XenClient test lab in the Citrix Cambridge office, provide a
somewhat more general and correct approach:

* Generalise host_pxefile to support [Tftp]PxeTemplatesReal as well
  as [Tftp]PxeTemplates.
* Default [Tftp]PxeTemplatesReal to ''

mg-hosts mkpxedir now uses these templates, as follow:
* Create the host's PxeTemplates-based pxe file's parent
  directories and make the parent directory be owned by PxeGroup.
* If the PxeTemplatesReal is specified and different, make a symlink
  named according to PxeTemplatesReal pointing at the previous file.

Signed-off-by: Ian Jackson <ian.jack...@eu.citrix.com>
---
 Osstest.pm             |    1 +
 Osstest/TestSupport.pm |   13 ++++++++-----
 README                 |    5 +++++
 mg-hosts               |   28 ++++++++++++++++++++--------
 4 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/Osstest.pm b/Osstest.pm
index 7a49591..22dbc8b 100644
--- a/Osstest.pm
+++ b/Osstest.pm
@@ -76,6 +76,7 @@ our %c = qw(
 
 $c{$_}='' foreach qw(
     DebianPreseed
+    TftpPxeTemplatesReal
 );
 
 #---------- general setup and config reading ----------
diff --git a/Osstest/TestSupport.pm b/Osstest/TestSupport.pm
index 5677946..2d1ba9e 100644
--- a/Osstest/TestSupport.pm
+++ b/Osstest/TestSupport.pm
@@ -860,7 +860,8 @@ sub selecthost ($) {
     logm("TftpScope is $tftpscope");
     $ho->{Tftp} = { };
     $ho->{Tftp}{$_} = $c{"Tftp${_}_${tftpscope}"} || $c{"Tftp${_}"}
-        foreach qw(Path TmpDir PxeDir PxeGroup PxeTemplates DiBase);
+        foreach qw(Path TmpDir PxeDir PxeGroup PxeTemplates PxeTemplatesReal
+                   DiBase);
 
     #----- finalise -----
 
@@ -2005,9 +2006,11 @@ sub file_link_contents ($$$) {
     logm("wrote $fn". (defined $stash ? " (stashed as $stash)" : ""));
 }
 
-sub host_pxefile ($) {
-    my ($ho) = @_;
+sub host_pxefile ($;$) {
+    my ($ho, $templatekey) = @_;
     my %v = %r;
+    $templatekey //= 'PxeTemplates';
+    my $templates = $ho->{Tftp}{$templatekey} || $ho->{Tftp}{PxeTemplates};
     if (defined $ho->{Ether}) {
        my $eth = $v{'ether'} = $ho->{Ether};
        $eth =~ y/A-Z/a-z/;
@@ -2024,7 +2027,7 @@ sub host_pxefile ($) {
        $v{'ipaddr'} = $ip;
        $v{'ipaddrhex'} = sprintf "%02X%02X%02X%02X", split /\./, $ip;
     }
-    foreach my $pat (split /\s+/, $ho->{Tftp}{PxeTemplates}) {
+    foreach my $pat (split /\s+/, $templates) {
        # we skip patterns that contain any references to undefined %var%s
        $pat =~ s{\%(\w*)\%}{
                    $1 eq '' ? '%' :
@@ -2034,7 +2037,7 @@ sub host_pxefile ($) {
        # and return the first pattern we managed to completely substitute
         return $pat;
     }
-    die "no pxe template matched $ho->{Tftp}{PxeTemplates} ".
+    die "no pxe template ($templatekey) matched $templates ".
         (join ",", sort keys %v)." ?";
 }
 
diff --git a/README b/README
index fe0abb2..e780762 100644
--- a/README
+++ b/README
@@ -441,6 +441,7 @@ TftpFoo_<scope> and TftpFoo
                       (e.g. pxelinux.cfg)
         PxeGroup      The Unix group which should own files under `PxeDir'.
         PxeTemplates  See TftpPxeTemplates
+        PxeTemplatesReal
 
         DiBase        The path under `Path' to the root of the debian installer
                       images.
@@ -463,6 +464,10 @@ TftpPxeTemplates
     ip address - are skipped.  The first template all of whose ingredients
     are known is used, with TftpPath and TftpPxeDir prepended.
 
+TftpPxeTemplatesReal
+    Template filename which mg-hosts mkpxedir should make be a symlink to
+    the TftpPxeTemplates.  Not used otherwise.
+
 ========================================
 
 Host-specific config settigs
diff --git a/mg-hosts b/mg-hosts
index 6ec8d56..f695f98 100755
--- a/mg-hosts
+++ b/mg-hosts
@@ -87,6 +87,7 @@ use strict qw(vars refs);
 use DBI;
 use Osstest;
 use Osstest::TestSupport;
+use File::Basename;
 
 csreadconfig();
 
@@ -108,17 +109,28 @@ sub cmd_mkpxedir () {
     my $sudo = $ENV{'OSSTEST_SUDO'} // 'sudo';
     foreach my $hn (@ARGV) {
         my $ho= selecthost("host=$hn");
-        my $macdir= $ho->{Ether};
-        $macdir =~ s/\:/-/g;
-        system_checked(<<END);
+       my $pxefile = host_pxefile($ho);
+       my $pxerealfile = host_pxefile($ho, 'PxeTemplatesReal');
+       my $dirname = dirname $pxefile;
+       my $cmd = <<END;
             set -e
            cd $ho->{Tftp}{Path}$ho->{Tftp}{PxeDir}
-            $sudo chown root.$ho->{Tftp}{PxeGroup} $macdir
-            $sudo chmod 2775 $macdir
-            $sudo rm -f $hn
-            $sudo ln -s $macdir $hn
-            ls -ald $hn $macdir
 END
+        $cmd .= <<END if $dirname ne '.';
+           $sudo mkdir -p $dirname
+END
+        $cmd .= <<END;
+            $sudo chown root.$ho->{Tftp}{PxeGroup} $dirname
+            $sudo chmod 2775 $dirname
+END
+        if ($pxefile ne $pxerealfile) {
+           my $subdirs = "../" x $pxefile =~ m#/#;
+            $cmd .= <<END;
+           $sudo ln -sf $subdirs$pxefile $pxerealfile
+END
+        }
+        print $cmd;
+        system_checked($cmd) unless $dryrun;
     }
 }
 
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

Reply via email to