fix(configure): gracefully handle missing ufw dirs and skip redundant apt calls
This commit is contained in:
3 files changed
+32
-25
No files matched your search
@@ -11,12 +11,16 @@ def run(ctx, args):
|
|||||||
log.info("Starting base system initialization...")
|
log.info("Starting base system initialization...")
|
||||||
if not os.path.isfile("/etc/debian_version"):
|
if not os.path.isfile("/etc/debian_version"):
|
||||||
log.error("Requires a Debian-based system (Debian/Ubuntu)."); raise SystemExit(1)
|
log.error("Requires a Debian-based system (Debian/Ubuntu)."); raise SystemExit(1)
|
||||||
log.info("Updating package lists..."); s.sudo(["apt-get", "update", "-y"])
|
needed = [p for p in ["curl", "wget", "git", "rsyslog"] if not shutil.which(p)]
|
||||||
log.info("Installing essential packages...")
|
if needed:
|
||||||
s.sudo(["apt-get", "install", "-y", "curl", "wget", "git", "rsyslog"])
|
log.info("Updating package lists..."); s.sudo(["apt-get", "update", "-y"])
|
||||||
|
log.info(f"Installing missing essential packages: {', '.join(needed)}...")
|
||||||
|
s.sudo(["apt-get", "install", "-y", "-o", "DPkg::Lock::Timeout=60", *needed])
|
||||||
|
else:
|
||||||
|
log.info("Essential packages already installed.")
|
||||||
s.ensure_running("rsyslog")
|
s.ensure_running("rsyslog")
|
||||||
if not shutil.which("ufw"):
|
if not shutil.which("ufw"):
|
||||||
log.info("Installing UFW..."); s.sudo(["apt-get", "install", "-y", "ufw"])
|
log.info("Installing UFW..."); s.sudo(["apt-get", "install", "-y", "-o", "DPkg::Lock::Timeout=60", "ufw"])
|
||||||
else:
|
else:
|
||||||
log.info("UFW already installed.")
|
log.info("UFW already installed.")
|
||||||
from engine.lib.ufw import resolve_ufw_docker_bin
|
from engine.lib.ufw import resolve_ufw_docker_bin
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ def run(ctx, args):
|
|||||||
docker_dir = os.path.join(cfg, "docker")
|
docker_dir = os.path.join(cfg, "docker")
|
||||||
host_dir = os.path.join(cfg, "host")
|
host_dir = os.path.join(cfg, "host")
|
||||||
ctx.log.info("Configuring UFW rules...")
|
ctx.log.info("Configuring UFW rules...")
|
||||||
# Mirrors the bash branch structure: host dir is passed unconditionally (a missing
|
has_docker = os.path.isdir(docker_dir)
|
||||||
# one exits 66 inside apply); both dirs missing = warn + success, like bash.
|
has_host = os.path.isdir(host_dir)
|
||||||
if os.path.isdir(docker_dir):
|
if not has_docker:
|
||||||
ctx.ufw.apply(docker_dir, host_dir, dry_run=args.dry_run)
|
|
||||||
else:
|
|
||||||
ctx.log.warn(f"Docker rules dir not found at {docker_dir}. Skipping ufw-docker rules.")
|
ctx.log.warn(f"Docker rules dir not found at {docker_dir}. Skipping ufw-docker rules.")
|
||||||
if os.path.isdir(host_dir):
|
if not has_host:
|
||||||
ctx.ufw.apply(docker_dir, host_dir, dry_run=args.dry_run)
|
ctx.log.warn(f"Host rules dir not found at {host_dir}. Skipping host rules.")
|
||||||
|
if has_docker or has_host:
|
||||||
|
ctx.ufw.apply(docker_dir if has_docker else None, host_dir if has_host else None, dry_run=args.dry_run)
|
||||||
ctx.log.success("Configured ufw.")
|
ctx.log.success("Configured ufw.")
|
||||||
+17
-14
@@ -45,14 +45,16 @@ class Ufw:
|
|||||||
anchor="# End required lines", old_pairs=OLD_PAIRS,
|
anchor="# End required lines", old_pairs=OLD_PAIRS,
|
||||||
dry_run=dry_run, sudo=True)
|
dry_run=dry_run, sudo=True)
|
||||||
|
|
||||||
def apply(self, rules_dir: str, config_dir=None, dry_run: bool = False) -> None:
|
def apply(self, rules_dir: str | None = None, config_dir: str | None = None, dry_run: bool = False) -> None:
|
||||||
# EX_NOINPUT guards first, tools second — same order/codes as root_template_ufw.
|
if rules_dir and not os.path.isdir(rules_dir):
|
||||||
if not os.path.isdir(rules_dir):
|
|
||||||
self.log.error(f"Rules directory does not exist: {rules_dir}")
|
self.log.error(f"Rules directory does not exist: {rules_dir}")
|
||||||
raise SystemExit(66)
|
raise SystemExit(66)
|
||||||
if config_dir and not os.path.isdir(config_dir):
|
if config_dir and not os.path.isdir(config_dir):
|
||||||
self.log.error(f"Config directory does not exist: {config_dir}")
|
self.log.error(f"Config directory does not exist: {config_dir}")
|
||||||
raise SystemExit(66)
|
raise SystemExit(66)
|
||||||
|
if not rules_dir and not config_dir:
|
||||||
|
self.log.warn("Neither docker nor host ufw rules directory found; nothing to apply.")
|
||||||
|
return
|
||||||
if not self.sys.ok(["sudo", "ufw", "--version"]):
|
if not self.sys.ok(["sudo", "ufw", "--version"]):
|
||||||
self.log.error("ufw not installed/working via sudo. Run configure/base first.")
|
self.log.error("ufw not installed/working via sudo. Run configure/base first.")
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
@@ -72,17 +74,18 @@ class Ufw:
|
|||||||
self._inject_file("/etc/ufw/before.rules", os.path.join(config_dir, "before.rules"), dry_run)
|
self._inject_file("/etc/ufw/before.rules", os.path.join(config_dir, "before.rules"), dry_run)
|
||||||
self._inject_file("/etc/ufw/after.rules", os.path.join(config_dir, "after.rules"), dry_run)
|
self._inject_file("/etc/ufw/after.rules", os.path.join(config_dir, "after.rules"), dry_run)
|
||||||
|
|
||||||
rule_files = [fn for fn in sorted(os.listdir(rules_dir))
|
if rules_dir:
|
||||||
if fn.endswith(".rules") and not fn.startswith(".")]
|
rule_files = [fn for fn in sorted(os.listdir(rules_dir))
|
||||||
if not rule_files:
|
if fn.endswith(".rules") and not fn.startswith(".")]
|
||||||
self.log.warn(f"No .rules files found in {rules_dir}, nothing to apply.")
|
if not rule_files:
|
||||||
for fn in rule_files:
|
self.log.warn(f"No .rules files found in {rules_dir}, nothing to apply.")
|
||||||
for rule in clean_rules(open(os.path.join(rules_dir, fn)).read()):
|
for fn in rule_files:
|
||||||
self.log.info(f"Applying ufw-docker rule ({fn}): {rule}")
|
for rule in clean_rules(open(os.path.join(rules_dir, fn)).read()):
|
||||||
if dry_run:
|
self.log.info(f"Applying ufw-docker rule ({fn}): {rule}")
|
||||||
self.log.info(f"[DRY-RUN] sudo {UFW_DOCKER_BIN} {rule}")
|
if dry_run:
|
||||||
else:
|
self.log.info(f"[DRY-RUN] sudo {UFW_DOCKER_BIN} {rule}")
|
||||||
self.sys.run(["sudo", UFW_DOCKER_BIN, *rule.split()], check=True)
|
else:
|
||||||
|
self.sys.run(["sudo", UFW_DOCKER_BIN, *rule.split()], check=True)
|
||||||
|
|
||||||
self.log.success("All ufw rules applied.")
|
self.log.success("All ufw rules applied.")
|
||||||
self.log.warn("Run `sudo systemctl restart ufw` to apply. Ensure ssh is allowed via plain ufw!")
|
self.log.warn("Run `sudo systemctl restart ufw` to apply. Ensure ssh is allowed via plain ufw!")
|
||||||
Reference in new issue
Block a user