def ssh_api(exp, api):
    user = api.auth.username
    info = api.get_experiment_info(exp.id)
    deployed_nodes = info["deploymentresults"]["0"]
    deployed_a8 = ["node-" + n for n in deployed_nodes if "a8" in n]
    ssh_api = OpenA8Ssh({"user": user}, _nodes_grouped(deployed_a8))
    return ssh_api
Beispiel #2
0
def test_run(join, run_command, run_on_frontend):
    # pylint: disable=unused-argument
    """Test running commands on ssh nodes."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }

    test_command = 'test'
    groups = _nodes_grouped(_ROOT_NODES)

    node_ssh = OpenA8Ssh(config_ssh, groups, verbose=True)

    # Print output of run_command
    if run_on_frontend:
        run_command.return_value = dict(
            ("{}.iot-lab.info".format(site),
             {'stdout': ['test'], 'exit_code': 0})
            for site in _SITES)
    else:
        run_command.return_value = dict(
            (node,
             {'stdout': ['test'], 'exit_code': 0})
            for node in _ROOT_NODES)

    node_ssh.run(test_command, with_proxy=not run_on_frontend)
    assert run_command.call_count == len(groups)
    run_command.assert_called_with(test_command, stop_on_errors=False)
Beispiel #3
0
def test_scp(connect, client, put, _open):
    # pylint: disable=unused-argument
    """Test wait for ssh nodes to be available."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }

    src = 'test_src'
    dst = 'test_dst'

    groups = _nodes_grouped(_ROOT_NODES)

    node_ssh = OpenA8Ssh(config_ssh, groups, verbose=True)
    ret = node_ssh.scp(src, dst)

    assert ret == {'0': ['saclay.iot-lab.info', 'grenoble.iot-lab.info']}

    connect.side_effect = ConnectionErrorException()
    ret = node_ssh.scp(src, dst)

    assert ret == {'1': ['saclay.iot-lab.info', 'grenoble.iot-lab.info']}

    # Simulating an exception
    # pylint: disable=redefined-variable-type
    connect.side_effect = AuthenticationException()

    with raises(OpenA8SshAuthenticationException):
        node_ssh.scp(src, dst)
Beispiel #4
0
def test_authentication_exception(join, run_command):
    # pylint: disable=unused-argument
    """Test SSH authentication exception with parallel-ssh
    run_command and stop_on_errors=False option.
    """
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }

    test_command = 'test'
    groups = _nodes_grouped(_ROOT_NODES)

    # normal boot
    node_ssh = OpenA8Ssh(config_ssh, groups, verbose=True)

    # Print output of run_command
    run_command.return_value = dict(
        ("{}.iot-lab.info".format(site),
         {'stdout': None, 'exit_code': None})
        for site in _SITES)

    with raises(OpenA8SshAuthenticationException):
        node_ssh.run(test_command)

    # only one iteration as there is an exception
    assert run_command.call_count == 1
    run_command.assert_called_with(test_command, stop_on_errors=False)
Beispiel #5
0
def test_wait_all_boot(join, run_command):
    # pylint: disable=unused-argument
    """Test wait for ssh nodes to be available."""
    config_ssh = {
        'user': '******',
        'exp_id': 123,
    }

    test_command = 'test'
    groups = _nodes_grouped(_ROOT_NODES)

    # normal boot
    node_ssh = OpenA8Ssh(config_ssh, groups, verbose=True)

    # Print output of run_command
    run_command.return_value = dict(
        (node,
         {'stdout': ['test'], 'exit_code': 0})
        for node in _ROOT_NODES)

    node_ssh.wait(120)
    assert run_command.call_count == len(_SITES)
    run_command.assert_called_with('uptime', stop_on_errors=False)
    run_command.reset_mock()

    node_ssh.run(test_command)
    assert run_command.call_count == len(_SITES)
    run_command.assert_called_with(test_command, stop_on_errors=False)
Beispiel #6
0
def run_cmd(config_ssh, nodes, cmd, run_on_frontend=False, verbose=False):
    """ Run a command on the A8 nodes or on the SSH frontend. """

    # Configure ssh.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)
    try:
        result = ssh.run(cmd, with_proxy=not run_on_frontend)
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        result = {"1": nodes}

    return {"run-cmd": result}
Beispiel #7
0
def wait_for_boot(config_ssh, nodes, max_wait=120, verbose=False):
    """Reset the M3 of open A8 nodes."""

    # Configure ssh.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)

    # Wait for A8 boot
    try:
        result = ssh.wait(max_wait)
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        return {"1": nodes}

    return {"wait-for-boot": result}
Beispiel #8
0
def reset_m3(config_ssh, nodes, verbose=False):
    """Reset the M3 of open A8 nodes."""

    # Configure ssh.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)

    # Run M3 reset command.
    try:
        result = ssh.run(_RESET_M3_CMD)
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        return {"1": nodes}

    return {"reset-m3": result}
Beispiel #9
0
def run_script(config_ssh,
               nodes,
               script,
               run_on_frontend=False,
               verbose=False):
    """Run a script in background on the A8 nodes
    or on the SSH frontend
    """

    # Configure ssh.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)

    screen = '{user}-{exp_id}'.format(**config_ssh)
    remote_script = os.path.join('~/A8/.iotlabsshcli',
                                 os.path.basename(script))
    script_data = {'screen': screen, 'path': remote_script}
    with_proxy = False

    try:
        # Create destination directory
        ssh.run(_MKDIR_DST_CMD.format(os.path.dirname(remote_script)),
                with_proxy=with_proxy)
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        result = {"1": nodes}
    else:
        # Copy script on sites.
        ssh.scp(script, remote_script)

        # Make script executable
        ssh.run(_MAKE_EXECUTABLE_CMD.format(remote_script),
                with_proxy=with_proxy)

        # Kill any running script
        ssh.run(_QUIT_SCRIPT_CMD.format(**script_data),
                with_proxy=not run_on_frontend)

        # Run script
        result = ssh.run(_RUN_SCRIPT_CMD.format(**script_data),
                         with_proxy=not run_on_frontend,
                         use_pty=False)

    return {"run-script": result}
Beispiel #10
0
def flash_m3(config_ssh, nodes, firmware, verbose=False):
    """Flash the firmware of M3 of open A8 nodes."""
    # Configure ssh and remote firmware names.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)
    remote_fw = os.path.join('~/A8/.iotlabsshcli', os.path.basename(firmware))

    # Create firmware destination directory
    try:
        ssh.run(_MKDIR_DST_CMD.format(os.path.dirname(remote_fw)))
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        return {"1": nodes}

    # Copy firmware on sites.
    ssh.scp(firmware, remote_fw)

    # Run firmware update.
    result = ssh.run(_UPDATE_M3_CMD.format(remote_fw))
    return {"flash-m3": result}
Beispiel #11
0
def copy_file(config_ssh, nodes, file_path, verbose=False):
    """ Copy a file on the A8 SSH frontend(s) directory(es)
    (~/A8/.iotlabsshcli/)
    """

    # Configure ssh.
    groups = _nodes_grouped(nodes)
    ssh = OpenA8Ssh(config_ssh, groups, verbose=verbose)
    remote_file = os.path.join('~/A8/.iotlabsshcli',
                               os.path.basename(file_path))
    try:
        # Create file destination directory
        ssh.run(_MKDIR_DST_CMD.format(os.path.dirname(remote_file)),
                with_proxy=False)
    except OpenA8SshAuthenticationException as exc:
        print(exc.msg)
        result = {"1": nodes}
    else:
        # Copy file on sites.
        result = ssh.scp(file_path, remote_file)

    return {"copy-file": result}