-
Notifications
You must be signed in to change notification settings - Fork 47
/
localnet.py
184 lines (157 loc) · 7.02 KB
/
localnet.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
import logging
import click
from algokit.cli.explore import explore_command
from algokit.cli.goal import goal_command
from algokit.core import proc
from algokit.core.sandbox import (
DOCKER_COMPOSE_MINIMUM_VERSION,
DOCKER_COMPOSE_VERSION_COMMAND,
ComposeFileStatus,
ComposeSandbox,
fetch_algod_status_data,
fetch_indexer_status_data,
)
from algokit.core.utils import extract_version_triple, is_minimum_version
logger = logging.getLogger(__name__)
@click.group("localnet", short_help="Manage the AlgoKit LocalNet.")
def localnet_group() -> None:
try:
compose_version_result = proc.run(DOCKER_COMPOSE_VERSION_COMMAND)
except OSError as ex:
# an IOError (such as PermissionError or FileNotFoundError) will only occur if "docker"
# isn't an executable in the user's path, which means docker isn't installed
raise click.ClickException(
"Docker not found; please install Docker and add to path.\n"
"See https://docs.docker.com/get-docker/ for more information."
) from ex
if compose_version_result.exit_code != 0:
raise click.ClickException(
"Docker Compose not found; please install Docker Compose and add to path.\n"
"See https://docs.docker.com/compose/install/ for more information."
)
try:
compose_version_str = extract_version_triple(compose_version_result.output)
compose_version_ok = is_minimum_version(compose_version_str, DOCKER_COMPOSE_MINIMUM_VERSION)
except Exception:
logger.warning(
"Unable to extract docker compose version from output: \n"
+ compose_version_result.output
+ f"\nPlease ensure a minimum of compose v{DOCKER_COMPOSE_MINIMUM_VERSION} is used",
exc_info=True,
)
else:
if not compose_version_ok:
raise click.ClickException(
f"Minimum docker compose version supported: v{DOCKER_COMPOSE_MINIMUM_VERSION}, "
f"installed = v{compose_version_str}\n"
"Please update your Docker install"
)
proc.run(["docker", "version"], bad_return_code_error_message="Docker engine isn't running; please start it.")
@localnet_group.command("start", short_help="Start the AlgoKit LocalNet.")
def start_localnet() -> None:
sandbox = ComposeSandbox()
compose_file_status = sandbox.compose_file_status()
sandbox.check_docker_compose_for_new_image_versions()
if compose_file_status is ComposeFileStatus.MISSING:
logger.debug("Sandbox compose file does not exist yet; writing it out for the first time")
sandbox.write_compose_file()
elif compose_file_status is ComposeFileStatus.UP_TO_DATE:
logger.debug("Sandbox compose file does not require updating")
else:
logger.warning("Sandbox definition is out of date; please run algokit localnet reset")
sandbox.up()
@localnet_group.command("stop", short_help="Stop the AlgoKit LocalNet.")
def stop_localnet() -> None:
sandbox = ComposeSandbox()
compose_file_status = sandbox.compose_file_status()
if compose_file_status is ComposeFileStatus.MISSING:
logger.debug(
"Sandbox compose file does not exist yet; run `algokit localnet start` to start the AlgoKit LocalNet"
)
else:
sandbox.stop()
@localnet_group.command("reset", short_help="Reset the AlgoKit LocalNet.")
@click.option(
"--update/--no-update",
default=False,
help="Enable or disable updating to the latest available LocalNet version, default: don't update",
)
def reset_localnet(*, update: bool) -> None:
sandbox = ComposeSandbox()
compose_file_status = sandbox.compose_file_status()
if compose_file_status is ComposeFileStatus.MISSING:
logger.debug("Existing LocalNet not found; creating from scratch...")
sandbox.write_compose_file()
else:
sandbox.down()
if compose_file_status is not ComposeFileStatus.UP_TO_DATE:
logger.info("Sandbox definition is out of date; updating it to latest")
sandbox.write_compose_file()
if update:
sandbox.pull()
else:
sandbox.check_docker_compose_for_new_image_versions()
sandbox.up()
SERVICE_NAMES = ("algod", "indexer", "indexer-db")
@localnet_group.command("status", short_help="Check the status of the AlgoKit LocalNet.")
def localnet_status() -> None:
sandbox = ComposeSandbox()
ps = sandbox.ps()
ps_by_name = {stats["Service"]: stats for stats in ps}
# if any of the required containers does not exist (ie it's not just stopped but hasn't even been created),
# then they will be missing from the output dictionary
if set(SERVICE_NAMES) != ps_by_name.keys():
raise click.ClickException("LocalNet has not been initialized yet, please run 'algokit localnet start'")
# initialise output dict by setting status
output_by_name = {
name: {"Status": "Running" if ps_by_name[name]["State"] == "running" else "Not running"}
for name in SERVICE_NAMES
}
# fill out remaining output_by_name["algod"] values
if output_by_name["algod"]["Status"] == "Running":
output_by_name["algod"].update(fetch_algod_status_data(ps_by_name["algod"]))
# fill out remaining output_by_name["indexer"] values
if output_by_name["indexer"]["Status"] == "Running":
output_by_name["indexer"].update(fetch_indexer_status_data(ps_by_name["indexer"]))
# Print the status details
for service_name, service_info in output_by_name.items():
logger.info(click.style(f"# {service_name} status", bold=True))
for key, value in service_info.items():
logger.info(click.style(f"{key}:", bold=True) + f" {value}")
# return non-zero if any container is not running
if not all(item["Status"] == "Running" for item in output_by_name.values()):
raise click.ClickException(
"At least one container isn't running; execute `algokit localnet start` to start the LocalNet"
)
@localnet_group.command(
"console",
short_help="Run the Algorand goal CLI against the AlgoKit LocalNet via a Bash console"
" so you can execute multiple goal commands and/or interact with a filesystem.",
)
@click.pass_context
def localnet_console(context: click.Context) -> None:
context.invoke(goal_command, console=True)
@localnet_group.command("explore", short_help="Explore the AlgoKit LocalNet using Dappflow")
@click.pass_context
def localnet_explore(context: click.Context) -> None:
context.invoke(explore_command)
@localnet_group.command(
"logs",
short_help="See the output of the Docker containers",
)
@click.option(
"--follow/-f",
is_flag=True,
help="Follow log output.",
default=False,
)
@click.option(
"--tail",
default="all",
help="Number of lines to show from the end of the logs for each container.",
show_default=True,
)
@click.pass_context
def localnet_logs(ctx: click.Context, *, follow: bool, tail: str) -> None:
sandbox = ComposeSandbox()
sandbox.logs(follow=follow, no_color=ctx.color is False, tail=tail)