Example 3 - Osirium/vcdriver GitHub Wiki

Creating a virtual machine

Virtual machines are created in vcdriver by cloning virtual machine templates. VirtualMachine objects are lazy, so the action won't start until you call the create or find methods.

The required configuration values for create are the ones in the [Virtual Machine Deployment] section of the ini file you can find in the Example 1.

>>> from vcdriver.vm import VirtualMachine
>>> from vcdriver.config import load
>>> load('path/to/vcdriver.ini')
>>> vm = VirtualMachine(name='vm_name', template='vm_template')
>>> vm.create()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
Waiting for [Create virtual machine "vm_name" from template "vm_template"] ... 0:00:10.074632
>>> vm.set_autostart()
>>> vm.created_at
datetime.datetime(2018, 6, 9, 15, 12, 43, 700814)

You can use any of the 4 ways of injecting the configuration to the create method as explained in Example 1.

The create method is idempotent. That means the second time you call it it won't do anything, because the virtual machine has already been created.

The set_autostart command will set the autostart flag on the virtual machine so it starts with the ESXI host. created_at will tell you when the vm was created.

Vcdriver also lets you find virtual machines by name. As the create method, find is also idempotent and won't do anything the second time you call it.

Find will update the internal state of the virtual machine object with the information gathered from Vsphere, so you can do further management with it.

>>> from vcdriver.vm import VirtualMachine
>>> vm = VirtualMachine(name='vm_name_to_be_found')
>>> vm.find()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61

As a final note, you can get the ip of any machine that has been created or found and also get a summary of it:

>>> from vcdriver.vm import VirtualMachine
>>> vm = VirtualMachine(name='vm_name_to_be_found')
>>> vm.find()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
>>> vm.ip()
'10.0.0.1'
>>> vm.summary()
'Virtual Machine Summary\n=======================\n* **Name**: vm_name_to_be_found\n* **Template**: \n* **IP**: 10.0.0.1'