This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcitadel
executable file
·703 lines (592 loc) · 17.9 KB
/
citadel
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#!/usr/bin/env bash
set -euo pipefail
source $(dirname $0)/../scripts/functions.sh
source $(dirname $0)/../scripts/spinner.sh
source $(dirname $0)/../scripts/utils.sh
CLI_NAME="$(basename $0)"
CLI_VERSION="1.3.3"
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
IMAGE_NAME="ghcr.io/runcitadel/citadel-dev"
if [ -z ${1+x} ]; then
command=""
else
command="$1"
fi
# Install dependencies
if [[ "$command" = "install" ]]; then
# install Docker
echo 'Installing Docker...'
if command -v "docker" >/dev/null 2>&1; then
echo 'Docker is already installed. Skipping...'
else
sudo $SCRIPT_DIR/../scripts/install-docker.sh
fi
# install Sysbox
echo 'Installing Sysbox...'
if command -v "sysbox-runc" >/dev/null 2>&1; then
echo 'Sysbox is already installed. Skipping...'
else
sudo $SCRIPT_DIR/../scripts/install-sysbox.sh
fi
echo 'Citadel dependencies installed successfully.'
exit
fi
# Initialize a Citadel environment
if [[ "$command" = "init" ]]; then
shift
POSITIONAL_ARGS=()
development=false
ssh=false
while [[ $# -gt 0 ]]; do
case $1 in
--dev | --development)
development=true
shift
;;
--ssh)
ssh=true
shift
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
directory=${POSITIONAL_ARGS:-"."}
# create directory if it doesn't exist
mkdir -p $directory
if [[ "$(ls -A $directory)" ]]; then
echo "Directory must be empty!"
echo "You can specify a target directory with \`$CLI_NAME init <directory>\`"
exit 1
fi
printf "\nCloning container repositories..."
if $development; then
touch "$directory/.citadel-dev"
repos="
runcitadel/core
runcitadel/manager
runcitadel/middleware
runcitadel/dashboard
runcitadel/ui
runcitadel/sdk
runcitadel/fs
runcitadel/utils
runcitadel/node-lndconnect
runcitadel/bitcoin-rpc"
else
touch "$directory/.citadel"
repos="runcitadel/core"
fi
for repo in $repos; do
echo
array=(${repo//// })
user=${array[0]}
repo=${array[1]}
if $ssh; then
url="[email protected]:$user/$repo.git"
else
url="https://github.com/$user/$repo.git"
fi
git clone "$url" "$directory/$repo"
if [[ $development == true ]] && [[ $repo == "core" ]]; then
printf "\nCopying docker-compose.override.yml...\n"
cp "$(get_script_location)/docker-compose.override.yml" "$directory/$repo"
fi
done
# TODO: move this to a helper
on_success="\xE2\x9C\x94"
on_fail="\xE2\x9D\x8C"
green="\e[1;32m"
red="\e[1;31m"
nc="\e[0m"
checkmark="${green}${on_success}${nc}"
if $development; then
printf "\n $checkmark Citadel development environment initialized successfully.\n"
printf "\nYou can boot the system container with:\n"
else
printf "\n $checkmark Citadel environment initialized successfully.\n"
printf "\nYou can boot the system container with:\n"
fi
printf "\`cd $directory && $CLI_NAME boot\`\n"
exit
fi
# Boot the container
if [[ "$command" = "boot" ]]; then
check_dependencies
shift
development=$(is_dev_environment)
# default values
$development && name="citadel-dev" || name="citadel"
$development && network="regtest" || network="mainnet"
$development && verbose=true || verbose=false
# parse arguments
for arg in "$@"; do
case "$arg" in
-n | --name)
name=$2
shift 1
;;
-n | --network)
case $2 in
"mainnet" | "testnet" | "signet" | "regtest")
network=$2
shift 1
;;
*)
echo "Not a valid value for network"
exit 1
;;
esac
;;
-v | --verbose)
verbose=true
shift 1
;;
*)
shift 1
;;
esac
done
if $development; then
echo "Found Citadel development environment. Booting in development mode..."
# save name to environment file
echo $name >$PWD/.citadel-dev
docker run \
--detach --publish-all \
--runtime sysbox-runc \
--publish 8333:8333 \
--publish 9735:9735 \
--env NETWORK=$network \
--name $name \
--hostname $name \
--mount type=bind,source="$(pwd)/core",target=/home/citadel/citadel \
--mount type=bind,source="$(pwd)/manager",target=/home/citadel/manager \
--mount type=bind,source="$(pwd)/middleware",target=/home/citadel/middleware \
--mount type=bind,source="$(pwd)/dashboard",target=/home/citadel/dashboard \
--mount type=bind,source="$(pwd)/ui",target=/home/citadel/ui \
--mount type=bind,source="$(pwd)/sdk",target=/home/citadel/sdk \
--mount type=bind,source="$(pwd)/fs",target=/home/citadel/fs \
--mount type=bind,source="$(pwd)/utils",target=/home/citadel/utils \
--mount type=bind,source="$(pwd)/node-lndconnect",target=/home/citadel/node-lndconnect \
--mount type=bind,source="$(pwd)/bitcoin-rpc",target=/home/citadel/bitcoin-rpc \
$IMAGE_NAME &>/dev/null || {
echo "Container with name \"$name\" exists already. Either remove (or rename) it or create a new one by passing a different name with \`$CLI_NAME boot --name <name>\`."
exit 1
}
else
echo "Booting with Bitcoin network set to $network..."
# save name to environment file
echo $name >$PWD/.citadel
docker run \
--detach --publish-all \
--runtime sysbox-runc \
--env NETWORK=$network \
--name $name \
--hostname $name \
--mount type=bind,source="$(pwd)/core",target=/home/citadel/citadel \
$IMAGE_NAME &>/dev/null || {
echo "Container with name \"$name\" exists already. Either remove (or rename) it or create a new one by passing a different name with \`$CLI_NAME boot --name <name>\`."
exit 1
}
fi
if $verbose; then
echo 'Citadel is starting up. Listening for logs...'
# wait for container
# TODO: this is very unreliable
sleep 5
# Log until Citadel is up
run_in_container "journalctl -f -u citadel-startup | sed '/Citadel is now accessible at/ q'" $name
else
start_spinner "Citadel is spinning up containers... This will only take a minute."
wait_for_dashboard $name
stop_spinner $?
fi
show_welcome $name $(get_host_ip) $(get_container_port_http $name) $network $development
read -p "Do you want to log in now? [y/N] " should_login
echo
if [[ $should_login =~ [Yy]$ ]]; then
$CLI_NAME ssh $name
fi
exit
fi
# List all Citadel containers with their status in a table format
if [[ "$command" = "list" ]] || [[ "$command" = "ls" ]]; then
check_dependencies
TEMPFILE=$(mktemp)
trap "rm -f $TEMPFILE" EXIT
echo "NAME|CONTAINER ID|STATUS|LOCAL|NETWORK" >>$TEMPFILE
containers=$(docker ps --all --filter "ancestor=$IMAGE_NAME" --format '{{.Names}}')
for container in $containers; do
id=$(docker container inspect --format '{{.Id}}' $container)
id_short=${id:0:12}
status=$(docker container inspect --format '{{.State.Status}}' $container)
hostname=$(get_container_hostname $container)
host_ip=$(get_host_ip)
if $(is_container_running $container); then
port_http=$(get_container_port_http $container)
local_url="http://$hostname.local"
network_url="http://$host_ip:$port_http"
else
local_url="—"
network_url="—"
fi
echo "$container|$id_short|$status|$local_url|$network_url" >>$TEMPFILE
done
column -t -s'|' $TEMPFILE
exit
fi
# Show information about a Citadel container
if [[ "$command" = "info" ]]; then
check_dependencies
# get target container
if [ -z ${2+x} ]; then
check_container_name
target_container=$(get_container_name)
else
target_container=$2
fi
check_container_running $target_container
status=$(docker container inspect --format '{{.State.Status}}' $target_container)
port_http=$(get_container_port_http $target_container)
port_ssh=$(get_container_port_ssh $target_container)
network=$(get_node_network $target_container)
show_info $target_container $status $(get_host_ip) $port_http $port_ssh $network
exit
fi
# Get an SSH session inside the container
if [[ "$command" = "ssh" ]]; then
check_dependencies
if [[ $# -eq 2 ]]; then
# target specified
target_container=$2
check_container_running $target_container
else
# get target container and its port
check_container_name
target_container=$(get_container_name)
check_container_running $target_container
port=$(get_container_port_ssh $target_container)
fi
ssh -t "citadel@$target_container.local" 'cd /home/citadel/citadel && exec bash -l'
exit
fi
# Start the container
if [[ "$command" = "start" ]]; then
check_dependencies
is_dev_env=$(is_dev_environment)
# default values
$is_dev_env && verbose=true || verbose=false
# get target container
if [ -z ${2+x} ]; then
with_target=false
check_container_name
target_container=$(get_container_name)
else
with_target=true
target_container=$2
fi
check_container_exists $target_container
if [ "$(docker container inspect -f '{{.State.Running}}' $target_container)" == "true" ]; then
echo "Citadel \"$target_container\" is already running."
exit 1
else
if $with_target; then
printf "Starting Citadel \"$target_container\"...\n\n"
else
printf "Starting Citadel...\n\n"
fi
# ignore stdout, but show stderr
docker start $target_container 2>&1 >/dev/null
if $verbose; then
echo 'Citadel is starting up. Listening for logs...'
# wait for container
# TODO: this is very unreliable
sleep 5
# Log until Citadel is up
run_in_container "journalctl -f -u citadel-startup | sed '/Citadel is now accessible at/ q'" $target_container
else
start_spinner "Citadel is spinning up containers... This will only take a minute."
wait_for_dashboard $target_container
stop_spinner $?
fi
printf "\nCitadel started.\n"
read -p "Do you want to log in now? [y/N] " should_login
echo
if [[ $should_login =~ [Yy]$ ]]; then
$CLI_NAME ssh $target_container
fi
fi
exit
fi
# Stop the container
if [[ "$command" = "stop" ]]; then
check_dependencies
# get target container
if [ -z ${2+x} ]; then
check_container_name
target_container=$(get_container_name)
check_container_running $target_container
echo "Shutting down Citadel..."
else
target_container=$2
check_container_running $target_container
echo "Shutting down Citadel \"$target_container\"..."
fi
# Shutdown Citadel safely
run_in_container "scripts/stop" $target_container
docker stop $target_container 2>&1 >/dev/null
exit
fi
# Reload the Citadel service
if [[ "$command" = "reload" ]]; then
check_dependencies
# get target container
if [ -z ${2+x} ]; then
check_container_name
target_container=$(get_container_name)
check_container_running $target_container
printf "Reloading the Citadel service...\n\n"
else
target_container=$2
check_container_running $target_container
printf "Reloading the Citadel service in \"$target_container\"...\n\n"
fi
run_in_container "scripts/stop && scripts/configure && scripts/start" $target_container
exit
fi
# Destroy the container
if [[ "$command" = "destroy" ]]; then
check_dependencies
# get target container
if [ -z ${2+x} ]; then
check_container_name
target_container=$(get_container_name)
check_container_exists $target_container
echo "WARNING: This will remove \"$target_container\"."
echo "If you just want to stop the container run \`$CLI_NAME stop\`."
else
target_container=$2
check_container_exists $target_container
echo "WARNING: This will remove \"$target_container\"."
echo "If you just want to stop the container run \`$CLI_NAME stop $target_container\`."
fi
read -p "Are you sure? [y/N] "
echo
if [[ $REPLY =~ [Yy]$ ]]; then
if $(is_container_running $target_container); then
echo "Shutting down Citadel..."
# Shutdown Citadel safely
run_in_container "scripts/stop" $target_container
fi
echo "Destroying container..."
docker rm -f $target_container &>/dev/null
echo "Citadel container destroyed."
else
echo "Cancelled."
fi
exit
fi
# Backup the container
if [[ "$command" = "backup" ]]; then
check_dependencies
check_environment
check_container_name
current_date=$(date '+%Y-%m-%d')
echo "Stopping Citadel services..."
run_in_container "scripts/stop" &>/dev/null
echo "Creating snapshot..."
docker commit $(get_container_name) citadel-backup:${current_date}
echo "Backing up..."
docker save citadel-backup:${current_date} | gzip >citadel-backup-${current_date}.tar.gz
echo "Cleaning up..."
docker rmi citadel-backup:${current_date} &>/dev/null
echo "Done! Backup saved to ./citadel-backup-${current_date}.tar.gz"
exit
fi
# Restore a backup
if [[ "$command" = "restore" ]]; then
check_dependencies
if [ -z ${2+x} ]; then
echo "A second argument is required!"
exit 1
fi
# TODO: we aren't really using the date here atm
backup_date=${2:15:10}
# check for current container
docker container inspect $(get_container_name) &>/dev/null || {
# load & rename
echo "Restoring from backup $2..."
docker load <$2
docker tag citadel-backup:${backup_date} $IMAGE_NAME
echo "Cleaning up..."
docker rmi citadel-backup:${backup_date} &>/dev/null
read -p "Backup restored successfully. Do you want to boot it now? [y/N] " should_boot
echo
if [[ $should_boot =~ [Yy]$ ]]; then
$CLI_NAME boot
exit
else
echo "All done."
exit
fi
exit
}
read -p "Found existing Citadel installation. Do you want to overwrite it? [y/N] " should_overwrite
echo
if [[ $should_overwrite =~ [Yy]$ ]]; then
echo "Destroying current Citadel..."
docker rm $(get_container_name) &>/dev/null || {
echo 'Citadel is still running. Stop it and try again.'
exit
}
docker rmi $IMAGE_NAME &>/dev/null
echo "Citadel image destroyed."
$CLI_NAME restore $2
else
echo "Cancelled."
fi
exit
fi
# Stream Citadel logs
if [[ "$command" = "logs" ]]; then
check_dependencies
check_environment
check_container_name
shift
args="$@"
while true; do
run_in_container "docker compose logs -f $args" || {
echo "$(date +"%T") Trying again in 1 second..."
}
sleep 1
done
exit
fi
# Run a command inside the container
if [[ "$command" = "run" ]]; then
check_dependencies
check_environment
check_container_name
if [ -z ${2+x} ]; then
echo "Specify the command you want to run."
echo "Usage: \`$CLI_NAME $command \"<command>\"\`."
exit 1
fi
run_in_container "$2"
exit
fi
# Run bitcoin-cli with arguments
if [[ "$command" = "bitcoin-cli" ]]; then
check_dependencies
check_container_name
check_inner_container "bitcoin"
if [ -z ${2+x} ]; then
args=""
else
args="${@:2}"
fi
run_in_container "docker exec -t bitcoin bitcoin-cli ${args}"
exit
fi
# Run lncli with arguments
if [[ "$command" = "lncli" ]]; then
check_dependencies
check_container_name
check_inner_container "lightning"
if [ -z ${2+x} ]; then
args=""
else
args="${@:2}"
fi
run_in_container "docker exec -t lightning lncli ${args}"
exit
fi
# Delete the user and wallet
if [[ "$command" = "reset" ]]; then
check_dependencies
check_dev_environment
printf "Deleting user and wallet...\n"
run_in_container "GLOBIGNORE=**/lnd.conf && rm -rf lnd/* && rm -f db/user.json && rm -f db/citadel-seed/seed"
printf "Reloading the Citadel service...\n"
run_in_container "systemctl restart citadel-startup"
exit
fi
# Fund the wallet
if [[ "$command" = "fund" ]]; then
check_dependencies
check_environment
check_regtest_mode
check_inner_container "lightning"
check_inner_container "bitcoin"
# default to 1 BTC
amount=${2:-1}
wallet_name="mywallet"
# Check if LND wallet exists
lnd_wallet_state=$($CLI_NAME lncli --network regtest state | jq -r '.state')
# echo $lnd_wallet_state
# exit
if [[ $lnd_wallet_state == "NON_EXISTING" ]]; then
echo "No LND Wallet found. Create a user first."
exit 1
fi
# Check Bitcoin Core wallet exists
$CLI_NAME bitcoin-cli unloadwallet $wallet_name &>/dev/null || true
$CLI_NAME bitcoin-cli loadwallet $wallet_name &>/dev/null || {
# Create a wallet if it doesn't exist
$CLI_NAME bitcoin-cli createwallet $wallet_name
}
# Generate some blocks to get funds
$CLI_NAME bitcoin-cli -generate 101
# Generate a new address with LND
address=$($CLI_NAME lncli --network regtest newaddress p2wkh | jq -r '.address')
# Send some funds to the new address
$CLI_NAME bitcoin-cli -named sendtoaddress address=$address amount=$amount fee_rate=1 replaceable=true
# Mine some blocks to confirm the transaction
$CLI_NAME bitcoin-cli -generate 6
printf "\nAddress $address successfully funded with $amount BTC.\n"
exit
fi
# Generate a block continuously
if [[ "$command" = "auto-mine" ]]; then
check_dependencies
check_environment
check_regtest_mode
check_inner_container "bitcoin"
# default to 5 seconds
interval=${2:-5}
wallet_name="mywallet"
# Check Bitcoin Core wallet exists
$CLI_NAME bitcoin-cli unloadwallet $wallet_name &>/dev/null || true
$CLI_NAME bitcoin-cli loadwallet $wallet_name &>/dev/null || {
# Create a wallet if it doesn't exist
$CLI_NAME bitcoin-cli createwallet $wallet_name &>/dev/null
echo "Created new Bitcoin Core wallet $wallet_name"
}
printf "Generating a block every $interval seconds. Press [CTRL+C] to stop...\n\n"
while true; do
$CLI_NAME bitcoin-cli -generate 1
sleep $interval
done
exit
fi
# Show version information for this CLI
if [[ "$command" = "--version" ]] || [[ "$command" = "-v" ]]; then
echo "$CLI_NAME v$CLI_VERSION"
exit
fi
# Show usage information for this CLI
if [[ "$command" = "--help" ]] || [[ "$command" = "-h" ]]; then
show_help
exit
fi
# If we get here it means no valid command was supplied
# Show help and exit
show_help
exit