diff --git a/backups/backup.py b/backups/backup.py index 9b4626fe..9ccd7d22 100644 --- a/backups/backup.py +++ b/backups/backup.py @@ -74,15 +74,17 @@ def run_importer_script() -> None: http_port, ] + stderr = None stdout = "" - try: - popen = subprocess.Popen( - command, cwd="/opt/capella", stdout=subprocess.PIPE, text=True - ) + with subprocess.Popen( + command, cwd="/opt/capella", stdout=subprocess.PIPE, text=True + ) as popen: assert popen.stdout for line in popen.stdout: stdout += line - print(line, end="") + + # Flush is needed, otherwise the logs are delayed + print(line, end="", flush=True) # Failed imports still have exit code 0. # In addition, the process hangs up when these log lines appear sometimes. @@ -103,22 +105,32 @@ def run_importer_script() -> None: raise RuntimeError( f"{ERROR_PREFIX} - Failed to copy to output folder ({OUTPUT_FOLDER})" ) - finally: - if popen: - popen.terminate() - if (return_code := popen.returncode) != 0: - raise subprocess.CalledProcessError(return_code, command) + if popen.stderr: + stderr = "" + stderr += popen.stderr.read() - if not re.search(r"[1-9][0-9]* projects imports succeeded", stdout): - raise RuntimeError( - f"{ERROR_PREFIX} - '[1-9][0-9]* projects imports succeeded' not found in logs" - ) - if not re.search(r"[1-9][0-9]* copies succeeded", stdout): + if (return_code := popen.returncode) != 0: + log.exception("Command failed with stderr: '%s'", stderr) raise RuntimeError( - f"{ERROR_PREFIX} - '[1-9][0-9]* copies succeeded' not found in logs" + f"Capella importer failed with exit code {return_code}" ) + if is_capella_5_0_x(): + if not re.search(r"!MESSAGE [1-9][0-9]* Succeeded", stdout): + raise RuntimeError( + f"{ERROR_PREFIX} - '!MESSAGE [1-9][0-9]* Succeeded' not found in logs" + ) + else: + if not re.search(r"[1-9][0-9]* projects imports succeeded", stdout): + raise RuntimeError( + f"{ERROR_PREFIX} - '[1-9][0-9]* projects imports succeeded' not found in logs" + ) + if not re.search(r"[1-9][0-9]* copies succeeded", stdout): + raise RuntimeError( + f"{ERROR_PREFIX} - '[1-9][0-9]* copies succeeded' not found in logs" + ) + log.info("Import of model from TeamForCapella server finished") @@ -289,6 +301,10 @@ def is_capella_5_x_x() -> bool: return bool(re.match(r"5.[0-9]+.[0-9]+", os.getenv("CAPELLA_VERSION", ""))) +def is_capella_5_0_x() -> bool: + return bool(re.match(r"5.0.[0-9]+", os.getenv("CAPELLA_VERSION", ""))) + + def get_http_envs() -> tuple[str, str, str]: http_login = os.getenv("HTTP_LOGIN") http_password = os.getenv("HTTP_PASSWORD")