def _get_primary_adapter_name(members, mac_address): conn = wmi.WMI(moniker='root/cimv2') adapters = conn.Win32_NetworkAdapter(MACAddress=mac_address) if not adapters: raise exception.ItemNotFoundException( "No adapter with MAC address \"%s\" found" % mac_address) primary_adapter_name = adapters[0].NetConnectionID if primary_adapter_name not in members: raise exception.ItemNotFoundException( "Adapter \"%s\" not found in members" % primary_adapter_name) return primary_adapter_name
def create_team(self, team_name, mode, load_balancing_algorithm, members, mac_address, primary_nic_name=None, primary_nic_vlan_id=None, lacp_timer=None): conn = wmi.WMI(moniker='root/standardcimv2') primary_adapter_name = self._get_primary_adapter_name( members, mac_address) teaming_mode = NETWORK_MODEL_TEAM_MODE_MAP.get(mode) if teaming_mode is None: raise exception.ItemNotFoundException( "Unsupported teaming mode: %s" % mode) if load_balancing_algorithm is None: lb_algo = LBFO_TEAM_ALGORITHM_DYNAMIC else: lb_algo = NETWORK_MODEL_LB_ALGO_MAP.get(load_balancing_algorithm) if lb_algo is None: raise exception.ItemNotFoundException( "Unsupported LB algorithm: %s" % load_balancing_algorithm) if lacp_timer is not None and teaming_mode == LBFO_TEAM_MODE_LACP: lacp_timer = NETWORK_MODEL_LACP_RATE_MAP[lacp_timer] nic_name = primary_nic_name or team_name self._create_team(conn, team_name, nic_name, teaming_mode, lb_algo, primary_adapter_name, lacp_timer) try: for member in members: if member != primary_adapter_name: self._add_team_member(conn, team_name, member) if primary_nic_vlan_id is not None: self._set_primary_nic_vlan_id(conn, team_name, primary_nic_vlan_id) nic_name = conn.MSFT_NetLbfoTeamNic(team=team_name)[0].Name self._wait_for_nic(nic_name) except Exception as ex: self.delete_team(team_name) raise ex
def delete_team(self, team_name): conn = wmi.WMI(moniker='root/standardcimv2') teams = conn.MSFT_NetLbfoTeam(name=team_name) if not teams: raise exception.ItemNotFoundException("Team not found: %s" % team_name) teams[0].Delete_()
def _use_machine_keyset(store_location): if store_location == constant.CERT_LOCATION_LOCAL_MACHINE: return True elif store_location == constant.CERT_LOCATION_CURRENT_USER: return False else: raise exception.ItemNotFoundException( "Unsupported certificate store location: %s" % store_location)
def get_user_data(self): try: return self.get_content(CUSTOM_DATA_FILENAME) except base.NotExistingMetadataException: if self._check_ovf_env_custom_data(): raise exception.ItemNotFoundException( "Custom data configuration exists, but the custom data " "file is not present") raise
def _get_user_info(self, username, level): try: return win32net.NetUserGetInfo(None, username, level) except win32net.error as ex: if ex.args[0] == self.NERR_UserNotFound: raise exception.ItemNotFoundException("User not found: %s" % username) else: raise exception.CloudbaseInitException( "Failed to get user info: %s" % ex.args[2])
def _get_ovf_env_path(self): base_path = self._get_config_set_drive_path() ovf_env_path = os.path.join(base_path, OVF_ENV_FILENAME) if not os.path.exists(ovf_env_path): raise exception.ItemNotFoundException( "ovf-env path does not exist: %s" % ovf_env_path) LOG.debug("ovs-env path: %s", ovf_env_path) return ovf_env_path
def get_kms_product(self): def _is_kms_client(product): # note(alexpilotti): could check for # KeyManagementServiceProductKeyID return u"VOLUME_KMSCLIENT" in product.Description for product in self._get_products(): app_id = product.ApplicationId.lower() if app_id == WINDOWS_APP_ID and _is_kms_client(product): return (product.Description, product.LicenseFamily, self._is_current_product(product)) raise exception.ItemNotFoundException("KMS client product not found")
def _get_config_set_drive_path(self): if not self._config_set_drive_path: base_paths = self._osutils.get_logical_drives() for base_path in base_paths: tag_path = os.path.join(base_path, OVF_ENV_DRIVE_TAG) if os.path.exists(tag_path): self._config_set_drive_path = base_path if not self._config_set_drive_path: raise exception.ItemNotFoundException( "No drive containing file %s could be found" % OVF_ENV_DRIVE_TAG) return self._config_set_drive_path
def _wait_for_nic(nic_name): conn = wmi.WMI(moniker='//./root/cimv2') max_count = 100 i = 0 while True: if len(conn.Win32_NetworkAdapter(NetConnectionID=nic_name)): break else: i += 1 if i >= max_count: raise exception.ItemNotFoundException( "Cannot find team NIC: %s" % nic_name) LOG.debug("Waiting for team NIC: %s", nic_name) time.sleep(1)
def _test_use_machine_keyset(self, store_location): if store_location == constant.CERT_LOCATION_LOCAL_MACHINE: expected_result = True elif store_location == constant.CERT_LOCATION_CURRENT_USER: expected_result = False else: ex = exception.ItemNotFoundException( "Unsupported certificate store location: %s" % store_location) with self.assertRaises(exception.ItemNotFoundException) as exc: (self._cert._use_machine_keyset(store_location)) self.assertEqual(str(ex), str(exc.exception)) return result = (self._cert._use_machine_keyset(store_location)) self.assertEqual(result, expected_result)
def wmi(): try: # PyMI depends on the MI API, not available by default on systems older # than Windows 8 / Windows Server 2012 import wmi return wmi except ImportError: LOG.debug("Couldn't load PyMI module, using legacy WMI") wmi_path = None for packages_path in site.getsitepackages(): path = os.path.join(packages_path, "wmi.py") if os.path.isfile(path): wmi_path = path break if wmi_path is None: raise exception.ItemNotFoundException("wmi module not found") return imp.load_source("wmi", wmi_path)
def create_team(self, team_name, mode, load_balancing_algorithm, members, mac_address, primary_nic_name=None, primary_nic_vlan_id=None, lacp_timer=None): conn = wmi.WMI(moniker='root/standardcimv2') primary_adapter_name = self._get_primary_adapter_name( members, mac_address) teaming_mode = NETWORK_MODEL_TEAM_MODE_MAP.get(mode) if teaming_mode is None: raise exception.ItemNotFoundException( "Unsupported teaming mode: %s" % mode) if load_balancing_algorithm is None: lb_algo = LBFO_TEAM_ALGORITHM_DYNAMIC else: lb_algo = NETWORK_MODEL_LB_ALGO_MAP.get(load_balancing_algorithm) if lb_algo is None: raise exception.ItemNotFoundException( "Unsupported LB algorithm: %s" % load_balancing_algorithm) team = conn.MSFT_NetLbfoTeam.new() team.Name = team_name team.TeamingMode = teaming_mode team.LoadBalancingAlgorithm = lb_algo if lacp_timer is not None and team.TeamingMode == LBFO_TEAM_MODE_LACP: team.LacpTimer = NETWORK_MODEL_LACP_RATE_MAP[lacp_timer] nic_name = primary_nic_name or team_name custom_options = [{ u'name': u'TeamMembers', u'value_type': mi.MI_ARRAY | mi.MI_STRING, u'value': [primary_adapter_name] }, { u'name': u'TeamNicName', u'value_type': mi.MI_STRING, u'value': nic_name }] operation_options = {u'custom_options': custom_options} team.put(operation_options=operation_options) try: for member in members: if member != primary_adapter_name: self._add_team_member(conn, team_name, member) if primary_nic_vlan_id is not None: self._set_primary_nic_vlan_id(conn, team_name, primary_nic_vlan_id) if primary_nic_name != team_name or primary_nic_vlan_id is None: # TODO(alexpilotti): query the new nic name # When the nic name equals the bond name and a VLAN ID is set, # the nick name is changed. self._wait_for_nic(nic_name) except Exception as ex: self.delete_team(team_name) raise ex
def execute(self, service, shared_data): provisioning_data = service.get_vm_agent_package_provisioning_data() if not provisioning_data: LOG.info("Azure guest agent provisioning data not present") elif not provisioning_data.get("provision"): LOG.info("Skipping Azure guest agent provisioning as by metadata " "request") else: osutils = osutils_factory.get_os_utils() self._remove_agent_services(osutils) # TODO(alexpilotti): Check for processes that might still be # running self._remove_azure_dirs() if not osutils.is_nano_server(): ga_package_name = provisioning_data.get("package_name") if not ga_package_name: raise exception.ItemNotFoundException( "Azure guest agent package_name not found in metadata") LOG.debug("Azure guest agent package name: %s", ga_package_name) ga_path = self._get_guest_agent_source_path(osutils) ga_zip_path = os.path.join(ga_path, ga_package_name) if not os.path.exists(ga_zip_path): raise exception.CloudbaseInitException( "Azure guest agent package file not found: %s" % ga_zip_path) self._stop_ga_event_traces(osutils) self._delete_ga_event_traces(osutils) ga_target_path = os.path.join(os.getenv("SystemDrive"), '\\', GUEST_AGENT_ROOT_PATH, "Packages") if os.path.exists(ga_target_path): shutil.rmtree(ga_target_path) os.makedirs(ga_target_path) with zipfile.ZipFile(ga_zip_path) as zf: zf.extractall(ga_target_path) self._configure_rd_agent(osutils, ga_target_path) if not osutils.check_dotnet_is_installed("4"): LOG.warn("The .Net framework 4.5 or greater is required " "by the Azure guest agent") else: osutils.set_service_start_mode( SERVICE_NAME_RDAGENT, osutils.SERVICE_START_MODE_AUTOMATIC) osutils.start_service(SERVICE_NAME_RDAGENT) else: vm_agent_target_path = os.path.join(os.getenv("SystemDrive"), '\\', GUEST_AGENT_ROOT_PATH, "Packages", "GuestAgent") if not os.path.exists(vm_agent_target_path): os.makedirs(vm_agent_target_path) self._configure_vm_agent(osutils, vm_agent_target_path) osutils.set_service_start_mode( SERVICE_NAME_WAGUESTAGENT, osutils.SERVICE_START_MODE_AUTOMATIC) osutils.start_service(SERVICE_NAME_WAGUESTAGENT) return base.PLUGIN_EXECUTION_DONE, False
def get_rdp_certificate_thumbprint(): conn = wmi.WMI(moniker='//./root/cimv2/TerminalServices') tsSettings = conn.Win32_TSGeneralSetting() if not tsSettings: raise exception.ItemNotFoundException("No RDP certificate found") return tsSettings[0].SSLCertificateSHA1Hash
def _wait_for_nic(nic_name): conn = wmi.WMI(moniker='//./root/cimv2') if not conn.Win32_NetworkAdapter(NetConnectionID=nic_name): raise exception.ItemNotFoundException("Cannot find NIC: %s" % nic_name)