def _execute_script(self): """ :raises HyperVPSScriptError """ try: resp_data = None ip = self.data[hyperv_const.EON_RESOURCE_IP_ADDRESS] self._copy_ps_script() resp = pywinrm.run_ps_script( self.session, self.script_path) if resp.status_code == 0: resp_data = json.loads(resp.std_out) LOG.info("Inventory data of hyperv host %(host)s : " "%(inventory)s", {"host": ip, "inventory": resp_data}) return resp_data else: raise exception.HyperVPSScriptError(err="%s" % resp.std_err) except exception as e: raise e finally: self._remove_activation_script()
def _run_ps_script_through_pywinrm(self, ps_script): resp = pywinrm.run_ps_script(self.session, ps_script) if resp.status_code == 0: self._validate_pywinrm_response(resp) else: raise exception.HyperVPyWinRMExectionError()
def test_run_ps_script(self): ses = MagicMock() resp = ses.run_cmd.return_value = MagicMock() resp_ret = pywinrm.run_ps_script(ses, self.ps_script) self.assertEqual(resp, resp_ret)
def get_csv(self): LOG.info("Getting Cluster information...") ps_script = hyperv_const.PS_SCRIPT_TO_CHECK_CSV resp = pywinrm.run_ps_script( self.session, ps_script) if resp.status_code != 0: raise exception.HyperVPSScriptError( err="%s" % resp.std_err) return resp.std_out.strip()
def get_hostname(self): LOG.info("Getting HyperV host's hostname...") ps_script = hyperv_const.PS_SCRIPT_TO_GET_HOSTNAME resp = pywinrm.run_ps_script( self.session, ps_script) if resp.status_code != 0: raise exception.HyperVPSScriptError( err="%s" % resp.std_err) return resp.std_out.strip()
def _remove_activation_script(self): host_ip = self.data[hyperv_const.EON_RESOURCE_IP_ADDRESS] LOG.info("Removing the activation script from hyperv host %s", host_ip) ps_script = self._get_ps_script_to_remove_file() resp = pywinrm.run_ps_script(self.session, ps_script) if resp.status_code != 0: LOG.warning("Deleting activation script from HyperV " "host %s failed", host_ip)
def get_host_validation_data(self, context): # execute the script and get all inventory data LOG.info("Getting the inventory data of hyperv host %s", self.data[hyperv_const.EON_RESOURCE_IP_ADDRESS]) collected_inventory = {} collected_inventory = self._execute_script() hux_obj = HLMFacadeWrapper(context) servers = hux_obj.get_server_by_role("MANAGEMENT-ROLE") hostname = hux_obj.model_generated_host_name(servers[0].get('id')) resp = pywinrm.run_ps_script( self.session, hyperv_const.PS_SCRIPT_TO_CHECK_TIME_SYNC % {"ntpserver_fqdn": hostname}) if resp.status_code != 0: raise exception.HyperVPSScriptError( err="%s" % resp.std_err) collected_inventory['host_date_configured'] = resp.std_out.strip() return collected_inventory
def _connectivity_check(self, update_data, cur_data): """Do a connection check only if connection related data is changed. """ conn_keys = set((hyperv_const.EON_RESOURCE_IP_ADDRESS, hyperv_const.EON_RESOURCE_PORT, hyperv_const.EON_RESOURCE_USERNAME, hyperv_const.EON_RESOURCE_PASSWORD)) if set(update_data).isdisjoint(conn_keys): # Won't proceed if no value change for connection data. return new_data = copy.deepcopy(cur_data) new_data.update(update_data) # TODO: HTTPS for winrm connection using port 5986" session = pywinrm.get_pywinrm_session( new_data[hyperv_const.EON_RESOURCE_IP_ADDRESS], new_data[hyperv_const.EON_RESOURCE_PORT], new_data[hyperv_const.EON_RESOURCE_USERNAME], new_data[hyperv_const.EON_RESOURCE_PASSWORD]) try: ps_script = hyperv_const.PS_SCRIPT_TO_GET_HOSTNAME resp = pywinrm.run_ps_script( session, ps_script) if resp.status_code != 0: raise exception.HyperVPSScriptError( err="%s" % resp.std_err) except exception.PyWinRMAuthenticationError as exc: LOG.error("Pywinrm exception: %s while connecting to %s" % (exc, new_data[hyperv_const.EON_RESOURCE_IP_ADDRESS])) raise exception.HyperVHostAuthenticationError() except exception.PyWinRMConnectivityError as exc: LOG.error("Pywinrm exception: %s while connecting to %s" % (exc, new_data[hyperv_const.EON_RESOURCE_IP_ADDRESS])) raise exception.HyperVHostConnectivityError(err=exc.message)