summaryrefslogtreecommitdiff
path: root/src/op_mode/image_manager.py
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-10-31 12:53:52 -0500
committerJohn Estabrook <jestabro@vyos.io>2023-12-16 20:37:10 -0600
commite812d494d16a29783254fb1edc0894e054184dcc (patch)
tree464b18fc9276fea7c835c4186697679528c3b4cc /src/op_mode/image_manager.py
parent5949df2ffe1a4bb19171f2bc2bfd3a1026101aee (diff)
downloadvyos-1x-e812d494d16a29783254fb1edc0894e054184dcc.tar.gz
vyos-1x-e812d494d16a29783254fb1edc0894e054184dcc.zip
image: T4516: restore select entry to set/delete image
(cherry picked from commit 9ffa3e82d951756696367578dd5e82ef0f690065)
Diffstat (limited to 'src/op_mode/image_manager.py')
-rwxr-xr-xsrc/op_mode/image_manager.py42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/op_mode/image_manager.py b/src/op_mode/image_manager.py
index 55fd5c07d..de53c4cf0 100755
--- a/src/op_mode/image_manager.py
+++ b/src/op_mode/image_manager.py
@@ -21,23 +21,39 @@ from argparse import ArgumentParser, Namespace
from pathlib import Path
from shutil import rmtree
from sys import exit
+from typing import Optional
from vyos.system import disk, grub, image, compat
-from vyos.utils.io import ask_yes_no
+from vyos.utils.io import ask_yes_no, select_entry
+
+SET_IMAGE_LIST_MSG: str = 'The following images are available:'
+SET_IMAGE_PROMPT_MSG: str = 'Select an image to set as default:'
+DELETE_IMAGE_LIST_MSG: str = 'The following images are installed:'
+DELETE_IMAGE_PROMPT_MSG: str = 'Select an image to delete:'
+MSG_DELETE_IMAGE_RUNNING: str = 'Currently running image cannot be deleted; reboot into another image first'
+MSG_DELETE_IMAGE_DEFAULT: str = 'Default image cannot be deleted; set another image as default first'
@compat.grub_cfg_update
-def delete_image(image_name: str) -> None:
+def delete_image(image_name: Optional[str] = None,
+ prompt: bool = True) -> None:
"""Remove installed image files and boot entry
Args:
image_name (str): a name of image to delete
"""
+ available_images: list[str] = grub.version_list()
+ if image_name is None:
+ if not prompt:
+ exit('An image name is required for delete action')
+ else:
+ image_name = select_entry(available_images,
+ DELETE_IMAGE_LIST_MSG,
+ DELETE_IMAGE_PROMPT_MSG)
if image_name == image.get_running_image():
- exit('Currently running image cannot be deleted')
+ exit(MSG_DELETE_IMAGE_RUNNING)
if image_name == image.get_default_image():
- exit('Default image cannot be deleted')
- available_images: list[str] = grub.version_list()
+ exit(MSG_DELETE_IMAGE_DEFAULT)
if image_name not in available_images:
exit(f'The image "{image_name}" cannot be found')
presistence_storage: str = disk.find_persistence()
@@ -59,15 +75,23 @@ def delete_image(image_name: str) -> None:
@compat.grub_cfg_update
-def set_image(image_name: str) -> None:
+def set_image(image_name: Optional[str] = None,
+ prompt: bool = True) -> None:
"""Set default boot image
Args:
image_name (str): an image name
"""
+ available_images: list[str] = grub.version_list()
+ if image_name is None:
+ if not prompt:
+ exit('An image name is required for set action')
+ else:
+ image_name = select_entry(available_images,
+ SET_IMAGE_LIST_MSG,
+ SET_IMAGE_PROMPT_MSG)
if image_name == image.get_default_image():
exit(f'The image "{image_name}" already configured as default')
- available_images: list[str] = grub.version_list()
if image_name not in available_images:
exit(f'The image "{image_name}" cannot be found')
presistence_storage: str = disk.find_persistence()
@@ -154,10 +178,6 @@ def parse_arguments() -> Namespace:
parser.add_argument('--image_new_name', help='a new name for image')
args: Namespace = parser.parse_args()
# Validate arguments
- if args.action == 'delete' and not args.image_name:
- exit('An image name is required for delete action')
- if args.action == 'set' and not args.image_name:
- exit('An image name is required for set action')
if args.action == 'rename' and (not args.image_name or
not args.image_new_name):
exit('Both old and new image names are required for rename action')