Coder Social home page Coder Social logo

Comments (13)

mjdorma avatar mjdorma commented on September 26, 2024

Does this do the job you're after?

https://gist.github.com/mjdorma/d4a2b126be02c9763d8c

You may also be interested in my other gist examples which are prefixed with pyvbox: https://gist.github.com/mjdorma

As a future reference, it is important to understand IVirtualBox and ISession:

The overview given by http://pythonhosted.org//pyvbox/virtualbox/library.html#module-virtualbox.library is also very useful.

Cheers

from virtualbox-python.

nilp0inter avatar nilp0inter commented on September 26, 2024

It's not exactly what I was intended to do; but it's working well.

For the record, I was trying to do the same thing that the GUI does in the "Import Appliance" wizard when it let you change the values of the machine being imported before the importation.

import_appliance

Anyhow, I'm happy with your solution.
Much appreciated! Thanks a lot.

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

Thanks for the coding challenge 👍 it gave me some light entertainment for the morning. When I get around to it I'll role this into library_ext for the appliance class.

The interface was a little clunky to use, but I'm guessing it needs to be this way to take into account all of the complexities of bundling loose metadata with N many components and machines.

from __future__ import print_function
import sys
import virtualbox
from virtualbox.library import VirtualSystemDescriptionType as DescType
from virtualbox.library import VirtualSystemDescriptionValueType as DescValueType

class MyAppliance(virtualbox.library.IAppliance):
    def __init__(self, appliance):
        super(MyAppliance, self).__init__(appliance._i)

    def read(self, ova_path):
        "Read and interpret ova file into this Appliance object."
        p = super(MyAppliance, self).read(ova_path)
        p.wait_for_completion()
        self.interpret()
        for warning in self.get_warnings():
            print("WARNING: %s" % warning, file=sys.stderr)

    def find_desc(self, name):
        "Find a description for the given appliance name."
        for desc in self.virtual_system_descriptions:
            values = desc.get_values_by_type(DescType.name,
                                             DescValueType.original)
            if name in values:
                break
        else:
            raise Exception("Failed to find description for %s" % name)
        return desc

    def change_name(self, original_name, new_name):
        "Change the name of the appliance."
        desc = self.find_desc(original_name)

        types, _, _, vbox_values, extra_config = desc.get_description()

        # find offset to Name
        nameoffset = 0
        for nameoffset, t in enumerate(types):
            if t == DescType.name:
                break
        else:
            raise Exception("Failed to find name type")

        enabled = [True] * len(types)
        vbox_values = list(vbox_values)
        extra_config = list(extra_config)
        vbox_values[nameoffset] = new_name
        desc.set_final_values(enabled, vbox_values, extra_config)

Example usage:

##  Using MyAppliance
vbox = virtualbox.VirtualBox()
appliance = MyAppliance(vbox.create_appliance())
appliance.read("~/Documents/ubuntu.ova")
appliance.change_name('ubuntu', 'foobarmoo')
p = appliance.import_machines([])
p.wait_for_completion()

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

With commit 3c41f92 you can now use the following gist:

https://gist.github.com/mjdorma/d134f437c16df1871fcd

This will be available in version 0.1.5+.

from virtualbox-python.

nilp0inter avatar nilp0inter commented on September 26, 2024

Wow, definitely the data given by get_description and co. needs a high level API :)

This is overwhelming:

        enabled = [True] * len(types)
        vbox_values = list(vbox_values)
        extra_config = list(extra_config)
        vbox_values[nameoffset] = new_name
        desc.set_final_values(enabled, vbox_values, extra_config)

I don't know all the scenarios and corner cases. But I'd like to give you feedback from my user point of view.

I feel that one interface like the used in the settings of the vm network adapters would fit well here.

I'm doing something like this with my VM NIC:

session = virtualbox.Session()

... locking the machine ...                                                                                

iface = session.machine.get_network_adapter(0)
iface.enabled = True
iface.host_only_interface='vboxnet0'
iface.attachment_type = virtualbox.library.NetworkAttachmentType(4)  # Hostonly

... save config & release session ...

So, what about something like this?:

appliance = MyAppliance(vbox.create_appliance())
appliance.read("~/Documents/ubuntu.ova")

mynewbox = appliance.find_machine('ubuntu')  # If the appliance can contain N machines you have to find the one you want.
mynewbox.config.name = 'foobarmoo'
mynewbox.config.cpu = 4

appliance.import_machines([mynewbox]).wait_for_completion()  # Or empty list for ALL (the default behavior).

One more thing. The first time I used the IAppliance object took me a lot to figure out that the machines attribute is not the machines contained in the appliance, but the machines imported after the import_machines is called. Why not rename it to imported_machines or something?

+1 for the call to interpret() in the read() method.

Thank you!

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

There is quite a lot of oach in IAppliance and its aggregate objects.

If you have more requirements to add "easier" setters into IVirtualSystemDescription feel free to crack opened another issue and I'll give it a go.

If you have already solved the various "setter" functions for the IVirtualSystemDescription object, feel free to post the code or submit a patch.

https://github.com/mjdorma/pyvbox/blob/master/virtualbox/library_ext/virtual_system_description.py

I'll hold off pushing a new release for a few days until this interface has settled.

Cheers

from virtualbox-python.

nilp0inter avatar nilp0inter commented on September 26, 2024

I haven't any code yet, but definitely I'll have a look at this.

I really like to contribute in this but I don't understand the vbox_values and extra_config data. Is there any documentation about?

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

Yeah I have no idea about vbox_values and extra_config. I also don't understand how one would add extra values (or if that's even possible).

6431b60 > generalises the function (but I've only tested it on name.).

https://github.com/mjdorma/pyvbox/blob/master/virtualbox/library_ext/virtual_system_description.py

from virtualbox-python.

nilp0inter avatar nilp0inter commented on September 26, 2024

Wow, 6431b60 looks great!
I'll test it as soon as possible.

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

Hmm.. yeah, just realised it probably won't work as we'd expect because we'll be blatting the "final" values with each additional value we're setting (this is an assumption, not tested.

I think the Python IVirtualSystemDescription will need to build up a list of things "set" and the Python IAppliance will have to interrogate it before doing the import_machines operation through the real IAppliance interface.

I won't do this today, but I'll need to do it before I release 0.1.5.

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

Scrap that previous comment... VirtualBox keeps the state of the updates are passed through.

Let me know how your testing goes.

Thanks for your help!

from virtualbox-python.

mjdorma avatar mjdorma commented on September 26, 2024

Thanks for your contribution and the neat code you put into test_appliance.py.

Cheers

from virtualbox-python.

nilp0inter avatar nilp0inter commented on September 26, 2024

Thanks to you for your excellent support!

from virtualbox-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.