Ejemplo n.º 1
0
def exec_helm_upload_cmd(tarball,
                         repo=None,
                         timeout=120,
                         con_ssh=None,
                         fail_ok=False):
    if not con_ssh:
        con_ssh = ControllerClient.get_active_controller()

    if not repo:
        repo = 'starlingx'
    cmd = 'helm-upload {} {}'.format(repo, tarball)
    con_ssh.send(cmd)
    pw_prompt = Prompt.PASSWORD_PROMPT
    prompts = [con_ssh.prompt, pw_prompt]

    index = con_ssh.expect(prompts,
                           timeout=timeout,
                           searchwindowsize=100,
                           fail_ok=fail_ok)
    if index == 1:
        con_ssh.send(con_ssh.password)
        prompts.remove(pw_prompt)
        con_ssh.expect(prompts,
                       timeout=timeout,
                       searchwindowsize=100,
                       fail_ok=fail_ok)

    code, output = con_ssh._process_exec_result(rm_date=True,
                                                get_exit_code=True)
    if code != 0 and not fail_ok:
        raise exceptions.SSHExecCommandFailed(
            "Non-zero return code for cmd: {}. Output: {}".format(cmd, output))

    return code, output
Ejemplo n.º 2
0
def source_openrc_file(ssh_client,
                       auth_info,
                       rc_file,
                       fail_ok=False,
                       force=True):
    """
    Source to the given openrc file on the ssh client.
    Args:
        ssh_client:
        auth_info:
        rc_file:
        fail_ok:
        force (bool): Whether to source even if already sourced.

    Returns:
        (-1, None)    # Already sourced, no action done
        (0, <cmd_output>)   # sourced to openrc file successfully
        (1, <err_output>)   # Failed to source

    """
    exit_code, cmd_output = -1, None
    user = auth_info['user']
    if force or 'keystone_{}'.format(user) not in ssh_client.prompt:
        password = auth_info['password']
        new_prompt = Prompt.TENANT_PROMPT.format(user)

        cmd = 'source {}'.format(rc_file)
        ssh_client.send(cmd)
        prompts = [new_prompt]
        index = ssh_client.expect(prompts, fail_ok=False)

        if index == 2:
            ssh_client.send()
            index = ssh_client.expect(prompts[0:3])
        if index == 1:
            ssh_client.send(password)
            index = ssh_client.expect(prompts[0:2])

        if index == 0:
            ssh_client.set_prompt(new_prompt)
            exit_code = ssh_client.get_exit_code()
        else:
            cmd_output = ssh_client.cmd_output
            ssh_client.send_control()
            ssh_client.expect()
            exit_code = 1

        if exit_code != 0:
            if not fail_ok:
                raise exceptions.SSHExecCommandFailed(
                    "Failed to Source. Output: {}".format(cmd_output))

    return exit_code, cmd_output
Ejemplo n.º 3
0
    def exec_cmd(self,
                 cmd,
                 expect_timeout=None,
                 reconnect=False,
                 reconnect_timeout=300,
                 err_only=False,
                 rm_date=False,
                 fail_ok=True,
                 get_exit_code=True,
                 blob=None,
                 force_end=False,
                 searchwindowsize=None):
        if blob is None:
            blob = self.prompt
        if expect_timeout is None:
            expect_timeout = self.timeout

        self.logger.debug("Executing command...")
        if err_only:
            cmd += ' 1> /dev/null'
        self.send(cmd, reconnect, reconnect_timeout)
        try:
            self.expect(blob_list=blob,
                        timeout=expect_timeout,
                        searchwindowsize=searchwindowsize)
        except exceptions.TelnetTimeout as e:
            self.send_control()
            self.expect(fail_ok=True, timeout=5)
            self.flush(timeout=1)
            if fail_ok:
                self.logger.warning(e)
            else:
                raise

        code, output = self._process_exec_result(rm_date,
                                                 get_exit_code=get_exit_code)

        self.__force_end(force_end)

        if code > 0 and not fail_ok:
            raise exceptions.SSHExecCommandFailed(
                "Non-zero return code for cmd: {}".format(cmd))

        return code, output
Ejemplo n.º 4
0
def _rsync_files_to_con1(con_ssh=None,
                         central_region=False,
                         file_to_check=None):
    region = 'RegionOne' if central_region else None
    auth_info = Tenant.get('admin_platform', dc_region=region)
    if less_than_two_controllers(auth_info=auth_info, con_ssh=con_ssh):
        LOG.info("Less than two controllers on system. Skip copying file to "
                 "controller-1.")
        return

    LOG.info("rsync test files from controller-0 to controller-1 if not "
             "already done")
    stx_home = HostLinuxUser.get_home()
    if not file_to_check:
        file_to_check = '{}/images/tis-centos-guest.img'.format(stx_home)
    try:
        with host_helper.ssh_to_host("controller-1",
                                     con_ssh=con_ssh) as con_1_ssh:
            if con_1_ssh.file_exists(file_to_check):
                LOG.info(
                    "Test files already exist on controller-1. Skip rsync.")
                return

    except Exception as e:
        LOG.error("Cannot ssh to controller-1. Skip rsync. "
                  "\nException caught: {}".format(e.__str__()))
        return

    cmd = "rsync -avr -e 'ssh -o UserKnownHostsFile=/dev/null -o " \
          "StrictHostKeyChecking=no ' " \
          "{}/* controller-1:{}".format(stx_home, stx_home)

    timeout = 1800
    with host_helper.ssh_to_host("controller-0", con_ssh=con_ssh) as con_0_ssh:
        LOG.info("rsync files from controller-0 to controller-1...")
        con_0_ssh.send(cmd)

        end_time = time.time() + timeout
        while time.time() < end_time:
            index = con_0_ssh.expect(
                [con_0_ssh.prompt, PASSWORD_PROMPT, Prompt.ADD_HOST],
                timeout=timeout,
                searchwindowsize=100)
            if index == 2:
                con_0_ssh.send('yes')

            if index == 1:
                con_0_ssh.send(HostLinuxUser.get_password())

            if index == 0:
                output = int(con_0_ssh.exec_cmd('echo $?')[1])
                if output in [0, 23]:
                    LOG.info(
                        "Test files are successfully copied to controller-1 "
                        "from controller-0")
                    break
                else:
                    raise exceptions.SSHExecCommandFailed(
                        "Failed to rsync files from controller-0 to "
                        "controller-1")

        else:
            raise exceptions.TimeoutException(
                "Timed out rsync files to controller-1")
Ejemplo n.º 5
0
def source_openrc_file(ssh_client,
                       auth_info,
                       rc_file,
                       fail_ok=False,
                       remote_cli=False,
                       force=False):
    """
    Source to the given openrc file on the ssh client.
    Args:
        ssh_client:
        auth_info:
        rc_file:
        fail_ok:
        remote_cli (bool): Whether it is remote cli client, where openrc files are on localhost.
        force (bool): Whether to source even if already sourced.

    Returns:
        (-1, None)    # Already sourced, no action done
        (0, <cmd_output>)   # sourced to openrc file successfully
        (1, <err_output>)   # Failed to source

    """
    exit_code, cmd_output = -1, None
    user = auth_info['user']
    if force or 'keystone_{}'.format(user) not in ssh_client.prompt:
        tenant = auth_info['tenant']
        password = auth_info['password']
        new_prompt = Prompt.REMOTE_CLI_PROMPT.format(
            user) if remote_cli else Prompt.TENANT_PROMPT.format(user)

        cmd = 'source {}'.format(rc_file)
        ssh_client.send(cmd)
        prompts = [new_prompt]
        if remote_cli:
            prompts += [
                'Password for project {} as user {}:'.format(tenant, user),
                'if you are not using HTTPS:'
            ]
        index = ssh_client.expect(prompts, fail_ok=False)

        if index == 2:
            ssh_client.send()
            index = ssh_client.expect(prompts[0:3])
        if index == 1:
            ssh_client.send(password)
            index = ssh_client.expect(prompts[0:2])

        if index == 0:
            ssh_client.set_prompt(new_prompt)
            exit_code = ssh_client.get_exit_code()
        else:
            cmd_output = ssh_client.cmd_output
            ssh_client.send_control()
            ssh_client.expect()
            exit_code = 1

        if exit_code != 0:
            if not fail_ok:
                raise exceptions.SSHExecCommandFailed(
                    "Failed to Source. Output: {}".format(cmd_output))

    return exit_code, cmd_output