Skip to content

Commit 67ca039

Browse files
committed
fix: add cleanup deploy script
1 parent d16ec3e commit 67ca039

File tree

2 files changed

+560
-0
lines changed

2 files changed

+560
-0
lines changed

go/deploy/cleanup-unkey-deploy.sh

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#!/bin/bash
2+
# Cleanup script for Unkey Deploy services and components
3+
# This script removes all installed services, configurations, and data
4+
5+
set -euo pipefail
6+
7+
# Color codes for output
8+
GREEN='\033[0;32m'
9+
RED='\033[0;31m'
10+
YELLOW='\033[1;33m'
11+
NC='\033[0m' # No Color
12+
13+
echo "============================================="
14+
echo "Unkey Deploy Complete Cleanup Script"
15+
echo "============================================="
16+
echo ""
17+
echo -e "${YELLOW}WARNING: This will remove all Unkey Deploy services and data!${NC}"
18+
echo "Services to be removed:"
19+
echo " - metald"
20+
echo " - builderd"
21+
echo " - assetmanagerd"
22+
echo " - SPIRE Server and Agent"
23+
echo " - All VM bridges and network configurations"
24+
echo " - All data directories"
25+
echo ""
26+
read -p "Are you sure you want to continue? [y/N] " -n 1 -r
27+
echo
28+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
29+
echo "Cleanup cancelled."
30+
exit 0
31+
fi
32+
33+
# Check if running as root
34+
if [ "$EUID" -ne 0 ]; then
35+
echo -e "${RED}Error: This script must be run as root${NC}"
36+
exit 1
37+
fi
38+
39+
echo ""
40+
echo "Starting cleanup process..."
41+
42+
# Function to safely stop and disable a service
43+
stop_and_disable_service() {
44+
local service=$1
45+
if systemctl list-unit-files | grep -q "^${service}.service"; then
46+
echo "Stopping and disabling ${service}..."
47+
systemctl stop "${service}" 2>/dev/null || true
48+
systemctl disable "${service}" 2>/dev/null || true
49+
fi
50+
}
51+
52+
# Function to remove systemd service files
53+
remove_service_files() {
54+
local service=$1
55+
echo "Removing ${service} service files..."
56+
rm -f "/etc/systemd/system/${service}.service"
57+
rm -f "/etc/systemd/system/${service}@.service"
58+
rm -f "/usr/lib/systemd/system/${service}.service"
59+
rm -f "/usr/lib/systemd/system/${service}@.service"
60+
}
61+
62+
# 1. Stop all services
63+
echo ""
64+
echo "=== Stopping Services ==="
65+
stop_and_disable_service "metald"
66+
stop_and_disable_service "metald-bridge-8"
67+
stop_and_disable_service "metald-bridge-32"
68+
stop_and_disable_service "builderd"
69+
stop_and_disable_service "assetmanagerd"
70+
stop_and_disable_service "spire-agent"
71+
stop_and_disable_service "spire-server"
72+
73+
# 2. Kill any remaining Firecracker processes
74+
echo ""
75+
echo "=== Cleaning up Firecracker VMs ==="
76+
pkill -9 firecracker 2>/dev/null || true
77+
pkill -9 jailer 2>/dev/null || true
78+
79+
# Clean up any remaining VM tap interfaces
80+
for tap in $(ip link show | grep -o 'tap[0-9a-f_-]*' | sort -u); do
81+
echo "Removing tap interface: $tap"
82+
ip link delete "$tap" 2>/dev/null || true
83+
done
84+
85+
# Clean up veth interfaces
86+
for veth in $(ip link show | grep -o 'vh_[0-9a-f]*' | sort -u); do
87+
echo "Removing veth interface: $veth"
88+
ip link delete "$veth" 2>/dev/null || true
89+
done
90+
91+
# 3. Remove network bridges
92+
echo ""
93+
echo "=== Removing Network Bridges ==="
94+
for i in {0..31}; do
95+
bridge="br-tenant-$i"
96+
if ip link show "$bridge" &>/dev/null; then
97+
echo "Removing bridge: $bridge"
98+
ip link set "$bridge" down 2>/dev/null || true
99+
ip link delete "$bridge" 2>/dev/null || true
100+
fi
101+
done
102+
103+
# Remove systemd-networkd configurations
104+
echo "Removing network configurations..."
105+
rm -rf /etc/systemd/network/10-br-tenant-*.net{dev,work}
106+
rm -rf /run/systemd/network/10-br-tenant-*.net{dev,work}
107+
108+
# 4. Remove binaries
109+
echo ""
110+
echo "=== Removing Binaries ==="
111+
binaries=(
112+
"/usr/local/bin/metald"
113+
"/usr/local/bin/metald-cli"
114+
"/usr/local/bin/metald-init"
115+
"/usr/local/bin/builderd"
116+
"/usr/local/bin/builderd-cli"
117+
"/usr/local/bin/assetmanagerd"
118+
"/usr/local/bin/assetmanagerd-cli"
119+
"/usr/local/bin/firecracker"
120+
"/usr/local/bin/jailer"
121+
"/opt/spire/bin/spire-server"
122+
"/opt/spire/bin/spire-agent"
123+
"/opt/spire/bin/spire"
124+
)
125+
126+
for binary in "${binaries[@]}"; do
127+
if [ -f "$binary" ]; then
128+
echo "Removing: $binary"
129+
rm -f "$binary"
130+
fi
131+
done
132+
133+
# 5. Remove service files
134+
echo ""
135+
echo "=== Removing Service Files ==="
136+
remove_service_files "metald"
137+
remove_service_files "metald-bridge-8"
138+
remove_service_files "metald-bridge-32"
139+
remove_service_files "builderd"
140+
remove_service_files "assetmanagerd"
141+
remove_service_files "spire-server"
142+
remove_service_files "spire-agent"
143+
144+
# 6. Remove configuration files
145+
echo ""
146+
echo "=== Removing Configuration Files ==="
147+
rm -rf /etc/metald
148+
rm -rf /etc/builderd
149+
rm -rf /etc/assetmanagerd
150+
rm -rf /etc/spire
151+
rm -rf /etc/default/unkey-deploy
152+
rm -f /etc/default/metald
153+
rm -f /etc/default/builderd
154+
rm -f /etc/default/assetmanagerd
155+
156+
# 7. Remove data directories
157+
echo ""
158+
echo "=== Removing Data Directories ==="
159+
echo -e "${YELLOW}Warning: This will delete all VM images and assets!${NC}"
160+
read -p "Remove all data directories? [y/N] " -n 1 -r
161+
echo
162+
if [[ $REPLY =~ ^[Yy]$ ]]; then
163+
# Service data directories
164+
rm -rf /opt/metald
165+
rm -rf /opt/builderd
166+
rm -rf /opt/assetmanagerd
167+
rm -rf /opt/vm-assets
168+
rm -rf /opt/spire
169+
170+
# Runtime directories
171+
rm -rf /var/lib/metald
172+
rm -rf /var/lib/builderd
173+
rm -rf /var/lib/assetmanagerd
174+
rm -rf /var/lib/spire
175+
rm -rf /var/lib/firecracker
176+
177+
# Jailer directories
178+
rm -rf /srv/jailer
179+
rm -rf /var/run/firecracker
180+
181+
# Log directories
182+
rm -rf /var/log/metald
183+
rm -rf /var/log/builderd
184+
rm -rf /var/log/assetmanagerd
185+
rm -rf /var/log/spire
186+
187+
echo -e "${GREEN}${NC} Data directories removed"
188+
else
189+
echo "Skipping data directory removal"
190+
fi
191+
192+
# 8. Remove users and groups
193+
echo ""
194+
echo "=== Removing Service Users ==="
195+
for user in metald builderd assetmanagerd firecracker spire; do
196+
if id -u "$user" &>/dev/null; then
197+
echo "Removing user: $user"
198+
userdel "$user" 2>/dev/null || true
199+
fi
200+
if getent group "$user" &>/dev/null; then
201+
echo "Removing group: $user"
202+
groupdel "$user" 2>/dev/null || true
203+
fi
204+
done
205+
206+
# 9. Clean up iptables rules
207+
echo ""
208+
echo "=== Cleaning up iptables rules ==="
209+
# Remove FORWARD rules for VM bridges
210+
for i in {0..31}; do
211+
iptables -D FORWARD -i br-tenant-$i -j ACCEPT 2>/dev/null || true
212+
iptables -D FORWARD -o br-tenant-$i -j ACCEPT 2>/dev/null || true
213+
done
214+
215+
# Remove NAT rules
216+
iptables -t nat -F 2>/dev/null || true
217+
iptables -t nat -X 2>/dev/null || true
218+
219+
# 10. Clean up cgroups
220+
echo ""
221+
echo "=== Cleaning up cgroups ==="
222+
if [ -d /sys/fs/cgroup/firecracker ]; then
223+
rmdir /sys/fs/cgroup/firecracker 2>/dev/null || true
224+
fi
225+
226+
# Clean up any VM-specific cgroups
227+
for cg in $(find /sys/fs/cgroup -name "*firecracker*" -type d 2>/dev/null); do
228+
rmdir "$cg" 2>/dev/null || true
229+
done
230+
231+
# 11. Reload systemd
232+
echo ""
233+
echo "=== Reloading systemd ==="
234+
systemctl daemon-reload
235+
systemctl restart systemd-networkd
236+
237+
# 12. Clean up any remaining artifacts
238+
echo ""
239+
echo "=== Final cleanup ==="
240+
# Remove any temporary VM files
241+
rm -rf /tmp/firecracker-*
242+
rm -rf /tmp/vm-*
243+
rm -f /tmp/*-vm-console.log
244+
245+
# Remove any socket files
246+
rm -f /var/run/firecracker.sock*
247+
rm -f /var/run/metald.sock
248+
rm -f /var/run/builderd.sock
249+
rm -f /var/run/assetmanagerd.sock
250+
rm -f /var/lib/spire/agent/agent.sock
251+
252+
# Clean up any remaining systemd runtime directories
253+
rm -rf /run/systemd/system/metald.service.d
254+
rm -rf /run/systemd/system/builderd.service.d
255+
rm -rf /run/systemd/system/assetmanagerd.service.d
256+
257+
echo ""
258+
echo "============================================="
259+
echo -e "${GREEN}✓ Cleanup completed successfully!${NC}"
260+
echo "============================================="
261+
echo ""
262+
echo "The following have been removed:"
263+
echo " - All Unkey Deploy services and binaries"
264+
echo " - All network bridges and configurations"
265+
echo " - All service users and groups"
266+
echo " - All configuration files"
267+
if [[ $REPLY =~ ^[Yy]$ ]]; then
268+
echo " - All data directories and VM assets"
269+
fi
270+
echo ""
271+
echo "System has been restored to pre-installation state."
272+
echo ""
273+
echo "Note: If you want to reinstall, you'll need to:"
274+
echo " 1. Reinstall SPIRE Server and Agent"
275+
echo " 2. Reinstall and configure all services"
276+
echo " 3. Re-run network bridge setup"
277+
echo " 4. Re-download base VM assets"

0 commit comments

Comments
 (0)