forked from lab-in-a-box/liab-deployments
This wraps `git.repo_w_credential` and also initializes an annex, gets content, and drops unusued keys.
155 lines
3.7 KiB
Python
155 lines
3.7 KiB
Python
from collections.abc import Sequence
|
|
|
|
from pyinfra import host
|
|
from pyinfra.api import (
|
|
StringCommand,
|
|
QuoteString,
|
|
operation,
|
|
)
|
|
from pyinfra.api.operation import OperationMeta
|
|
from pyinfra.facts.server import (
|
|
User,
|
|
Hostname,
|
|
)
|
|
from pyinfra.operations import (
|
|
git,
|
|
server,
|
|
)
|
|
from liab_deployments.operations import git as liab_git
|
|
|
|
|
|
@operation()
|
|
def init(repo: str, *, private: bool = False):
|
|
"""Initialize the annex of an existing repository clone
|
|
|
|
With `private` set, the `annex.private` configuration flag
|
|
is set before calling `git annex init`.
|
|
|
|
If no Git identity is set, a fallback identity is set in the
|
|
scope of the repository.
|
|
|
|
When repository remotes require authorized access, the necessary
|
|
credentials need to be deposited before this operation runs.
|
|
"""
|
|
globalcfg = host.get_fact(git.GitConfig)
|
|
repocfg = host.get_fact(git.GitConfig, repo=repo)
|
|
|
|
private_setting = repocfg.get('annex.private', [])
|
|
private_ok = \
|
|
private and 'true' in private_setting \
|
|
or (not private and 'true' not in private_setting)
|
|
|
|
if private_ok and 'annex.uuid' in repocfg:
|
|
# already initialized and congruent with request
|
|
return
|
|
|
|
user_name = host.get_fact(User)
|
|
hostname = host.get_fact(Hostname)
|
|
|
|
# we need a git-identity for git-annex-init to function.
|
|
# place into repo config, use system
|
|
if not any(('user.name' in cfg for cfg in (globalcfg, repocfg))):
|
|
yield from git.config._inner(
|
|
key="user.name",
|
|
value=user_name,
|
|
repo=repo,
|
|
)
|
|
if not any(('user.email' in cfg for cfg in (globalcfg, repocfg))):
|
|
yield from git.config._inner(
|
|
key="user.email",
|
|
value=f'{user_name}@{hostname}',
|
|
repo=repo,
|
|
)
|
|
|
|
if private:
|
|
yield from git.config._inner(
|
|
key='annex.private',
|
|
value='true',
|
|
repo=repo,
|
|
)
|
|
|
|
yield from server.shell._inner(
|
|
commands=[
|
|
f'git -C "{repo}" annex init',
|
|
],
|
|
)
|
|
|
|
|
|
@operation()
|
|
def get(
|
|
repo: str,
|
|
get: Sequence[str],
|
|
):
|
|
cmd = StringCommand(
|
|
'git', '-C', QuoteString(repo),
|
|
'annex', 'get',
|
|
*[QuoteString(g) for g in get],
|
|
)
|
|
yield from server.shell._inner(commands=str(cmd))
|
|
|
|
|
|
@operation()
|
|
def drop_unused(
|
|
repo: str,
|
|
):
|
|
|
|
unused_cmd = StringCommand(
|
|
'git', '-C', QuoteString(repo), 'annex', 'unused')
|
|
drop_cmd = StringCommand(
|
|
'git', '-C', QuoteString(repo), 'annex', 'dropunused all')
|
|
yield from server.shell._inner(
|
|
commands=[
|
|
str(unused_cmd),
|
|
str(drop_cmd),
|
|
],
|
|
)
|
|
|
|
|
|
def annexrepo_w_credential(
|
|
src: str,
|
|
dest: str,
|
|
*,
|
|
cred_file: str | None,
|
|
annex_get: str | Sequence[str] | None = None,
|
|
name: str | None = None,
|
|
_sudo_user: str | None = None,
|
|
**kwargs,
|
|
) -> OperationMeta:
|
|
repo = liab_git.repo_w_credential(
|
|
name=name,
|
|
src=src,
|
|
dest=dest,
|
|
cred_file=cred_file,
|
|
_sudo_user=_sudo_user,
|
|
**kwargs
|
|
)
|
|
|
|
if not annex_get:
|
|
return repo
|
|
|
|
# TODO: conditional on git config fact
|
|
init(
|
|
name=f'Init annex {dest!r}',
|
|
repo=dest,
|
|
private=True,
|
|
_sudo_user=_sudo_user,
|
|
_if=repo.did_change,
|
|
)
|
|
|
|
if isinstance(annex_get, str):
|
|
annex_get = [annex_get]
|
|
get(
|
|
name=f'Get annex content {dest!r}',
|
|
repo=dest,
|
|
get=annex_get,
|
|
_sudo_user=_sudo_user,
|
|
_if=repo.did_change,
|
|
)
|
|
|
|
drop_unused(
|
|
name=f'Drop unused annex content {dest!r}',
|
|
repo=dest,
|
|
_sudo_user=_sudo_user,
|
|
_if=repo.did_change,
|
|
)
|
|
return repo
|