-
Notifications
You must be signed in to change notification settings - Fork 47
/
sandbox.py
287 lines (245 loc) · 10.3 KB
/
sandbox.py
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
import enum
import json
import logging
import time
from pathlib import Path
from typing import Any, cast
import httpx
from algokit.core.conf import get_app_config_dir
from algokit.core.proc import RunResult, run, run_interactive
logger = logging.getLogger(__name__)
DOCKER_COMPOSE_MINIMUM_VERSION = "2.5.0"
class ComposeFileStatus(enum.Enum):
MISSING = enum.auto()
UP_TO_DATE = enum.auto()
OUT_OF_DATE = enum.auto()
class ComposeSandbox:
def __init__(self) -> None:
self.directory = get_app_config_dir() / "sandbox"
if not self.directory.exists():
logger.debug("Sandbox directory does not exist yet; creating it")
self.directory.mkdir()
self._latest_yaml = get_docker_compose_yml()
self._latest_config_json = get_config_json()
@property
def compose_file_path(self) -> Path:
return self.directory / "docker-compose.yml"
@property
def algod_config_file_path(self) -> Path:
return self.directory / "algod_config.json"
def compose_file_status(self) -> ComposeFileStatus:
try:
compose_content = self.compose_file_path.read_text()
config_content = self.algod_config_file_path.read_text()
except FileNotFoundError:
# treat as out of date if compose file exists but algod config doesn't
# so that existing setups aren't suddenly reset
if self.compose_file_path.exists():
return ComposeFileStatus.OUT_OF_DATE
return ComposeFileStatus.MISSING
else:
if compose_content == self._latest_yaml and config_content == self._latest_config_json:
return ComposeFileStatus.UP_TO_DATE
else:
return ComposeFileStatus.OUT_OF_DATE
def write_compose_file(self) -> None:
self.compose_file_path.write_text(self._latest_yaml)
self.algod_config_file_path.write_text(self._latest_config_json)
def _run_compose_command(
self,
compose_args: str,
stdout_log_level: int = logging.INFO,
bad_return_code_error_message: str | None = None,
) -> RunResult:
return run(
["docker", "compose", *compose_args.split()],
cwd=self.directory,
stdout_log_level=stdout_log_level,
bad_return_code_error_message=bad_return_code_error_message,
)
def up(self) -> None:
logger.info("Starting AlgoKit LocalNet now...")
self._run_compose_command(
"up --detach --quiet-pull --wait", bad_return_code_error_message="Failed to start LocalNet"
)
logger.debug("AlgoKit LocalNet started, waiting for health check")
if _wait_for_algod():
logger.info("Started; execute `algokit explore` to explore LocalNet in a web user interface.")
else:
logger.warning("AlgoKit LocalNet failed to return a successful health check")
def stop(self) -> None:
logger.info("Stopping AlgoKit LocalNet now...")
self._run_compose_command("stop", bad_return_code_error_message="Failed to stop LocalNet")
logger.info("Sandbox Stopped; execute `algokit localnet start` to start it again.")
def down(self) -> None:
logger.info("Deleting any existing LocalNet...")
self._run_compose_command("down", stdout_log_level=logging.DEBUG)
def pull(self) -> None:
logger.info("Looking for latest Sandbox images from DockerHub...")
self._run_compose_command("pull --ignore-pull-failures --quiet")
def logs(self, *, follow: bool = False, no_color: bool = False, tail: str | None = None) -> None:
compose_args = ["logs"]
if follow:
compose_args += ["--follow"]
if no_color:
compose_args += ["--no-color"]
if tail is not None:
compose_args += ["--tail", tail]
run_interactive(
["docker", "compose", *compose_args],
cwd=self.directory,
bad_return_code_error_message="Failed to get logs, are the containers running?",
)
def ps(self, service_name: str | None = None) -> list[dict[str, Any]]:
run_results = self._run_compose_command(
f"ps {service_name or ''} --format json", stdout_log_level=logging.DEBUG
)
if run_results.exit_code != 0:
return []
data = json.loads(run_results.output)
assert isinstance(data, list)
return cast(list[dict[str, Any]], data)
DEFAULT_ALGOD_SERVER = "http://localhost"
DEFAULT_ALGOD_TOKEN = "a" * 64
DEFAULT_ALGOD_PORT = 4001
DEFAULT_INDEXER_PORT = 8980
DEFAULT_WAIT_FOR_ALGOD = 60
DEFAULT_HEALTH_TIMEOUT = 1
ALGOD_HEALTH_URL = f"{DEFAULT_ALGOD_SERVER}:{DEFAULT_ALGOD_PORT}/v2/status"
def _wait_for_algod() -> bool:
end_time = time.time() + DEFAULT_WAIT_FOR_ALGOD
last_exception: httpx.RequestError | None = None
while time.time() < end_time:
try:
health = httpx.get(
ALGOD_HEALTH_URL, timeout=DEFAULT_HEALTH_TIMEOUT, headers={"X-Algo-API-Token": DEFAULT_ALGOD_TOKEN}
)
except httpx.RequestError as ex:
last_exception = ex
else:
if health.is_success:
logger.debug("AlgoKit LocalNet health check successful, algod is ready")
return True
logger.debug(f"AlgoKit LocalNet health check returned {health.status_code}, waiting")
time.sleep(DEFAULT_HEALTH_TIMEOUT)
if last_exception:
logger.debug("AlgoKit LocalNet health request failed", exc_info=last_exception)
return False
def get_config_json() -> str:
return (
'{ "Version": 12, "GossipFanout": 1, "EndpointAddress": "0.0.0.0:8080", "DNSBootstrapID": "",'
' "IncomingConnectionsLimit": 0, "Archival":false, "isIndexerActive":false, "EnableDeveloperAPI":true}"'
)
def get_docker_compose_yml(
name: str = "algokit",
algod_port: int = DEFAULT_ALGOD_PORT,
kmd_port: int = 4002,
tealdbg_port: int = 9392,
indexer_port: int = DEFAULT_INDEXER_PORT,
) -> str:
return f"""version: '3'
name: "{name}_sandbox"
services:
algod:
container_name: {name}_algod
image: algorand/algod:latest
ports:
- {algod_port}:8080
- {kmd_port}:7833
- {tealdbg_port}:9392
environment:
DEV_MODE: 1
START_KMD: 1
TOKEN: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
KMD_TOKEN: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
volumes:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
indexer:
container_name: {name}_indexer
image: makerxau/algorand-indexer-dev:latest
ports:
- {indexer_port}:8980
restart: unless-stopped
environment:
ALGOD_HOST: algod
ALGOD_PORT: 8080
POSTGRES_HOST: indexer-db
POSTGRES_PORT: 5432
POSTGRES_USER: algorand
POSTGRES_PASSWORD: algorand
POSTGRES_DB: indexer_db
depends_on:
- indexer-db
- algod
indexer-db:
container_name: {name}_postgres
image: postgres:13-alpine
ports:
- 5443:5432
user: postgres
environment:
POSTGRES_USER: algorand
POSTGRES_PASSWORD: algorand
POSTGRES_DB: indexer_db
"""
def fetch_algod_status_data(service_info: dict[str, Any]) -> dict[str, Any]:
results: dict[str, Any] = {}
try:
# Docker image response
# Search for DEFAULT_ALGOD_PORT in ports, if found use it, if not found this is an error
if not any(item["PublishedPort"] == DEFAULT_ALGOD_PORT for item in service_info["Publishers"]):
return {"Status": "Error"}
results["Port"] = DEFAULT_ALGOD_PORT
# container specific response
with httpx.Client() as client:
algod_headers = {"X-Algo-API-Token": DEFAULT_ALGOD_TOKEN}
http_status_response = client.get(
f"{DEFAULT_ALGOD_SERVER}:{DEFAULT_ALGOD_PORT}/v2/status", headers=algod_headers, timeout=3
)
http_versions_response = client.get(
f"{DEFAULT_ALGOD_SERVER}:{DEFAULT_ALGOD_PORT}/versions", headers=algod_headers, timeout=3
)
if (
http_status_response.status_code != httpx.codes.OK
or http_versions_response.status_code != httpx.codes.OK
):
return {"Status": "Error"}
# status response
status_response = http_status_response.json()
results["Last round"] = status_response["last-round"]
results["Time since last round"] = "%.1fs" % (status_response["time-since-last-round"] / 1e9)
# genesis response
genesis_response = http_versions_response.json()
results["Genesis ID"] = genesis_response["genesis_id"]
results["Genesis hash"] = genesis_response["genesis_hash_b64"]
major_version = genesis_response["build"]["major"]
minor_version = genesis_response["build"]["minor"]
build_version = genesis_response["build"]["build_number"]
results["Version"] = f"{major_version}.{minor_version}.{build_version}"
return results
except Exception as err:
logger.debug(f"Error checking algod status: {err}", exc_info=True)
return {"Status": "Error"}
def fetch_indexer_status_data(service_info: dict[str, Any]) -> dict[str, Any]:
results: dict[str, Any] = {}
try:
# Docker image response
if not any(item["PublishedPort"] == DEFAULT_INDEXER_PORT for item in service_info["Publishers"]):
return {"Status": "Error"}
results["Port"] = service_info["Publishers"][0]["PublishedPort"]
# container specific response
health_url = f"{DEFAULT_ALGOD_SERVER}:{DEFAULT_INDEXER_PORT}/health"
http_response = httpx.get(health_url, timeout=5)
if http_response.status_code != httpx.codes.OK:
return {"Status": "Error"}
response = http_response.json()
logger.debug(f"{health_url} response: {response}")
results["Last round"] = response["round"]
results["Version"] = response["version"]
return results
except Exception as err:
logger.debug(f"Error checking indexer status: {err}", exc_info=True)
return {"Status": "Error"}
DOCKER_COMPOSE_VERSION_COMMAND = ["docker", "compose", "version", "--format", "json"]