Ejemplo n.º 1
0
def ensure_sd_unit(lnx: linux.LinuxShell, services: typing.List[str]) -> None:
    """
    check if all systemd services in list services run on linux machine lnx.
    If not, try to start them.

    :param lnx: linux shell
    :param services: list of systemd services
    """
    if lnx.name not in _SERVICES_CACHE:
        _SERVICES_CACHE[lnx.name] = {}

    for s in services:
        if s in _SERVICES_CACHE[lnx.name]:
            continue

        if not lnx.test("systemctl", "is-active", s):
            lnx.exec0("sudo", "systemctl", "start", s)

        _SERVICES_CACHE[lnx.name][s] = True
Ejemplo n.º 2
0
Archivo: shell.py Proyecto: zkrx/tbot
def check_for_tool(host: linux.LinuxShell, tool: str) -> bool:
    """
    Check whether a certain tool/program is installed on a host.

    Results from previous invocations are cached.

    **Example**:

    .. code-block:: python

        if shell.check_for_tool(lh, "wget"):
            lh.exec0("wget", download_url, "-O", lh.workdir / "download.tgz")
        elif shell.check_for_tool(lh, "curl"):
            lh.exec0("curl", download_url, "-o", lh.workdir / "download.tgz")
        else:
            raise Exception("Need either 'wget' or 'curl'!")

    :param linux.LinuxShell host: The host to ceck on.
    :param str tool: Name of the binary to check for.
    :rtype: bool
    :returns: ``True`` if the tool was found and ``False`` otherwise.
    """
    if host not in _TOOL_CACHE:
        _TOOL_CACHE[host] = {}

    if tool not in _TOOL_CACHE[host]:
        with tbot.testcase("check_for_tool"):
            has_tool = host.test("which", tool)
            _TOOL_CACHE[host][tool] = has_tool

            if has_tool:
                tbot.log.message(
                    f"Host '{tbot.log.c(host.name).yellow}' has "
                    + f"'{tbot.log.c(tool).bold}' installed."
                )
            else:
                tbot.log.message(
                    f"Host '{tbot.log.c(host.name).yellow}' does "
                    + f"{tbot.log.c('not').red} have '{tbot.log.c(tool).bold}' installed."
                )

    return _TOOL_CACHE[host][tool]