-
Notifications
You must be signed in to change notification settings - Fork 10
/
dev
executable file
·249 lines (180 loc) · 5.53 KB
/
dev
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
#!/usr/bin/env python3
"""
Various tools for interacting with a bmon deployment; works both locally and
in production.
Should run `bmon-config` before using this tool.
The difference between `bmon-util` and this file is that the former is meant to be
run from within a docker container, whereas this is run on the host.
"""
import json
import time
import sys
import subprocess
import functools
import os
import clii
from bmon_infra import infra, config
from fscm import p, RunReturn
cli = clii.App()
os.environ["PYTHONUNBUFFERED"] = "1"
def sh(cmd, **kwargs):
return subprocess.run(cmd, shell=True, **kwargs)
@functools.cache
def getenv():
return config.get_env_object()
def is_dev() -> bool:
"""True if we're in the dev environment."""
return getenv().BMON_ENV == "dev"
def is_regtest() -> bool:
"""True if we're running on regtest."""
return (rpcport := getenv().BITCOIN_RPC_PORT) and int(rpcport) == "18443"
def brpc(cmd, **kwargs):
"""Run a bitcoin RPC command."""
flags = "-regtest" if is_regtest() else ""
return sh(
"docker-compose exec bitcoind "
f"bitcoin-cli {flags} -datadir=/bitcoin/data {cmd}",
**kwargs,
)
def dev_only(func):
"""Decorator that enforces a command is run only in the dev environment."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
if not is_dev():
print("Shouldn't be running {func} outside of a dev environment")
sys.exit(1)
else:
return func(*args, **kwargs)
return wrapper
@cli.cmd
def bitcoinrpc(*cmd):
return brpc(" ".join(cmd))
@cli.cmd
def bitcoind_wait_for_synced():
"""
Wait until bitcoind's tip is reasonably current.
This is helpful for bootstrapping new monited bitcoind instances without
generating a bunch of spurious data.
"""
tries = 12
backoff_secs = 2
is_synced = False
got = {}
i = 0
while tries and not is_synced:
try:
got = json.loads(
brpc("getblockchaininfo", text=True, capture_output=True).stdout
)
except Exception as e:
print(f"exception getting verification progress: {e}")
tries -= 1
time.sleep(backoff_secs)
if backoff_secs < 120:
backoff_secs *= 2
else:
is_synced = float(got["verificationprogress"]) > 0.9999
time.sleep(1)
tries = 12
if i % 40 == 0:
print(f"At height {got['blocks']} ({got['verificationprogress']})", flush=True)
i += 1
if not is_synced:
print("Failed to sync!")
sys.exit(1)
print(f"Synced to height: {got['blocks']}")
@cli.cmd
@dev_only
def generateblock():
wallets = json.loads(brpc("listwallets", capture_output=True).stdout)
if "test" not in wallets:
brpc("createwallet test false false '' false true true")
if '"test"' not in brpc("getwalletinfo", capture_output=True, text=True).stdout:
brpc("loadwallet test")
sh(
"docker-compose exec bitcoind bitcoin-cli -regtest -datadir=/bitcoin/data -generate"
)
@cli.cmd
def managepy(*cmd):
sh(f"docker-compose run --rm shell python manage.py {' '.join(cmd)}")
@cli.cmd
def shell():
managepy("shell")
@cli.cmd
@dev_only
def reup(
service: str = "",
rebuild_docker: bool = False,
logs: bool = False,
data: bool = False,
):
sh(f"docker-compose down {service} ; docker-compose rm -f {service} ")
if rebuild_docker:
sh("docker-compose build")
if data:
cleardata()
sh("bmon-config")
env = config.get_env_object()
p(env.BITCOIND_VERSION_PATH).contents(infra.get_bitcoind_version())
sh("docker-compose up -d db")
managepy("migrate")
sh(f"docker-compose up -d {service}")
if logs:
sh(f"docker-compose logs -f {service}")
@cli.cmd
def watchlogs(others: str = ""):
"""Tail interesting logs."""
sh(
"docker-compose logs -f bitcoind server-task-worker "
f"bitcoind-task-worker bitcoind-watcher bitcoind-mempool-worker {others}"
)
@cli.cmd
@dev_only
def cleardata():
sh("sudo rm -fr services/dev/*")
sh("bmon-config")
def _testrun(cmd: str) -> bool:
return sh(
f"docker-compose run --rm -e RUN_DB_MIGRATIONS= test -- bash -c '{cmd}'",
env={'BMON_BITCOIND_PORT': '8555',
'BMON_BITCOIND_RPC_PORT': '8554',
'BMON_REDIS_OPTIONS': '',
**os.environ},
).returncode == 0
@cli.cmd
@dev_only
def test(run_mypy: bool = False):
"""Run automated tests."""
flake8_command = "flake8 %s --count --show-source --statistics"
bmon_failed = not _testrun(flake8_command % 'bmon/')
infra_failed = not _testrun(flake8_command % 'infra/')
if run_mypy:
mypy()
test_failed = not _testrun("pytest -vv bmon")
if bmon_failed or infra_failed:
sys.exit(1)
if test_failed:
sys.exit(2)
@cli.cmd
@dev_only
def mypy():
bmon_failed = not _testrun("mypy bmon/")
infra_failed = not _testrun("mypy --exclude infra/build infra/")
if bmon_failed or infra_failed:
sys.exit(3)
@cli.cmd
@dev_only
def watchjs():
"""Watch the frontend javascript and rebuild as necessary."""
sh("docker-compose run --rm js yarn run start")
@cli.cmd
def sql_shell(db_host: str = "localhost"):
e = getenv()
db = f"postgres://bmon:{e.DB_PASSWORD}@{db_host}:5432/bmon"
print(f"Connecting to {db}")
sh(f"pgcli {db}")
@cli.cmd
def pgcli():
sql_shell()
if __name__ == "__main__":
cli.run()