Skip to content

Commit

Permalink
Merge pull request #115 from adfinis/jlf/fix-create-mirror-using-Comm…
Browse files Browse the repository at this point in the history
…and-fixes-#108-

fix: create mirror using Command (fixes #108)
  • Loading branch information
Jean-Louis Fuchs authored May 30, 2024
2 parents 8e18167 + 4f1f76b commit 8bba42a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
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]

0 comments on commit 8bba42a

Please sign in to comment.