sync 2024-10-09, pyinfra v3

This commit is contained in:
Luna 2024-10-09 14:33:22 -03:00
parent b3b451ce96
commit 924e170299
7 changed files with 137 additions and 66 deletions

View file

@ -2,24 +2,21 @@ from pyinfra.operations import apk, files, server, git, systemd, python
from pyinfra import host
from pyinfra.facts.files import Directory
from pyinfra.facts.git import GitBranch
from pyinfra.facts.git import GitBranch, GitFactBase
from pyinfra.api import deploy, operation, FactBase
from pyinfra.operations.util.files import chown, unix_path_join
class CoolerGitBranch(FactBase):
requires_command = "git"
@staticmethod
def command(repo):
class CoolerGitBranch(GitFactBase):
def command(self, repo) -> str:
# TODO should inject _sudo / _sudo_user if user is provided in repo()
return "! test -d {0} || (cd {0} && git rev-parse --abbrev-ref HEAD)".format(
repo
)
class GitFetch(FactBase):
class GitFetch(GitFactBase):
def command(self, repo: str):
return f"git -C {repo} fetch"
@ -27,9 +24,10 @@ class GitFetch(FactBase):
return output
class GitRevListComparison(FactBase):
class GitRevListComparison(GitFactBase):
def command(self, repo: str, branch: str):
assert branch
if not branch:
raise AssertionError(f"branch must be provided. its now {branch!r}")
return f"git -C {repo} rev-list HEAD..origin/{branch} | wc -l"
def process(self, output):
@ -48,11 +46,7 @@ class RawCommandOutput(FactBase):
return "\n".join(output) # re-join and return the output lines
@operation(
pipeline_facts={
"git_branch": "target",
}
)
@operation()
def repo(
src,
dest,
@ -91,7 +85,7 @@ def repo(
"""
# Ensure our target directory exists
yield from files.directory(dest)
yield from files.directory._inner(dest)
if ssh_keyscan:
raise NotImplementedError("TODO copypaste ssh_keyscan code")
@ -108,13 +102,14 @@ def repo(
else:
git_commands.append("clone {0} .".format(src))
host.create_fact(GitBranch, kwargs={"repo": dest}, data=branch)
host.create_fact(CoolerGitBranch, kwargs={"repo": dest}, data=branch)
host.create_fact(
Directory,
kwargs={"path": git_dir},
data={"user": user, "group": group},
)
# pyinfra-v3: not needed anymore
# host.create_fact(GitBranch, kwargs={"repo": dest}, data=branch)
# host.create_fact(CoolerGitBranch, kwargs={"repo": dest}, data=branch)
# host.create_fact(
# Directory,
# kwargs={"path": git_dir},
# data={"user": user, "group": group},
# )
# Ensuring existing repo
else: