forked from ethersphere/bee-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbee.sh
executable file
·186 lines (169 loc) · 4.83 KB
/
bee.sh
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
#!/bin/bash
echoerr() { if [[ $QUIET -ne 1 ]] ; then echo "$@" 1>&2; fi }
usage() {
cat << USAGE >&2
USAGE:
$ bee.sh [COMMAND] [PARAMETERS]
COMMANDS:
start create Bee cluster with the given parameters
stop stop Bee cluster
PARAMETERS:
--ephemeral create ephemeral container for bee-client. Data won't be persisted.
--workers=number all Bee nodes in the test environment. Default is 2.
--port-maps=number map ports of the cluster nodes to the hosting machine in the following manner:
1. 1633:1635
2. 11633:11635
3. 21633:21635 (...)
number represents the nodes number to map from. Default is 2.
--password=string password for Bee client(s).
--version=x.y.z used version of Bee client.
USAGE
exit 1
}
stop() {
echo "Stop following containers:"
docker container stop $QUEEN_CONTAINER_NAME;
WORKER_NAMES=`docker container ls -f name="$WORKER_CONTAINER_NAME*" --format "{{.Names}}"`
for WORKER_NAME in $WORKER_NAMES; do
docker container stop "$WORKER_NAME"
done
trap - SIGINT
exit 0;
}
fetch_queen_underlay_addr() {
if [[ ! -z "$QUEEN_UNDERLAY_ADDRESS" ]] ; then return; fi
while : ; do
QUEEN_UNDERLAY_ADDRESS=$(curl -s localhost:1635/addresses | python -mjson.tool 2>&1 | grep "/ip4/" | awk '!/127.0.0.1/' | xargs)
if [[ -z "$QUEEN_UNDERLAY_ADDRESS" ]] ; then
echo "Waiting for the Queen initialization..."
sleep 5
else
break;
fi
done
}
log_queen() {
trap stop SIGINT
docker logs --tail 25 -f $QUEEN_CONTAINER_NAME
}
# Init variables
EPHEMERAL=false
WORKERS=2
QUEEN_CONTAINER_NAME="bee-queen-test"
WORKER_CONTAINER_NAME="bee-worker-test"
QUEEN_CONTAINER_IN_DOCKER=`docker container ls -qaf name=$QUEEN_CONTAINER_NAME`
BEE_VERSION="0.5.2"
BEE_IMAGE="ethersphere/bee:$BEE_VERSION"
BEE_PASSWORD="password"
QUEEN_BOOTNODE=""
PORT_MAPS=2
# Decide script action
case "$1" in
start)
shift 1
;;
stop)
stop
;;
*)
echoerr "Unknown command: $1"
usage
;;
esac
# Alter variables from flags
while [ $# -gt 0 ]
do
case "$1" in
--ephemeral)
EPHEMERAL=true
shift 1
;;
--workers=*)
WORKERS=${1#*=}
shift 1
;;
--password=*)
BEE_PASSWORD="${1#*=}"
shift 1
;;
--version=*)
BEE_VERSION="${1#*=}"
BEE_IMAGE="ethersphere/bee:$BEE_VERSION"
shift 1
;;
--port-maps=*)
PORT_MAPS="${1#*=}"
shift 1
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
if $EPHEMERAL ; then
EXTRA_DOCKER_PARAMS=" --rm"
fi
# Start Bee Queen
if [ -z "$QUEEN_CONTAINER_IN_DOCKER" ] || $EPHEMERAL ; then
EXTRA_QUEEN_PARAMS=""
if [ $PORT_MAPS -ge 1 ] ; then
EXTRA_QUEEN_PARAMS=" -p 127.0.0.1:1633-1635:1633-1635"
fi
echo "start Bee Queen process"
docker run \
-d \
--name $QUEEN_CONTAINER_NAME \
$EXTRA_DOCKER_PARAMS \
$EXTRA_QUEEN_PARAMS \
$BEE_IMAGE \
start \
--password $BEE_PASSWORD \
--bootnode=$QUEEN_BOOTNODE \
--swap-enable=false \
--debug-api-enable \
--welcome-message="You have found the queen of the beehive..." \
--cors-allowed-origins="*" \
--payment-tolerance=2147483647
else
docker start "$QUEEN_CONTAINER_IN_DOCKER"
fi
# Start Bee workers
for i in $(seq 1 1 $WORKERS); do
WORKER_NAME="$WORKER_CONTAINER_NAME-$i"
WORKER_CONTAINER_IN_DOCKER=`docker container ls -qaf name=$WORKER_NAME`
if [ -z "$WORKER_CONTAINER_IN_DOCKER" ] || $EPHEMERAL ; then
# fetch queen underlay address
fetch_queen_underlay_addr
# construct additional params
EXTRA_WORKER_PARAMS=""
if [ $PORT_MAPS -gt $i ] ; then
PORT_START=$((1633+(10000*$i)))
PORT_END=$(($PORT_START + 2))
EXTRA_WORKER_PARAMS=" -p 127.0.0.1:$PORT_START-$PORT_END:1633-1635"
fi
# run docker container
echo "start Bee worker $i process"
docker run \
-d \
--name $WORKER_NAME \
$EXTRA_DOCKER_PARAMS \
$EXTRA_WORKER_PARAMS \
$BEE_IMAGE \
start \
--password $BEE_PASSWORD \
--bootnode="$QUEEN_UNDERLAY_ADDRESS" \
--swap-enable=false \
--debug-api-enable \
--welcome-message="I'm just Bee worker ${i} in the beehive." \
--cors-allowed-origins="*" \
--payment-tolerance=2147483647
else
docker start "$WORKER_CONTAINER_IN_DOCKER"
fi
done
# log Bee Queen
log_queen