Skip to content

Commit

Permalink
Merge pull request #126 from DSD-DBS/fix-capella-5-backup
Browse files Browse the repository at this point in the history
Use different success message for Capella 5.0.x
  • Loading branch information
MoritzWeber0 authored Apr 17, 2023
2 parents 9487514 + 04d692f commit 5408b6a
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions backups/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")


Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5408b6a

Please sign in to comment.