| 1 |
===== |
| 2 |
Usage |
| 3 |
===== |
| 4 |
|
| 5 |
The interface to the ``os_vif`` library is very simple. To begin using the |
| 6 |
library, first call the ``os_vif.initialize()`` function. This will load all |
| 7 |
installed plugins and register the object model: |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
import os_vif |
| 12 |
|
| 13 |
os_vif.initialize() |
| 14 |
|
| 15 |
Once the ``os_vif`` library is initialized, there are only two other library |
| 16 |
functions: ``os_vif.plug()`` and ``os_vif.unplug()``. Both methods accept an |
| 17 |
argument of (a subclass of) type ``os_vif.objects.vif.VIFBase`` and an argument |
| 18 |
of type ``os_vif.objects.instance_info.InstanceInfo``: |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
import uuid |
| 23 |
|
| 24 |
from nova import objects as nova_objects |
| 25 |
from os_vif import exception as vif_exc |
| 26 |
from os_vif.objects import fields |
| 27 |
from os_vif.objects import instance_info |
| 28 |
from os_vif.objects import network |
| 29 |
from os_vif.objects import subnet as os_subnet |
| 30 |
from os_vif.objects import vif as vif_obj |
| 31 |
|
| 32 |
instance_uuid = 'd7a730ca-3c28-49c3-8f26-4662b909fe8a' |
| 33 |
instance = nova_objects.Instance.get_by_uuid(instance_uuid) |
| 34 |
instance_info = instance_info.InstanceInfo( |
| 35 |
uuid=instance.uuid, |
| 36 |
name=instance.name, |
| 37 |
project_id=instance.project_id) |
| 38 |
|
| 39 |
subnet = os_subnet.Subnet(cidr='192.168.1.0/24') |
| 40 |
subnets = os_subnet.SubnetList([subnet]) |
| 41 |
network = network.Network(label='tenantnet', |
| 42 |
subnets=subnets, |
| 43 |
multi_host=False, |
| 44 |
should_provide_vlan=False, |
| 45 |
should_provide_bridge=False) |
| 46 |
|
| 47 |
vif_uuid = uuid.uuid4() |
| 48 |
vif = vif_obj.VIFVHostUser(id=vif_uuid, |
| 49 |
address=None, |
| 50 |
network=network, |
| 51 |
plugin='vhostuser', |
| 52 |
path='/path/to/socket', |
| 53 |
mode=fields.VIFVHostUserMode.SERVER) |
| 54 |
|
| 55 |
# Now do the actual plug operations to connect the VIF to |
| 56 |
# the backing network interface. |
| 57 |
try: |
| 58 |
os_vif.plug(vif, instance_info) |
| 59 |
except vif_exc.PlugException as err: |
| 60 |
# Handle the failure... |
| 61 |
|
| 62 |
# If you are removing a virtual machine and its interfaces, |
| 63 |
# you would use the unplug() operation: |
| 64 |
try: |
| 65 |
os_vif.unplug(vif, instance_info) |
| 66 |
except vif_exc.UnplugException as err: |
| 67 |
# Handle the failure... |