From 481cb47bd005b04098aff6b576a5a757a4a0e05d Mon Sep 17 00:00:00 2001 From: Wes Barnett Date: Sun, 14 Feb 2021 15:40:43 -0500 Subject: [PATCH] Updates, refactoring, logging --- scripts/snap-pac | 109 +++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/scripts/snap-pac b/scripts/snap-pac index 5b6af1a..930d31a 100755 --- a/scripts/snap-pac +++ b/scripts/snap-pac @@ -1,48 +1,46 @@ #!/usr/bin/env python +# Copyright (C) 2021 Wes Barnett +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ -Copyright (C) 2021 Wes Barnett - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +Script for taking pre/post snapshots; run from pacman hooks. """ from argparse import ArgumentParser from configparser import ConfigParser +import logging import os import sys +logging.basicConfig(format="%(message)s", level=logging.INFO) -def create_snapper_cmd(args, config): + +def create_snapper_cmd(args, config, section): chroot = os.stat("/") != os.stat("/proc/1/root/.") snapper_cmd = ["snapper"] if chroot: snapper_cmd.append("--no-dbus") - snapper_cmd.append("--config") - snapper_cmd.append(c) - snapper_cmd.append("create") - snapper_cmd.append("--type") - snapper_cmd.append(args.preorpost) - snapper_cmd.append("--cleanup-algorithm") - snapper_cmd.append(config.get(x, "cleanup_algorithm")) + snapper_cmd.append(f"--config {section} create") + snapper_cmd.append(f"--type {args.preorpost}") + snapper_cmd.append(f"--cleanup-algorithm {config.get(section, 'cleanup_algorithm')}") snapper_cmd.append("--print-number") - snapper_cmd.append("--description") - desc_limit = config.getint(x, "desc_limit") + desc_limit = config.getint(section, "desc_limit") if args.preorpost == "pre": - snapper_cmd.append(config.get(x, "pre_description")[:desc_limit]) + snapper_cmd.append(f"--description {config.get(section, 'pre_description')[:desc_limit]}") else: - snapper_cmd.append(config.get(x, "post_description")[:desc_limit]) + snapper_cmd.append(f"--description {config.get(section, 'post_description')[:desc_limit]}") return snapper_cmd @@ -63,12 +61,11 @@ def do_pre_snapshot(cmd, prefile): def do_post_snapshot(cmd, prefile): """Read pre snapshot number from file, run snapper, delete prefile.""" - cmd.append("--pre-number") with open(prefile, "r") as f: - cmd.append(f.read().rstrip("\n")) - num = os.popen(" ".join(cmd)).read().rstrip("\n") + pre_number = f.read().rstrip("\n") + cmd.append(f"--pre-number {pre_number}") os.remove(prefile) - return num + return os.popen(" ".join(cmd)).read().rstrip("\n") def get_snapper_configs(conf_file): @@ -76,10 +73,8 @@ def get_snapper_configs(conf_file): with open(conf_file, "r") as f: for line in f: if line.startswith("SNAPPER_CONFIGS"): - line = line.rstrip("\n").rstrip("\"") - line = line.split("=") - line = line[1].lstrip("\"") - return line.split() + line = line.rstrip("\n").rstrip("\"").split("=") + return line[1].lstrip("\"").split() def setup_config_parser(ini_file): @@ -102,30 +97,40 @@ def setup_config_parser(ini_file): return config +def main(snap_pac_ini, snapper_conf_file, args): + config = setup_config_parser(snap_pac_ini) + snapper_configs = get_snapper_configs(snapper_conf_file) + + if os.getenv("SNAP_PAC_SKIP", "n") in ["y", "Y", "yes", "Yes", "YES"]: + return + + for c in snapper_configs: + + if c not in config: + config.add_section(c) + + logging.debug(f"{c = }") + if config.getboolean(c, "snapshot"): + prefile = f"/tmp/snap-pac-pre_{c}" + logging.debug(f"{prefile = }") + snapper_cmd = create_snapper_cmd(args, config, c) + logging.debug(f"{snapper_cmd = }") + num = do_snapshot(args.preorpost, snapper_cmd, prefile) + logging.info(f"==> {c}: {num}") + + if __name__ == "__main__": snap_pac_ini = "/etc/snap-pac.ini" + logging.debug(f"{snap_pac_ini = }") snapper_conf_file = "/etc/conf.d/snapper" + logging.debug(f"{snapper_conf_file = }") parser = ArgumentParser() parser.add_argument(dest="preorpost") args = parser.parse_args() - config = setup_config_parser(snap_pac_ini) - snapper_configs = get_snapper_configs(snapper_conf_file) + if args.preorpost not in ["pre", "post"]: + raise ValueError("preorpost must be 'pre' or 'post'") - if os.getenv("SNAP_PAC_SKIP", "n") not in ["y", "Y", "yes", "Yes", "YES"]: - for c in snapper_configs: - - if c in config: - x = c - else: - x = "DEFAULT" - - if config.getboolean(x, "snapshot"): - prefile = f"/tmp/snap-pac-pre_{c}" - snapper_cmd = create_snapper_cmd(args, config) - num = do_snapshot(args.preorpost, snapper_cmd, prefile) - print(f"==> {c}: {num}") - else: - print("SNAP_PAC_SKIP set, so skipping snapshots.") + main(snap_pac_ini, snapper_conf_file, args)