-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathgoal.py
60 lines (55 loc) · 2.3 KB
/
goal.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
import logging
import click
from algokit.core import proc
from algokit.core.sandbox import ComposeSandbox
logger = logging.getLogger(__name__)
@click.command(
"goal",
short_help="Run the Algorand goal CLI against the AlgoKit LocalNet.",
context_settings={
"ignore_unknown_options": True,
},
)
@click.option(
"--console",
is_flag=True,
help="Open a Bash console so you can execute multiple goal commands and/or interact with a filesystem.",
default=False,
)
@click.argument("goal_args", nargs=-1, type=click.UNPROCESSED)
def goal_command(*, console: bool, goal_args: list[str]) -> None:
try:
proc.run(["docker", "version"], bad_return_code_error_message="Docker engine isn't running; please start it.")
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 console:
if goal_args:
logger.warning("--console opens an interactive shell, remaining arguments are being ignored")
logger.info("Opening Bash console on the algod node; execute `exit` to return to original console")
result = proc.run_interactive("docker exec -it -w /root algokit_algod bash".split())
else:
cmd = "docker exec --interactive --workdir /root algokit_algod goal".split()
cmd.extend(goal_args)
result = proc.run(
cmd,
stdout_log_level=logging.INFO,
prefix_process=False,
pass_stdin=True,
)
if result.exit_code != 0:
sandbox = ComposeSandbox()
ps_result = sandbox.ps("algod")
match ps_result:
case [{"State": "running"}]:
pass # container is running, failure must have been with command
case _:
logger.warning(
"algod container does not appear to be running, "
"ensure localnet is started by executing `algokit localnet start`"
)
raise click.exceptions.Exit(result.exit_code) # pass on the exit code