Changes to allow exporting robot env

This commit is contained in:
Alex Davies 2025-02-27 16:41:45 -04:00
parent f0fa9f4ae9
commit 0d9db01ced

View File

@ -100,6 +100,8 @@ class Robot:
"""Start a Docker-in-Docker instance for the robot, using a registry cache."""
client = docker.from_env()
cache_proxy = "http://sdk-host.internal:3128"
# sdk_root = os.environ.get("SDK_ROOT")
# buildx_shared_cache = str((Path(sdk_root) / "/cache/buildx-cache").absolute())
self.docker_daemon = client.containers.run(
"docker:dind",
@ -115,10 +117,16 @@ class Robot:
),
environment={
"DOCKER_BUILDKIT": "1",
"DOCKER_DRIVER": "overlay2",
# "DOCKER_TLS_CERTDIR": "",
},
extra_hosts={"sdk-host.internal": "host-gateway"},
# remove=True,
volumes={
"/var/lib/docker/buildx-cache": {
"bind": "/var/lib/docker/buildx-cache",
"mode": "rw",
},
},
detach=True,
)
@ -260,7 +268,6 @@ class Robot:
self.robot_ui.clear()
with self.robot_ui:
ui.label(f"{self}").classes("text-2xl")
ui.label(f"DOCKER_HOST={self.docker_host}")
ui.button("Start", on_click=self.async_start).classes("m-2")
ui.button("Stop", on_click=self.stop).classes("m-2")
@ -277,6 +284,7 @@ class Robot:
with ui.tabs() as tabs:
# tab_logs = ui.tab("Logs")
tab_containers = ui.tab("Containers")
tab_environment = ui.tab("Environment")
# tabs_iframe = ui.tab("ui")
# with ui.tab_panels(tabs, value=tab_logs):
# tab = ui.tab_panel(tab_logs).classes("w-full")
@ -285,6 +293,38 @@ class Robot:
containers = ui.tab_panel(tab_containers).classes("w-full")
asyncio.create_task(self.ui_containers(containers))
with ui.tab_panel(tab_environment):
environment = []
for key, value in self.environ().items():
environment.append(f"{key}={value}")
for key, value in self.extra_env().items():
environment.append(f"{key}={value}")
with ui.row():
ui.button(
"Copy to clipboard",
on_click=lambda: ui.clipboard.write(
"export " + " ".join(environment)
),
)
ui.button(
"Save to file",
on_click=lambda: ui.download(
"\n".join(environment).encode("utf-8"),
f"sdk_env_{self.robot_name}.txt",
),
)
env_vars = ui.codemirror(
value="\n".join(environment),
language="bash",
theme="basicDark",
)
env_vars.enabled = False
ui.label(
"These environment variables allow running arbitrary docker containers and stacks "
"in the same context as the robot, as well as interacting with the simulated robot's "
"docker daemon. Simply copy to clipboard, paste into your terminal, and your docker "
"cli and other docker tools will start using the robot's environment."
).classes("text-sm")
# iframe = ui.tab_panel(tabs_iframe).classes("w-full")
# with iframe:
# ui.link(f"http://{self.robot_ip}",f"http://{self.robot_ip}").classes("m-2")
@ -308,15 +348,20 @@ class Robot:
def extra_env(self):
return {}
def environ(self):
return dict(
ROBOT_NAME=self.robot_name,
WORLD_NAME="citadel_hill",
DOCKER_HOST=self.docker_host,
)
def start(self):
"""Starts the simulated drone with a given sysid,
each drone must have it's own unique ID.
"""
with logger.contextualize(drone=str(self)):
with modified_environ(
ROBOT_NAME=self.robot_name,
WORLD_NAME="citadel_hill",
DOCKER_HOST=self.docker_host,
**self.environ(),
**self.extra_env(),
):
self.spawn_gz_model()