-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·277 lines (236 loc) · 8.77 KB
/
Copy pathuninstall.sh
File metadata and controls
executable file
·277 lines (236 loc) · 8.77 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
#!/bin/bash
# AKS Flex Node Uninstall Script
# This script removes all components installed by the AKS Flex Node installation script
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration (should match install.sh)
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/aks-flex-node"
DATA_DIR="/var/lib/aks-flex-node"
LOG_DIR="/var/log/aks-flex-node"
SERVICE_UNIT="aks-flex-node-agent.service"
SERVICE_UNIT_PATH="/etc/systemd/system/$SERVICE_UNIT"
SKIP_AZCLI="${SKIP_AZCLI:-false}"
# Interfaces created by unbounded-net that should not remain on the host after uninstall.
KNOWN_NETWORK_INTERFACES=("geneve0" "vxlan0" "ipip0" "unbounded0" "cbr0")
WIREGUARD_KEYS=("/etc/wireguard/server.priv" "/etc/wireguard/server.pub")
# Functions
log_info() {
echo -e "${BLUE}INFO:${NC} $1"
}
log_success() {
echo -e "${GREEN}SUCCESS:${NC} $1"
}
log_warning() {
echo -e "${YELLOW}WARNING:${NC} $1"
}
log_error() {
echo -e "${RED}ERROR:${NC} $1"
}
confirm_uninstall() {
echo -e "${YELLOW}AKS Flex Node Uninstaller${NC}"
echo -e "${YELLOW}===========================${NC}"
echo ""
echo "This will remove the following components:"
echo "• AKS Flex Node binary ($INSTALL_DIR/aks-flex-node)"
echo "• Systemd service (aks-flex-node-agent.service)"
echo "• Configuration directory ($CONFIG_DIR)"
echo "• Data directory ($DATA_DIR)"
echo "• Log directory ($LOG_DIR)"
echo "• Host network state created by unbounded-net"
echo "• Azure CLI"
echo ""
echo -e "${YELLOW}NOTE: This will first run 'aks-flex-node reset' to clean up cluster and Arc resources.${NC}"
echo ""
# Always prompt for confirmation, even when piped
if [[ "${1:-}" != "--force" ]]; then
read -p "Are you sure you want to continue? (y/N): " -n 1 -r response </dev/tty
echo
if [[ ! $response =~ ^[Yy]$ ]]; then
echo "Uninstall cancelled."
exit 0
fi
else
log_info "Force flag provided, skipping confirmation."
fi
}
run_reset() {
log_info "Running reset to clean up cluster and Arc resources..."
# Check if aks-flex-node binary exists
if [[ ! -f "$INSTALL_DIR/aks-flex-node" ]]; then
log_warning "AKS Flex Node binary not found at $INSTALL_DIR/aks-flex-node"
log_info "Skipping reset - binary may already be removed"
log_info "Removing systemd service directly..."
systemctl stop "$SERVICE_UNIT" 2>/dev/null || true
systemctl disable "$SERVICE_UNIT" 2>/dev/null || true
if [[ -e "$SERVICE_UNIT_PATH" ]]; then
rm -f "$SERVICE_UNIT_PATH"
log_success "Removed systemd unit: $SERVICE_UNIT_PATH"
else
log_info "Systemd unit not found: $SERVICE_UNIT_PATH"
fi
systemctl daemon-reload 2>/dev/null || true
return 0
fi
# Try to find config file
local config_file=""
if [[ -f "$CONFIG_DIR/config.json" ]]; then
config_file="$CONFIG_DIR/config.json"
log_info "Using config file: $config_file"
else
config_file="$CONFIG_DIR/config.json"
log_warning "Config file not found at $CONFIG_DIR/config.json"
log_warning "Resource cleanup may be skipped, but systemd service removal will still be attempted"
log_info "Manual cleanup of Azure resources may be required"
fi
# Run reset to clean up resources
# Use the root-owned auth copy prepared by 'aks-flex-node bootstrap'.
local azure_config_dir="$CONFIG_DIR/azure"
if [[ -d "$azure_config_dir" ]]; then
log_info "Using Azure CLI credentials from: $azure_config_dir"
env AZURE_CONFIG_DIR="$azure_config_dir" TERM="${TERM:-dumb}" "$INSTALL_DIR/aks-flex-node" reset 2>&1 || {
log_warning "Reset failed - this may be expected if resources are already cleaned up"
}
else
log_warning "Azure CLI credentials not found at $azure_config_dir"
log_info "Attempting reset without Azure CLI credentials..."
env TERM="${TERM:-dumb}" "$INSTALL_DIR/aks-flex-node" reset 2>&1 || {
log_warning "Reset failed - this may be expected if resources are already cleaned up"
}
fi
log_success "Reset completed"
}
remove_network_interface() {
local iface="$1"
if ip link show "$iface" &>/dev/null; then
log_info "Removing network interface: $iface"
if ip link delete "$iface" 2>/dev/null; then
log_success "Removed network interface: $iface"
else
log_warning "Failed to remove network interface: $iface"
fi
else
log_info "Network interface not found: $iface"
fi
}
reset_host_network() {
log_info "Resetting host network state..."
if command -v ip &>/dev/null; then
local iface
local wireguard_interfaces=()
# ip may render names as iface@ifindex; trim that suffix before matching unbounded-net wg<port> interfaces.
while IFS= read -r iface; do
wireguard_interfaces+=("$iface")
done < <(ip -o link show 2>/dev/null | awk -F': ' '{name=$2; sub(/@.*/,"",name); if(name~/^wg[0-9]+$/) print name}')
for iface in "${wireguard_interfaces[@]}" "${KNOWN_NETWORK_INTERFACES[@]}"; do
remove_network_interface "$iface"
done
else
log_warning "ip command not found - skipping network interface cleanup"
fi
local key_path
for key_path in "${WIREGUARD_KEYS[@]}"; do
if [[ -f "$key_path" ]]; then
log_info "Removing WireGuard key: $key_path"
if command -v shred &>/dev/null; then
if shred -u "$key_path" 2>/dev/null && [[ ! -e "$key_path" ]]; then
log_success "Removed WireGuard key: $key_path"
continue
fi
log_warning "Failed to shred WireGuard key, removing without shredding: $key_path"
else
log_warning "shred command not found, removing WireGuard key without shredding: $key_path"
fi
rm -f "$key_path"
log_success "Removed WireGuard key: $key_path"
else
log_info "WireGuard key not found: $key_path"
fi
done
}
remove_directories() {
log_info "Removing directories..."
# Remove directories
for dir in "$CONFIG_DIR" "$DATA_DIR" "$LOG_DIR"; do
if [[ -d "$dir" ]]; then
log_info "Removing directory: $dir"
rm -rf "$dir"
log_success "Removed directory: $dir"
else
log_info "Directory not found: $dir"
fi
done
}
remove_binary() {
log_info "Removing binary..."
if [[ -f "$INSTALL_DIR/aks-flex-node" ]]; then
rm -f "$INSTALL_DIR/aks-flex-node"
log_success "Removed binary: $INSTALL_DIR/aks-flex-node"
else
log_info "Binary not found: $INSTALL_DIR/aks-flex-node"
fi
}
remove_azure_cli() {
if [[ "$SKIP_AZCLI" == "true" || "$SKIP_AZCLI" == "1" ]]; then
log_info "Skipping Azure CLI removal (SKIP_AZCLI=$SKIP_AZCLI)"
return 0
fi
log_info "Removing Azure CLI..."
if command -v az &> /dev/null; then
# Uninstall Azure CLI package
log_info "Uninstalling Azure CLI package..."
export DEBIAN_FRONTEND=noninteractive
apt-get remove -y azure-cli 2>/dev/null || true
apt-get purge -y azure-cli 2>/dev/null || true
# Verify removal
if command -v az &> /dev/null; then
log_warning "az command still available after cleanup - manual removal may be required"
else
log_success "Azure CLI removed successfully"
fi
else
log_info "Azure CLI not found - already removed or never installed"
fi
}
show_completion_message() {
log_success "AKS Flex Node uninstallation completed!"
echo ""
echo -e "${YELLOW}What was removed:${NC}"
echo "✅ AKS Flex Node binary"
echo "✅ Systemd service configuration"
echo "✅ Service user and permissions"
echo "✅ Configuration and data directories"
echo "✅ Log files"
echo "✅ Host network state"
echo "✅ Azure CLI"
echo ""
echo -e "${GREEN}Complete uninstallation finished!${NC}"
echo ""
echo "The system has been returned to its pre-installation state."
}
main() {
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root (use sudo)"
exit 1
fi
# Confirm uninstall
confirm_uninstall "${1:-}"
echo ""
log_info "Starting AKS Flex Node uninstallation..."
# Uninstall components in reverse order of installation
run_reset
reset_host_network
remove_directories
remove_binary
remove_azure_cli
# Show completion message
show_completion_message
}
# Run main function
main "$@"