def do_execute_cmd_with_stdout(host, user, password, cmd): LOG.debug("execute ssh command, host = %s, cmd = %s" % (host, cmd)) if host is None: raise SSHCommandFailure(host=host, command=cmd, error="host is None") ssh = sshclient.SSH(host=host, user=user, password=password) try: operate_result = ssh.execute(cmd) except Exception as e: LOG.error( "execute ssh command failed: host = %s, cmd = %s, reason = %s" % (ssh.host, cmd, e.message)) raise SSHCommandFailure(host=ssh.host, command=cmd, error=e.message) finally: ssh.close() exit_code = operate_result[0] if exit_code == 0: return operate_result[1] else: LOG.error( "execute ssh command failed: host = %s, cmd = %s, reason = %s" % (ssh.host, cmd, operate_result[2])) raise SSHCommandFailure(host=ssh.host, command=cmd, error=operate_result[2])
def _attach_volume_iscsi(self, instance, connection_info): user = CONF.vcloud.image_user pwd = CONF.vcloud.image_password vapp_ip = self._get_vapp_ip(instance) if vapp_ip: host = vapp_ip else: LOG.error("vapp_ip is None ,attach volume failed") raise Exception(_("vapp_ip is None ,attach volume failed")) ssh_client = sshclient.SSH(user, host, password=pwd) target_iqn = connection_info['data']['target_iqn'] target_portal = connection_info['data']['target_portal'] cmd1 = "sudo iscsiadm -m node -T %s -p %s" % (target_iqn, target_portal) while True: try: cmd1_status, cmd1_out, cmd1_err = ssh_client.execute(cmd1) LOG.debug("sudo cmd1 info status=%s ,out=%s, err=%s " % (cmd1_status, cmd1_out, cmd1_err)) if cmd1_status in [21, 255]: cmd2 = "sudo iscsiadm -m node -T %s -p %s --op new" % ( target_iqn, target_portal) cmd2_status, cmd2_out, cmd2_err = ssh_client.execute(cmd2) LOG.debug("sudo cmd2 info status=%s ,out=%s, err=%s " % (cmd2_status, cmd2_out, cmd2_err)) break except sshclient.SSHError: LOG.debug("wait for vm to initialize network") time.sleep(5) cmd3 = "sudo iscsiadm -m session" cmd3_status, cmd3_out, cmd3_err = ssh_client.execute(cmd3) portals = [{ 'portal': p.split(" ")[2], 'iqn': p.split(" ")[3] } for p in cmd3_out.splitlines() if p.startswith("tcp:")] stripped_portal = connection_info['data']['target_portal'].split( ",")[0] if len(portals) == 0 or len([ s for s in portals if stripped_portal == s['portal'].split(",")[0] and s['iqn'] == connection_info['data']['target_iqn'] ]) == 0: cmd4 = "sudo iscsiadm -m node -T %s -p %s --login" % ( target_iqn, target_portal) cmd4_status, cmd4_out, cmd4_err = ssh_client.execute(cmd4) LOG.debug("sudo cmd4 info status=%s ,out=%s, err=%s " % (cmd4_status, cmd4_out, cmd4_err)) cmd5 = "sudo iscsiadm -m node -T %s -p %s --op update -n node.startup -v automatic" % \ (target_iqn, target_portal) cmd5_status, cmd5_out, cmd5_err = ssh_client.execute(cmd5) LOG.debug("sudo cmd5 info status=%s ,out=%s, err=%s " % (cmd5_status, cmd5_out, cmd5_err)) ssh_client.close()
def check_host_status(host, user, password, retry_time=100, interval=1): LOG.info("check host status, host: %s" % host) if host is None: raise SSHCommandFailure(host=host, command=cmd, error="host is None") ssh = sshclient.SSH(host=host, user=user, password=password) for i in range(retry_time): try: ssh.execute("ls") LOG.info("host is ok, host: %s" % host) return True except Exception as e: time.sleep(interval) continue LOG.error("check host status failed, host = %s" % host) raise CheckHostStatusFailure(host=host)
def _detach_volume_iscsi(self, instance, connection_info): user = CONF.vcloud.image_user pwd = CONF.vcloud.image_password vapp_ip = self._get_vapp_ip(instance) if vapp_ip: host = vapp_ip else: LOG.debug("vapp_ip is None ,attach volume failed") raise ssh_client = sshclient.SSH(user, host, password=pwd) target_iqn = connection_info['data']['target_iqn'] target_portal = connection_info['data']['target_portal'] cmd1 = "ls -l /dev/disk/by-path/ | grep %s | awk -F '/' '{print $NF}'" % target_iqn cmd1_status, cmd1_out, cmd1_err = ssh_client.execute(cmd1) LOG.debug(" cmd1 info status=%s ,out=%s, err=%s " % (cmd1_status, cmd1_out, cmd1_err)) device = "/dev/" + cmd1_out.split('\n')[0] path = "/sys/block/" + cmd1_out.split('\n')[0] + "/device/delete" cmd2 = "sudo blockdev --flushbufs %s" % device cmd2_status, cmd2_out, cmd2_err = ssh_client.execute(cmd2) LOG.debug(" cmd2 info status=%s ,out=%s, err=%s " % (cmd2_status, cmd2_out, cmd2_err)) cmd3 = "echo 1 | sudo tee -a %s" % path cmd3_status, cmd3_out, cmd3_err = ssh_client.execute(cmd3) LOG.debug("sudo cmd3 info status=%s ,out=%s, err=%s " % (cmd3_status, cmd3_out, cmd3_err)) cmd4 = "sudo iscsiadm -m node -T %s -p %s --op update -n node.startup -v manual" % ( target_iqn, target_portal) cmd4_status, cmd4_out, cmd4_err = ssh_client.execute(cmd4) LOG.debug("sudo cmd4 info status=%s ,out=%s, err=%s " % (cmd4_status, cmd4_out, cmd4_err)) cmd5 = "sudo iscsiadm -m node -T %s -p %s --logout" % (target_iqn, target_portal) cmd5_status, cmd5_out, cmd5_err = ssh_client.execute(cmd5) LOG.debug("sudo cmd5 info status=%s ,out=%s, err=%s " % (cmd5_status, cmd5_out, cmd5_err)) cmd6 = "sudo iscsiadm -m node -T %s -p %s --op delete" % ( target_iqn, target_portal) cmd6_status, cmd6_out, cmd6_err = ssh_client.execute(cmd6) LOG.debug("sudo cmd6 info status=%s ,out=%s, err=%s " % (cmd6_status, cmd6_out, cmd6_err))
def scp_file_to_host(host, user, password, file_name, local_dir, remote_dir): LOG.debug("spc file to host, host = %s, file_name = %s, " "local_dir = %s, remote_dir = %s" % (host, file_name, local_dir, remote_dir)) ssh = sshclient.SSH(host=host, user=user, password=password) try: ssh.put_file(os.path.join(local_dir, file_name), remote_dir + "/" + file_name) except (sshclient.SSHError, sshclient.SSHTimeout) as e: LOG.error( "spc file to host failed, host = %s, " "file_name = %s, local_dir = %s, remote_dir = %s, reason = %s" % (ssh.host, file_name, local_dir, remote_dir, e.message)) raise ScpFileToHostFailure(host=ssh.host, file_name=file_name, local_dir=local_dir, remote_dir=remote_dir, error=e.message) finally: ssh.close() return True