-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpersistence_utils.py
More file actions
118 lines (94 loc) · 3.37 KB
/
Copy pathpersistence_utils.py
File metadata and controls
118 lines (94 loc) · 3.37 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Persistence utilities for DevBox applications.
Provides configurable persistent storage setup to eliminate code duplication.
"""
import os
import shutil
import sys
def setup_persistence(
items_to_persist, persistent_storage_dir="/data/.config_persistence"
):
"""
Set up persistent dotfiles and configs using symbolic links.
Args:
items_to_persist (list): List of files/directories to persist
persistent_storage_dir (str): Base directory for persistent storage
"""
# --- Set up persistent dotfiles and desktop configs using symbolic links ---
print("Linking persistent configuration files...", file=sys.stderr)
os.makedirs(persistent_storage_dir, exist_ok=True)
for item in items_to_persist:
# Determine paths
volume_path = os.path.join(persistent_storage_dir, item)
home_path = os.path.join("/root", item)
# Create parent directory in volume if needed
if os.path.dirname(volume_path):
os.makedirs(os.path.dirname(volume_path), exist_ok=True)
# Ensure the target exists before creating symlink
if not os.path.exists(volume_path):
if os.path.isdir(home_path):
os.makedirs(volume_path, exist_ok=True)
else:
# Create empty file at target location
open(volume_path, "a").close()
# If a default file/dir exists at destination, remove it to allow symlink
if os.path.lexists(home_path):
if os.path.isdir(home_path) and not os.path.islink(home_path):
shutil.rmtree(home_path)
else:
os.remove(home_path)
# Ensure parent directory exists at home_path before symlinking
os.makedirs(os.path.dirname(home_path), exist_ok=True)
# Create symbolic link from home directory to persistent volume
os.symlink(volume_path, home_path)
print(f" - Linked {home_path} -> {volume_path}", file=sys.stderr)
print("...done linking files.", file=sys.stderr)
# Standard persistence configurations for different DevBox types
SSH_PERSISTENCE_ITEMS = [
".bash_history",
".bashrc",
".profile",
".viminfo",
".vimrc",
".gitconfig",
".ssh/config",
".ssh/known_hosts",
]
RDP_PERSISTENCE_ITEMS = SSH_PERSISTENCE_ITEMS + [
# Desktop-specific configs
".config/xfce4",
".local/share/xfce4",
".cache/sessions",
"Desktop",
".xsession",
]
GEMINI_PERSISTENCE_ITEMS = SSH_PERSISTENCE_ITEMS + [
# Gemini-specific configs
".config/gemini"
]
LLM_PERSISTENCE_ITEMS = SSH_PERSISTENCE_ITEMS + [
# LLM playroom specific configs
".config/llm",
".models",
]
UNSLOTH_PERSISTENCE_ITEMS = SSH_PERSISTENCE_ITEMS + [
# Unsloth specific configs
".config/unsloth",
".models/unsloth",
]
def get_persistence_items(devbox_type="ssh"):
"""
Get standard persistence items for different DevBox types.
Args:
devbox_type (str): Type of DevBox ('ssh', 'rdp', 'gemini', 'llm', 'unsloth')
Returns:
list: Items to persist for the specified DevBox type
"""
persistence_configs = {
"ssh": SSH_PERSISTENCE_ITEMS,
"rdp": RDP_PERSISTENCE_ITEMS,
"gemini": GEMINI_PERSISTENCE_ITEMS,
"llm": LLM_PERSISTENCE_ITEMS,
"unsloth": UNSLOTH_PERSISTENCE_ITEMS,
}
return persistence_configs.get(devbox_type, SSH_PERSISTENCE_ITEMS)