Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create mirror using Command (fixes #108) #115

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyaptly/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, cmd: list[str]):
self._provides: set[tuple[str, str]] = set()
self._finished: bool = False
self._known_dependency_types = (
"mirror",
"snapshot",
"repo",
"publish",
Expand Down Expand Up @@ -87,6 +88,8 @@ def clear_caches(self):
for provide in provides:
lg.debug("clearing cache for " + provide)
match provide:
case "mirror":
state_reader.state_reader().mirrors.cache_clear()
case "snapshot":
state_reader.state_reader().snapshots.cache_clear()
state_reader.state_reader().snapshot_map.cache_clear()
Expand Down
29 changes: 17 additions & 12 deletions pyaptly/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from . import state_reader, util
from . import command, state_reader, util

lg = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,17 +79,24 @@ def mirror(cfg, args):

cmd_mirror = mirror_cmds[args.task]

cmds = []
if args.mirror_name == "all":
for mirror_name, mirror_config in cfg["mirror"].items():
cmd_mirror(cfg, mirror_name, mirror_config)
cmds.extend(cmd_mirror(cfg, mirror_name, mirror_config))
else:
if args.mirror_name in cfg["mirror"]:
cmd_mirror(cfg, args.mirror_name, cfg["mirror"][args.mirror_name])
cmds.extend(
cmd_mirror(cfg, args.mirror_name, cfg["mirror"][args.mirror_name])
)
else:
raise ValueError(
"Requested mirror is not defined in config file: %s"
% (args.mirror_name)
)
for cmd in command.Command.order_commands(
cmds, state_reader.state_reader().has_dependency
):
cmd.execute()


def cmd_mirror_create(cfg, mirror_name, mirror_config):
Expand All @@ -103,7 +110,7 @@ def cmd_mirror_create(cfg, mirror_name, mirror_config):
:type mirror_config: dict
"""
if mirror_name in state_reader.state_reader().mirrors(): # pragma: no cover
return
return []

add_gpg_keys(mirror_config)
aptly_cmd = ["aptly", "mirror", "create"]
Expand All @@ -128,9 +135,9 @@ def cmd_mirror_create(cfg, mirror_name, mirror_config):
aptly_cmd.append(mirror_config["distribution"])
aptly_cmd.extend(util.unit_or_list_to_list(mirror_config["components"]))

lg.debug("Running command: %s", " ".join(aptly_cmd))
util.run_command(aptly_cmd, check=True)
state_reader.state_reader().mirrors.cache_clear()
cmd = command.Command(aptly_cmd)
cmd.provide("mirror", mirror_name)
return [cmd]


def cmd_mirror_update(cfg, mirror_name, mirror_config):
Expand All @@ -143,14 +150,12 @@ def cmd_mirror_update(cfg, mirror_name, mirror_config):
:param mirror_config: Configuration of the snapshot from the toml file.
:type mirror_config: dict
"""
if mirror_name not in state_reader.state_reader().mirrors(): # pragma: no cover
raise Exception("Mirror not created yet")
add_gpg_keys(mirror_config)
aptly_cmd = ["aptly", "mirror", "update"]
if "max-tries" in mirror_config:
aptly_cmd.append("-max-tries=%d" % mirror_config["max-tries"])

aptly_cmd.append(mirror_name)
lg.debug("Running command: %s", " ".join(aptly_cmd))
util.run_command(aptly_cmd, check=True)
state_reader.state_reader().mirrors.cache_clear()
cmd = command.Command(aptly_cmd)
cmd.require("mirror", mirror_name)
return cmd_mirror_create(cfg, mirror_name, mirror_config) + [cmd]