-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.5090.sh
More file actions
272 lines (228 loc) · 9.23 KB
/
start.5090.sh
File metadata and controls
272 lines (228 loc) · 9.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
set -e # Exit the script if any statement returns a non-true return value
COMFYUI_DIR="/workspace/runpod-slim/ComfyUI"
VENV_DIR="$COMFYUI_DIR/.venv-cu128"
FILEBROWSER_CONFIG="/root/.config/filebrowser/config.json"
DB_FILE="/workspace/runpod-slim/filebrowser.db"
# ---------------------------------------------------------------------------- #
# Function Definitions #
# ---------------------------------------------------------------------------- #
# Setup SSH with optional key or random password
setup_ssh() {
mkdir -p ~/.ssh
# Generate host keys if they don't exist
for type in rsa dsa ecdsa ed25519; do
if [ ! -f "/etc/ssh/ssh_host_${type}_key" ]; then
ssh-keygen -t ${type} -f "/etc/ssh/ssh_host_${type}_key" -q -N ''
echo "${type^^} key fingerprint:"
ssh-keygen -lf "/etc/ssh/ssh_host_${type}_key.pub"
fi
done
# If PUBLIC_KEY is provided, use it
if [[ $PUBLIC_KEY ]]; then
echo "$PUBLIC_KEY" >> ~/.ssh/authorized_keys
chmod 700 -R ~/.ssh
else
# Generate random password if no public key
RANDOM_PASS=$(openssl rand -base64 12)
echo "root:${RANDOM_PASS}" | chpasswd
echo "Generated random SSH password for root: ${RANDOM_PASS}"
fi
# Configure SSH to preserve environment variables
echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config
# Start SSH service
/usr/sbin/sshd
}
# Export environment variables
export_env_vars() {
echo "Exporting environment variables..."
# Create environment files
ENV_FILE="/etc/environment"
PAM_ENV_FILE="/etc/security/pam_env.conf"
SSH_ENV_DIR="/root/.ssh/environment"
# Backup original files
cp "$ENV_FILE" "${ENV_FILE}.bak" 2>/dev/null || true
cp "$PAM_ENV_FILE" "${PAM_ENV_FILE}.bak" 2>/dev/null || true
# Clear files
> "$ENV_FILE"
> "$PAM_ENV_FILE"
mkdir -p /root/.ssh
> "$SSH_ENV_DIR"
# Export to multiple locations for maximum compatibility
printenv | grep -E '^RUNPOD_|^PATH=|^_=|^CUDA|^LD_LIBRARY_PATH|^PYTHONPATH' | while read -r line; do
# Get variable name and value
name=$(echo "$line" | cut -d= -f1)
value=$(echo "$line" | cut -d= -f2-)
# Add to /etc/environment (system-wide)
echo "$name=\"$value\"" >> "$ENV_FILE"
# Add to PAM environment
echo "$name DEFAULT=\"$value\"" >> "$PAM_ENV_FILE"
# Add to SSH environment file
echo "$name=\"$value\"" >> "$SSH_ENV_DIR"
# Add to current shell
echo "export $name=\"$value\"" >> /etc/rp_environment
done
# Add sourcing to shell startup files
echo 'source /etc/rp_environment' >> ~/.bashrc
echo 'source /etc/rp_environment' >> /etc/bash.bashrc
# Set permissions
chmod 644 "$ENV_FILE" "$PAM_ENV_FILE"
chmod 600 "$SSH_ENV_DIR"
}
# Start Jupyter Lab server for remote access
start_jupyter() {
mkdir -p /workspace
echo "Starting Jupyter Lab on port 8888..."
nohup jupyter lab \
--allow-root \
--no-browser \
--port=8888 \
--ip=0.0.0.0 \
--FileContentsManager.delete_to_trash=False \
--FileContentsManager.preferred_dir=/workspace \
--ServerApp.root_dir=/workspace \
--ServerApp.terminado_settings='{"shell_command":["/bin/bash"]}' \
--IdentityProvider.token="${JUPYTER_PASSWORD:-}" \
--ServerApp.allow_origin=* &> /jupyter.log &
echo "Jupyter Lab started"
}
# ---------------------------------------------------------------------------- #
# Main Program #
# ---------------------------------------------------------------------------- #
# Setup environment
setup_ssh
export_env_vars
# Initialize FileBrowser if not already done
if [ ! -f "$DB_FILE" ]; then
echo "Initializing FileBrowser..."
filebrowser config init
filebrowser config set --address 0.0.0.0
filebrowser config set --port 8080
filebrowser config set --root /workspace
filebrowser config set --auth.method=json
filebrowser users add admin adminadmin12 --perm.admin
else
echo "Using existing FileBrowser configuration..."
fi
# Start FileBrowser
echo "Starting FileBrowser on port 8080..."
nohup filebrowser &> /filebrowser.log &
start_jupyter
# Create default comfyui_args.txt if it doesn't exist
ARGS_FILE="/workspace/runpod-slim/comfyui_args.txt"
if [ ! -f "$ARGS_FILE" ]; then
echo "# Add your custom ComfyUI arguments here (one per line)" > "$ARGS_FILE"
echo "Created empty ComfyUI arguments file at $ARGS_FILE"
fi
# Setup ComfyUI if needed
if [ ! -d "$COMFYUI_DIR" ] || [ ! -d "$VENV_DIR" ]; then
echo "First time setup: Installing ComfyUI and dependencies..."
# Clone ComfyUI if not present
if [ ! -d "$COMFYUI_DIR" ]; then
cd /workspace/runpod-slim
git clone https://github.com/comfyanonymous/ComfyUI.git
fi
# Install ComfyUI-Manager if not present
if [ ! -d "$COMFYUI_DIR/custom_nodes/ComfyUI-Manager" ]; then
echo "Installing ComfyUI-Manager..."
mkdir -p "$COMFYUI_DIR/custom_nodes"
cd "$COMFYUI_DIR/custom_nodes"
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
fi
# Install additional custom nodes
CUSTOM_NODES=(
"https://github.com/kijai/ComfyUI-KJNodes"
"https://github.com/MoonGoblinDev/Civicomfy"
"https://github.com/MadiatorLabs/ComfyUI-RunpodDirect"
)
for repo in "${CUSTOM_NODES[@]}"; do
repo_name=$(basename "$repo")
if [ ! -d "$COMFYUI_DIR/custom_nodes/$repo_name" ]; then
echo "Installing $repo_name..."
cd "$COMFYUI_DIR/custom_nodes"
git clone "$repo"
fi
done
# Create and setup virtual environment if not present
if [ ! -d "$VENV_DIR" ]; then
cd $COMFYUI_DIR
# Create venv with access to system packages (torch cu128, numpy, etc. pre-installed in image)
python3.12 -m venv --system-site-packages $VENV_DIR
source $VENV_DIR/bin/activate
# Ensure pip is available in the venv (needed for ComfyUI-Manager)
python -m ensurepip --upgrade
python -m pip install --upgrade pip
echo "Base packages (torch cu128, numpy, etc.) available from system site-packages"
echo "Installing custom node dependencies..."
# Install dependencies for all custom nodes
cd "$COMFYUI_DIR/custom_nodes"
for node_dir in */; do
if [ -d "$node_dir" ]; then
echo "Checking dependencies for $node_dir..."
cd "$COMFYUI_DIR/custom_nodes/$node_dir"
# Check for requirements.txt
if [ -f "requirements.txt" ]; then
echo "Installing requirements.txt for $node_dir"
pip install --no-cache-dir -r requirements.txt
fi
# Check for install.py
if [ -f "install.py" ]; then
echo "Running install.py for $node_dir"
python install.py
fi
# Check for setup.py
if [ -f "setup.py" ]; then
echo "Running setup.py for $node_dir"
pip install --no-cache-dir -e .
fi
fi
done
fi
else
# Just activate the existing venv
source $VENV_DIR/bin/activate
echo "Checking for custom node dependencies..."
# Install dependencies for all custom nodes
cd "$COMFYUI_DIR/custom_nodes"
for node_dir in */; do
if [ -d "$node_dir" ]; then
echo "Checking dependencies for $node_dir..."
cd "$COMFYUI_DIR/custom_nodes/$node_dir"
# Check for requirements.txt
if [ -f "requirements.txt" ]; then
echo "Installing requirements.txt for $node_dir"
uv pip install --no-cache -r requirements.txt
fi
# Check for install.py
if [ -f "install.py" ]; then
echo "Running install.py for $node_dir"
python install.py
fi
# Check for setup.py
if [ -f "setup.py" ]; then
echo "Running setup.py for $node_dir"
uv pip install --no-cache -e .
fi
fi
done
fi
# Start ComfyUI with custom arguments if provided
cd $COMFYUI_DIR
FIXED_ARGS="--listen 0.0.0.0 --port 8188"
if [ -s "$ARGS_FILE" ]; then
# File exists and is not empty, combine fixed args with custom args
CUSTOM_ARGS=$(grep -v '^#' "$ARGS_FILE" | tr '\n' ' ')
if [ ! -z "$CUSTOM_ARGS" ]; then
echo "Starting ComfyUI with additional arguments: $CUSTOM_ARGS"
nohup python main.py $FIXED_ARGS $CUSTOM_ARGS &> /workspace/runpod-slim/comfyui.log &
else
echo "Starting ComfyUI with default arguments"
nohup python main.py $FIXED_ARGS &> /workspace/runpod-slim/comfyui.log &
fi
else
# File is empty, use only fixed args
echo "Starting ComfyUI with default arguments"
nohup python main.py $FIXED_ARGS &> /workspace/runpod-slim/comfyui.log &
fi
# Tail the log file
tail -f /workspace/runpod-slim/comfyui.log