Updates, refactoring, logging
This commit is contained in:
parent
0aff4920b4
commit
481cb47bd0
1 changed files with 57 additions and 52 deletions
109
scripts/snap-pac
109
scripts/snap-pac
|
|
@ -1,48 +1,46 @@
|
||||||
#!/usr/bin/env python
|
#!/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
|
Script for taking pre/post snapshots; run from pacman hooks.
|
||||||
|
|
||||||
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.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
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/.")
|
chroot = os.stat("/") != os.stat("/proc/1/root/.")
|
||||||
snapper_cmd = ["snapper"]
|
snapper_cmd = ["snapper"]
|
||||||
if chroot:
|
if chroot:
|
||||||
snapper_cmd.append("--no-dbus")
|
snapper_cmd.append("--no-dbus")
|
||||||
snapper_cmd.append("--config")
|
snapper_cmd.append(f"--config {section} create")
|
||||||
snapper_cmd.append(c)
|
snapper_cmd.append(f"--type {args.preorpost}")
|
||||||
snapper_cmd.append("create")
|
snapper_cmd.append(f"--cleanup-algorithm {config.get(section, 'cleanup_algorithm')}")
|
||||||
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("--print-number")
|
snapper_cmd.append("--print-number")
|
||||||
snapper_cmd.append("--description")
|
desc_limit = config.getint(section, "desc_limit")
|
||||||
desc_limit = config.getint(x, "desc_limit")
|
|
||||||
if args.preorpost == "pre":
|
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:
|
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
|
return snapper_cmd
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,12 +61,11 @@ def do_pre_snapshot(cmd, prefile):
|
||||||
|
|
||||||
def do_post_snapshot(cmd, prefile):
|
def do_post_snapshot(cmd, prefile):
|
||||||
"""Read pre snapshot number from file, run snapper, delete prefile."""
|
"""Read pre snapshot number from file, run snapper, delete prefile."""
|
||||||
cmd.append("--pre-number")
|
|
||||||
with open(prefile, "r") as f:
|
with open(prefile, "r") as f:
|
||||||
cmd.append(f.read().rstrip("\n"))
|
pre_number = f.read().rstrip("\n")
|
||||||
num = os.popen(" ".join(cmd)).read().rstrip("\n")
|
cmd.append(f"--pre-number {pre_number}")
|
||||||
os.remove(prefile)
|
os.remove(prefile)
|
||||||
return num
|
return os.popen(" ".join(cmd)).read().rstrip("\n")
|
||||||
|
|
||||||
|
|
||||||
def get_snapper_configs(conf_file):
|
def get_snapper_configs(conf_file):
|
||||||
|
|
@ -76,10 +73,8 @@ def get_snapper_configs(conf_file):
|
||||||
with open(conf_file, "r") as f:
|
with open(conf_file, "r") as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
if line.startswith("SNAPPER_CONFIGS"):
|
if line.startswith("SNAPPER_CONFIGS"):
|
||||||
line = line.rstrip("\n").rstrip("\"")
|
line = line.rstrip("\n").rstrip("\"").split("=")
|
||||||
line = line.split("=")
|
return line[1].lstrip("\"").split()
|
||||||
line = line[1].lstrip("\"")
|
|
||||||
return line.split()
|
|
||||||
|
|
||||||
|
|
||||||
def setup_config_parser(ini_file):
|
def setup_config_parser(ini_file):
|
||||||
|
|
@ -102,30 +97,40 @@ def setup_config_parser(ini_file):
|
||||||
return config
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
snap_pac_ini = "/etc/snap-pac.ini"
|
snap_pac_ini = "/etc/snap-pac.ini"
|
||||||
|
logging.debug(f"{snap_pac_ini = }")
|
||||||
snapper_conf_file = "/etc/conf.d/snapper"
|
snapper_conf_file = "/etc/conf.d/snapper"
|
||||||
|
logging.debug(f"{snapper_conf_file = }")
|
||||||
|
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument(dest="preorpost")
|
parser.add_argument(dest="preorpost")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
config = setup_config_parser(snap_pac_ini)
|
if args.preorpost not in ["pre", "post"]:
|
||||||
snapper_configs = get_snapper_configs(snapper_conf_file)
|
raise ValueError("preorpost must be 'pre' or 'post'")
|
||||||
|
|
||||||
if os.getenv("SNAP_PAC_SKIP", "n") not in ["y", "Y", "yes", "Yes", "YES"]:
|
main(snap_pac_ini, snapper_conf_file, args)
|
||||||
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.")
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue