示例#1
0
文件: util.py 项目: samip5/hyperglass
def move_files(src, dst, files):  # noqa: C901
    """Move iterable of files from source to destination.

    Arguments:
        src {Path} -- Current directory of files
        dst {Path} -- Target destination directory
        files {Iterable} -- Iterable of files
    """

    if not isinstance(src, Path):
        try:
            src = Path(src)
        except TypeError:
            error("{p} is not a valid path", p=src)
    if not isinstance(dst, Path):
        try:
            dst = Path(dst)
        except TypeError:
            error("{p} is not a valid path", p=dst)

    if not isinstance(files, Iterable):
        error(
            "{fa} must be an iterable (list, tuple, or generator). Received {f}",
            fa="Files argument",
            f=files,
        )

    for path in (src, dst):
        if not path.exists():
            error("{p} does not exist", p=str(path))

    migrated = 0

    for file in files:
        dst_file = dst / file.name

        if not file.exists():
            error("{f} does not exist", f=file)

        try:
            if dst_file.exists():
                warning("{f} already exists", f=dst_file)
            else:
                shutil.copyfile(file, dst_file)
                migrated += 1
                info("Migrated {f}", f=dst_file)
        except Exception as e:
            error("Failed to migrate {f}: {e}", f=dst_file, e=e)

    if migrated == 0:
        warning("Migrated {n} files", n=migrated)
    elif migrated > 0:
        success("Successfully migrated {n} files", n=migrated)
    return True
示例#2
0
def make_systemd(user):
    """Generate a systemd file based on the local system.

    Arguments:
        user {str} -- User hyperglass needs to be run as

    Returns:
        {str} -- Generated systemd template
    """

    # Third Party
    import distro

    template = """
[Unit]
Description=hyperglass
After=network.target
Requires={redis_name}

[Service]
User={user}
Group={group}
ExecStart={hyperglass_path} start

[Install]
WantedBy=multi-user.target
    """
    known_rhel = ("rhel", "centos")
    distro = distro.linux_distribution(full_distribution_name=False)
    if distro[0] in known_rhel:
        redis_name = "redis"
    else:
        redis_name = "redis-server"

    hyperglass_path = shutil.which("hyperglass")

    if not hyperglass_path:
        hyperglass_path = "python3 -m hyperglass.console"
        warning("hyperglass executable not found, using {h}",
                h=hyperglass_path)

    systemd = template.format(redis_name=redis_name,
                              user=user,
                              group=user,
                              hyperglass_path=hyperglass_path)
    info(f"Generated systemd service:\n{systemd}")
    return systemd