-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevenv
executable file
·211 lines (179 loc) · 6.81 KB
/
devenv
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
#!/usr/bin/env bash
#
# Mostro dev environment.
readonly PROJECT_ROOT="$(cd "$(dirname $(realpath "${BASH_SOURCE[0]}"))" && pwd)"
readonly SCRIPTS_DIR="$PROJECT_ROOT/scripts"
# Import common script configurations and utilities
source "$SCRIPTS_DIR/_common.sh" || exit 1
source "$SCRIPTS_DIR/docker.sh" || exit 1
source "$SCRIPTS_DIR/bitcoin.sh" || exit 1
source "$SCRIPTS_DIR/lightning.sh" || exit 1
source "$SCRIPTS_DIR/nostr.sh" || exit 1
source "$SCRIPTS_DIR/mostro.sh" || exit 1
readonly SCRIPT_NAME=${0##*/}
readonly SCRIPT_VERSION=0.0.1
readonly SCRIPT_DESCRIPTION="Mostro dev environment"
readonly COMMANDS=( \
help version start restart stop sh info private_key public_key bitcoin_cli \
mostro_lncli alice_lncli bob_lncli nak mostro_cli create_random_orders clean \
)
readonly LONG_OPTS=(help version log-level:)
readonly SHORT_OPTS=hv
readonly MOSTRO_MNEMONIC="monster monster monster monster monster monster monster monster monster monster monster trade"
readonly ALICE_MNEMONIC="test test test test test test test test test test test alone"
readonly BOB_MNEMONIC="test test test test test test test test test test test boy"
# -----------------------------------------------------------------------------
# Initialization
# -----------------------------------------------------------------------------
function on_init() {
load_environment_if_exists "$PROJECT_ROOT/.env"
required docker jq
if ! command -v docker version &> /dev/null; then
fatal "dockerd must be running"
fi
}
function on_option() {
case "$1" in
h|help) execute_help ;;
v|version) execute_version ;;
log-level) log_level "$OPTARG" ;;
*) fatal "Internal script error, unmatched option '$1'" ;;
esac
}
# -----------------------------------------------------------------------------
# Commands
# -----------------------------------------------------------------------------
function execute_command() {
local cmd="$1"
shift
case "$cmd" in
help) execute_help ;;
version) execute_version ;;
start) execute_start "$@" ;;
restart) execute_restart "$@" ;;
stop) execute_stop "$@" ;;
sh) execute_sh "$@" ;;
info) execute_info "$@" ;;
private_key) execute_private_key "$@" ;;
public_key) execute_public_key "$@" ;;
bitcoin_cli) execute_bitcoin_cli "$@" ;;
mostro_lncli) execute_lncli mostro_lnd "$@" ;;
alice_lncli) execute_lncli alice_lnd "$@" ;;
bob_lncli) execute_lncli bob_lnd "$@" ;;
nak) execute_nak "$@" ;;
mostro_cli) execute_mostro_cli "$@" ;;
create_random_orders) execute_create_random_orders "$@" ;;
clean) execute_clean "$@" ;;
*) execute_help ;;
esac
}
function execute_start() {
info start dev environment
docker_compose up -d --remove-orphans "$@"
bitcoin_init
lightning_init
}
function execute_restart() {
info restart dev environment
execute_clean
execute_start --build "$@"
}
function execute_stop() {
info stop dev environment
docker_compose stop
}
function execute_sh() {
local service_name="${1-}"
[[ -z "$service_name" ]] && fatal "Service name is required"
if ! docker_compose ps --services | grep -q "^${service_name}$"; then
fatal "Service '$service_name' not found"
fi
info sh into $service_name
docker_compose exec $service_name bash 2>/dev/null || docker_compose exec $service_name sh
}
function execute_info() {
printf -- "identity key\n - derivation path: $MOSTRO_DERIVATION_PATH/0\n\n"
local private_key public_key
private_key=$(mostro_identity_private_key)
public_key=$(mostro_identity_public_key)
printf -- "mostro\n - mnemonic: $MOSTRO_MNEMONIC\n - private key: $private_key\n - public key: $public_key\n\n"
private_key=$(alice_identity_private_key)
public_key=$(alice_identity_public_key)
printf -- "alice\n - mnemonic: $ALICE_MNEMONIC\n - private key: $private_key\n - public key: $public_key\n\n"
private_key=$(bob_identity_private_key)
public_key=$(bob_identity_public_key)
printf -- "bob\n - mnemonic: $BOB_MNEMONIC\n - private key: $private_key\n - public key: $public_key\n"
}
function execute_private_key() {
private_key_from_mnemonic_path "$@"
}
function execute_public_key() {
public_key_from_private_key "$@"
}
function execute_bitcoin_cli() {
bitcoin_cli "$@"
}
function execute_lncli() {
lncli "$@"
}
function execute_nak() {
nak "$@"
}
function execute_mostro_cli() {
mostro_cli "$@"
}
function execute_create_random_orders() {
mostro_create_random_orders
}
function execute_clean() {
read -p "This will remove containers and volumes. Are you sure? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
fatal "operation cancelled"
fi
info clean up dev environment
docker_compose down -v
docker_compose rm -fsv
}
function version_message() {
printf "%s mostro version %s\n" ${SCRIPT_NAME-0##*/} ${SCRIPT_VERSION-unknown}
}
function help_message() {
cat <<END
$SCRIPT_DESCRIPTION
Usage:
$SCRIPT_NAME [options] [command] [args]
Available Commands:
start Start dev environment.
restart Restart dev environment.
stop Stop dev environment.
sh <service_name> Open a shell inside a running container.
info Dev env info.
private_key <mnemonic> <path> Generate private key from mnemonic and
derivation path.
public_key <private_key> Generate public key from private key.
bitcoin_cli [args] Bitcoind client.
mostro_lncli [args] Mostro lnd client.
alice_lncli [args] Alice lnd client.
bob_lncli [args] Bob lnd client.
nak [args] Nostr relay client.
mostro_cli [args] Mostro client.
create_random_orders Create random generated Mostro orders.
clean Clean dev environment.
help Display detailed help.
version Print version information.
Options:
--help, -h Alias help command.
--version, -v Alias version command.
--log-level <level> Set the log level severity. Lower level will be
ignored. Must be an integer or a level name:
${SEVERITY_RANGES_NAMES[@]}.
-- Denotes the end of the options. Arguments after this
will be handled as parameters even if they start with
a '-'.
END
}
# -----------------------------------------------------------------------------
# Run the script
# -----------------------------------------------------------------------------
parse_and_execute "$@"