From 4beacf60378a970590f131cd7afae1c10c46d6b7 Mon Sep 17 00:00:00 2001 From: Robert Tingirica Date: Wed, 17 Sep 2014 18:34:33 +0300 Subject: [PATCH] Added CloudbaseInitException class --- cloudbaseinit/exception.py | 16 +++ cloudbaseinit/metadata/factory.py | 3 +- .../services/osconfigdrive/windows.py | 13 +- cloudbaseinit/osutils/windows.py | 122 ++++++++++-------- cloudbaseinit/plugins/windows/licensing.py | 8 +- .../plugins/windows/networkconfig.py | 7 +- cloudbaseinit/plugins/windows/ntpclient.py | 5 +- .../plugins/windows/sshpublickeys.py | 3 +- .../plugins/windows/winrmcertificateauth.py | 11 +- .../services/osconfigdrive/test_windows.py | 4 +- .../metadata/services/test_httpservice.py | 8 +- cloudbaseinit/tests/metadata/test_factory.py | 9 +- cloudbaseinit/tests/osutils/test_windows.py | 62 +++++---- .../tests/plugins/windows/test_licensing.py | 4 +- .../plugins/windows/test_networkconfig.py | 9 +- .../tests/plugins/windows/test_ntpclient.py | 3 +- .../plugins/windows/test_sshpublickeys.py | 4 +- .../windows/test_winrmcertificateauth.py | 3 +- .../tests/utils/windows/test_network.py | 17 ++- .../tests/utils/windows/test_physical_disk.py | 15 ++- .../tests/utils/windows/test_virtual_disk.py | 18 ++- cloudbaseinit/utils/windows/network.py | 10 +- cloudbaseinit/utils/windows/physical_disk.py | 11 +- cloudbaseinit/utils/windows/virtual_disk.py | 19 ++- 24 files changed, 245 insertions(+), 139 deletions(-) create mode 100644 cloudbaseinit/exception.py diff --git a/cloudbaseinit/exception.py b/cloudbaseinit/exception.py new file mode 100644 index 00000000..e78aa442 --- /dev/null +++ b/cloudbaseinit/exception.py @@ -0,0 +1,16 @@ +# Copyright 2014 Cloudbase Solutions Srl +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific + + +class CloudbaseInitException(Exception): + pass diff --git a/cloudbaseinit/metadata/factory.py b/cloudbaseinit/metadata/factory.py index e6acc858..8ef4ca48 100644 --- a/cloudbaseinit/metadata/factory.py +++ b/cloudbaseinit/metadata/factory.py @@ -16,6 +16,7 @@ from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.utils import classloader @@ -50,4 +51,4 @@ def get_metadata_service(): except Exception as ex: LOG.error("Failed to load metadata service '%s'" % class_path) LOG.exception(ex) - raise Exception("No available service found") + raise exception.CloudbaseInitException("No available service found") diff --git a/cloudbaseinit/metadata/services/osconfigdrive/windows.py b/cloudbaseinit/metadata/services/osconfigdrive/windows.py index 9ea895c9..b2500367 100644 --- a/cloudbaseinit/metadata/services/osconfigdrive/windows.py +++ b/cloudbaseinit/metadata/services/osconfigdrive/windows.py @@ -23,6 +23,7 @@ import uuid from ctypes import wintypes from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.metadata.services.osconfigdrive import base from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory @@ -118,12 +119,12 @@ class WindowsConfigDriveManager(base.BaseConfigDriveManager): (out, err, exit_code) = osutils.execute_process(args, False) if exit_code: - raise Exception('Failed to execute "bsdtar" from path ' - '"%(bsdtar_path)s" with exit code: %(exit_code)s\n' - '%(out)s\n%(err)s' % - {'bsdtar_path': CONF.bsdtar_path, - 'exit_code': exit_code, - 'out': out, 'err': err}) + raise exception.CloudbaseInitException( + 'Failed to execute "bsdtar" from path "%(bsdtar_path)s" with ' + 'exit code: %(exit_code)s\n%(out)s\n%(err)s' % { + 'bsdtar_path': CONF.bsdtar_path, + 'exit_code': exit_code, + 'out': out, 'err': err}) def _extract_iso_disk_file(self, osutils, iso_file_path): iso_disk_found = False diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 6a448c7f..0b47cd2f 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -28,6 +28,7 @@ from ctypes import wintypes from six.moves import winreg from win32com import client +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import base from cloudbaseinit.utils.windows import network @@ -311,7 +312,7 @@ class WindowsUtils(base.BaseOSUtils): ret_val = advapi32.InitiateSystemShutdownW(0, "Cloudbase-Init reboot", 0, True, True) if not ret_val: - raise Exception("Reboot failed") + raise exception.CloudbaseInitException("Reboot failed") def _get_user_wmi_object(self, username): conn = wmi.WMI(moniker='//./root/cimv2') @@ -342,7 +343,7 @@ class WindowsUtils(base.BaseOSUtils): msg = "Create user failed: %s" else: msg = "Set user password failed: %s" - raise Exception(msg % err) + raise exception.CloudbaseInitException(msg % err) def _sanitize_wmi_input(self, value): return value.replace('\'', '\'\'') @@ -375,7 +376,7 @@ class WindowsUtils(base.BaseOSUtils): 0, six.text_type(username), sid, ctypes.byref(cbSid), domainName, ctypes.byref(cchReferencedDomainName), ctypes.byref(sidNameUse)) if not ret_val: - raise Exception("Cannot get user SID") + raise exception.CloudbaseInitException("Cannot get user SID") return (sid, domainName.value) @@ -388,18 +389,18 @@ class WindowsUtils(base.BaseOSUtils): 3, ctypes.addressof(lmi), 1) if ret_val == self.NERR_GroupNotFound: - raise Exception('Group not found') + raise exception.CloudbaseInitException('Group not found') elif ret_val == self.ERROR_ACCESS_DENIED: - raise Exception('Access denied') + raise exception.CloudbaseInitException('Access denied') elif ret_val == self.ERROR_NO_SUCH_MEMBER: - raise Exception('Username not found') + raise exception.CloudbaseInitException('Username not found') elif ret_val == self.ERROR_MEMBER_IN_ALIAS: # The user is already a member of the group pass elif ret_val == self.ERROR_INVALID_MEMBER: - raise Exception('Invalid user') + raise exception.CloudbaseInitException('Invalid user') elif ret_val != 0: - raise Exception('Unknown error') + raise exception.CloudbaseInitException('Unknown error') def get_user_sid(self, username): r = self._get_user_wmi_object(username) @@ -415,7 +416,7 @@ class WindowsUtils(base.BaseOSUtils): six.text_type(password), 2, 0, ctypes.byref(token)) if not ret_val: - raise Exception("User logon failed") + raise exception.CloudbaseInitException("User logon failed") if load_profile: pi = Win32_PROFILEINFO() @@ -424,7 +425,8 @@ class WindowsUtils(base.BaseOSUtils): ret_val = userenv.LoadUserProfileW(token, ctypes.byref(pi)) if not ret_val: kernel32.CloseHandle(token) - raise Exception("Cannot load user profile") + raise exception.CloudbaseInitException( + "Cannot load user profile") return token @@ -449,7 +451,7 @@ class WindowsUtils(base.BaseOSUtils): self.ComputerNamePhysicalDnsHostname, six.text_type(new_host_name)) if not ret_val: - raise Exception("Cannot set host name") + raise exception.CloudbaseInitException("Cannot set host name") def get_network_adapters(self): l = [] @@ -483,14 +485,15 @@ class WindowsUtils(base.BaseOSUtils): (out, err, ret_val) = self.execute_process(args, False) if ret_val: - raise Exception('w32tm failed to configure NTP.\n' - 'Output: %(out)s\nError: %(err)s' % - {'out': out, 'err': err}) + raise exception.CloudbaseInitException( + 'w32tm failed to configure NTP.\nOutput: %(out)s\nError:' + ' %(err)s' % {'out': out, 'err': err}) def set_network_adapter_mtu(self, mac_address, mtu): if not self.check_os_version(6, 0): - raise Exception('Setting the MTU is currently not supported on ' - 'Windows XP and Windows Server 2003') + raise exception.CloudbaseInitException( + 'Setting the MTU is currently not supported on Windows XP ' + 'and Windows Server 2003') iface_index_list = [ net_addr["interface_index"] for net_addr @@ -498,8 +501,9 @@ class WindowsUtils(base.BaseOSUtils): if net_addr["mac_address"] == mac_address] if not iface_index_list: - raise Exception('Network interface with MAC address "%s" ' - 'not found' % mac_address) + raise exception.CloudbaseInitException( + 'Network interface with MAC address "%s" not found' % + mac_address) else: iface_index = iface_index_list[0] @@ -515,10 +519,10 @@ class WindowsUtils(base.BaseOSUtils): "store=persistent"] (out, err, ret_val) = self.execute_process(args, False) if ret_val: - raise Exception('Setting MTU for interface ' - '"%(mac_address)s" with ' - 'value "%(mtu)s" failed' % - {'mac_address': mac_address, 'mtu': mtu}) + raise exception.CloudbaseInitException( + 'Setting MTU for interface "%(mac_address)s" with ' + 'value "%(mtu)s" failed' % {'mac_address': mac_address, + 'mtu': mtu}) def set_static_network_config(self, adapter_name, address, netmask, broadcast, gateway, dnsnameservers): @@ -529,7 +533,8 @@ class WindowsUtils(base.BaseOSUtils): 'MACAddress IS NOT NULL AND ' 'Name = \'%s\'' % adapter_name_san) if not len(q): - raise Exception("Network adapter not found") + raise exception.CloudbaseInitException( + "Network adapter not found") adapter_config = q[0].associators( wmi_result_class='Win32_NetworkAdapterConfiguration')[0] @@ -537,19 +542,22 @@ class WindowsUtils(base.BaseOSUtils): LOG.debug("Setting static IP address") (ret_val,) = adapter_config.EnableStatic([address], [netmask]) if ret_val > 1: - raise Exception("Cannot set static IP address on network adapter") + raise exception.CloudbaseInitException( + "Cannot set static IP address on network adapter") reboot_required = (ret_val == 1) LOG.debug("Setting static gateways") (ret_val,) = adapter_config.SetGateways([gateway], [1]) if ret_val > 1: - raise Exception("Cannot set gateway on network adapter") + raise exception.CloudbaseInitException( + "Cannot set gateway on network adapter") reboot_required = reboot_required or ret_val == 1 LOG.debug("Setting static DNS servers") (ret_val,) = adapter_config.SetDNSServerSearchOrder(dnsnameservers) if ret_val > 1: - raise Exception("Cannot set DNS on network adapter") + raise exception.CloudbaseInitException( + "Cannot set DNS on network adapter") reboot_required = reboot_required or ret_val == 1 return reboot_required @@ -624,27 +632,30 @@ class WindowsUtils(base.BaseOSUtils): service = self._get_service(service_name) (ret_val,) = service.ChangeStartMode(start_mode) if ret_val != 0: - raise Exception('Setting service %(service_name)s start mode ' - 'failed with return value: %(ret_val)d' % - {'service_name': service_name, 'ret_val': ret_val}) + raise exception.CloudbaseInitException( + 'Setting service %(service_name)s start mode failed with ' + 'return value: %(ret_val)d' % {'service_name': service_name, + 'ret_val': ret_val}) def start_service(self, service_name): LOG.debug('Starting service %s' % service_name) service = self._get_service(service_name) (ret_val,) = service.StartService() if ret_val != 0: - raise Exception('Starting service %(service_name)s failed with ' - 'return value: %(ret_val)d' % - {'service_name': service_name, 'ret_val': ret_val}) + raise exception.CloudbaseInitException( + 'Starting service %(service_name)s failed with return value: ' + '%(ret_val)d' % {'service_name': service_name, + 'ret_val': ret_val}) def stop_service(self, service_name): LOG.debug('Stopping service %s' % service_name) service = self._get_service(service_name) (ret_val,) = service.StopService() if ret_val != 0: - raise Exception('Stopping service %(service_name)s failed with ' - 'return value: %(ret_val)d' % - {'service_name': service_name, 'ret_val': ret_val}) + raise exception.CloudbaseInitException( + 'Stopping service %(service_name)s failed with return value:' + ' %(ret_val)d' % {'service_name': service_name, + 'ret_val': ret_val}) def terminate(self): # Wait for the service to start. Polling the service "Started" property @@ -668,8 +679,8 @@ class WindowsUtils(base.BaseOSUtils): size = wintypes.ULONG(ctypes.sizeof(Win32_MIB_IPFORWARDTABLE)) p = kernel32.HeapAlloc(heap, 0, size) if not p: - raise Exception('Unable to allocate memory for the IP forward ' - 'table') + raise exception.CloudbaseInitException( + 'Unable to allocate memory for the IP forward table') p_forward_table = ctypes.cast( p, ctypes.POINTER(Win32_MIB_IPFORWARDTABLE)) @@ -680,8 +691,8 @@ class WindowsUtils(base.BaseOSUtils): kernel32.HeapFree(heap, 0, p_forward_table) p = kernel32.HeapAlloc(heap, 0, size) if not p: - raise Exception('Unable to allocate memory for the IP ' - 'forward table') + raise exception.CloudbaseInitException( + 'Unable to allocate memory for the IP forward table') p_forward_table = ctypes.cast( p, ctypes.POINTER(Win32_MIB_IPFORWARDTABLE)) @@ -689,8 +700,8 @@ class WindowsUtils(base.BaseOSUtils): ctypes.byref(size), 0) if err != self.ERROR_NO_DATA: if err: - raise Exception('Unable to get IP forward table. ' - 'Error: %s' % err) + raise exception.CloudbaseInitException( + 'Unable to get IP forward table. Error: %s' % err) forward_table = p_forward_table.contents table = ctypes.cast( @@ -723,7 +734,8 @@ class WindowsUtils(base.BaseOSUtils): (out, err, ret_val) = self.execute_process(args) # Cannot use the return value to determine the outcome if ret_val or err: - raise Exception('Unable to add route: %s' % err) + raise exception.CloudbaseInitException( + 'Unable to add route: %s' % err) def check_os_version(self, major, minor, build=0): vi = Win32_OSVERSIONINFOEX_W() @@ -748,8 +760,8 @@ class WindowsUtils(base.BaseOSUtils): if err == self.ERROR_OLD_WIN_VERSION: return False else: - raise Exception("VerifyVersionInfo failed with error: %s" % - err) + raise exception.CloudbaseInitException( + "VerifyVersionInfo failed with error: %s" % err) def get_volume_label(self, drive): max_label_size = 261 @@ -791,7 +803,8 @@ class WindowsUtils(base.BaseOSUtils): buf = ctypes.create_unicode_buffer(buf_size + 1) buf_len = kernel32.GetLogicalDriveStringsW(buf_size, buf) if not buf_len: - raise Exception("GetLogicalDriveStringsW failed") + raise exception.CloudbaseInitException( + "GetLogicalDriveStringsW failed") return self._split_str_buf_list(buf, buf_len) @@ -808,7 +821,8 @@ class WindowsUtils(base.BaseOSUtils): ctypes.byref(disk_guid), None, None, self.DIGCF_PRESENT | self.DIGCF_DEVICEINTERFACE) if handle_disks == self.INVALID_HANDLE_VALUE: - raise Exception("SetupDiGetClassDevs failed") + raise exception.CloudbaseInitException( + "SetupDiGetClassDevs failed") try: did = Win32_SP_DEVICE_INTERFACE_DATA() @@ -827,8 +841,8 @@ class WindowsUtils(base.BaseOSUtils): ctypes.byref(required_size), None): if (kernel32.GetLastError() != self.ERROR_INSUFFICIENT_BUFFER): - raise Exception("SetupDiGetDeviceInterfaceDetailW " - "failed") + raise exception.CloudbaseInitException( + "SetupDiGetDeviceInterfaceDetailW failed") pdidd = ctypes.cast( msvcrt.malloc(required_size), @@ -844,8 +858,8 @@ class WindowsUtils(base.BaseOSUtils): if not setupapi.SetupDiGetDeviceInterfaceDetailW( handle_disks, ctypes.byref(did), pdidd, required_size, None, None): - raise Exception("SetupDiGetDeviceInterfaceDetailW " - "failed") + raise exception.CloudbaseInitException( + "SetupDiGetDeviceInterfaceDetailW failed") device_path = ctypes.cast( pdidd.contents.DevicePath, wintypes.LPWSTR).value @@ -854,7 +868,8 @@ class WindowsUtils(base.BaseOSUtils): device_path, 0, self.FILE_SHARE_READ, None, self.OPEN_EXISTING, 0, 0) if handle_disk == self.INVALID_HANDLE_VALUE: - raise Exception('CreateFileW failed') + raise exception.CloudbaseInitException( + 'CreateFileW failed') sdn = Win32_STORAGE_DEVICE_NUMBER() @@ -863,7 +878,8 @@ class WindowsUtils(base.BaseOSUtils): handle_disk, self.IOCTL_STORAGE_GET_DEVICE_NUMBER, None, 0, ctypes.byref(sdn), ctypes.sizeof(sdn), ctypes.byref(b), None): - raise Exception('DeviceIoControl failed') + raise exception.CloudbaseInitException( + 'DeviceIoControl failed') physical_disks.append( r"\\.\PHYSICALDRIVE%d" % sdn.DeviceNumber) @@ -914,7 +930,7 @@ class WindowsUtils(base.BaseOSUtils): ret_val = wintypes.BOOL() if not kernel32.IsWow64Process(kernel32.GetCurrentProcess(), ctypes.byref(ret_val)): - raise Exception("IsWow64Process failed") + raise exception.CloudbaseInitException("IsWow64Process failed") return bool(ret_val.value) def get_system32_dir(self): diff --git a/cloudbaseinit/plugins/windows/licensing.py b/cloudbaseinit/plugins/windows/licensing.py index ec293073..96820314 100644 --- a/cloudbaseinit/plugins/windows/licensing.py +++ b/cloudbaseinit/plugins/windows/licensing.py @@ -16,6 +16,7 @@ import os from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins import base @@ -48,9 +49,10 @@ class WindowsLicensingPlugin(base.BasePlugin): [cscript_path, slmgr_path] + args, False) if exit_code: - raise Exception('slmgr.vbs failed with error code %(exit_code)s.\n' - 'Output: %(out)s\nError: %(err)s' % - {'exit_code': exit_code, 'out': out, 'err': err}) + raise exception.CloudbaseInitException( + 'slmgr.vbs failed with error code %(exit_code)s.\n' + 'Output: %(out)s\nError: %(err)s' % {'exit_code': exit_code, + 'out': out, 'err': err}) return out def execute(self, service, shared_data): diff --git a/cloudbaseinit/plugins/windows/networkconfig.py b/cloudbaseinit/plugins/windows/networkconfig.py index 32c86a05..ba2c9234 100644 --- a/cloudbaseinit/plugins/windows/networkconfig.py +++ b/cloudbaseinit/plugins/windows/networkconfig.py @@ -18,6 +18,7 @@ import re from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins import base @@ -58,7 +59,8 @@ class NetworkConfigPlugin(base.BasePlugin): r'dns\-nameservers\s+(?P[^\r\n]+)\s+', debian_network_conf) if not m: - raise Exception("network_config format not recognized") + raise exception.CloudbaseInitException( + "network_config format not recognized") address = m.group('address') netmask = m.group('netmask') @@ -73,7 +75,8 @@ class NetworkConfigPlugin(base.BasePlugin): # Get the first available one available_adapters = osutils.get_network_adapters() if not len(available_adapters): - raise Exception("No network adapter available") + raise exception.CloudbaseInitException( + "No network adapter available") network_adapter_name = available_adapters[0] LOG.info('Configuring network adapter: \'%s\'' % network_adapter_name) diff --git a/cloudbaseinit/plugins/windows/ntpclient.py b/cloudbaseinit/plugins/windows/ntpclient.py index 41d1fa1f..8deef8c5 100644 --- a/cloudbaseinit/plugins/windows/ntpclient.py +++ b/cloudbaseinit/plugins/windows/ntpclient.py @@ -17,6 +17,7 @@ import time from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins import base @@ -55,8 +56,8 @@ class NTPClientPlugin(base.BasePlugin): max_retries = 30 while svc_status != osutils.SERVICE_STATUS_RUNNING: if i >= max_retries: - raise Exception('Service %s did not start' % - _W32TIME_SERVICE) + raise exception.CloudbaseInitException( + 'Service %s did not start' % _W32TIME_SERVICE) time.sleep(1) svc_status = osutils.get_service_status(_W32TIME_SERVICE) i += 1 diff --git a/cloudbaseinit/plugins/windows/sshpublickeys.py b/cloudbaseinit/plugins/windows/sshpublickeys.py index 645e6676..a53bcfce 100644 --- a/cloudbaseinit/plugins/windows/sshpublickeys.py +++ b/cloudbaseinit/plugins/windows/sshpublickeys.py @@ -18,6 +18,7 @@ import os from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins import base @@ -40,7 +41,7 @@ class SetUserSSHPublicKeysPlugin(base.BasePlugin): user_home = osutils.get_user_home(username) if not user_home: - raise Exception("User profile not found!") + raise exception.CloudbaseInitException("User profile not found!") LOG.debug("User home: %s" % user_home) diff --git a/cloudbaseinit/plugins/windows/winrmcertificateauth.py b/cloudbaseinit/plugins/windows/winrmcertificateauth.py index a8616a9a..5d66bef2 100644 --- a/cloudbaseinit/plugins/windows/winrmcertificateauth.py +++ b/cloudbaseinit/plugins/windows/winrmcertificateauth.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. +from cloudbaseinit import exception from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.plugins import base from cloudbaseinit.plugins import constants @@ -27,13 +28,15 @@ class ConfigWinRMCertificateAuthPlugin(base.BasePlugin): def _get_credentials(self, shared_data): user_name = shared_data.get(constants.SHARED_DATA_USERNAME) if not user_name: - raise Exception("Cannot execute plugin as the username has " - "not been set in the plugins shared data") + raise exception.CloudbaseInitException( + "Cannot execute plugin as the username has not been set in " + "the plugins shared data") password = shared_data.get(constants.SHARED_DATA_PASSWORD) if not password: - raise Exception("Cannot execute plugin as the password has " - "not been set in the plugins shared data") + raise exception.CloudbaseInitException( + "Cannot execute plugin as the password has not been set in the" + " plugins shared data") # For security reasons unset the password in the shared_data # as it is currently not needed by other plugins diff --git a/cloudbaseinit/tests/metadata/services/osconfigdrive/test_windows.py b/cloudbaseinit/tests/metadata/services/osconfigdrive/test_windows.py index 4419774e..ecb0de73 100644 --- a/cloudbaseinit/tests/metadata/services/osconfigdrive/test_windows.py +++ b/cloudbaseinit/tests/metadata/services/osconfigdrive/test_windows.py @@ -21,6 +21,8 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception + CONF = cfg.CONF @@ -197,7 +199,7 @@ class TestWindowsConfigDriveManager(unittest.TestCase): mock_os_utils.execute_process.return_value = ('fake out', 'fake err', exit_code) if exit_code: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._config_manager._extract_iso_files, mock_os_utils, fake_path, fake_target_path) else: diff --git a/cloudbaseinit/tests/metadata/services/test_httpservice.py b/cloudbaseinit/tests/metadata/services/test_httpservice.py index da3f41d8..48b44825 100644 --- a/cloudbaseinit/tests/metadata/services/test_httpservice.py +++ b/cloudbaseinit/tests/metadata/services/test_httpservice.py @@ -63,8 +63,12 @@ class HttpServiceTest(unittest.TestCase): mock_req) elif side_effect and side_effect.code: mock_urlopen.side_effect = [side_effect] - self.assertRaises(Exception, self._httpservice._get_response, - mock_req) + if side_effect.code == 404: + self.assertRaises(base.NotExistingMetadataException, + self._httpservice._get_response, + mock_req) + else: + self.assertRaises(error.HTTPError) else: mock_urlopen.return_value = 'fake url' response = self._httpservice._get_response(mock_req) diff --git a/cloudbaseinit/tests/metadata/test_factory.py b/cloudbaseinit/tests/metadata/test_factory.py index 7bee08b2..aa24b3cc 100644 --- a/cloudbaseinit/tests/metadata/test_factory.py +++ b/cloudbaseinit/tests/metadata/test_factory.py @@ -17,6 +17,7 @@ import mock import unittest +from cloudbaseinit import exception from cloudbaseinit.metadata import factory @@ -25,8 +26,9 @@ class MetadataServiceFactoryTests(unittest.TestCase): @mock.patch('cloudbaseinit.utils.classloader.ClassLoader.load_class') def _test_get_metadata_service(self, mock_load_class, ret_value): mock_load_class.side_effect = ret_value - if ret_value is Exception: - self.assertRaises(Exception, factory.get_metadata_service) + if ret_value is exception.CloudbaseInitException: + self.assertRaises(exception.CloudbaseInitException, + factory.get_metadata_service) else: response = factory.get_metadata_service() self.assertEqual(mock_load_class()(), response) @@ -36,4 +38,5 @@ class MetadataServiceFactoryTests(unittest.TestCase): self._test_get_metadata_service(ret_value=m) def test_get_metadata_service_exception(self): - self._test_get_metadata_service(ret_value=Exception) + self._test_get_metadata_service( + ret_value=exception.CloudbaseInitException) diff --git a/cloudbaseinit/tests/osutils/test_windows.py b/cloudbaseinit/tests/osutils/test_windows.py index e55bcc09..90b23302 100644 --- a/cloudbaseinit/tests/osutils/test_windows.py +++ b/cloudbaseinit/tests/osutils/test_windows.py @@ -22,6 +22,8 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception + CONF = cfg.CONF @@ -101,7 +103,8 @@ class WindowsUtilsTest(unittest.TestCase): return_value=ret_value) if not ret_value: - self.assertRaises(Exception, self._winutils.reboot) + self.assertRaises(exception.CloudbaseInitException, + self._winutils.reboot) else: self._winutils.reboot() @@ -192,7 +195,8 @@ class WindowsUtilsTest(unittest.TestCase): self._USERNAME, password_expires) else: self.assertRaises( - Exception, self._winutils._create_or_change_user, + exception.CloudbaseInitException, + self._winutils._create_or_change_user, self._USERNAME, self._PASSWORD, create, password_expires) mock_execute_process.assert_called_with(args) @@ -264,7 +268,8 @@ class WindowsUtilsTest(unittest.TestCase): advapi32.LookupAccountNameW.return_value = ret_val if ret_val is None: self.assertRaises( - Exception, self._winutils._get_user_sid_and_domain, + exception.CloudbaseInitException, + self._winutils._get_user_sid_and_domain, self._USERNAME) else: response = self._winutils._get_user_sid_and_domain(self._USERNAME) @@ -298,7 +303,8 @@ class WindowsUtilsTest(unittest.TestCase): if ret_value is not 0 and is_in_alias: self.assertRaises( - Exception, self._winutils.add_user_to_local_group, + exception.CloudbaseInitException, + self._winutils.add_user_to_local_group, self._USERNAME, group_name) else: self._winutils.add_user_to_local_group(self._USERNAME, @@ -373,7 +379,8 @@ class WindowsUtilsTest(unittest.TestCase): if not logon: self.assertRaises( - Exception, self._winutils.create_user_logon_session, + exception.CloudbaseInitException, + self._winutils.create_user_logon_session, self._USERNAME, self._PASSWORD, domain='.', load_profile=load_profile) @@ -381,7 +388,7 @@ class WindowsUtilsTest(unittest.TestCase): userenv.LoadUserProfileW.return_value = None kernel32.CloseHandle.return_value = None - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._winutils.create_user_logon_session, self._USERNAME, self._PASSWORD, domain='.', load_profile=load_profile) @@ -451,8 +458,8 @@ class WindowsUtilsTest(unittest.TestCase): mock_SetComputerNameExW.return_value = ret_value if not ret_value: - self.assertRaises(Exception, self._winutils.set_host_name, - 'fake name') + self.assertRaises(exception.CloudbaseInitException, + self._winutils.set_host_name, 'fake name') else: self._winutils.set_host_name('fake name') @@ -531,7 +538,8 @@ class WindowsUtilsTest(unittest.TestCase): if not adapter: self.assertRaises( - Exception, self._winutils.set_static_network_config, + exception.CloudbaseInitException, + self._winutils.set_static_network_config, adapter_name, address, self._NETMASK, broadcast, self._GATEWAY, dns_list) else: @@ -545,19 +553,22 @@ class WindowsUtilsTest(unittest.TestCase): if ret_val1[0] > 1: self.assertRaises( - Exception, self._winutils.set_static_network_config, + exception.CloudbaseInitException, + self._winutils.set_static_network_config, adapter_name, address, self._NETMASK, broadcast, self._GATEWAY, dns_list) elif ret_val2[0] > 1: self.assertRaises( - Exception, self._winutils.set_static_network_config, + exception.CloudbaseInitException, + self._winutils.set_static_network_config, adapter_name, address, self._NETMASK, broadcast, self._GATEWAY, dns_list) elif ret_val3[0] > 1: self.assertRaises( - Exception, self._winutils.set_static_network_config, + exception.CloudbaseInitException, + self._winutils.set_static_network_config, adapter_name, address, self._NETMASK, broadcast, self._GATEWAY, dns_list) @@ -785,7 +796,7 @@ class WindowsUtilsTest(unittest.TestCase): mock_service.ChangeStartMode.return_value = (ret_val,) if ret_val != 0: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._winutils.set_service_start_mode, 'fake name', 'fake mode') else: @@ -807,7 +818,7 @@ class WindowsUtilsTest(unittest.TestCase): mock_service.StartService.return_value = (ret_val,) if ret_val != 0: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._winutils.start_service, 'fake name') else: @@ -829,7 +840,7 @@ class WindowsUtilsTest(unittest.TestCase): mock_service.StopService.return_value = (ret_val,) if ret_val != 0: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._winutils.stop_service, 'fake name') else: @@ -904,7 +915,8 @@ class WindowsUtilsTest(unittest.TestCase): mock_execute_process.return_value = (None, err, None) if err: - self.assertRaises(Exception, self._winutils.add_static_route, + self.assertRaises(exception.CloudbaseInitException, + self._winutils.add_static_route, self._DESTINATION, self._NETMASK, next_hop, interface_index, metric) @@ -927,8 +939,8 @@ class WindowsUtilsTest(unittest.TestCase): old_version = self._winutils.ERROR_OLD_WIN_VERSION if error_value and error_value is not old_version: - self.assertRaises(Exception, self._winutils.check_os_version, 3, - 1, 2) + self.assertRaises(exception.CloudbaseInitException, + self._winutils.check_os_version, 3, 1, 2) self._windll_mock.kernel32.GetLastError.assert_called_once_with() else: @@ -1007,7 +1019,8 @@ class WindowsUtilsTest(unittest.TestCase): mock_get_drives.return_value = buf_length if buf_length is None: - self.assertRaises(Exception, self._winutils._get_logical_drives) + self.assertRaises(exception.CloudbaseInitException, + self._winutils._get_logical_drives) else: response = self._winutils._get_logical_drives() @@ -1074,7 +1087,8 @@ class WindowsUtilsTest(unittest.TestCase): disk_handle == self._winutils.INVALID_HANDLE_VALUE) or ( not io_control): - self.assertRaises(Exception, self._winutils.get_physical_disks) + self.assertRaises(exception.CloudbaseInitException, + self._winutils.get_physical_disks) else: response = self._winutils.get_physical_disks() @@ -1195,7 +1209,8 @@ class WindowsUtilsTest(unittest.TestCase): self._wintypes_mock.BOOL.return_value.value = ret_val if ret_val is False: - self.assertRaises(Exception, self._winutils.is_wow64) + self.assertRaises(exception.CloudbaseInitException, + self._winutils.is_wow64) else: response = self._winutils.is_wow64() @@ -1317,8 +1332,9 @@ class WindowsUtilsTest(unittest.TestCase): '/syncfromflags:manual', '/update'] if ret_val: - self.assertRaises(Exception, self._winutils.set_ntp_client_config, - args, False) + self.assertRaises(exception.CloudbaseInitException, + self._winutils.set_ntp_client_config, + 'fake ntp host') else: self._winutils.set_ntp_client_config(ntp_host='fake ntp host') diff --git a/cloudbaseinit/tests/plugins/windows/test_licensing.py b/cloudbaseinit/tests/plugins/windows/test_licensing.py index 8b31df48..1894ceec 100644 --- a/cloudbaseinit/tests/plugins/windows/test_licensing.py +++ b/cloudbaseinit/tests/plugins/windows/test_licensing.py @@ -20,6 +20,7 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.plugins import base from cloudbaseinit.plugins.windows import licensing @@ -48,7 +49,8 @@ class WindowsLicensingPluginTests(unittest.TestCase): exit_code) if exit_code: - self.assertRaises(Exception, self._licensing._run_slmgr, + self.assertRaises(exception.CloudbaseInitException, + self._licensing._run_slmgr, mock_osutils, ['fake args']) else: response = self._licensing._run_slmgr(osutils=mock_osutils, diff --git a/cloudbaseinit/tests/plugins/windows/test_networkconfig.py b/cloudbaseinit/tests/plugins/windows/test_networkconfig.py index 6abfd8cb..02d72d59 100644 --- a/cloudbaseinit/tests/plugins/windows/test_networkconfig.py +++ b/cloudbaseinit/tests/plugins/windows/test_networkconfig.py @@ -20,6 +20,7 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.plugins import base from cloudbaseinit.plugins.windows import networkconfig from cloudbaseinit.tests.metadata import fake_json_response @@ -47,12 +48,14 @@ class NetworkConfigPluginPluginTests(unittest.TestCase): mock_get_os_utils.return_value = mock_osutils mock_osutils.set_static_network_config.return_value = False if search_result is None: - self.assertRaises(Exception, self._network_plugin.execute, + self.assertRaises(exception.CloudbaseInitException, + self._network_plugin.execute, mock_service, fake_shared_data) elif no_adapters: CONF.set_override('network_adapter', None) - mock_osutils.get_network_adapters.return_value = None - self.assertRaises(Exception, self._network_plugin.execute, + mock_osutils.get_network_adapters.return_value = [] + self.assertRaises(exception.CloudbaseInitException, + self._network_plugin.execute, mock_service, fake_shared_data) else: diff --git a/cloudbaseinit/tests/plugins/windows/test_ntpclient.py b/cloudbaseinit/tests/plugins/windows/test_ntpclient.py index cbdf1256..93023b29 100644 --- a/cloudbaseinit/tests/plugins/windows/test_ntpclient.py +++ b/cloudbaseinit/tests/plugins/windows/test_ntpclient.py @@ -17,6 +17,7 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.plugins import base from cloudbaseinit.plugins.windows import ntpclient from cloudbaseinit.utils import dhcp @@ -42,7 +43,7 @@ class NTPClientPluginTests(unittest.TestCase): if fail_service_start: mock_osutils.get_service_status.return_value = "stopped" - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._ntpclient._check_w32time_svc_status, mock_osutils) diff --git a/cloudbaseinit/tests/plugins/windows/test_sshpublickeys.py b/cloudbaseinit/tests/plugins/windows/test_sshpublickeys.py index a2ffec54..1baa1100 100644 --- a/cloudbaseinit/tests/plugins/windows/test_sshpublickeys.py +++ b/cloudbaseinit/tests/plugins/windows/test_sshpublickeys.py @@ -20,6 +20,7 @@ import unittest from oslo.config import cfg +from cloudbaseinit import exception from cloudbaseinit.plugins import base from cloudbaseinit.plugins.windows import sshpublickeys from cloudbaseinit.tests.metadata import fake_json_response @@ -49,7 +50,8 @@ class SetUserSSHPublicKeysPluginTests(unittest.TestCase): mock_os_path.exists.return_value = False if user_home is None: - self.assertRaises(Exception, self._set_ssh_keys_plugin, + self.assertRaises(exception.CloudbaseInitException, + self._set_ssh_keys_plugin.execute, mock_service, fake_shared_data) else: with mock.patch('cloudbaseinit.plugins.windows.sshpublickeys' diff --git a/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py b/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py index 56150c2d..94358707 100644 --- a/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py +++ b/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py @@ -18,6 +18,7 @@ import importlib import mock import unittest +from cloudbaseinit import exception from cloudbaseinit.plugins import base from cloudbaseinit.plugins import constants @@ -50,7 +51,7 @@ class ConfigWinRMCertificateAuthPluginTests(unittest.TestCase): mock_shared_data = mock.MagicMock() mock_shared_data.get.side_effect = [fake_user, fake_password] if fake_user is None or fake_password is None: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._certif_auth._get_credentials, mock_shared_data) else: diff --git a/cloudbaseinit/tests/utils/windows/test_network.py b/cloudbaseinit/tests/utils/windows/test_network.py index 5722a981..1a50ad59 100644 --- a/cloudbaseinit/tests/utils/windows/test_network.py +++ b/cloudbaseinit/tests/utils/windows/test_network.py @@ -16,6 +16,8 @@ import importlib import mock import unittest +from cloudbaseinit import exception as cbinit_exception + class WindowsNetworkUtilsTests(unittest.TestCase): @@ -55,7 +57,8 @@ class WindowsNetworkUtilsTests(unittest.TestCase): self.network.ws2_32.WSAAddressToStringW.return_value = ret_val if ret_val: - self.assertRaises(Exception, self.network._socket_addr_to_str, + self.assertRaises(cbinit_exception.CloudbaseInitException, + self.network._socket_addr_to_str, mock_socket_addr) self.network.ws2_32.WSAGetLastError.assert_called_once_with() else: @@ -88,7 +91,7 @@ class WindowsNetworkUtilsTests(unittest.TestCase): self._moves_mock.winreg.QueryValueEx.side_effect = [exception] if exception.errno != 2: - self.assertRaises(Exception, + self.assertRaises(cbinit_exception.CloudbaseInitException, self.network._get_registry_dhcp_server, fake_adapter) else: @@ -116,12 +119,12 @@ class WindowsNetworkUtilsTests(unittest.TestCase): self._test_get_registry_dhcp_server(dhcp_server="255.255.255.255") def test_get_registry_dhcp_server_expeption_not_found(self): - ex = Exception() + ex = cbinit_exception.CloudbaseInitException() ex.errno = 2 self._test_get_registry_dhcp_server(dhcp_server="", exception=ex) def test_get_registry_dhcp_server_expeption_other(self): - ex = Exception() + ex = cbinit_exception.CloudbaseInitException() ex.errno = 3 self._test_get_registry_dhcp_server(dhcp_server="", exception=ex) @@ -170,10 +173,12 @@ class WindowsNetworkUtilsTests(unittest.TestCase): None, None, mock_byref.return_value)] if not p: - self.assertRaises(Exception, self.network.get_adapter_addresses) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self.network.get_adapter_addresses) if ret_val2 and ret_val2 != self.network.kernel32.ERROR_NO_DATA: - self.assertRaises(Exception, self.network.get_adapter_addresses) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self.network.get_adapter_addresses) compare_cast.append(mock.call(p, mock_POINTER.return_value)) compare_GetAdaptersAddresses.append(mock.call( diff --git a/cloudbaseinit/tests/utils/windows/test_physical_disk.py b/cloudbaseinit/tests/utils/windows/test_physical_disk.py index 63144969..ab410c57 100644 --- a/cloudbaseinit/tests/utils/windows/test_physical_disk.py +++ b/cloudbaseinit/tests/utils/windows/test_physical_disk.py @@ -16,6 +16,8 @@ import importlib import mock import unittest +from cloudbaseinit import exception as cbinit_exception + class WindowsPhysicalDiskUtilsTests(unittest.TestCase): @@ -49,7 +51,8 @@ class WindowsPhysicalDiskUtilsTests(unittest.TestCase): self.physical_disk.kernel32.CreateFileW.return_value = \ self._phys_disk_class.INVALID_HANDLE_VALUE - self.assertRaises(Exception, self._phys_disk_class.open) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self._phys_disk_class.open) else: self._phys_disk_class.open() @@ -103,7 +106,8 @@ class WindowsPhysicalDiskUtilsTests(unittest.TestCase): self.physical_disk.kernel32.DeviceIoControl.return_value = ret_val if not ret_val: - self.assertRaises(Exception, self._phys_disk_class.get_geometry) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self._phys_disk_class.get_geometry) elif _geom: response = self._phys_disk_class.get_geometry() self.assertEqual(_geom, response) @@ -145,7 +149,8 @@ class WindowsPhysicalDiskUtilsTests(unittest.TestCase): self.physical_disk.kernel32.SetFilePointer.return_value = \ self._phys_disk_class.INVALID_SET_FILE_POINTER - self.assertRaises(Exception, self._phys_disk_class.seek, 1) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self._phys_disk_class.seek, 1) else: self._phys_disk_class.seek(1) self.physical_disk.kernel32.SetFilePointer.assert_called_once_with( @@ -170,8 +175,8 @@ class WindowsPhysicalDiskUtilsTests(unittest.TestCase): self.physical_disk.kernel32.ReadFile.return_value = ret_val if not ret_val: - self.assertRaises(Exception, self._phys_disk_class.read, - bytes_to_read) + self.assertRaises(cbinit_exception.CloudbaseInitException, + self._phys_disk_class.read, bytes_to_read) else: response = self._phys_disk_class.read(bytes_to_read) diff --git a/cloudbaseinit/tests/utils/windows/test_virtual_disk.py b/cloudbaseinit/tests/utils/windows/test_virtual_disk.py index e035d162..fdef303b 100644 --- a/cloudbaseinit/tests/utils/windows/test_virtual_disk.py +++ b/cloudbaseinit/tests/utils/windows/test_virtual_disk.py @@ -16,6 +16,8 @@ import importlib import mock import unittest +from cloudbaseinit import exception + class WindowsVirtualDiskUtilsTests(unittest.TestCase): @@ -67,7 +69,8 @@ class WindowsVirtualDiskUtilsTests(unittest.TestCase): self._vdisk_class._handle = handle if ret_val: - self.assertRaises(Exception, self._vdisk_class.open) + self.assertRaises(exception.CloudbaseInitException, + self._vdisk_class.open) else: self._vdisk_class.open() @@ -99,7 +102,8 @@ class WindowsVirtualDiskUtilsTests(unittest.TestCase): virtdisk.AttachVirtualDisk.return_value = ret_val if ret_val: - self.assertRaises(Exception, self._vdisk_class.attach) + self.assertRaises(exception.CloudbaseInitException, + self._vdisk_class.attach) else: self._vdisk_class.attach() @@ -119,7 +123,8 @@ class WindowsVirtualDiskUtilsTests(unittest.TestCase): virtdisk.DetachVirtualDisk.return_value = ret_val if ret_val: - self.assertRaises(Exception, self._vdisk_class.detach) + self.assertRaises(exception.CloudbaseInitException, + self._vdisk_class.detach) else: self._vdisk_class.detach() @@ -141,7 +146,8 @@ class WindowsVirtualDiskUtilsTests(unittest.TestCase): buf = self._ctypes_mock.create_unicode_buffer.return_value if ret_val: - self.assertRaises(Exception, self._vdisk_class.get_physical_path) + self.assertRaises(exception.CloudbaseInitException, + self._vdisk_class.get_physical_path) else: response = self._vdisk_class.get_physical_path() self.assertEqual(buf.value, response) @@ -176,10 +182,10 @@ class WindowsVirtualDiskUtilsTests(unittest.TestCase): expected_create_unicode_buffer = [mock.call(2048)] if not buf_len: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._vdisk_class.get_cdrom_drive_mount_point) elif not ret_val: - self.assertRaises(Exception, + self.assertRaises(exception.CloudbaseInitException, self._vdisk_class.get_cdrom_drive_mount_point) expected_create_unicode_buffer.append(mock.call(2048)) diff --git a/cloudbaseinit/utils/windows/network.py b/cloudbaseinit/utils/windows/network.py index a61a2c74..df790c65 100644 --- a/cloudbaseinit/utils/windows/network.py +++ b/cloudbaseinit/utils/windows/network.py @@ -17,6 +17,7 @@ import ctypes from ctypes import wintypes from six.moves import winreg +from cloudbaseinit import exception from cloudbaseinit.utils.windows import iphlpapi from cloudbaseinit.utils.windows import kernel32 from cloudbaseinit.utils.windows import ws2_32 @@ -41,8 +42,8 @@ def _socket_addr_to_str(socket_addr): socket_addr.iSockaddrLength, None, addr_str, ctypes.byref(addr_str_len)) if ret_val: - raise Exception("WSAAddressToStringW failed: %s" % - ws2_32.WSAGetLastError()) + raise exception.CloudbaseInitException( + "WSAAddressToStringW failed: %s" % ws2_32.WSAGetLastError()) return addr_str.value @@ -80,7 +81,7 @@ def get_adapter_addresses(): proc_heap = kernel32.GetProcessHeap() p = kernel32.HeapAlloc(proc_heap, 0, size.value) if not p: - raise Exception("Cannot allocate memory") + raise exception.CloudbaseInitException("Cannot allocate memory") ws2_32.init_wsa() @@ -97,7 +98,8 @@ def get_adapter_addresses(): return net_adapters if ret_val: - raise Exception("GetAdaptersAddresses failed") + raise exception.CloudbaseInitException( + "GetAdaptersAddresses failed") p_curr_addr = p_addr while p_curr_addr: diff --git a/cloudbaseinit/utils/windows/physical_disk.py b/cloudbaseinit/utils/windows/physical_disk.py index a7f1bf25..04feb135 100644 --- a/cloudbaseinit/utils/windows/physical_disk.py +++ b/cloudbaseinit/utils/windows/physical_disk.py @@ -19,6 +19,8 @@ import ctypes from ctypes import windll from ctypes import wintypes +from cloudbaseinit import exception + kernel32 = windll.kernel32 @@ -62,7 +64,7 @@ class PhysicalDisk(object): self.FILE_ATTRIBUTE_READONLY, 0) if handle == self.INVALID_HANDLE_VALUE: - raise Exception('Cannot open file') + raise exception.CloudbaseInitException('Cannot open file') self._handle = handle def close(self): @@ -84,7 +86,8 @@ class PhysicalDisk(object): ctypes.byref(bytes_returned), 0) if not ret_val: - raise Exception("Cannot get disk geometry") + raise exception.CloudbaseInitException( + "Cannot get disk geometry") self._geom = geom return self._geom @@ -96,7 +99,7 @@ class PhysicalDisk(object): ctypes.byref(high), self.FILE_BEGIN) if ret_val == self.INVALID_SET_FILE_POINTER: - raise Exception("Seek error") + raise exception.CloudbaseInitException("Seek error") def read(self, bytes_to_read): buf = ctypes.create_string_buffer(bytes_to_read) @@ -104,5 +107,5 @@ class PhysicalDisk(object): ret_val = kernel32.ReadFile(self._handle, buf, bytes_to_read, ctypes.byref(bytes_read), 0) if not ret_val: - raise Exception("Read exception") + raise exception.CloudbaseInitException("Read exception") return (buf, bytes_read.value) diff --git a/cloudbaseinit/utils/windows/virtual_disk.py b/cloudbaseinit/utils/windows/virtual_disk.py index fbb2bae8..c7693723 100644 --- a/cloudbaseinit/utils/windows/virtual_disk.py +++ b/cloudbaseinit/utils/windows/virtual_disk.py @@ -19,6 +19,8 @@ import ctypes from ctypes import windll from ctypes import wintypes +from cloudbaseinit import exception + kernel32 = windll.kernel32 # VirtDisk.dll is available starting from Windows Server 2008 R2 / Windows7 virtdisk = None @@ -84,20 +86,22 @@ class VirtualDisk(object): self.OPEN_VIRTUAL_DISK_FLAG_NONE, 0, ctypes.byref(handle)) if ret_val: - raise Exception("Cannot open virtual disk") + raise exception.CloudbaseInitException("Cannot open virtual disk") self._handle = handle def attach(self): ret_val = virtdisk.AttachVirtualDisk( self._handle, 0, self.ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY, 0, 0, 0) if ret_val: - raise Exception("Cannot attach virtual disk") + raise exception.CloudbaseInitException( + "Cannot attach virtual disk") def detach(self): ret_val = virtdisk.DetachVirtualDisk( self._handle, self.DETACH_VIRTUAL_DISK_FLAG_NONE, 0) if ret_val: - raise Exception("Cannot detach virtual disk") + raise exception.CloudbaseInitException( + "Cannot detach virtual disk") def get_physical_path(self): buf = ctypes.create_unicode_buffer(1024) @@ -106,7 +110,8 @@ class VirtualDisk(object): ctypes.byref(bufLen), buf) if ret_val: - raise Exception("Cannot get virtual disk physical path") + raise exception.CloudbaseInitException( + "Cannot get virtual disk physical path") return buf.value def get_cdrom_drive_mount_point(self): @@ -117,7 +122,8 @@ class VirtualDisk(object): buf_len = kernel32.GetLogicalDriveStringsW( ctypes.sizeof(buf) / ctypes.sizeof(wintypes.WCHAR), buf) if not buf_len: - raise Exception("Cannot enumerate logical devices") + raise exception.CloudbaseInitException( + "Cannot enumerate logical devices") cdrom_dev = self.get_physical_path().rsplit('\\')[-1].upper() @@ -131,7 +137,8 @@ class VirtualDisk(object): ctypes.sizeof(dev) / ctypes.sizeof(wintypes.WCHAR)) if not ret_val: - raise Exception("Cannot query NT device") + raise exception.CloudbaseInitException( + "Cannot query NT device") if dev.value.rsplit('\\')[-1].upper() == cdrom_dev: mount_point = curr_drive