-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsetup-sync.sh
More file actions
379 lines (312 loc) · 8.48 KB
/
Copy pathsetup-sync.sh
File metadata and controls
379 lines (312 loc) · 8.48 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash
# Edge Gateway Sync Setup Wizard
# Interactive script to set up multi-node synchronization
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_header() {
echo -e "${CYAN}"
echo "=========================================="
echo " Edge Gateway Sync Setup Wizard"
echo "=========================================="
echo -e "${NC}"
}
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
print_step() {
echo -e "${CYAN}[Step $1]${NC} $2"
}
# Check prerequisites
check_prerequisites() {
print_step "1" "Checking prerequisites..."
local missing=()
if ! command -v go &> /dev/null; then
missing+=("Go")
fi
if ! command -v ssh &> /dev/null; then
missing+=("SSH")
fi
if ! command -v scp &> /dev/null; then
missing+=("SCP")
fi
if [ ${#missing[@]} -ne 0 ]; then
print_error "Missing prerequisites: ${missing[*]}"
exit 1
fi
print_success "All prerequisites met"
echo ""
}
# Build project
build_project() {
print_step "2" "Building project..."
print_info "Building for local (Windows)..."
go build -o bin/edgex.exe ./cmd/main.go
go build -o bin/sync-test.exe ./cmd/sync-test/main.go
print_info "Building for remote (Linux ARM64)..."
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bin/edgex-linux-arm64 ./cmd/main.go
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bin/sync-test-linux-arm64 ./cmd/sync-test/main.go
print_success "Build completed"
echo ""
}
# Test SSH connection
test_ssh() {
local host=$1
print_info "Testing SSH connection to $host..."
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "$host" "echo 'OK'" &>/dev/null; then
print_success "SSH connection successful"
return 0
else
print_error "SSH connection failed"
return 1
fi
}
# Deploy to remote
deploy_remote() {
local host=$1
local node_id=$2
print_step "3" "Deploying to remote host..."
print_info "Host: $host"
print_info "Node ID: $node_id"
# Test connection
if ! test_ssh "$host"; then
print_info "Please set up SSH key authentication:"
print_info " ssh-copy-id $host"
return 1
fi
# Deploy
print_info "Deploying files..."
ssh "$host" "mkdir -p /opt/edgex/{bin,conf,data,logs}"
scp bin/edgex-linux-arm64 "$host:/opt/edgex/bin/edgex"
scp bin/sync-test-linux-arm64 "$host:/opt/edgex/bin/sync-test"
scp -r conf/* "$host:/opt/edgex/conf/" 2>/dev/null || true
ssh "$host" "chmod +x /opt/edgex/bin/*"
# Create service
print_info "Creating systemd service..."
ssh "$host" << EOF
cat > /etc/systemd/system/edgex.service << 'SERVICE'
[Unit]
Description=Edge Gateway Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/edgex
ExecStart=/opt/edgex/bin/edgex -conf conf
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable edgex
systemctl restart edgex
EOF
print_success "Deployment completed"
echo ""
}
# Start local node
start_local() {
local node_id=$1
print_step "4" "Starting local node..."
print_info "Node ID: $node_id"
# Create data directory
mkdir -p data logs
print_info "Starting node in background..."
./bin/sync-test.exe start "$node_id" > "logs/$node_id.log" 2>&1 &
local pid=$!
echo $pid > "data/$node_id.pid"
sleep 3
if ps -p $pid > /dev/null 2>&1; then
print_success "Local node started (PID: $pid)"
print_info "Log file: logs/$node_id.log"
else
print_error "Failed to start local node"
return 1
fi
echo ""
}
# Show status
show_status() {
print_step "5" "Checking status..."
print_info "Local nodes:"
for pid_file in data/*.pid; do
if [ -f "$pid_file" ]; then
local node_id=$(basename "$pid_file" .pid)
local pid=$(cat "$pid_file")
if ps -p "$pid" > /dev/null 2>&1; then
print_success "$node_id: Running (PID: $pid)"
else
print_error "$node_id: Not running"
fi
fi
done
echo ""
print_info "Remote nodes:"
ssh root@192.168.3.230 "systemctl is-active edgex" &>/dev/null && \
print_success "NODE-2@192.168.3.230: Running" || \
print_error "NODE-2@192.168.3.230: Not running"
echo ""
}
# Main menu
show_menu() {
echo ""
echo "=========================================="
echo " What would you like to do?"
echo "=========================================="
echo ""
echo " 1) Full Setup (Deploy + Start)"
echo " 2) Deploy to Remote Only"
echo " 3) Start Local Node Only"
echo " 4) Check Status"
echo " 5) View Logs"
echo " 6) Stop All Nodes"
echo " 7) Cleanup"
echo " 0) Exit"
echo ""
echo -n "Enter your choice [0-7]: "
}
# Full setup
full_setup() {
print_header
echo "This will:"
echo " 1. Build the project"
echo " 2. Deploy to remote host (192.168.3.230)"
echo " 3. Start local node (NODE-1)"
echo ""
read -p "Continue? [Y/n]: " confirm
if [[ "$confirm" =~ ^[Nn]$ ]]; then
return
fi
check_prerequisites
build_project
deploy_remote "root@192.168.3.230" "NODE-2"
start_local "NODE-1"
echo ""
echo "=========================================="
print_success "Setup completed!"
echo "=========================================="
echo ""
echo "Local Node: NODE-1 (Running)"
echo "Remote Node: NODE-2@192.168.3.230 (Running)"
echo ""
echo "Next steps:"
echo " - Wait 30 seconds for nodes to discover each other"
echo " - Check status: ./setup-sync.sh (option 4)"
echo " - View logs: ./setup-sync.sh (option 5)"
echo ""
}
# View logs
view_logs() {
echo ""
echo "Select log to view:"
echo " 1) Local NODE-1"
echo " 2) Remote NODE-2"
echo " 3) Back"
echo ""
echo -n "Choice: "
read choice
case $choice in
1)
if [ -f "logs/NODE-1.log" ]; then
tail -f "logs/NODE-1.log"
else
print_error "Log file not found"
fi
;;
2)
ssh root@192.168.3.230 "tail -f /opt/edgex/logs/edgex.log"
;;
*)
return
;;
esac
}
# Stop all nodes
stop_all() {
print_info "Stopping all nodes..."
# Stop local nodes
for pid_file in data/*.pid; do
if [ -f "$pid_file" ]; then
local node_id=$(basename "$pid_file" .pid)
local pid=$(cat "$pid_file")
if ps -p "$pid" > /dev/null 2>&1; then
print_info "Stopping $node_id (PID: $pid)..."
kill "$pid" 2>/dev/null || true
fi
rm -f "$pid_file"
fi
done
# Stop remote node
print_info "Stopping remote node..."
ssh root@192.168.3.230 "systemctl stop edgex" 2>/dev/null || true
print_success "All nodes stopped"
}
# Cleanup
cleanup() {
print_warning "This will delete all data and logs!"
read -p "Are you sure? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
stop_all
rm -rf data logs
print_success "Cleanup completed"
fi
}
# Main
main() {
if [ "$1" == "--full" ]; then
full_setup
exit 0
fi
while true; do
show_menu
read choice
case $choice in
1)
full_setup
;;
2)
build_project
deploy_remote "root@192.168.3.230" "NODE-2"
;;
3)
start_local "NODE-1"
;;
4)
show_status
;;
5)
view_logs
;;
6)
stop_all
;;
7)
cleanup
;;
0)
echo "Goodbye!"
exit 0
;;
*)
print_error "Invalid choice"
;;
esac
done
}
# Run main
main "$@"