Files
update/software/upgrade-scripts/202-remove-lvm-snapshots.py
Heitor Matsui 65881821dc Create upgrade script to delete LVM snapshots
This commit creates an upgrade script that runs on 'delete'
action only for AIO-SX systems only, and its main goal is to
delete existing LVM snapshots at the end of the deployment
process, in both forward and rollback paths.

Test Plan
PASS: run script in AIO-SX with existing snapshots, verify
      snapshots are deleted and logs are logged as expected
      and script return success
PASS: run script in AIO-SX with no snapshots, verify script
      logs that there are no snapshots to be deleted
PASS: run script in system other than AIO-SX, verify script
      logs 'nothing to do'
PASS: run script with action != 'delete' and verify script
      logs 'nothing to do'

Story: 2011357
Task: 52364

Change-Id: I21fa3c40a43f1b6929ae53a13fde07082df6d321
Signed-off-by: Heitor Matsui <heitorvieira.matsui@windriver.com>
2025-06-18 10:47:51 -03:00

86 lines
2.2 KiB
Python

#!/usr/bin/env python
#
# Copyright (c) 2025 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import configparser
import logging
from pathlib import Path
import sys
import subprocess
from software.utilities.utils import configure_logging
LOG = logging.getLogger('main_logger')
def get_system_mode():
with open("/etc/platform/platform.conf", "r") as fp:
platform_conf = "[DEFAULT]\n" + fp.read()
parser = configparser.ConfigParser()
parser.read_string(platform_conf)
if parser.has_option('DEFAULT', 'system_mode'):
return parser.get('DEFAULT', 'system_mode')
return None
def delete_lvm_snapshots():
script_path = Path("/usr/sbin/software-deploy/manage-lvm-snapshots")
if not script_path.is_file():
raise FileNotFoundError(f"{script_path} not found")
cmd = [script_path, "--delete"]
try:
subprocess.run(cmd, check=True, capture_output=True, text=True)
LOG.info("Snapshots deleted with success")
except subprocess.CalledProcessError as e:
LOG.error("Error deleting snapshots: %s", e.stderr)
raise
def main():
action = None
from_release = None
to_release = None
arg = 1
while arg < len(sys.argv):
if arg == 1:
from_release = sys.argv[arg]
elif arg == 2:
to_release = sys.argv[arg]
elif arg == 3:
action = sys.argv[arg]
elif arg == 4:
pass
else:
print("Invalid option %s." % sys.argv[arg])
return 1
arg += 1
configure_logging()
LOG.info("%s invoked from_release = %s to_release = %s action = %s"
% (sys.argv[0], from_release, to_release, action))
res = 0
if action == "delete":
try:
system_mode = get_system_mode()
if system_mode == "simplex":
delete_lvm_snapshots()
else:
LOG.info("The system_mode is %s, nothing to do", system_mode)
except Exception as e:
LOG.error("Error running script: %s", str(e))
res = 1
else:
LOG.info("Nothing to do for action '%s'", action)
return res
if __name__ == "__main__":
sys.exit(main())