Skip to content

Commit 13e828d

Browse files
committed
Fixes and refactor
fix: apply implies continue. Warn user when rebasing from older snapshot and other niceties.
1 parent 1deb1c7 commit 13e828d

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

atomic-update.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
VERSION = "0.1.0"
3232
ZYPPER_PID_FILE = "/run/zypp.pid"
3333
VALID_CMD = ["dup", "run", "rollback"]
34-
VALID_OPT = ["--reboot", "--apply", "--shell", "--continue", "--no-verify", "--interactive", "--debug", "--help", "--version"]
34+
VALID_OPT = ["--reboot", "--apply", "--shell", "--continue", "--no-verify", \
35+
"--interactive", "--debug", "--help", "--version"]
3536

3637
# Command help/usage info
3738
help_text = """
@@ -59,27 +60,6 @@
5960

6061
################################
6162

62-
# Function to query user for yes or no
63-
def query_yes_no(question, default=None):
64-
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
65-
if default is None:
66-
prompt = " [y/n]: "
67-
elif default == "yes":
68-
prompt = " [Y/n]: "
69-
elif default == "no":
70-
prompt = " [y/N]: "
71-
else:
72-
raise ValueError(f"Invalid default answer: {default!r}")
73-
while True:
74-
sys.stdout.write(question + prompt)
75-
choice = input().lower()
76-
if default is not None and choice == "":
77-
return valid[default]
78-
elif choice in valid:
79-
return valid[choice]
80-
else:
81-
sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
82-
8363
# Function to get output and exit code of shell command
8464
def shell_exec(command):
8565
res = subprocess.run(command, shell=True, capture_output=True, encoding="utf8", errors="replace")
@@ -287,10 +267,16 @@ def sigint_handler(signum, frame):
287267
active_snap, default_snap = get_snaps(snapper_root_config)
288268
logging.debug(f"Active snapshot number: {active_snap}, Default snapshot number: {default_snap}")
289269
base_snap = active_snap
290-
if CONTINUE:
270+
if CONTINUE or APPLY:
291271
base_snap = default_snap
292272
if continue_num:
293273
base_snap = continue_num
274+
# warn user when rebasing from old snapshot
275+
# thus losing changes to snapshots made in the interim
276+
if not continue_num and base_snap != default_snap:
277+
logging.warn(f"This snapshot is being created from a different base ({base_snap}) " \
278+
f"than the previous default snapshot ({default_snap}) and does not " \
279+
f"contain the changes from the latter.")
294280
# create new read-write snapshot to perform atomic update in
295281
out, ret = shell_exec(f"snapper -c {snapper_root_config} create -c number " \
296282
f"-d 'Atomic update of #{base_snap}' " \
@@ -409,12 +395,15 @@ def sigint_handler(signum, frame):
409395

410396
# Handle command: rollback
411397
elif COMMAND == "rollback":
412-
warn_opts = ["--apply", "--reboot"]
413-
if warn_opts in OPT:
414-
logging.warn(f"Options {', '.join(warn_opts)!r} do not apply to rollback command")
398+
invalid_opts = OPT.copy()
399+
invalid_opts.remove("--debug")
400+
if invalid_opts:
401+
logging.warn(f"Options {', '.join(invalid_opts)!r} do not apply to rollback command")
415402
if rollback_num:
403+
logging.info(f"Rolling back to snapshot {rollback_num}")
416404
os.system(f"snapper rollback {rollback_num}")
417405
else:
406+
logging.info("Rolling back to currently booted snapshot")
418407
os.system("snapper rollback")
419408

420409
# If we're here, remind user to reboot

0 commit comments

Comments
 (0)