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#!/bin/bash
# Nacos Admin Password Initialization Script
# This script waits for Nacos to be ready and then sets the admin password
set -e
echo "[init-password] Starting Nacos password initialization script..."
echo "[init-password] Waiting for Nacos server to be ready on port 8848..."
# Wait for Nacos port 8848 to be open
MAX_RETRIES=120 # 2 minutes max wait
RETRY_INTERVAL=1
RETRIES=0
while [ $RETRIES -lt $MAX_RETRIES ]; do
# Check if port 8848 is open using nc (netcat) or bash's /dev/tcp
if command -v nc &> /dev/null; then
if nc -z localhost 8848 2>/dev/null; then
echo "[init-password] Port 8848 is now open!"
break
fi
else
# Fallback to /dev/tcp if nc is not available
if (echo > /dev/tcp/localhost/8848) 2>/dev/null; then
echo "[init-password] Port 8848 is now open!"
break
fi
fi
RETRIES=$((RETRIES + 1))
if [ $((RETRIES % 10)) -eq 0 ]; then
echo "[init-password] Still waiting for port 8848... (attempt $RETRIES/$MAX_RETRIES)"
fi
sleep $RETRY_INTERVAL
done
if [ $RETRIES -eq $MAX_RETRIES ]; then
echo "[init-password] ERROR: Timeout waiting for Nacos server to start"
exit 1
fi
# Additional wait for Nacos to fully initialize its API
echo "[init-password] Port is open, waiting additional 10 seconds for API to be ready..."
sleep 10
# Set admin password
echo "[init-password] Setting admin password..."
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "http://localhost:8080/v3/auth/user/admin" \
-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
-d "password=nacos" 2>&1)
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
echo "[init-password] SUCCESS: Admin password set successfully"
echo "[init-password] Response: $BODY"
else
echo "[init-password] WARNING: Password setting returned HTTP $HTTP_CODE"
echo "[init-password] Response: $BODY"
echo "[init-password] This might be expected if password was already set"
fi
echo "[init-password] Password initialization script completed"