-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-node.sh
More file actions
422 lines (365 loc) · 11.2 KB
/
run-node.sh
File metadata and controls
422 lines (365 loc) · 11.2 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/bash
# Luna Coin Node Wrapper Script
# Run this directly without installation
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color
# Configuration
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NODE_SCRIPT="${SCRIPT_DIR}/luna_node.py" # Change this if your node script has a different name
CONFIG_FILE="${SCRIPT_DIR}/node_config.json"
DATA_DIR="${SCRIPT_DIR}/node-data"
LOG_FILE="${DATA_DIR}/node.log"
PID_FILE="${DATA_DIR}/node.pid"
VERSION="1.0"
DEFAULT_PORT=9335
DEFAULT_HOST="0.0.0.0"
# Functions
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
print_debug() { echo -e "${CYAN}[DEBUG]${NC} $1"; }
print_node() { echo -e "${MAGENTA}[NODE]${NC} $1"; }
check_python() {
print_status "Checking for Python installation..."
if command -v python3 &> /dev/null; then
PYTHON=python3
PYTHON_VERSION=$($PYTHON -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
print_success "Found Python ${PYTHON_VERSION} (python3)"
elif command -v python &> /dev/null; then
PYTHON=python
PYTHON_VERSION=$($PYTHON -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
print_success "Found Python ${PYTHON_VERSION} (python)"
else
print_error "Python is not installed. Please install Python 3.7 or higher."
echo "Ubuntu/Debian: sudo apt install python3"
echo "Fedora/RHEL: sudo dnf install python3"
echo "Arch: sudo pacman -S python"
exit 1
fi
# Check Python version
if ! $PYTHON -c "import sys; exit(0) if sys.version_info >= (3, 7) else exit(1)"; then
print_error "Python 3.7 or higher is required. Found version ${PYTHON_VERSION}"
exit 1
fi
}
check_dependencies() {
print_status "Checking Python dependencies..."
local missing_modules=()
local required_modules=("json" "hashlib" "secrets" "socket" "threading" "base64" "binascii" "select" "time" "os" "sys")
for module in "${required_modules[@]}"; do
if ! $PYTHON -c "import ${module}" 2>/dev/null; then
missing_modules+=("${module}")
fi
done
if [ ${#missing_modules[@]} -ne 0 ]; then
print_error "Missing required Python modules: ${missing_modules[*]}"
exit 1
fi
print_success "All required Python modules are available"
}
check_node_files() {
print_status "Checking node files..."
if [ ! -f "$NODE_SCRIPT" ]; then
print_error "Node script not found: $NODE_SCRIPT"
echo "Please make sure you're running this script from the directory containing your node script"
echo "Looking for: node.py, luna_node.py, server.py, or main.py"
# Try to find common node script names
local possible_scripts=("node.py" "luna_node.py" "server.py" "main.py" "blockchain_node.py")
for script in "${possible_scripts[@]}"; do
if [ -f "${SCRIPT_DIR}/${script}" ]; then
NODE_SCRIPT="${SCRIPT_DIR}/${script}"
print_warning "Found alternative node script: $script"
break
fi
done
if [ ! -f "$NODE_SCRIPT" ]; then
exit 1
fi
fi
if [ ! -f "$CONFIG_FILE" ]; then
print_warning "Config file not found: $CONFIG_FILE"
print_warning "Node will use default settings"
else
print_success "Found config file: $CONFIG_FILE"
fi
print_success "Found node script: $(basename "$NODE_SCRIPT")"
}
setup_data_dir() {
if [ ! -d "$DATA_DIR" ]; then
print_status "Creating data directory: ${DATA_DIR}"
mkdir -p "$DATA_DIR"
chmod 700 "$DATA_DIR"
fi
# Copy config to data directory if it doesn't exist there but exists in script dir
if [ -f "$CONFIG_FILE" ] && [ ! -f "${DATA_DIR}/$(basename "$CONFIG_FILE")" ]; then
cp "$CONFIG_FILE" "${DATA_DIR}/"
print_status "Copied config file to data directory"
fi
}
setup_logging() {
local log_dir=$(dirname "$LOG_FILE")
if [ ! -d "$log_dir" ]; then
mkdir -p "$log_dir"
fi
if [ -f "$LOG_FILE" ] && [ $(wc -c < "$LOG_FILE" 2>/dev/null || echo 0) -gt 10485760 ]; then
mv "$LOG_FILE" "${LOG_FILE}.old"
print_status "Rotated log file"
fi
}
check_port() {
local port=${1:-$DEFAULT_PORT}
if command -v nc &> /dev/null; then
if nc -z localhost "$port" 2>/dev/null; then
print_warning "Port $port is already in use"
return 1
else
print_success "Port $port is available"
return 0
fi
fi
return 0
}
show_help() {
echo "Luna Coin Node Wrapper Script"
echo "Usage: $0 [command] [options]"
echo ""
echo "Commands:"
echo " start Start the node (default)"
echo " stop Stop the node"
echo " restart Restart the node"
echo " status Show node status"
echo " logs Show node logs"
echo " console Run in console mode"
echo " help Show this help message"
echo " version Show version information"
echo ""
echo "Options:"
echo " --port PORT Specify port (default: $DEFAULT_PORT)"
echo " --host HOST Specify host (default: $DEFAULT_HOST)"
echo " --debug Enable debug mode"
echo ""
echo "Examples:"
echo " $0 start"
echo " $0 start --port 9335 --host 0.0.0.0"
echo " $0 stop"
echo " $0 status"
echo " $0 logs"
echo ""
echo "Files:"
echo " Node script: $NODE_SCRIPT"
echo " Config file: $CONFIG_FILE"
echo " Data directory: $DATA_DIR"
}
show_version() {
echo "Luna Coin Node Wrapper v${VERSION}"
$PYTHON --version
echo "Node script: $(basename "$NODE_SCRIPT")"
echo "Running from: $SCRIPT_DIR"
}
show_banner() {
echo -e "${MAGENTA}"
echo "=========================================="
echo " Luna Coin Node"
echo " Blockchain Node Server"
echo "=========================================="
echo -e "${NC}"
}
get_pid() {
if [ -f "$PID_FILE" ]; then
cat "$PID_FILE" 2>/dev/null
else
echo ""
fi
}
is_running() {
local pid=$(get_pid)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
return 0
else
return 1
fi
}
start_node() {
local port=$1
local host=$2
local debug=$3
if is_running; then
print_warning "Node is already running (PID: $(get_pid))"
return 0
fi
check_port "$port"
print_status "Starting Luna Coin Node on ${host}:${port}"
# Build command
local cmd="$PYTHON $NODE_SCRIPT"
local log_cmd=">> $LOG_FILE 2>&1"
if [ "$debug" = "true" ]; then
print_debug "Debug mode enabled"
log_cmd="2>&1 | tee -a $LOG_FILE"
fi
# Start the node in the background
cd "$SCRIPT_DIR"
eval "$cmd $log_cmd &"
local pid=$!
# Save PID
echo $pid > "$PID_FILE"
# Wait a moment to see if it starts successfully
sleep 2
if is_running; then
print_success "Node started successfully (PID: $pid)"
print_success "Listening on: ${host}:${port}"
print_success "Log file: $LOG_FILE"
else
print_error "Node failed to start"
rm -f "$PID_FILE"
return 1
fi
}
stop_node() {
if ! is_running; then
print_warning "Node is not running"
return 0
fi
local pid=$(get_pid)
print_status "Stopping node (PID: $pid)"
kill "$pid" 2>/dev/null
sleep 2
if is_running; then
print_warning "Node did not stop gracefully, forcing termination"
kill -9 "$pid" 2>/dev/null
sleep 1
fi
if is_running; then
print_error "Failed to stop node"
return 1
else
rm -f "$PID_FILE"
print_success "Node stopped successfully"
fi
}
show_status() {
if is_running; then
local pid=$(get_pid)
print_success "Node is running (PID: $pid)"
# You could add more status information here
else
print_warning "Node is not running"
fi
}
show_logs() {
if [ -f "$LOG_FILE" ]; then
echo "=== Node Logs ==="
tail -20 "$LOG_FILE"
else
print_warning "No log file found"
fi
}
run_console() {
print_status "Starting node in console mode..."
cd "$SCRIPT_DIR"
$PYTHON "$NODE_SCRIPT"
}
parse_arguments() {
local command="start"
local port=$DEFAULT_PORT
local host=$DEFAULT_HOST
local debug="false"
while [ $# -gt 0 ]; do
case "$1" in
start|stop|restart|status|logs|console|help|version)
command="$1"
shift
;;
--port)
port="$2"
shift 2
;;
--host)
host="$2"
shift 2
;;
--debug)
debug="true"
shift
;;
-h|--help)
show_help
exit 0
;;
-v|--version)
show_version
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
echo "$command $port $host $debug"
}
# Main execution
main() {
show_banner
check_python
check_dependencies
check_node_files
setup_data_dir
setup_logging
print_success "All checks passed!"
echo "Python: $($PYTHON --version)"
echo "Node: $(basename "$NODE_SCRIPT")"
echo "Directory: $SCRIPT_DIR"
echo "Data: $DATA_DIR"
echo "=========================================="
# Parse arguments
read -r command port host debug <<< $(parse_arguments "$@")
case "$command" in
start)
start_node "$port" "$host" "$debug"
;;
stop)
stop_node
;;
restart)
stop_node
sleep 1
start_node "$port" "$host" "$debug"
;;
status)
show_status
;;
logs)
show_logs
;;
console)
run_console
;;
help)
show_help
;;
version)
show_version
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac
}
# Handle script termination
cleanup() {
print_status "Cleaning up..."
}
trap cleanup EXIT INT TERM
# Run main function with all arguments
main "$@"