Add _wrap_db_error() support to SessionTransaction.commit()

This patch adds _wrap_db_error() to the commit()
and rollback() methods of sqlalchemy.orm.session.SessionTransaction,
which is the object that is dealt with when one invokes
code of the form "with session.begin():".  The context manager
form does not invoke the commit() method on the
SQLAlchemy Session directly, and instead calls the one
local to the SessionTransaction.

In order to intercept this, we must build a subclass
of SessionTransaction with the appropriate wrapping, and
then patch it into the object that is returned by
Session.begin(), ensuring that it is compatible with
_wrap_db_error().

This whole approach is legacy; newer oslo.db versions
intercept errors at the point at which they occur
via engine events.

Tests are omitted here as we are relying upon the tests
that were added to the corresponding oslo-incubator code.

Closes-bug: #1370191

Change-Id: Ie0456e6daa86c99cf6fbe56ca5dfd8a618f14232
libvirt: Make `fakelibvirt.libvirtError` match

When test code uses `libvirtError`, it may refer to
`libvirt.libvirtError` or `fakelibvirt.libvirtError` depending on
whether `libvirt-python` is installed.

The existing code had incompatible `__init__` singatures between the
two, meaning that a test could pass locally but fail when run on
Jenkins.

The solution is to make `fakelibvirt.libvirtError` match libvirt 1.2.3
exactly.

A convenience function, `make_libvirtError`, is introduced so that
exceptions can still be created in one shot.

Change-Id: I5aaecc57504c4aea3c049610ef188978c25cfdc4
Closes-Bug: 1305186
(cherry picked from commit 78156a7650237bd4875fbbf9cad8db87348f333f)
libvirt: Handle unsupported host capabilities

Neither libvirt-xen nor libvirt-lxc support
VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES so check for the proper error
code that is returned by libvirt.

The existing code was performing this check improperly by trying to
catch an enum value; instead, we need to catch the `libvirtError`
exception and then check its return code.

Co-Authored-By: Rick Harris <rconradharris@gmail.com>
Co-Authored-By: Andrew Melton <andrew.melton@rackspace.com>
Closes-Bug: 1297962
Change-Id: Ie8bf5c9d1f2a27c387f8b2f54a9bb729fa2f0985
(cherry picked from commit 5fc157e0f5245eb79be90cbc8ed545c396fd38af)
Adds tests for Hyper-V VM Utils

Adds unit tests for Hyper-V VMUtils and VMUtilsV2 classes.

Co-Authored-By: Bogdan Teleaga <bteleaga@cloudbasesolutions.com>

Partial-Bug: #1220256
(cherry picked from commit cafa300a72313ccc7831f20ac1ca0df117a47cf5)

Conflicts:
	nova/tests/virt/hyperv/test_vmutils.py
	nova/tests/virt/hyperv/test_vmutilsv2.py

Change-Id: I87a13697b0a83dc8905573081f735b542bc9bf17
Merge "Adds tests for Hyper-V VM Utils" into stable/icehouse
Ignore errors when deleting non-existing vifs

If an instance fails during its network creation (for example
if the network-vif-plugged event doesn't arrive in time) a
subsequent delete will also fail when it tries to delete the
vif, leaving the instance in a Error(deleting) state.

This can be avoided by including the "--if-exists" option to
the ovs-vsctl command so that the command doesn't fail if the
vif doesn't exist.

Change-Id: Ic613c9d6e0619f51fcd931e62f6d6bce404ebe81
Closes-bug: #1308544
(cherry picked from commit 476dc5efcdb11c9859b062dc69a8c205e69be861)
Fix instance cross AZ check when attaching volumes

For CONF.cinder_cross_az_attach=False, we currently only check the desired AZ
during scheduling but don't actually check the final AZ of the node.

When attaching volumes to running instances we need to check the actual
AZ on the instance and not the desired AZ at boot time. The desired AZ
attribute is optional at boot and may not be set or the desired AZ
could be different from the final AZ.
When we are booting with a volume however we don't know the final AZ so we
check on the desired AZ.

Closes-Bug: #1255347
Change-Id: Ied912c171100a450754d315027bf5c407ac067bb
(cherry picked from commit 18a1486293b15c5713ac1352b9b9b30fdf2b7413)
Bump stable/icehouse next version to 2014.1.4

Change-Id: I1b2e9b58d2eccbe4e7a00b1549adcdcea5845afb
Fixes missing ec2 api address disassociate error on failure

Fixes ec2 api address disassociate so that the ec2 error
InvalidAssociationID.NotFound is returned when it is passed an
invalid association rather than returning success. Note this is
slightly different than the original bug report but the type
of failure has changed since the bug was originally filed.

Change-Id: I10034a2fed62d4e93bc384aef3ff594b66e285f2
Closes-Bug: 1179816
(cherry picked from commit 00d8c334bbfaef3ffec4b04b494e609740b96253)
Raise descriptive error for over volume quota

If you attempt to create a new instance booting from an image and you
were over your quota on cinder, the nova manager would see that as an
invalid block device. This raises a more descriptive error message in
the logs.

Closes-bug: #1013417
Change-Id: Ie50b97c31580da7fc38c6891d1f3646b91f8aa10
(cherry picked from commit 4711ff3adff2b4c9a54f5fb4448ace6558f82f6b)
Fix CellStateManagerFile init to failure

Currently, specifying a cells_config file in nova.conf causes
CellStateManager to fail and in turn stops the nova-cells service from
starting. The reason is that CellsManager creates an instance of
CellStateManager with no arguments. CellStateManager __new__ runs and
creates an instance of CellStateManagerFile which runs __new__ and
__init__ with cell_state_cls and cells_config_path set. At this point
__new__ returns CellStateManagerFile and the new instance's __init__
method is invoked (CellStateManagerFile.__init__) with the original
arguments (there weren't any) which then results in:
2014-04-29 11:52:05.240 16759 TRACE nova self.state_manager =
cell_state_manager()
2014-04-29 11:52:05.240 16759 TRACE nova TypeError: __init__() takes
exactly 3 arguments (1 given)

It seems reasonable for CellStateManagerFile to derive the
cells_config_path info for itself so I have updated the code with that
change and added unit tests to catch this bug and to check that the
correct managers are still returned

Closes-Bug: #1314677
(cherry picked from commit 695191fa89387d96e60120ff32965493c844e7f5)

Conflicts:
	nova/tests/cells/test_cells_state_manager.py

Change-Id: I9021640515142a3ca95c2d9e7b03e19b529bc175
Fixes Hyper-V volume mapping issue on reboot

Hyper-V does not manage iSCSI mounted passthrough disks
correctly on reboot in some circumstances where disk
number assignment can change, resulting in instances potentially
booting with the wrong volume attached.

Co-Authored-By: Adelina Tuvenie <atuvenie@cloudbasesolutions.com>
Co-Authored-By: Adrian Bora <abora@cloudbasesolutions.com>

Closes-Bug: #1322926
(cherry picked from commit 50636a881b60f112027494bcd84af66888db8c1c)

Conflicts:
	nova/tests/virt/hyperv/test_vmutils.py
	nova/tests/virt/hyperv/test_vmutilsv2.py
	nova/virt/hyperv/vmutils.py
	nova/virt/hyperv/vmutilsv2.py

Change-Id: I99fa75aaa950de2ada8a03bd23c910c3ff810d69
VMware: prevent race condition with VNC port allocation

When spawning some instances, nova VMware driver could have a race condition
in VNC port allocation. This fix ensures that the lock is done on the
actual setting in the VM configuration spec.

Co-authored-by: Marcio Roberto Starke <marcio.starke@serpro.gov.br>

Change-Id: I70fab021bbf2df418df53e5f47e19cf16dbe45ac
Closes-bug: #1357372
(cherry picked from commit 948ff4f3d0a159f1aed9fab65d205ede845b3eb9)
Merge "Raise descriptive error for over volume quota" into stable/icehouse
Merge "libvirt: Make `fakelibvirt.libvirtError` match" into stable/icehouse
Merge "libvirt: Handle unsupported host capabilities" into stable/icehouse
Sync process utils from oslo

This patch backports the missing change to fix ssh_execute password leak

------------------------------------------------
The sync pulls in the following changes:

105169f8 - Mask passwords in exceptions and error messages (SSH)
-----------------------------------------------

Closes-Bug: 1377981
Change-Id: Ie0caf32469126dd9feb44867adf27acb6e383958
VMWare: Fix VM leak when deletion of VM during resizing

During the VM resizing, before VM arrive RESIZED state, driver
migrate_disk_and_power_off will initially rename orginal vm
'uuid' to be 'uuid-orig' and clone a new vm with 'uuid' name.
When deletion VM is triggered at this time window, it wouldn't
be able to delete the VM uuid-orig in VCenter and so cause VM leak.
As VM task state will be set to 'deleting' and it can not be used to
determine the resize migrating/migrated state, this fix will
attempt to delete orig VM within destroy phase.

Conflicts:
	nova/tests/virt/vmwareapi/test_driver_api.py
	nova/virt/vmwareapi/vmops.py

Closes-Bug: #1359138

NOTE: the aformentioned patch broke Minesweeper. The fix was also
cherry picked from commit e464bc518e8590d59c2741948466777982ca3319.
This was to do two things:
1. Solve the actual bug
2. Ensure that the unit tests and Minesweeper passed

Change-Id: I7598afbf0dc3c527471af34224003d28e64daaff
(cherry-picked from e1f8664c9fa83f77f5bb763ffcc3157905ed954c)
Merge "Fixes Hyper-V volume mapping issue on reboot" into stable/icehouse
Merge "VMware: prevent race condition with VNC port allocation" into stable/icehouse