def _connect(self): self.connected = False self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: self.ssh.connect(self.host, username=self.user, password=self.password, timeout=CLI_CONNECT_TIMEOUT) self.connected = True except paramiko.ssh_exception.AuthenticationException: raise exception.RdxAPIConnectionException( _("Authentication Error. Check login credentials")) except Exception: LOG.exception(_LE("Exception in connecting to Reduxio CLI")) raise exception.RdxAPIConnectionException( _("Failed to create ssh connection to Reduxio." " Please check network connection or Reduxio hostname/IP."))
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"]