virt: utils_net helpers

Move commonly used helper functions to methods on virtnet
classes.  This helps organize them closer to relevant data
and while keeping the global and local namespaces clean.
This also helps prevents the need for burried, private
functions, and organizes them in a common location where
they can be unittested.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: QemuIface auto-convert int() from str. params.

For parameters which expect integers, QemuIface will
attempt to parse them as integers, but provides for
disabling this conversion if necessary (i.e. unittesting).
Therefor, explicit calls to int on reference are no
longer necessary.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Initialize/Persist virtnet when needed

Previously it was very un-clear where/how networking parameters were
referenced in terms of initializing or restoring virtnet during
a VM instance's life-cycle.  With the recent updates, it's now possible
to initialize and update close to where those stages are required
and do so in an unambiguous way.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Don't be ambiguous about method params.

Previously, add/del nic and netdev methods accepted an arbitrary number
of keyword arguments that were never defined anywhere.  Replace this
with a single parameter defined as dict-like, and rely on duck-typing
to ensure it references expected values.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Clarify variable name

'propertea' while "cute", doesn't convey the actual meaning and use of
this particular variable.  Rename it to 'prop' - short for 'property'
(which is a python keyword).

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Update add netdev/nic methods

Much of the id/fd creative-work has been reorganized
centrally into utils_net.QemuIface.  This commit reflects
that change.

Looking through the other add netdev/nic methods, I found using
the set_if_none() method has hidden-implication of property
existence.  Instead, it's clearer to just use a ``if nic.get() is None``
construct which is clear it also checks for existence

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Update _cleanup to use new VirtNet

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Make vm.make_create_command() VirtNet

Some networking parameters were being parsed outside of virtnet, leading
to ambiguity and possibly interoperability problems with libvirt's use
of virtnet.  These extra parameters have been included into the qemu-kvm
specific QemuIface VirtNet container class.  This ensures they're
defined and properly accounted for.  It also makes the code easier
to read/maintain since all networking information is coming from a
single source.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Update vm.create() to use new VirtNet

Some networking parameters were being parsed outside of virtnet,
leading to ambiguity and possibly interoperability problems with
libvirt's use of virtnet.

I moved the parameters and parsing into virtnet, with all defaults
being established in vm.add_nic() (similar to before).  Most of
these were incorporated into the qemu-kvm specific QemuIface VirtNet
container class.

Keeping data from params parsing & qemu-specific helpers located
close together ensures less code is duplicated and behavior is
defined in a _minimum_ of different locations.  This improves code
clarity and eases bug-hunting and maintenance due to more consistent
call stack and less duplication.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: Expand two acronyms in docstrings

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: Standardize vm.clone() and md5sum checks

Reduce code duplication and ongoing maintenance since the majority
of this code can be shared between VM subclasses.  The
single exception can override only the necessary behavior.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-libvirt: libvirt_vm cobweb removal

*  Cleaned up a few docstrings / whitespace

*  vm.create() is so dissimilar from qemu-kvm it doesn't
   need to represent disused parameters or their docstrings.

*  Since create locking cleanup is mostly centralized now,
   it can referr to a common filename in base module.

*  Nic-hotplug is handled in test code, no need to provide
   generalized methods for it.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-libvirt: Update libvirt_vm virtnet usage

*  Virtnet now has xen-specific networking-info container class.
   This will allow for easier extension or special-handling
   as they related to xen specific networking.  No need to hold
   libvirt hostage to xen limitations or behavior.

*  The VM subclasses dictate their own use of networking info.
   cache.  For libvirt, virsh/virt commands are used to manupulate
   it, however for vm.clone() and mac-address allocation, specialized
   searching is required.

*  Made some mostly cosmetic updates to make_create_command()'s
   add_nic() nested-function.

*  The BaseVM.destroy() method specifies a 'free_mac_addresses'
   parameter that doesn't apply to libvirt_vm.  However, class
   shouldn't silently ignore the parameter in case it's ever
   helpful to debugging.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: Fix needs_mac() bug

This function is mainly used by vm classes to decide whether or not
to call the mac-address generation code.  It intentionally does not
validate the value of the address, in case testing with invalid macs
is required.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Cleanup bridge if nettype=bridge/network

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-qemu: Rename param driver -> pa_driver

It's very hard to tell w/o other context what a generic term like
'driver' means.  Add a simple prefix to give it more meaning.
Same problem and fix for param 'driver_option'.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: Simplify needs_restart()

It's impossible to perfectly track vm state from test to test.
This area / behavior is currently being discussed for further
refinement, for now rather than try and be smart, just assume
a restart is needed.

The thinking is: The consequences of a wrong restart determination
are more costly in the long-run than the time-savings gained by
not restarting the qemu process.  Really this decision needs more
refinement inside env_process anyway, not the BaseVM class.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt: unattended_install bugfix + cleanup

* The import variant will never see any completion message.
  Added 'wait_no_ack = yes' to it's params.
* Re-arranged the 'waiting for install to be done' loop
  with some more nesting that makes the end condition clearer.
* Found/removed a hidden 'catch-all' Exception case.  Replaced
  Exception -> virt_vm.VMIPAddressMissingError but maybe more
  could be added.  This one seemed likely since it can be thrown
  before entering the 'remote' module code.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-libvirt: Fix virsh_define test bug

Test was not performing operations safely which could
result in original vm state not being restored.  Wrapping
this code in a try...finally... is the common way to address
this.

Signed-off-by: Chris Evich <cevich@redhat.com>
virt-libvirt: small libguestfs codespell fix