forked from informalsystems/malachite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspawn.bash
83 lines (67 loc) · 2.18 KB
/
spawn.bash
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
#!/usr/bin/env bash
# This script takes:
# - a number of nodes to run as an argument,
# - the home directory for the nodes configuration folders
function help {
echo "Usage: spawn.sh [--help] --nodes NODES_COUNT --home NODES_HOME [--app APP_BINARY] [--no-reset]"
}
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--help) help; exit 0 ;;
--nodes) NODES_COUNT="$2"; shift ;;
--home) NODES_HOME="$2"; shift ;;
--app) APP_BINARY="$2"; shift ;;
--no-reset) NO_RESET=1; shift ;;
*) echo "Unknown parameter passed: $1"; help; exit 1 ;;
esac
shift
done
# Check required arguments
if [[ -z "$NODES_COUNT" ]]; then
help
exit 1
fi
if [[ -z "$NODES_HOME" ]]; then
help
exit 1
fi
if [[ -z "$APP_BINARY" ]]; then
APP_BINARY="informalsystems-malachitebft-example-channel"
fi
echo "Compiling '$APP_BINARY'..."
cargo build -p $APP_BINARY
# Create nodes and logs directories, run nodes
for NODE in $(seq 0 $((NODES_COUNT - 1))); do
if [[ -z "$NO_RESET" ]]; then
echo "[Node $NODE] Resetting the database..."
rm -rf "$NODES_HOME/$NODE/db"
mkdir -p "$NODES_HOME/$NODE/db"
rm -rf "$NODES_HOME/$NODE/wal"
mkdir -p "$NODES_HOME/$NODE/wal"
fi
rm -rf "$NODES_HOME/$NODE/logs"
mkdir -p "$NODES_HOME/$NODE/logs"
rm -rf "$NODES_HOME/$NODE/traces"
mkdir -p "$NODES_HOME/$NODE/traces"
echo "[Node $NODE] Spawning node..."
cargo run -p $APP_BINARY -q -- start --home "$NODES_HOME/$NODE" > "$NODES_HOME/$NODE/logs/node.log" 2>&1 &
echo $! > "$NODES_HOME/$NODE/node.pid"
echo "[Node $NODE] Logs are available at: $NODES_HOME/$NODE/logs/node.log"
done
# Function to handle cleanup on interrupt
function exit_and_cleanup {
echo "Stopping all nodes..."
for NODE in $(seq 0 $((NODES_COUNT - 1))); do
NODE_PID=$(cat "$NODES_HOME/$NODE/node.pid")
echo "[Node $NODE] Stopping node (PID: $NODE_PID)..."
kill "$NODE_PID"
done
exit 0
}
# Trap the INT signal (Ctrl+C) to run the cleanup function
trap exit_and_cleanup INT
echo "Spawned $NODES_COUNT nodes."
echo "Press Ctrl+C to stop the nodes."
# Keep the script running
while true; do sleep 1; done