def get_single_assignment(self, vol, host, raise_on_non_exists=True): """Get a single assignment details between a host and a volume.""" for assign in self.list_assignments(vol=vol): if assign["host"] == host: return assign if raise_on_non_exists: raise exception.RdxAPICommandException( _("No such assignment vol:%(vol)s, host:%(host)s") % { 'vol': vol, 'host': host }) else: return None
def _run_cmd(self, cmd): """Run the command and returns a dictionary of the response. On failure, the function retries the command. After retry threshold the function throws an error. """ cmd.set_json_output() LOG.info(_LI("Running cmd: %s"), cmd) success = False for x in range(1, CONNECTION_RETRY_NUM): try: self._reconnect_if_needed() stdin, stdout, stderr = self.ssh.exec_command( # nosec # command input from authorized users on command line command=six.text_type(cmd), timeout=CLI_SSH_CMD_TIMEOUT) success = True break except Exception: LOG.exception(_LE("Error in running Reduxio CLI command")) LOG.error(_LE("retrying(%(cur)s/%(overall)s)"), { 'cur': x, 'overall': CONNECTION_RETRY_NUM }) self.connected = False eventlet.sleep(CLI_CONNECTION_RETRY_SLEEP) if not success: raise exception.RdxAPIConnectionException( _("Failed to connect to Redxuio CLI." " Check your username, password or Reduxio Hostname/IP")) str_out = stdout.read() # Python 2.7/3.4 compatibility with the decode method if hasattr(str_out, "decode"): data = json.loads(str_out.decode("utf8")) else: data = json.loads(str_out) if stdout.channel.recv_exit_status() != 0: LOG.error(_LE("Failed running cli command: %s"), data["msg"]) raise exception.RdxAPICommandException(data["msg"]) LOG.debug("Command output is: %s", str_out) return data["data"]