示例#1
0
文件: __init__.py 项目: zkrx/tbot
def swupdate_update_web(lh: linux.Lab,
                        swu_file: linux.Path,
                        target_ip: str,
                        timeout: int = 300) -> None:
    """
    Upload an ``.swu`` file to a running swupdate server.

    :param linux.Lab lh: Optionally the lab-host from where to initiate the update.
    :param linux.Path swu_file: Path to the ``.swu`` file (on the lab-host or locally).
    :param str target_ip: IP-Address of the target host.
    :param int timeout: Timeout.
    """
    with tbot.acquire_local() as lo:  # Needed for the script
        script_path = lh.workdir / "tbot_swupdate_web.py"
        swu_path = lh.workdir / "image.swu"
        script_source = linux.Path(lo, __file__).parent / "swupdate_script.py"
        shell.copy(script_source, script_path)
        shell.copy(swu_file, swu_path)
        lh.exec0("python3", script_path, swu_path, target_ip, str(timeout))
示例#2
0
文件: minisshd.py 项目: Rahix/tbot
def minisshd(h: linux.Lab, port: int = 2022) -> typing.Generator:
    """
    Create a new minisshd server and machine.

    Intended for use in a ``with``-statement::

        if check_minisshd(lh):
            with minisshd(lh) as ssh:
                ssh.exec0("uname", "-a")

    :param linux.Lab h: lab-host
    :param int port: Port for the ssh server, defaults to ``2022``.
    :rtype: MiniSSHMachine
    """
    server_dir = h.workdir / "minisshd"
    h.exec0("mkdir", "-p", server_dir)

    key_file = server_dir / "ssh_host_key"
    pid_file = server_dir / "dropbear.pid"

    # Create host key
    if not key_file.exists():
        h.exec0("dropbearkey", "-t", "rsa", "-f", key_file)

    h.exec0("dropbear", "-p", "127.0.0.1:2022", "-r", key_file, "-P", pid_file)

    # Try reading the file again if it does not yet exist
    for i in range(10):
        ret, pid = h.exec("cat", pid_file)
        if ret == 0:
            pid = pid.strip()
            break
    else:
        raise RuntimeError("dropbear did not create a pid-file!")

    try:
        with MiniSSHMachine(h, port) as ssh_machine:
            yield ssh_machine
    finally:
        tbot.log.message("Stopping dropbear ...")
        h.exec0("kill", pid)
示例#3
0
文件: git.py 项目: Lusus/tbot
def git_prepare(lab: linux.Lab) -> str:
    """Prepare a test git repo."""
    global _GIT

    if _GIT is None:
        # Git committer and author information in case the user's git
        # environment is not set up yet
        lab.env("GIT_AUTHOR_NAME", "tbot selftest")
        lab.env("GIT_AUTHOR_EMAIL", "*****@*****.**")
        lab.env("GIT_COMMITTER_NAME", "tbot selftest")
        lab.env("GIT_COMMITTER_EMAIL", "*****@*****.**")

        p = lab.workdir / "selftest-git-remote"

        if p.exists():
            lab.exec0("rm", "-rf", p)

        tbot.log.message("Setting up test repo ...")

        lab.exec0("mkdir", "-p", p)
        lab.exec0("git", "-C", p, "init")
        repo = git.GitRepository(p, clean=False)

        lab.exec0(
            "echo",
            """\
# tbot Selftest
This repo exists to test tbot's git testcase.

You can safely remove it, but please **do not** modify it as that might
break the tests.""",
            linux.RedirStdout(repo / "README.md"),
        )

        repo.add(repo / "README.md")
        repo.commit("Initial", author="tbot Selftest <none@none>")

        tbot.log.message("Creating test patch ...")
        lab.exec0(
            "echo",
            """\
# File 2
A second file that will have been added by patching.""",
            linux.RedirStdout(repo / "file2.md"),
        )

        repo.add(repo / "file2.md")
        repo.commit("Add file2", author="tbot Selftest <none@none>")
        patch_name = repo.git0("format-patch", "HEAD~1").strip()
        lab.exec0("mv", repo / patch_name, lab.workdir / "selftest-git.patch")

        tbot.log.message("Resetting repo ...")
        repo.reset("HEAD~1", git.ResetMode.HARD)

        _GIT = p._local_str()
        return _GIT
    else:
        return _GIT
示例#4
0
def selftest_decorated_lab(lh: linux.Lab) -> None:
    lh.exec0("uname", "-a")