def _available_versions(self): content = str(self._get_cache_data("openstack")) versions = list(filter(None, content.splitlines())) if not versions: msg = 'No metadata versions were found.' raise exceptions.CloudInitError(msg) for version in versions: if not self._valid_api_version(version): msg = 'Invalid API version %r' % (version, ) raise exceptions.CloudInitError(msg) return versions
def _get_forward_table(): heap = kernel32.GetProcessHeap() forward_table_size = ctypes.sizeof(iphlpapi.Win32_MIB_IPFORWARDTABLE) size = wintypes.ULONG(forward_table_size) table_mem = _heap_alloc(heap, size) p_forward_table = ctypes.cast( table_mem, ctypes.POINTER(iphlpapi.Win32_MIB_IPFORWARDTABLE)) try: err = iphlpapi.GetIpForwardTable(p_forward_table, ctypes.byref(size), 0) if err == iphlpapi.ERROR_INSUFFICIENT_BUFFER: kernel32.HeapFree(heap, 0, p_forward_table) table_mem = _heap_alloc(heap, size) p_forward_table = ctypes.cast( table_mem, ctypes.POINTER(iphlpapi.Win32_MIB_IPFORWARDTABLE)) err = iphlpapi.GetIpForwardTable(p_forward_table, ctypes.byref(size), 0) if err and err != kernel32.ERROR_NO_DATA: raise exceptions.CloudInitError( 'Unable to get IP forward table. Error: %s' % err) yield p_forward_table finally: kernel32.HeapFree(heap, 0, p_forward_table)
def check_os_version(major, minor, build=0): """Check if this OS version is equal or higher than (major, minor)""" version_info = kernel32.Win32_OSVERSIONINFOEX_W() version_info.dwOSVersionInfoSize = ctypes.sizeof( kernel32.Win32_OSVERSIONINFOEX_W) version_info.dwMajorVersion = major version_info.dwMinorVersion = minor version_info.dwBuildNumber = build mask = 0 for type_mask in [ kernel32.VER_MAJORVERSION, kernel32.VER_MINORVERSION, kernel32.VER_BUILDNUMBER ]: mask = kernel32.VerSetConditionMask(mask, type_mask, kernel32.VER_GREATER_EQUAL) type_mask = (kernel32.VER_MAJORVERSION | kernel32.VER_MINORVERSION | kernel32.VER_BUILDNUMBER) ret_val = kernel32.VerifyVersionInfoW(ctypes.byref(version_info), type_mask, mask) if ret_val: return True else: err = kernel32.GetLastError() if err == kernel32.ERROR_OLD_WIN_VERSION: return False else: raise exceptions.CloudInitError( "VerifyVersionInfo failed with error: %s" % err)
def _get_data(self, path): norm_path = self._path_join(self._config['metadata_url'], path) LOG.debug('Getting metadata from: %s', norm_path) response = url_helper.wait_any_url([norm_path], timeout=self._config['timeout'], max_wait=self._config['max_wait']) if response: _, request = response return base_source.APIResponse(request.contents, encoding=request.encoding) msg = "Metadata for url {0} was not accessible in due time" raise exceptions.CloudInitError(msg.format(norm_path))
def test__get_forward_table_no_memory(self): self._network_module._heap_alloc = mock.Mock() error_msg = 'Unable to allocate memory for the IP forward table' exc = exceptions.CloudInitError(error_msg) self._network_module._heap_alloc.side_effect = exc e = self.assertRaises(exceptions.CloudInitError, self._check_raises_forward) self.assertEqual(error_msg, str(e)) self._network_module._heap_alloc.assert_called_once_with( self._kernel32.GetProcessHeap.return_value, self._ctypes_mock.wintypes.ULONG.return_value)
def add(cls, route): """Add a new route in the underlying OS. The function should expect an instance of :class:`Route`. """ args = [ 'ROUTE', 'ADD', route.destination, 'MASK', route.netmask, route.gateway ] popen = subprocess.Popen(args, shell=False, stderr=subprocess.PIPE) _, stderr = popen.communicate() if popen.returncode or stderr: # Cannot use the return value to determine the outcome raise exceptions.CloudInitError('Unable to add route: %s' % stderr)
def get_data_source(names, module_iterator, strategies=None): """Get an instance of any data source available. :param names: A list of possible data source names, from which the loader should pick. This can be used to filter the data sources that can be found from outside of cloudinit control. :param module_iterator: A subclass of :class:`cloudinit.plugin_finder.BaseModuleIterator`, which is used to find possible modules where the data sources can be found. :param strategies: An iterator of search strategy classes, where each strategy is capable of filtering the data sources that can be used by cloudinit. """ if names: default_strategies = [strategy.FilterNameStrategy(names)] else: default_strategies = [] if strategies is None: strategies = [] strategy_instances = [strategy_cls() for strategy_cls in strategies] strategies = default_strategies + strategy_instances iterator = module_iterator(sources.__path__) loader = DataSourceLoader(names, iterator, strategies) valid_sources = loader.valid_data_sources() data_source = next(valid_sources, None) if not data_source: raise exceptions.CloudInitError('No available data source found') return data_source
def _heap_alloc(heap, size): table_mem = kernel32.HeapAlloc(heap, 0, ctypes.c_size_t(size.value)) if not table_mem: raise exceptions.CloudInitError( 'Unable to allocate memory for the IP forward table') return table_mem