-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdelete.py
More file actions
99 lines (81 loc) · 3.29 KB
/
Copy pathdelete.py
File metadata and controls
99 lines (81 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Delete command implementation."""
import sys
from pathlib import Path
import click
from .formats import parse_file
from .k8s import delete_resource, exec_in_pod, patch_resource
from .resources import compute_hash, detect_content_type, resource_name
from .swap import delete_swap_file, read_swap_file
from .sync import sync_local_mounts
def delete_notebook(
file_path: str,
namespace: str | None = None,
force: bool = False,
no_sync: bool = False,
delete_pvc: bool = False,
) -> None:
"""Delete notebook deployment from cluster."""
path = Path(file_path)
if not path.exists():
click.echo(f"Error: File '{file_path}' not found", err=True)
sys.exit(1)
# Parse file to get resource name
content, frontmatter = parse_file(file_path)
name = resource_name(file_path, frontmatter)
# Read swap file for sync
meta = read_swap_file(file_path)
# Use namespace from swap file if not specified
# If no swap file, leave as None to let kubectl use context namespace
if namespace is None and meta:
namespace = meta.namespace
# Sync before delete (unless --no-sync)
if not no_sync and meta is not None:
click.echo("Syncing changes from pod before delete...")
# Check for local modifications
if not force:
current_hash = compute_hash(path.read_text())
if current_hash != meta.file_hash:
click.echo(f"Warning: Local file '{file_path}' modified since deploy.")
if not click.confirm("Overwrite with pod content?"):
click.echo(
"Delete cancelled. Use --no-sync to delete without syncing."
)
return
# Determine notebook filename in pod
content_type = detect_content_type(content or "")
if content_type == "markdown":
notebook_file = "notebook.md"
else:
notebook_file = "notebook.py"
# Try to pull content from pod
success, pod_content = exec_in_pod(
meta.name,
namespace,
f"cat /home/marimo/notebooks/{notebook_file}",
)
if not success:
click.echo(f"Warning: Could not sync from pod: {pod_content}", err=True)
click.echo("Continuing with delete...")
else:
path.write_text(pod_content)
click.echo(f"Synced content to {file_path}")
# Also sync local mounts if present
if meta.local_mounts:
sync_local_mounts(meta.name, namespace, meta.local_mounts)
# By default, preserve PVC by removing owner references before delete
# With --delete-pvc, skip patching so PVC is garbage collected
if not delete_pvc:
pvc_name = f"{name}-pvc"
patch_json = '{"metadata":{"ownerReferences":null}}'
if not patch_resource("pvc", pvc_name, namespace, patch_json):
click.echo(
"Warning: Could not patch PVC to remove owner references. "
"PVC may be deleted with the notebook.",
err=True,
)
# Delete the MarimoNotebook resource
if not delete_resource("marimos.marimo.io", name, namespace):
sys.exit(1)
# Remove swap file
delete_swap_file(file_path)
click.echo(f"Deleted {namespace}/{name}")