[gnome-boxes/wip/image-import: 19/27] Add VMImporter class



commit 83580d47bca5f432b4edb565ef3ec9a9224c20e9
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Jun 5 04:25:28 2013 +0300

    Add VMImporter class
    
    This subclass of VMCreator will be responsible for importing the VM
    after its creation.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=690757

 src/Makefile.am          |    1 +
 src/installed-media.vala |    4 +++
 src/vm-creator.vala      |   36 ++++++++++++------------
 src/vm-importer.vala     |   67 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 90 insertions(+), 18 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5035778..12c41db 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -112,6 +112,7 @@ gnome_boxes_SOURCES =                               \
        util-app.vala                           \
        vm-configurator.vala                    \
        vm-creator.vala                         \
+       vm-importer.vala                        \
        vnc-display.vala                        \
        wizard-source.vala                      \
        wizard.vala                             \
diff --git a/src/installed-media.vala b/src/installed-media.vala
index 20facc1..6c142c1 100644
--- a/src/installed-media.vala
+++ b/src/installed-media.vala
@@ -61,4 +61,8 @@ private class Boxes.InstalledMedia : Boxes.InstallerMedia {
         return properties;
     }
 
+
+    public override VMCreator get_vm_creator () {
+        return new VMImporter (this);
+    }
 }
diff --git a/src/vm-creator.vala b/src/vm-creator.vala
index 3bcdd02..d4eaaa0 100644
--- a/src/vm-creator.vala
+++ b/src/vm-creator.vala
@@ -8,7 +8,7 @@ private class Boxes.VMCreator {
     // of error. Its better to report '100%' done while its not exactly 100% than reporting '99%' done 
forever..
     private const int INSTALL_COMPLETE_PERCENT = 99;
 
-    public InstallerMedia? install_media { get; private set; }
+    public InstallerMedia? install_media { get; protected set; }
 
     private Connection? connection { owned get { return App.app.default_connection; } }
     private ulong state_changed_id;
@@ -60,7 +60,7 @@ private class Boxes.VMCreator {
         return LibvirtBroker.get_default ().add_domain (App.app.default_source, App.app.default_connection, 
domain);
     }
 
-    public void launch_vm (LibvirtMachine machine) throws GLib.Error {
+    public virtual void launch_vm (LibvirtMachine machine) throws GLib.Error {
         if (!(install_media is UnattendedInstaller) || !(install_media as 
UnattendedInstaller).express_install) {
             ulong signal_id = 0;
 
@@ -82,7 +82,7 @@ private class Boxes.VMCreator {
         update_machine_info (machine);
     }
 
-    private async void continue_installation (LibvirtMachine machine) {
+    protected virtual async void continue_installation (LibvirtMachine machine) {
         install_media = yield MediaManager.get_instance ().create_installer_media_from_config 
(machine.domain_config);
         num_reboots = VMConfigurator.get_num_reboots (machine.domain_config);
         var name = machine.domain.get_name ();
@@ -171,7 +171,7 @@ private class Boxes.VMCreator {
         }
     }
 
-    private void update_machine_info (LibvirtMachine machine) {
+    protected virtual void update_machine_info (LibvirtMachine machine) {
         if (VMConfigurator.is_install_config (machine.domain_config)) {
             machine.info = _("Installing...");
 
@@ -180,7 +180,7 @@ private class Boxes.VMCreator {
             machine.info = _("Live");
     }
 
-    private void set_post_install_config (LibvirtMachine machine) {
+    protected void set_post_install_config (LibvirtMachine machine) {
         debug ("Setting post-installation configuration on '%s'", machine.name);
         try {
             var config = machine.domain.get_config (GVir.DomainXMLFlags.INACTIVE);
@@ -191,6 +191,19 @@ private class Boxes.VMCreator {
         }
     }
 
+    protected async StoragePool get_storage_pool () throws GLib.Error {
+        var pool = Boxes.get_storage_pool (connection);
+        if (pool == null) {
+            var config = VMConfigurator.get_pool_config ();
+            pool = connection.create_storage_pool (config, 0);
+            yield pool.build_async (0, null);
+            yield pool.start_async (0, null);
+            yield pool.refresh_async (null);
+        }
+
+        return pool;
+    }
+
     private void increment_num_reboots (LibvirtMachine machine) {
         num_reboots++;
         try {
@@ -311,17 +324,4 @@ private class Boxes.VMCreator {
 
         return volume;
     }
-
-    private async StoragePool get_storage_pool () throws GLib.Error {
-        var pool = Boxes.get_storage_pool (connection);
-        if (pool == null) {
-            var config = VMConfigurator.get_pool_config ();
-            pool = connection.create_storage_pool (config, 0);
-            yield pool.build_async (0, null);
-            yield pool.start_async (0, null);
-            yield pool.refresh_async (null);
-        }
-
-        return pool;
-    }
 }
diff --git a/src/vm-importer.vala b/src/vm-importer.vala
new file mode 100644
index 0000000..c96aa79
--- /dev/null
+++ b/src/vm-importer.vala
@@ -0,0 +1,67 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+using Osinfo;
+using GVir;
+
+private class Boxes.VMImporter : Boxes.VMCreator {
+    public InstalledMedia source_media { get { return install_media as InstalledMedia; } }
+
+    public VMImporter (InstalledMedia source_media) {
+        base (source_media);
+    }
+
+    public VMImporter.for_import_completion (LibvirtMachine machine) {
+        base.for_install_completion (machine);
+    }
+
+    public override void launch_vm (LibvirtMachine machine) throws GLib.Error {
+        machine.vm_creator = this;
+        machine.config.access_last_time = get_real_time ();
+        update_machine_info (machine);
+
+        import_vm.begin (machine);
+    }
+
+    protected override async void continue_installation (LibvirtMachine machine) {
+        install_media = yield MediaManager.get_instance ().create_installer_media_from_config 
(machine.domain_config);
+        machine.vm_creator = this;
+        update_machine_info (machine);
+
+        yield import_vm (machine);
+    }
+
+    protected override void update_machine_info (LibvirtMachine machine) {
+        machine.info = _("Importing...");
+    }
+
+    private async void import_vm (LibvirtMachine machine) {
+        try {
+            var destination_path = machine.storage_volume.get_path ();
+
+            yield source_media.convert_to_native_format (destination_path);
+
+            // Without refreshing the pool, libvirt will not know of changes to volume we just made
+            var pool = yield get_storage_pool ();
+            yield pool.refresh_async (null);
+        } catch (GLib.Error error) {
+            warning ("Failed to import box '%s' from file '%s': %s",
+                     machine.name,
+                     source_media.device_file,
+                     error.message);
+            var ui_message = _("Box import from file '%s' failed.").printf (source_media.device_file);
+            App.app.notificationbar.display_error (ui_message);
+            machine.delete ();
+
+            return;
+        }
+
+        set_post_install_config (machine);
+        try {
+            machine.domain.start (0);
+        } catch (GLib.Error error) {
+            warning ("Failed to start domain '%s': %s", machine.domain.get_name (), error.message);
+        }
+        machine.info = null;
+        machine.vm_creator = null;
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]