Skip to content

Commit

Permalink
automounting home and cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
faermanj committed Nov 17, 2023
1 parent 479d0ef commit 03dd8f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 33 additions & 3 deletions uplib/uplib/containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os
import docker
import subprocess
from rich.console import Console
Expand Down Expand Up @@ -29,8 +30,25 @@ class ContainerRun:


class DockerContainers:
@classmethod
def volumes_of(cls, run:ContainerRun):
settings_vols = settings_maps.get("volumes", {})
cwd = os.getcwd()
home = os.path.expanduser("~")
default_vols = {
home : {
"bind": "/tmp/up_home",
"mode": "rw"
},
cwd : {
"bind": "/tmp/up_cwd",
"mode": "rw"
}
}
result = settings_vols | default_vols
return result

def run(self, run: ContainerRun):
log.info("Running container: %s", run)
client = docker.from_env()
#TODO: Catch errors, print properly, pass all params
#TODO: Locate bash properly
Expand All @@ -40,15 +58,27 @@ def run(self, run: ContainerRun):
command = ["sh", "-c", subprocess.list2cmdline(command)]
log.debug("$: %s", run)
name = run.name if run.name else generate_container_name(run)
volumes = DockerContainers.volumes_of(run)
ports = settings_maps.get("ports")
console = Console()
console.log(f"Running container: {name}")
console.log({
"name": name,
"image": run.image,
"command": command,
"auto_remove": run.auto_remove,
"volumes": volumes,
"ports": ports,
"detach": True
})
try:
container = client.containers.run(
name=name,
image=run.image,
command= command,
auto_remove=run.auto_remove,
volumes=settings_maps.get("volumes"),
ports=settings_maps.get("ports"),
volumes=volumes,
ports=ports,
detach=True
)
for line in container.logs(stream=True):
Expand Down
2 changes: 2 additions & 0 deletions uplib/uplib/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def load_ports():
settings_maps["ports"] = Config.ports.get()

def settings(prompt):
#TODO: Delegate to Settings.of_prompt(prompt)
#TODO: Create type-safe PromptConfig enum?
settings_map = settings_maps.get(prompt)
if not settings_map:
settings_map = {}
Expand Down

0 comments on commit 03dd8f5

Please sign in to comment.