-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·435 lines (378 loc) · 14.1 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·435 lines (378 loc) · 14.1 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
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env bash
# marketd – docker management script
# Usage: ./run.sh [dev|prod|start|stop|restart|logs|status|help]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$SCRIPT_DIR/.docker-config"
# defaults
DEFAULT_BITCOIN_NETWORK="signet"
DEFAULT_BITCOIN_RPC_PORT="38332"
DEFAULT_BITCOIN_ZMQ_PORT="28332"
DEFAULT_BITCOIN_RPC_USER="user"
DEFAULT_BITCOIN_RPC_PASS="password"
DEFAULT_TOR_SOCKS_PORT="9050"
DEFAULT_TOR_CONTROL_PORT="9051"
DEFAULT_TOR_AUTH_PASSWORD="coinswap"
DEFAULT_MARKETD_PORT="3000"
# colours
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; BOLD='\033[1m'; NC='\033[0m'
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
success() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*"; }
header() { echo -e "\n${BOLD}$*${NC}"; printf '%.0s-' {1..50}; echo; }
# config I/O
# Keys accepted from CONFIG_FILE. Anything else is ignored.
_CONFIG_KEYS=(
BITCOIN_NETWORK BITCOIN_RPC_PORT BITCOIN_ZMQ_PORT
BITCOIN_RPC_USER BITCOIN_RPC_PASS BITCOIN_RPC_HOST
TOR_SOCKS_PORT TOR_CONTROL_PORT TOR_AUTH_PASSWORD
MARKETD_PORT USE_EXTERNAL_BITCOIND USE_EXTERNAL_TOR
)
load_config() {
if [[ -f "$CONFIG_FILE" ]]; then
# Parse CONFIG_FILE without sourcing it, so values cannot trigger
# command substitution or variable expansion.
local line key raw val pat_sq pat_dq q bs k allowed
q=\'; bs='\'
pat_sq="^([A-Z_][A-Z0-9_]*)='(.*)'$"
pat_dq="^([A-Z_][A-Z0-9_]*)=\"([^\"]*)\"$"
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" =~ $pat_sq ]]; then
key="${BASH_REMATCH[1]}"
raw="${BASH_REMATCH[2]}"
# decode '\'' back to a literal single quote
val=${raw//"$q$bs$q$q"/"$q"}
elif [[ "$line" =~ $pat_dq ]]; then
# Legacy double-quoted format. Treat the matched content as
# literal and refuse anything that would have undergone shell
# expansion when sourced.
key="${BASH_REMATCH[1]}"
val="${BASH_REMATCH[2]}"
if [[ "$val" == *'$'* || "$val" == *'`'* || "$val" == *'\'* ]]; then
warn "Refusing legacy config value with shell metacharacters in $key"
continue
fi
else
warn "Ignoring malformed config line: $line"
continue
fi
allowed=0
for k in "${_CONFIG_KEYS[@]}"; do
[[ "$key" == "$k" ]] && { allowed=1; break; }
done
if (( ! allowed )); then
warn "Ignoring unknown config key: $key"
continue
fi
# Assign without any shell expansion of $val.
printf -v "$key" '%s' "$val"
done < "$CONFIG_FILE"
info "Loaded config from $CONFIG_FILE"
fi
# apply defaults for anything still unset
BITCOIN_NETWORK="${BITCOIN_NETWORK:-$DEFAULT_BITCOIN_NETWORK}"
BITCOIN_RPC_PORT="${BITCOIN_RPC_PORT:-$DEFAULT_BITCOIN_RPC_PORT}"
BITCOIN_ZMQ_PORT="${BITCOIN_ZMQ_PORT:-$DEFAULT_BITCOIN_ZMQ_PORT}"
BITCOIN_RPC_USER="${BITCOIN_RPC_USER:-$DEFAULT_BITCOIN_RPC_USER}"
BITCOIN_RPC_PASS="${BITCOIN_RPC_PASS:-$DEFAULT_BITCOIN_RPC_PASS}"
BITCOIN_RPC_HOST="${BITCOIN_RPC_HOST:-localhost:$BITCOIN_RPC_PORT}"
TOR_SOCKS_PORT="${TOR_SOCKS_PORT:-$DEFAULT_TOR_SOCKS_PORT}"
TOR_CONTROL_PORT="${TOR_CONTROL_PORT:-$DEFAULT_TOR_CONTROL_PORT}"
TOR_AUTH_PASSWORD="${TOR_AUTH_PASSWORD:-$DEFAULT_TOR_AUTH_PASSWORD}"
MARKETD_PORT="${MARKETD_PORT:-$DEFAULT_MARKETD_PORT}"
USE_EXTERNAL_BITCOIND="${USE_EXTERNAL_BITCOIND:-false}"
USE_EXTERNAL_TOR="${USE_EXTERNAL_TOR:-false}"
}
save_config() {
local q=\' bs='\' k v esc
{
echo "# marketd docker configuration – generated by run.sh"
for k in "${_CONFIG_KEYS[@]}"; do
v="${!k-}"
# escape ' as '\'' so the value is safe inside a single-quoted literal
esc=${v//"$q"/"$q$bs$q$q"}
printf "%s='%s'\n" "$k" "$esc"
done
} > "$CONFIG_FILE"
success "Config saved to $CONFIG_FILE"
}
# helpers
check_docker() {
command -v docker &>/dev/null || { error "Docker not found."; exit 1; }
docker info &>/dev/null || { error "Docker daemon not running."; exit 1; }
}
port_in_use() { nc -z 127.0.0.1 "$1" 2>/dev/null; }
prompt() {
# prompt <variable_name> <label> <default>
local var="$1" label="$2" default="$3" input
read -rp " $label [$default]: " input
printf -v "$var" '%s' "${input:-$default}"
}
prompt_secret() {
local var="$1" label="$2" default="$3" input
read -rsp " $label [$default]: " input; echo
printf -v "$var" '%s' "${input:-$default}"
}
confirm() {
# confirm <prompt> → returns 0 for yes, 1 for no
local answer
read -rp " $1 [y/N]: " answer
[[ "${answer,,}" == "y" ]]
}
export_env() {
export BITCOIN_NETWORK BITCOIN_RPC_PORT BITCOIN_ZMQ_PORT
export BITCOIN_RPC_USER BITCOIN_RPC_PASS BITCOIN_RPC_HOST
export TOR_SOCKS_PORT TOR_CONTROL_PORT TOR_AUTH_PASSWORD
export MARKETD_PORT
}
# compose helpers
compose_up_services() {
# Start only the listed services (pass service names as args).
docker compose -f "$SCRIPT_DIR/docker-compose.yml" up -d --build "$@"
}
compose_down() {
docker compose -f "$SCRIPT_DIR/docker-compose.yml" down "$@"
}
wait_bitcoind_healthy() {
info "Waiting for bitcoind to become healthy (initial sync can take a while)…"
warn "Press Ctrl+C to stop watching – sync continues in the background."
echo ""
docker compose -f "$SCRIPT_DIR/docker-compose.yml" logs -f bitcoind &
local logs_pid=$!
trap 'kill "$logs_pid" 2>/dev/null; trap - INT TERM EXIT' INT TERM EXIT
while true; do
sleep 30
local status
status=$(docker compose -f "$SCRIPT_DIR/docker-compose.yml" \
exec -T bitcoind bitcoin-cli \
"-${BITCOIN_NETWORK}" \
"-rpcuser=${BITCOIN_RPC_USER}" \
"-rpcpassword=${BITCOIN_RPC_PASS}" \
"-rpcport=${BITCOIN_RPC_PORT}" \
getblockchaininfo 2>/dev/null \
| grep -o '"initialblockdownload":[^,]*' | cut -d: -f2 | tr -d ' ' || true)
[[ "$status" == "false" ]] && break
done
kill "$logs_pid" 2>/dev/null || true
trap - INT TERM EXIT
success "Bitcoin node synced."
}
# dev mode
cmd_dev() {
header "Dev mode – starting all services"
check_docker
load_config
export_env
compose_up_services bitcoind tor marketd
success "All services started."
docker compose -f "$SCRIPT_DIR/docker-compose.yml" ps
}
# production configure
configure_prod() {
header "Production setup"
# network
header "Bitcoin network"
echo " 1) Signet (default, where coinswap operates)"
echo " 2) Mainnet"
local net_choice
read -rp " Choice [1]: " net_choice
case "${net_choice:-1}" in
2) BITCOIN_NETWORK="mainnet"; BITCOIN_RPC_PORT="8332" ;;
*) BITCOIN_NETWORK="signet"; BITCOIN_RPC_PORT="38332" ;;
esac
prompt BITCOIN_ZMQ_PORT "ZMQ port" "$DEFAULT_BITCOIN_ZMQ_PORT"
# bitcoin node
header "Bitcoin node"
echo " 1) Run bitcoind in Docker (quick start, syncs from scratch)"
echo " 2) Use an existing bitcoind (requires -rest and -txindex=1)"
local btc_choice
read -rp " Choice [1]: " btc_choice
if [[ "${btc_choice:-1}" == "2" ]]; then
USE_EXTERNAL_BITCOIND="true"
prompt BITCOIN_RPC_HOST "RPC host:port" "localhost:$BITCOIN_RPC_PORT"
prompt BITCOIN_RPC_USER "RPC user" "$DEFAULT_BITCOIN_RPC_USER"
prompt_secret BITCOIN_RPC_PASS "RPC password" "$DEFAULT_BITCOIN_RPC_PASS"
warn "Make sure bitcoind is running with -rest and -txindex=1."
else
USE_EXTERNAL_BITCOIND="false"
BITCOIN_RPC_HOST="localhost:$BITCOIN_RPC_PORT"
prompt BITCOIN_RPC_PORT "RPC port" "$BITCOIN_RPC_PORT"
prompt BITCOIN_RPC_USER "RPC user" "$DEFAULT_BITCOIN_RPC_USER"
prompt_secret BITCOIN_RPC_PASS "RPC password" "$DEFAULT_BITCOIN_RPC_PASS"
fi
# tor
header "Tor"
if port_in_use "$DEFAULT_TOR_SOCKS_PORT"; then
info "Detected something on port $DEFAULT_TOR_SOCKS_PORT."
fi
echo " 1) Run Tor in Docker (default)"
echo " 2) Use an existing Tor daemon (needs ControlPort + auth)"
local tor_choice
read -rp " Choice [1]: " tor_choice
if [[ "${tor_choice:-1}" == "2" ]]; then
USE_EXTERNAL_TOR="true"
prompt TOR_SOCKS_PORT "SOCKS port" "$DEFAULT_TOR_SOCKS_PORT"
prompt TOR_CONTROL_PORT "control port" "$DEFAULT_TOR_CONTROL_PORT"
prompt_secret TOR_AUTH_PASSWORD "control password" "$DEFAULT_TOR_AUTH_PASSWORD"
warn "Make sure ControlPort and HashedControlPassword are set in your torrc."
else
USE_EXTERNAL_TOR="false"
if port_in_use "$DEFAULT_TOR_SOCKS_PORT"; then
warn "Port $DEFAULT_TOR_SOCKS_PORT already in use – choose a different port."
prompt TOR_SOCKS_PORT "SOCKS port" "19050"
else
TOR_SOCKS_PORT="$DEFAULT_TOR_SOCKS_PORT"
fi
if port_in_use "$DEFAULT_TOR_CONTROL_PORT"; then
warn "Port $DEFAULT_TOR_CONTROL_PORT already in use – choose a different port."
prompt TOR_CONTROL_PORT "control port" "19051"
else
TOR_CONTROL_PORT="$DEFAULT_TOR_CONTROL_PORT"
fi
prompt_secret TOR_AUTH_PASSWORD "Tor auth password (choose anything)" "$DEFAULT_TOR_AUTH_PASSWORD"
fi
# marketd
header "marketd"
prompt MARKETD_PORT "HTTP port" "$DEFAULT_MARKETD_PORT"
# summary
header "Summary"
echo " Bitcoin network : $BITCOIN_NETWORK"
echo " Bitcoin node : $([ "$USE_EXTERNAL_BITCOIND" == "true" ] && echo "external ($BITCOIN_RPC_HOST)" || echo "Docker")"
echo " Bitcoin RPC port : $BITCOIN_RPC_PORT"
echo " Bitcoin ZMQ port : $BITCOIN_ZMQ_PORT"
echo " Bitcoin RPC user : $BITCOIN_RPC_USER"
echo " Tor : $([ "$USE_EXTERNAL_TOR" == "true" ] && echo "external" || echo "Docker")"
echo " Tor SOCKS port : $TOR_SOCKS_PORT"
echo " Tor control port : $TOR_CONTROL_PORT"
echo " marketd HTTP port : $MARKETD_PORT"
echo ""
confirm "Save and start?" || { info "Aborted."; exit 0; }
save_config
}
# prod mode
cmd_prod() {
check_docker
load_config
# If no saved config exists yet, run the wizard
if [[ ! -f "$CONFIG_FILE" ]]; then
configure_prod
else
info "Using saved config ($CONFIG_FILE). Run './run.sh configure' to change it."
if confirm "Reconfigure now?"; then
configure_prod
fi
fi
export_env
local services=()
if [[ "$USE_EXTERNAL_BITCOIND" == "false" ]]; then
services+=(bitcoind)
else
info "Skipping bitcoind – using external node at $BITCOIN_RPC_HOST"
# Override RPC URL to point at external host
export MARKETD_BITCOIN_RPC_URL="$BITCOIN_RPC_HOST"
fi
if [[ "$USE_EXTERNAL_TOR" == "false" ]]; then
services+=(tor)
else
info "Skipping tor – using external Tor on SOCKS $TOR_SOCKS_PORT / control $TOR_CONTROL_PORT"
fi
if [[ "${#services[@]}" -gt 0 ]]; then
info "Starting infrastructure: ${services[*]}"
compose_up_services "${services[@]}"
# Wait for bitcoind if we started it
if [[ " ${services[*]} " == *" bitcoind "* ]]; then
wait_bitcoind_healthy
fi
fi
info "Starting marketd…"
compose_up_services marketd
success "marketd is up."
docker compose -f "$SCRIPT_DIR/docker-compose.yml" ps
}
# other commands
cmd_configure() {
load_config
configure_prod
}
cmd_start() {
check_docker
if [[ ! -f "$CONFIG_FILE" ]]; then
error "No config found. Run './run.sh prod' first."
exit 1
fi
load_config
export_env
local services=(marketd)
[[ "$USE_EXTERNAL_BITCOIND" == "false" ]] && services=(bitcoind "${services[@]}")
[[ "$USE_EXTERNAL_TOR" == "false" ]] && services=(tor "${services[@]}")
compose_up_services "${services[@]}"
success "Started: ${services[*]}"
}
cmd_stop() {
check_docker
load_config
export_env
compose_down
success "All services stopped."
}
cmd_restart() {
cmd_stop
sleep 2
cmd_start
}
cmd_logs() {
load_config
export_env
docker compose -f "$SCRIPT_DIR/docker-compose.yml" logs -f "${1:-}"
}
cmd_status() {
load_config
export_env
docker compose -f "$SCRIPT_DIR/docker-compose.yml" ps
}
cmd_help() {
cat << EOF
${BOLD}marketd – docker management script${NC}
${BOLD}MODES${NC}
dev Start all services (bitcoind + tor + marketd) for local development.
Uses built-in defaults – no prompts.
prod Interactive production wizard. Prompts for configuration,
lets you skip bitcoind or tor if you have existing ones,
then starts the configured services.
${BOLD}OTHER COMMANDS${NC}
configure Re-run the production configuration wizard.
start Start services using the saved config (must run prod first).
stop Stop all running services.
restart Stop then start.
logs [svc] Stream logs. Optionally filter to a service (bitcoind|tor|marketd).
status Show running container status.
help Show this message.
${BOLD}EXAMPLES${NC}
./run.sh dev
./run.sh prod
./run.sh logs marketd
./run.sh stop
${BOLD}CONFIGURATION${NC}
Saved to .docker-config (git-ignored). Delete it to re-run the wizard.
EOF
}
# dispatch
case "${1:-help}" in
dev) cmd_dev ;;
prod) cmd_prod ;;
configure) cmd_configure ;;
start) cmd_start ;;
stop) cmd_stop ;;
restart) cmd_restart ;;
logs) cmd_logs "${2:-}" ;;
status) cmd_status ;;
help|--help|-h) cmd_help ;;
*)
error "Unknown command: $1"
cmd_help
exit 1
;;
esac