Skip to content

Commit

Permalink
resolve bug in status check
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Sep 4, 2023
1 parent 51e8557 commit 02b82bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
42 changes: 13 additions & 29 deletions lib/charms/mongodb/v0/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Simple functions, which can be used in both K8s and VM charms."""
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
import json
import logging
import os
import re
Expand All @@ -27,7 +28,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 7
LIBPATCH = 8


# path to store mongodb ketFile
Expand Down Expand Up @@ -223,43 +224,26 @@ def process_pbm_error(error_string: Optional[_StrOrBytes]) -> str:

def current_pbm_op(pbm_status: str) -> str:
"""Parses pbm status for the operation that pbm is running."""
pbm_status_lines = pbm_status.splitlines()
for i in range(0, len(pbm_status_lines)):
line = pbm_status_lines[i]

# operation is two lines after the line "Currently running:"
if line == "Currently running:":
return pbm_status_lines[i + 2]

return ""
pbm_status = json.loads(pbm_status)
return pbm_status["running"] if "running" in pbm_status else ""


def process_pbm_status(pbm_status: str) -> StatusBase:
"""Parses current pbm operation and returns unit status."""
if type(pbm_status) == bytes:
pbm_status = pbm_status.decode("utf-8")

# pbm is running resync operation
if "Resync" in current_pbm_op(pbm_status):
return WaitingStatus("waiting to sync s3 configurations.")

current_op = current_pbm_op(pbm_status)
# no operations are currently running with pbm
if "(none)" in current_pbm_op(pbm_status):
if current_op == {}:
return ActiveStatus("")

# Example of backup id: 2023-08-21T13:08:22Z
backup_match = re.search(
r'Snapshot backup "(?P<backup_id>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)"', pbm_status
)
if backup_match:
backup_id = backup_match.group("backup_id")
if current_op["type"] == "backup":
backup_id = current_op["name"]
return MaintenanceStatus(f"backup started/running, backup id:'{backup_id}'")

restore_match = re.search(
r'Snapshot restore "(?P<backup_id>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)"', pbm_status
)
if restore_match:
backup_id = restore_match.group("backup_id")
if current_op["type"] == "restore":
backup_id = current_op["name"]
return MaintenanceStatus(f"restore started/running, backup id:'{backup_id}'")

if current_op["type"] == "resync":
return WaitingStatus("waiting to sync s3 configurations.")

return ActiveStatus()
2 changes: 1 addition & 1 deletion lib/charms/mongodb/v0/mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1
LIBPATCH = 2

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit 02b82bf

Please sign in to comment.