From b6cc163bc617c8d41b090268b5f0abc9ac114d9e Mon Sep 17 00:00:00 2001 From: Bin Qian Date: Thu, 15 Sep 2022 14:41:54 +0000 Subject: [PATCH] Log data migration script output when failure Capture script stdout and stderr. Log outputs when script fails. Do not log exception unless script cannot run. Closes-bug: 1989742 TCs: 1. No log for execution succeed 2. script output is logged when script fails 3. exception is logged when script not executed (e.g, file not found, etc) Signed-off-by: Bin Qian Change-Id: I9c5e01e0937db02624f8c4b9bff5ccb2fae73249 --- .../controllerconfig/upgrades/utils.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/controllerconfig/controllerconfig/controllerconfig/upgrades/utils.py b/controllerconfig/controllerconfig/controllerconfig/upgrades/utils.py index 8e594f35a4..5a0ea80380 100644 --- a/controllerconfig/controllerconfig/controllerconfig/upgrades/utils.py +++ b/controllerconfig/controllerconfig/controllerconfig/upgrades/utils.py @@ -59,8 +59,6 @@ def execute_migration_scripts(from_release, to_release, action, controller-1 is performing its upgrade. """ - devnull = open(os.devnull, 'w') - LOG.info("Executing migration scripts with from_release: %s, " "to_release: %s, action: %s" % (from_release, to_release, action)) @@ -85,16 +83,23 @@ def execute_migration_scripts(from_release, to_release, action, migration_script = os.path.join(migration_script_dir, f) try: LOG.info("Executing migration script %s" % migration_script) - subprocess.check_call([migration_script, - from_release, - to_release, - action], - stdout=devnull, stderr=devnull) + subprocess.check_output([migration_script, + from_release, + to_release, + action], + stderr=subprocess.STDOUT, + universal_newlines=True) except subprocess.CalledProcessError as e: - LOG.exception("Migration script %s failed with returncode %d" % - (migration_script, e.returncode)) + # log script output if script executed but failed. + LOG.error("Migration script %s failed with returncode %d" + "Script output: \n%s" % + (migration_script, e.returncode, e.output)) # Abort when a migration script fails - raise e + raise + except Exception: + # log exception if scipt not executed. + LOG.exception() + raise def get_db_connection(hiera_db_records, database):