Skip to content

Commit

Permalink
activate_changes: pass down the active_config.debug attribute when cr…
Browse files Browse the repository at this point in the history
…eating a snapshot process

Change-Id: I8aaf3394184cb60556d3350a92f8e5dc45a5dfde
  • Loading branch information
TribeGav committed Jan 20, 2025
1 parent fbe34a4 commit 9c14e98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
5 changes: 4 additions & 1 deletion cmk/gui/wato/pages/activate_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def _get_snapshots() -> list[str]:

def _get_last_wato_snapshot_file():
for snapshot_file in _get_snapshots():
status = backup_snapshots.get_snapshot_status(snapshot_file)
status = backup_snapshots.get_snapshot_status(
snapshot=snapshot_file,
debug=active_config.debug,
)
if status["type"] == "automatic" and not status["broken"]:
return snapshot_file
return None
Expand Down
1 change: 1 addition & 0 deletions cmk/gui/watolib/activate_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,7 @@ def _create_snapshots(
backup_snapshots.snapshot_secret(),
active_config.wato_max_snapshots,
active_config.wato_use_git,
active_config.debug,
trace.get_current_span().get_span_context(),
),
)
Expand Down
14 changes: 8 additions & 6 deletions cmk/gui/watolib/backup_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import cmk.utils.paths
from cmk.utils.user import UserId

from cmk.gui.config import active_config
from cmk.gui.i18n import _
from cmk.gui.log import logger
from cmk.gui.watolib.audit_log import log_audit
Expand Down Expand Up @@ -87,12 +86,13 @@ def create_snapshot_subprocess(
secret: bytes,
max_snapshots: int,
use_git: bool,
debug: bool,
parent_span_context: trace.SpanContext,
) -> None:
"""Entry point of the backup subprocess"""
context = trace.set_span_in_context(trace.NonRecordingSpan(parent_span_context))
with trace.get_tracer().start_as_current_span("create_backup_snapshot", context):
create_snapshot(comment, created_by, secret, max_snapshots, use_git)
create_snapshot(comment, created_by, secret, max_snapshots, use_git, debug)


def create_snapshot(
Expand All @@ -101,6 +101,7 @@ def create_snapshot(
secret: bytes,
max_snapshots: int,
use_git: bool,
debug: bool,
) -> None:
logger.debug("Start creating backup snapshot")
start = time.time()
Expand All @@ -122,7 +123,7 @@ def create_snapshot(
data["snapshot_name"] = snapshot_name

_do_create_snapshot(data, secret)
_do_snapshot_maintenance(max_snapshots)
_do_snapshot_maintenance(max_snapshots, debug)

log_audit(
"snapshot-created",
Expand Down Expand Up @@ -235,11 +236,11 @@ def get_basic_tarinfo(name: str) -> tarfile.TarInfo:
shutil.rmtree(work_dir)


def _do_snapshot_maintenance(max_snapshots: int) -> None:
def _do_snapshot_maintenance(max_snapshots: int, debug: bool) -> None:
snapshots = []
for f in os.listdir(snapshot_dir):
if f.startswith("wato-snapshot-"):
status = get_snapshot_status(f, check_correct_core=False)
status = get_snapshot_status(f, debug, check_correct_core=False)
# only remove automatic and legacy snapshots
if status.get("type") in ["automatic", "legacy"]:
snapshots.append(f)
Expand All @@ -254,6 +255,7 @@ def _do_snapshot_maintenance(max_snapshots: int) -> None:
# Returns status information for snapshots or snapshots in progress
def get_snapshot_status( # pylint: disable=too-many-branches
snapshot: str,
debug: bool,
validate_checksums: bool = False,
check_correct_core: bool = True,
) -> SnapshotStatus:
Expand Down Expand Up @@ -446,7 +448,7 @@ def handler(x: str | IO[bytes], filename: str = filename) -> bytes:
check_checksums()

except Exception as e:
if active_config.debug:
if debug:
status["broken_text"] = traceback.format_exc()
status["broken"] = True
else:
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/cmk/gui/watolib/test_backup_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def _snapshot_files() -> Generator[pathlib.Path, None, None]:
@pytest.mark.usefixtures("patch_omd_site")
def test_create_snapshot() -> None:
backup_snapshots.create_snapshot(
comment="", created_by="", secret=b"abc", max_snapshots=10, use_git=False
comment="",
created_by="",
secret=b"abc",
max_snapshots=10,
use_git=False,
debug=False,
)
assert list(_snapshot_files())

Expand All @@ -34,8 +39,12 @@ def test_snapshot_status() -> None:
secret=b"abc",
max_snapshots=10,
use_git=False,
debug=False,
)
snapshot_status = backup_snapshots.get_snapshot_status(
snapshot=next(_snapshot_files()).name,
debug=False,
)
snapshot_status = backup_snapshots.get_snapshot_status(next(_snapshot_files()).name)
assert "test snapshot" in snapshot_status["comment"]
assert not snapshot_status["broken"]
assert "broken_text" not in snapshot_status
Expand All @@ -44,7 +53,12 @@ def test_snapshot_status() -> None:
@pytest.mark.usefixtures("patch_omd_site")
def test_extract_snapshot() -> None:
backup_snapshots.create_snapshot(
"", created_by=UserId("harry"), secret=b"abc", max_snapshots=10, use_git=False
"",
created_by=UserId("harry"),
secret=b"abc",
max_snapshots=10,
use_git=False,
debug=False,
)
with tarfile.open(next(_snapshot_files()), mode="r") as snapshot_tar:
backup_snapshots.extract_snapshot(
Expand Down

0 comments on commit 9c14e98

Please sign in to comment.