from pyinfra import host
from pyinfra.api import deploy
from pyinfra.facts.files import File
from pyinfra.operations import apt
from pyinfra.operations import server


@deploy("Setup Tailscale")
def setup_tailscale():
    if not host.get_fact(File, "/usr/share/keyrings/tailscale-archive-keyring.gpg"):
        server.shell(
            name="Install tailscale signing key",
            commands=[
                "curl -fsSL https://pkgs.tailscale.com/stable/debian/bookworm.noarmor.gpg "
                "| tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null 2>&1",
            ],
        )
    apt.repo(
        name="Add tailscale repository",
        src="deb [signed-by=/usr/share/keyrings/tailscale-archive-keyring.gpg] "
            "https://pkgs.tailscale.com/stable/debian bookworm main",
        filename="tailscale",
    )
    apt.packages(
        name="Install tailscale",
        packages=["tailscale"],
        update=True,
    )