-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·187 lines (166 loc) · 5.49 KB
/
setup.sh
File metadata and controls
executable file
·187 lines (166 loc) · 5.49 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
#!/usr/bin/env bash
set -euo pipefail
# --- Safety check ---
if [ "$(id -u)" -eq 0 ]; then
echo "[ERROR] Do not run setup.sh as root. Please run as your normal user."
exit 1
fi
# --- Dependency checks ---
missing=()
for cmd in git docker curl; do
if ! command -v "$cmd" &>/dev/null; then
missing+=("$cmd")
fi
done
if ! docker compose version &>/dev/null 2>&1; then
missing+=("docker-compose-plugin")
fi
if [ ${#missing[@]} -gt 0 ]; then
echo "[ERROR] Missing required tools: ${missing[*]}"
echo " See README.md for installation instructions."
exit 1
fi
# --- NVIDIA Container Toolkit check ---
if ! docker info 2>/dev/null | grep -qi nvidia; then
echo "[WARNING] NVIDIA runtime not detected in Docker."
echo " GPU passthrough may fail. Install the NVIDIA Container Toolkit:"
echo " https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html"
echo ""
read -rp "Continue anyway? [y/N] " answer
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
exit 0
fi
fi
# --- Config ---
REPO_URL="https://github.com/tsondo/a1111-docker.git"
REPO_DIR="$(cd "$(dirname "$0")" && pwd)"
CONTAINER_UID=1000
CONTAINER_GID=1000
# Ensure .env exists
if [ ! -f "$REPO_DIR/.env" ]; then
echo "[INFO] No .env file found. Copying from .env.sample..."
cp "$REPO_DIR/.env.sample" "$REPO_DIR/.env"
fi
# Migrate old .env variable names to new ones
if grep -q '^REPO_DIR=' "$REPO_DIR/.env" 2>/dev/null; then
echo "[INFO] Migrating .env: REPO_DIR -> REPOSITORIES_DIR"
sed -i 's/^REPO_DIR=/REPOSITORIES_DIR=/' "$REPO_DIR/.env"
fi
if grep -q '^HF_MODELS_PATH=' "$REPO_DIR/.env" 2>/dev/null; then
echo "[INFO] Migrating .env: HF_MODELS_PATH -> HF_MODELS_DIR"
sed -i 's/^HF_MODELS_PATH=/HF_MODELS_DIR=/' "$REPO_DIR/.env"
fi
if grep -q '^WILDCARD_DIR=' "$REPO_DIR/.env" 2>/dev/null; then
echo "[INFO] Migrating .env: removing unused WILDCARD_DIR"
sed -i '/^WILDCARD_DIR=/d' "$REPO_DIR/.env"
fi
echo "[INFO] Starting setup..."
# --- Parse flags ---
USE_CACHE=true
DETACH=false
for arg in "$@"; do
case "$arg" in
--no-cache) USE_CACHE=false; echo "[INFO] Rebuilding container image with --no-cache" ;;
-d|--detach) DETACH=true ;;
esac
done
# --- Clone or update repo ---
if [ -d "$REPO_DIR/.git" ]; then
echo "[INFO] Repo already exists..."
current_branch=$(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD)
if [ "$current_branch" = "main" ]; then
echo "[INFO] On main branch, pulling latest..."
OLD_HASH=$(git -C "$REPO_DIR" rev-parse HEAD:setup.sh 2>/dev/null || echo "none")
git -C "$REPO_DIR" pull --ff-only || {
echo "[WARNING] Fast-forward pull failed (upstream has diverged or there are local changes)."
echo " Skipping update — resolve manually with: git -C $REPO_DIR pull"
}
NEW_HASH=$(git -C "$REPO_DIR" rev-parse HEAD:setup.sh 2>/dev/null || echo "none")
if [ "$OLD_HASH" != "$NEW_HASH" ]; then
echo "[INFO] setup.sh was updated during pull. Please re-run it to apply changes."
exit 0
fi
else
echo "[INFO] On branch '$current_branch', skipping git pull to preserve local changes."
fi
else
echo "[INFO] Cloning fresh repo..."
git clone "$REPO_URL" "$REPO_DIR"
fi
cd "$REPO_DIR"
# --- Persistent directories (must match docker-compose mounts) ---
PERSIST_DIRS=(
configs
models
outputs
extensions
extensions/wildcards
embeddings
logs
cache
cache/huggingface
repositories
pip-cache
hf_models
)
echo "[INFO] Creating persistent directories..."
for d in "${PERSIST_DIRS[@]}"; do
mkdir -p "$REPO_DIR/$d"
done
# Fix ownership only for directories that don't already match the container UID:GID.
needs_chown=false
for d in "${PERSIST_DIRS[@]}"; do
dir_uid=$(stat -c '%u' "$REPO_DIR/$d")
dir_gid=$(stat -c '%g' "$REPO_DIR/$d")
if [ "$dir_uid" != "$CONTAINER_UID" ] || [ "$dir_gid" != "$CONTAINER_GID" ]; then
needs_chown=true
break
fi
done
if $needs_chown; then
echo "[INFO] Fixing host-side ownership to match container UID:GID ($CONTAINER_UID:$CONTAINER_GID)..."
for d in "${PERSIST_DIRS[@]}"; do
sudo chown "$CONTAINER_UID:$CONTAINER_GID" "$REPO_DIR/$d"
done
else
echo "[INFO] Directory ownership already correct, skipping chown."
fi
# --- Prepopulate UI config files if missing ---
for f in config.json ui-config.json; do
TARGET="$REPO_DIR/$f"
if [ ! -s "$TARGET" ]; then
echo "{}" > "$TARGET"
echo "[INFO] Created empty $f in repo root"
fi
done
# styles.csv needs a valid CSV header, not JSON
if [ ! -s "$REPO_DIR/styles.csv" ]; then
echo "name,prompt,negative_prompt" > "$REPO_DIR/styles.csv"
echo "[INFO] Created styles.csv with header row"
fi
# --- Ensure default model config exists in expected path ---
CONFIG_PATH="$REPO_DIR/configs/v1-inference.yaml"
if [ ! -f "$CONFIG_PATH" ]; then
echo "[INFO] Downloading v1-inference.yaml to configs/"
curl -fsSL -o "$CONFIG_PATH" https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml || {
echo "[WARNING] Failed to download v1-inference.yaml. You may need to add it manually."
rm -f "$CONFIG_PATH"
}
fi
# --- Build container ---
if $USE_CACHE; then
docker compose build
else
docker compose build --no-cache
fi
# --- Launch container ---
if $DETACH; then
echo "[INFO] Launching container in background..."
docker compose up -d
echo "[INFO] WebUI starting at http://localhost:7860"
echo " View logs: docker compose logs -f"
echo " Stop: docker compose down"
else
echo "[INFO] Launching container (Ctrl+C to stop)..."
docker compose up
fi