[Libguestfs] [PATCH 1/2] Config: NFC: always create and pass round a Config object

Matthew Booth mbooth at redhat.com
Tue May 4 19:07:16 UTC 2010


We previously wouldn't create a Config object if no config file was specified.
This change ensures that a Config object is always created, but will do nothing
interesting if there is no config file.

Apart from being slightly cleaner, this allows information provided by Config to
be later supplied from the command line instead.
---
 lib/Sys/VirtV2V/Config.pm         |   34 ++++++++++++++++++++--------------
 lib/Sys/VirtV2V/Converter.pm      |    4 ++--
 lib/Sys/VirtV2V/GuestOS.pm        |    2 +-
 lib/Sys/VirtV2V/GuestOS/RedHat.pm |   18 +-----------------
 v2v/virt-v2v.pl                   |    5 ++---
 5 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/lib/Sys/VirtV2V/Config.pm b/lib/Sys/VirtV2V/Config.pm
index 57bf24a..50c613c 100644
--- a/lib/Sys/VirtV2V/Config.pm
+++ b/lib/Sys/VirtV2V/Config.pm
@@ -69,6 +69,9 @@ sub new
     my $self = {};
     bless($self, $class);
 
+    # No further config required if no config path was specified
+    return $self if (!defined($path));
+
     die(user_message(__x("Config file {path} doesn't exist",
                          path => $path))) unless (-e $path);
 
@@ -99,6 +102,8 @@ sub get_transfer_iso
 
     my $dom = $self->{dom};
 
+    return undef unless (defined($dom));
+
     # path-root doesn't have to be defined
     my ($root) = $dom->findnodes('/virt-v2v/path-root/text()');
     $root = $root->getData() if (defined($root));
@@ -175,15 +180,7 @@ sub get_transfer_iso
     return $iso_path;
 }
 
-=item get_app_search(desc, name, arch)
-
-Return a string describing what v2v is looking for in the config file. The
-string is intended to be presented to the user to help improve the configuration
-file.
-
-=cut
-
-sub get_app_search
+sub _get_app_search
 {
     my ($desc, $name, $arch) = @_;
 
@@ -216,6 +213,10 @@ sub match_app
 
     my $dom = $self->{dom};
 
+    die(user_message(__x("No config specified. No app match for {search}",
+                         search => _get_app_search($desc, $name, $arch))))
+        unless (defined($dom));
+
     my $distro = $desc->{distro};
     my $major  = $desc->{major_version};
     my $minor  = $desc->{minor_version};
@@ -248,7 +249,7 @@ sub match_app
     }
 
     die(user_message(__x("No app in config matches {search}",
-                         search => get_app_search($desc, $name, $arch))))
+                         search => _get_app_search($desc, $name, $arch))))
         unless (defined($app));
 
     my %app;
@@ -263,7 +264,7 @@ sub match_app
     die(user_message(__x("Matched local file {path} for {search}. ".
                          "However, this file is not available.",
                          path => $abs,
-                         search => get_app_search($desc, $name, $arch))))
+                         search => _get_app_search($desc, $name, $arch))))
         unless (-r $abs);
 
     my @deps;
@@ -301,9 +302,14 @@ sub map_network
     my $self = shift;
     my ($oldname, $oldtype) = @_;
 
-    my ($mapping) = $self->{dom}->findnodes
-        ("/virt-v2v/network[\@type='$oldtype' and \@name='$oldname']".
-         "/network");
+    my $dom = $self->{dom};
+
+    my $mapping;
+    if (defined($dom)) {
+        my ($mapping) = $dom->findnodes
+            ("/virt-v2v/network[\@type='$oldtype' and \@name='$oldname']".
+             "/network");
+    }
 
     unless (defined($mapping)) {
         print STDERR user_message(__x("WARNING: No mapping found for ".
diff --git a/lib/Sys/VirtV2V/Converter.pm b/lib/Sys/VirtV2V/Converter.pm
index 45038ee..a5b7e43 100644
--- a/lib/Sys/VirtV2V/Converter.pm
+++ b/lib/Sys/VirtV2V/Converter.pm
@@ -137,7 +137,7 @@ sub convert
 
     my ($g, $guestos, $config, $dom, $desc, $devices) = @_;
     carp("convert called without guestos argument") unless defined($guestos);
-    # config will be undefined if no config was specified
+    carp("convert called without config argument") unless defined($config);
     carp("convert called without dom argument") unless defined($dom);
     carp("convert called without desc argument") unless defined($desc);
     carp("convert called without devices argument") unless defined($devices);
@@ -156,7 +156,7 @@ sub convert
         unless (defined($guestcaps));
 
     # Map network names from config
-    _map_networks($dom, $config) if (defined($config));
+    _map_networks($dom, $config);
 
     # Convert the metadata
     _convert_metadata($dom, $desc, $devices, $guestcaps);
diff --git a/lib/Sys/VirtV2V/GuestOS.pm b/lib/Sys/VirtV2V/GuestOS.pm
index fea4ba1..e5b1704 100644
--- a/lib/Sys/VirtV2V/GuestOS.pm
+++ b/lib/Sys/VirtV2V/GuestOS.pm
@@ -100,7 +100,7 @@ sub new
     defined($g)      or carp("instantiate called without g argument");
     defined($desc)   or carp("instantiate called without desc argument");
     defined($dom)    or carp("instantiate called without dom argument");
-    # config will be undefined if no config was specified
+    defined($config) or carp("instantiate called without config argument");
 
     my $self = {};
 
diff --git a/lib/Sys/VirtV2V/GuestOS/RedHat.pm b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
index a4680c5..ee074c1 100644
--- a/lib/Sys/VirtV2V/GuestOS/RedHat.pm
+++ b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
@@ -475,17 +475,8 @@ sub add_kernel
     eval {
         my $desc = $self->{desc};
 
-        my $config = $self->{config};
-        unless (defined($config)) {
-            my $search = Sys::VirtV2V::Config::get_app_search
-                            ($desc, $kernel_pkg, $kernel_arch);
-            die(user_message(__x("No config specified. No app match for ".
-                                 "{search}",
-                                 search => $search)));
-        }
-
         ($app, $depnames) =
-            $config->match_app($desc, $kernel_pkg, $kernel_arch);
+            $self->{config}->match_app($desc, $kernel_pkg, $kernel_arch);
     };
     # Return undef if we didn't find a kernel
     if ($@) {
@@ -619,13 +610,6 @@ sub add_application
     my $user_arch = $desc->{arch};
 
     my $config = $self->{config};
-    unless (defined($config)) {
-        my $search = Sys::VirtV2V::Config::get_app_search($desc, $label,
-                                                          $user_arch);
-        die(user_message(__x("No config specified. No app match for {search}",
-                             search => $search)));
-    }
-
     my ($app, $deps) = $config->match_app($self->{desc}, $label, $user_arch);
 
     # Nothing to do if it's already installed
diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl
index ea89e48..d9f6bb3 100755
--- a/v2v/virt-v2v.pl
+++ b/v2v/virt-v2v.pl
@@ -217,8 +217,7 @@ GetOptions ("help|?"      => sub {
 ) or pod2usage(2);
 
 # Read the config file if one was given
-my $config;
-$config = Sys::VirtV2V::Config->new($config_file) if defined($config_file);
+my $config = Sys::VirtV2V::Config->new($config_file);
 
 my $target;
 if ($output_method eq "libvirt") {
@@ -302,7 +301,7 @@ my $storage = $conn->get_storage_paths();
 
 # Create the transfer iso if required
 my $transferiso;
-$transferiso = $config->get_transfer_iso() if (defined($config));
+$transferiso = $config->get_transfer_iso();
 
 if ($output_method eq 'rhev') {
     $) = "36 36";
-- 
1.6.6.1




More information about the Libguestfs mailing list