Repository navigation
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·364 lines (318 loc) · 12.3 KB
/
Copy pathuninstall.sh
File metadata and controls
executable file
·364 lines (318 loc) · 12.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env bash
set -e
# Keep uninstaller output visible even when prompts are piped.
exec 3>&1
if [ -t 3 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]; then
ESC="$(printf '\033')"
BOLD="${ESC}[1m"
RED="${ESC}[31m"
GREEN="${ESC}[32m"
YELLOW="${ESC}[33m"
BLUE="${ESC}[34m"
CYAN="${ESC}[36m"
RESET="${ESC}[0m"
else
BOLD=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
CYAN=""
RESET=""
fi
function print_header() {
printf '\n%s%s%s\n' "${RED}${BOLD}" "MirrorNeuron Uninstaller" "$RESET" >&3
}
function print_step() { printf '%s==>%s %s\n' "${CYAN}${BOLD}" "$RESET" "$1" >&3; }
function print_success() { printf '%s✓%s %s\n' "${GREEN}${BOLD}" "$RESET" "$1" >&3; }
function print_warning() { printf '%swarning:%s %s\n' "${YELLOW}${BOLD}" "$RESET" "$1" >&3; }
function print_error() { printf '%serror:%s %s\n' "${RED}${BOLD}" "$RESET" "$1" >&3; }
ASSUME_YES="N"
function usage() {
cat >&3 <<EOF
Usage: ./uninstall.sh [options]
Options:
--yes, -y Run non-interactively and answer yes to prompts.
MN_HOME=/path Override the runtime state directory. Defaults to ${HOME}/.mn.
-h, --help Show this help.
EOF
}
for arg in "$@"; do
case "$arg" in
--yes|-y) ASSUME_YES="Y" ;;
-h|--help) usage; exit 0 ;;
*)
print_warning "Unknown option: $arg"
usage
exit 1
;;
esac
done
function ask() {
local prompt="$1"
local default="$2"
local answer
if [ "$ASSUME_YES" = "Y" ]; then
echo "Y"
return
fi
if [ "$default" = "Y" ]; then
prompt="${prompt} [Y/n]: "
elif [ "$default" = "N" ]; then
prompt="${prompt} [y/N]: "
else
prompt="${prompt} [${default}]: "
fi
echo -ne "${BLUE}${BOLD}?${RESET} ${prompt}" >&3
if [ -c /dev/tty ]; then
read -r answer < /dev/tty
else
read -r answer
fi
if [ -z "$answer" ]; then
answer="$default"
fi
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
case "$answer" in
y|yes) echo "Y" ;;
n|no) echo "N" ;;
*) echo "$answer" ;;
esac
}
print_header
print_warning "This permanently removes MirrorNeuron components and configuration."
CONFIRM=$(ask "Are you sure you want to proceed?" "N")
if [ "$CONFIRM" != "Y" ]; then
print_warning "Uninstallation aborted."
exit 0
fi
INSTALL_DIR="${MN_HOME:-${HOME}/.mn}"
UI_DIR="${INSTALL_DIR}/webui"
BIN_DIR="${HOME}/.local/bin"
RUNTIME_BIN_DIR="${INSTALL_DIR}/bin"
VENV_DIR="${INSTALL_DIR}/venv"
RUNTIME_COMPOSE_FILE="${INSTALL_DIR}/docker-compose.yml"
RUNTIME_COMPOSE_ENV="${INSTALL_DIR}/docker-compose.env"
function read_env_value() {
local file="$1"
local key="$2"
[ -f "$file" ] || return 0
awk -F= -v key="$key" '$1 == key {print substr($0, index($0, "=") + 1); exit}' "$file"
}
COMPOSE_PROJECT_NAME="$(read_env_value "$RUNTIME_COMPOSE_ENV" "COMPOSE_PROJECT_NAME")"
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-mirror-neuron}"
WORKER_COMPOSE_PROJECT_NAME="$(read_env_value "$RUNTIME_COMPOSE_ENV" "MN_DOCKER_WORKER_COMPOSE_PROJECT")"
WORKER_COMPOSE_PROJECT_NAME="${WORKER_COMPOSE_PROJECT_NAME:-${MN_DOCKER_WORKER_COMPOSE_PROJECT:-mirror-neuron-workers}}"
DOCKER_NETWORK_NAME="$(read_env_value "$RUNTIME_COMPOSE_ENV" "MN_DOCKER_NETWORK_NAME")"
DOCKER_NETWORK_NAME="${DOCKER_NETWORK_NAME:-mirror-neuron-runtime}"
DOCKER_NETWORK_EXTERNAL_VALUE="$(read_env_value "$RUNTIME_COMPOSE_ENV" "MN_DOCKER_NETWORK_EXTERNAL")"
DOCKER_NETWORK_EXTERNAL_KNOWN="N"
if [ -n "$DOCKER_NETWORK_EXTERNAL_VALUE" ]; then
DOCKER_NETWORK_EXTERNAL_KNOWN="Y"
fi
case "$(printf '%s' "$DOCKER_NETWORK_EXTERNAL_VALUE" | tr '[:upper:]' '[:lower:]')" in
1|true|yes|y) DOCKER_NETWORK_EXTERNAL="Y" ;;
*) DOCKER_NETWORK_EXTERNAL="N" ;;
esac
function runtime_compose_down() {
if docker compose version >/dev/null 2>&1; then
if [ -f "$RUNTIME_COMPOSE_ENV" ]; then
docker compose --env-file "$RUNTIME_COMPOSE_ENV" \
--project-name "$COMPOSE_PROJECT_NAME" \
-f "$RUNTIME_COMPOSE_FILE" \
down --remove-orphans --volumes --rmi local
else
docker compose \
--project-name "$COMPOSE_PROJECT_NAME" \
-f "$RUNTIME_COMPOSE_FILE" \
down --remove-orphans --volumes --rmi local
fi
elif command -v docker-compose >/dev/null 2>&1; then
if [ -f "$RUNTIME_COMPOSE_ENV" ]; then
docker-compose \
--env-file "$RUNTIME_COMPOSE_ENV" \
--project-name "$COMPOSE_PROJECT_NAME" \
-f "$RUNTIME_COMPOSE_FILE" \
down --remove-orphans --volumes --rmi local
else
docker-compose \
--project-name "$COMPOSE_PROJECT_NAME" \
-f "$RUNTIME_COMPOSE_FILE" \
down --remove-orphans --volumes --rmi local
fi
else
print_warning "Docker Compose is not available; using project-label cleanup."
return 1
fi
}
function remove_compose_project_resources() {
local resource_id
local resource_name
local network_project
local cleanup_failed="N"
local project_name
local resources
local projects=("$COMPOSE_PROJECT_NAME")
if [ "$WORKER_COMPOSE_PROJECT_NAME" != "$COMPOSE_PROJECT_NAME" ]; then
projects+=("$WORKER_COMPOSE_PROJECT_NAME")
fi
# Workers use a separate SDK Compose project and attach to the runtime
# network. Remove every project's containers before shared dependencies.
for project_name in "${projects[@]}"; do
if ! resources=$(docker ps -aq --filter "label=com.docker.compose.project=${project_name}" 2>/dev/null); then
print_error "Could not list containers for Compose project $project_name."
return 1
fi
while IFS= read -r resource_id; do
[ -n "$resource_id" ] || continue
if ! docker rm -f "$resource_id" >/dev/null 2>&1; then
print_error "Could not remove container $resource_id (project $project_name)."
cleanup_failed="Y"
fi
done <<< "$resources"
done
[ "$cleanup_failed" = "N" ] || return 1
for project_name in "${projects[@]}"; do
if ! resources=$(docker volume ls -q --filter "label=com.docker.compose.project=${project_name}" 2>/dev/null); then
print_error "Could not list volumes for Compose project $project_name."
return 1
fi
while IFS= read -r resource_id; do
[ -n "$resource_id" ] || continue
if ! docker volume rm -f "$resource_id" >/dev/null 2>&1; then
print_error "Could not remove volume $resource_id (project $project_name)."
cleanup_failed="Y"
fi
done <<< "$resources"
if ! resources=$(docker network ls \
--filter "label=com.docker.compose.project=${project_name}" \
--format '{{.ID}} {{.Name}}' 2>/dev/null); then
print_error "Could not list networks for Compose project $project_name."
return 1
fi
while IFS=' ' read -r resource_id resource_name; do
[ -n "$resource_id" ] || continue
if [ "$DOCKER_NETWORK_EXTERNAL" = "Y" ] &&
[ "$resource_name" = "$DOCKER_NETWORK_NAME" ]; then
continue
fi
if ! docker network rm "$resource_id" >/dev/null 2>&1; then
print_error "Could not remove network $resource_name ($resource_id); check its attached containers."
cleanup_failed="Y"
fi
done <<< "$resources"
done
if [ "$DOCKER_NETWORK_EXTERNAL" != "Y" ] &&
docker network inspect "$DOCKER_NETWORK_NAME" >/dev/null 2>&1; then
network_project="$(docker network inspect -f '{{ index .Labels "com.docker.compose.project" }}' "$DOCKER_NETWORK_NAME" 2>/dev/null || true)"
if [ "$network_project" = "$COMPOSE_PROJECT_NAME" ] ||
[ "$DOCKER_NETWORK_EXTERNAL_KNOWN" = "Y" ]; then
if ! docker network rm "$DOCKER_NETWORK_NAME" >/dev/null 2>&1; then
print_error "Could not remove network $DOCKER_NETWORK_NAME; check its attached containers."
cleanup_failed="Y"
fi
fi
fi
[ "$cleanup_failed" = "N" ]
}
function remove_docker_runtime_project() {
local cleanup_failed="N"
if ! docker info >/dev/null 2>&1; then
print_error "Docker is installed but the daemon is not available."
printf 'Next: start Docker, then rerun ./uninstall.sh --yes\n' >&3
return 1
fi
print_step "Removing complete Docker Compose project"
if [ -f "$RUNTIME_COMPOSE_FILE" ]; then
if ! runtime_compose_down >/dev/null 2>&1; then
print_warning "Docker Compose teardown was incomplete; cleaning resources by project label."
fi
else
print_warning "Installed Compose file not found; cleaning resources by project label."
fi
if ! remove_compose_project_resources; then
cleanup_failed="Y"
fi
if [ "$cleanup_failed" = "Y" ]; then
print_error "Some MirrorNeuron Docker resources could not be removed."
printf 'Next: resolve active Docker attachments, then rerun ./uninstall.sh --yes\n' >&3
return 1
fi
print_success "Removed Compose containers, orphan services, named volumes, and owned networks."
}
if command -v docker >/dev/null 2>&1; then
if ! remove_docker_runtime_project; then
exit 1
fi
else
print_warning "Docker not installed, skipping container, volume, and network cleanup."
fi
print_step "Removing Symlinks"
rm -f "$BIN_DIR/mn" "$BIN_DIR/mn-api" "$RUNTIME_BIN_DIR/mn" "$RUNTIME_BIN_DIR/mn-api"
print_success "Removed mn and mn-api command links."
print_step "Removing Python Virtual Environment"
if [ -d "$VENV_DIR" ]; then
rm -rf "$VENV_DIR"
print_success "Removed virtual environment at $VENV_DIR"
else
print_success "Virtual environment not found, skipping."
fi
print_step "Removing Core Installation"
if [ -d "$INSTALL_DIR" ]; then
rm -rf "$INSTALL_DIR"
print_success "Removed core installation at $INSTALL_DIR"
else
print_success "Core installation not found, skipping."
fi
print_step "Removing Web UI Installation"
if [ -d "$UI_DIR" ] || [ -L "$UI_DIR" ]; then
rm -rf "$UI_DIR"
print_success "Removed Web UI installation at $UI_DIR"
else
print_success "Web UI installation not found, skipping."
fi
print_step "Removing Docker Images and OpenShell Artifacts"
if command -v docker &> /dev/null; then
if docker image inspect mirror-neuron-core:latest >/dev/null 2>&1; then
docker rmi mirror-neuron-core:latest >/dev/null 2>&1 || true
print_success "Removed Core image."
else
print_success "Core image not found, skipping."
fi
if command -v openshell >/dev/null 2>&1; then
openshell gateway destroy --name openshell >/dev/null 2>&1 || true
fi
if docker ps -a --format '{{.Names}}' | grep -q '^openshell-cluster-openshell$'; then
docker rm -f openshell-cluster-openshell >/dev/null 2>&1 || true
print_success "Removed OpenShell gateway container."
else
print_success "OpenShell gateway container not found, skipping."
fi
removed_image="N"
for image in \
"ghcr.io/nvidia/openshell/gateway:latest" \
"ghcr.io/nvidia/openshell/gateway:0.0.47" \
"ghcr.io/nvidia/openshell/cluster:0.0.16" \
"ghcr.io/nvidia/openshell/cluster:latest"; do
if docker image inspect "$image" >/dev/null 2>&1; then
docker rmi "$image" >/dev/null 2>&1 || true
removed_image="Y"
fi
done
if [ "$removed_image" = "Y" ]; then
print_success "Removed OpenShell Docker image(s)."
else
print_success "OpenShell Docker images not found, skipping."
fi
OPENSHELL_CONTAINER_CONFIG_DIR="${OPENSHELL_CONTAINER_CONFIG_DIR:-$HOME/.config/openshell-mirror-neuron}"
if [ -d "$OPENSHELL_CONTAINER_CONFIG_DIR" ]; then
rm -rf "$OPENSHELL_CONTAINER_CONFIG_DIR"
print_success "Removed MirrorNeuron OpenShell container config at $OPENSHELL_CONTAINER_CONFIG_DIR."
else
print_success "MirrorNeuron OpenShell container config not found, skipping."
fi
else
print_warning "Docker not installed, skipping image cleanup."
fi
echo "" >&3
print_success "MirrorNeuron uninstallation completed."