Add option to pass any userdata
This commit is contained in:
parent
8dfba55e07
commit
9ab0a7833d
3 changed files with 49 additions and 11 deletions
|
|
@ -21,12 +21,14 @@ cleanup_algorithm = number
|
|||
#post_description = pacman post snapshot
|
||||
|
||||
# Uncomment to add "important=yes" to userdata for snapshots referring to these packages
|
||||
#important_packages = ["linux"]
|
||||
#important_packages = ["linux", "linux-lts]
|
||||
|
||||
# Uncomment to add "important=yes" to userdata for snapshots that were created with the
|
||||
#following commands
|
||||
# Uncomment to add "important=yes" to userdata for snapshots that were created with the following commands
|
||||
#important_commands = ["pacman -Syu"]
|
||||
|
||||
# Add custom userdata. Each key-value pair should be an item in the list
|
||||
#userdata = ["key=value","foo=bar"]
|
||||
|
||||
# Example for another snapper configuration named "home"
|
||||
# [home]
|
||||
# snapshot = True
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ logging.basicConfig(format="%(message)s", level=logging.INFO)
|
|||
class SnapperCmd:
|
||||
|
||||
def __init__(self, config, snapshot_type, cleanup_algorithm, description="",
|
||||
nodbus=False, pre_number=None, important=False):
|
||||
nodbus=False, pre_number=None, userdata=""):
|
||||
self.cmd = ["snapper"]
|
||||
if nodbus:
|
||||
self.cmd.append("--no-dbus")
|
||||
|
|
@ -42,8 +42,8 @@ class SnapperCmd:
|
|||
self.cmd.append("--print-number")
|
||||
if description:
|
||||
self.cmd.append(f"--description \"{description}\"")
|
||||
if important:
|
||||
self.cmd.append("--userdata \"important=yes\"")
|
||||
if userdata:
|
||||
self.cmd.append(f"--userdata \"{userdata}\"")
|
||||
if snapshot_type == "post":
|
||||
if pre_number is not None:
|
||||
self.cmd.append(f"--pre-number {pre_number}")
|
||||
|
|
@ -76,7 +76,8 @@ def setup_config_parser(ini_file, parent_cmd, packages):
|
|||
"post_description": " ".join(packages),
|
||||
"desc_limit": 72,
|
||||
"important_packages": [],
|
||||
"important_commands": []
|
||||
"important_commands": [],
|
||||
"userdata": set()
|
||||
}
|
||||
config["root"] = {
|
||||
"snapshot": True
|
||||
|
|
@ -116,6 +117,13 @@ def check_important_packages(config, snapper_config, packages):
|
|||
return any(x in important_packages for x in packages)
|
||||
|
||||
|
||||
def get_userdata(config, snapper_config, important):
|
||||
userdata = set(json.loads(config.get(snapper_config, "userdata")))
|
||||
if important:
|
||||
userdata.add("important=yes")
|
||||
return ",".join(sorted(list(userdata)))
|
||||
|
||||
|
||||
def main(snap_pac_ini, snapper_conf_file, args):
|
||||
|
||||
if os.getenv("SNAP_PAC_SKIP", "n").lower() in ["y", "yes", "true", "1"]:
|
||||
|
|
@ -142,8 +150,10 @@ def main(snap_pac_ini, snapper_conf_file, args):
|
|||
important = (check_important_commands(config, snapper_config, parent_cmd) or
|
||||
check_important_packages(config, snapper_config, packages))
|
||||
|
||||
userdata = get_userdata(config, snapper_config, important)
|
||||
|
||||
snapper_cmd = SnapperCmd(snapper_config, args.type, cleanup_algorithm,
|
||||
description, chroot, pre_number, important)
|
||||
description, chroot, pre_number, userdata)
|
||||
num = snapper_cmd()
|
||||
logging.info(f"==> {snapper_config}: {num}")
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import pytest
|
|||
|
||||
from scripts.snap_pac import (
|
||||
SnapperCmd, check_important_commands, check_important_packages, get_pre_number, get_snapper_configs,
|
||||
main, setup_config_parser, get_description
|
||||
get_userdata, main, setup_config_parser, get_description
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -21,7 +21,8 @@ def config():
|
|||
"post_description": "bar",
|
||||
"desc_limit": 72,
|
||||
"important_packages": [],
|
||||
"important_commands": []
|
||||
"important_commands": [],
|
||||
"userdata": set()
|
||||
}
|
||||
config["root"] = {
|
||||
"snapshot": True
|
||||
|
|
@ -58,9 +59,14 @@ def prefile():
|
|||
" --description \"bar\" --pre-number 1234"
|
||||
),
|
||||
(
|
||||
SnapperCmd("root", "post", "number", "bar", False, 1234, True),
|
||||
SnapperCmd("root", "post", "number", "bar", False, 1234, "important=yes"),
|
||||
"snapper --config root create --type post --cleanup-algorithm number --print-number"
|
||||
" --description \"bar\" --userdata \"important=yes\" --pre-number 1234"
|
||||
),
|
||||
(
|
||||
SnapperCmd("root", "post", "number", "bar", False, 1234, "foo=bar,important=yes"),
|
||||
"snapper --config root create --type post --cleanup-algorithm number --print-number"
|
||||
" --description \"bar\" --userdata \"foo=bar,important=yes\" --pre-number 1234"
|
||||
)
|
||||
])
|
||||
def test_snapper_cmd(snapper_cmd, actual_cmd):
|
||||
|
|
@ -133,3 +139,23 @@ def test_important_packages():
|
|||
config = setup_config_parser(name, "pacman -S", packages)
|
||||
important = check_important_packages(config, "root", packages)
|
||||
assert important
|
||||
|
||||
|
||||
def test_load_userdata():
|
||||
with tempfile.NamedTemporaryFile("w", delete=False) as f:
|
||||
f.write("[DEFAULT]\n")
|
||||
f.write("userdata = [\"foo=bar\", \"requestid=42\"]\n")
|
||||
name = f.name
|
||||
config = setup_config_parser(name, "pacman -Syu", ["bar"])
|
||||
userdata = get_userdata(config, "root", False)
|
||||
assert userdata == "foo=bar,requestid=42"
|
||||
|
||||
|
||||
def test_load_userdata_and_important():
|
||||
with tempfile.NamedTemporaryFile("w", delete=False) as f:
|
||||
f.write("[DEFAULT]\n")
|
||||
f.write("userdata = [\"foo=bar\", \"requestid=42\"]\n")
|
||||
name = f.name
|
||||
config = setup_config_parser(name, "pacman -Syu", ["bar"])
|
||||
userdata = get_userdata(config, "root", True)
|
||||
assert userdata == "foo=bar,important=yes,requestid=42"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue