Mount host repositories for Docker execution with host user ownership

This commit is contained in:
2026-10-10 01:22:02 +01:00
parent 7565693957
commit 68042aad5d
10 changed files with 161 additions and 12 deletions
+9 -1
View File
@@ -25,6 +25,12 @@ def host_timezone():
continue
raise ValueError("Cannot detect the host timezone; provide --timezone explicitly")
def host_workspaces(directory):
home, repository = str(Path.home().resolve()), str(Path(directory).resolve())
if any(any(char in value for char in "\r\n\x00$#'\"\\") or value in ("/", "/data", "/workspace", "/home/pipelines") for value in [home, repository]) or home == repository:
raise ValueError("Host home/checkout cannot be mounted safely at its original path; configure a repository bind mount explicitly")
return {"PIPELINES_HOST_HOME": "'"+home+"'", "PIPELINES_HOST_REPOSITORY": "'"+repository+"'", "PIPELINES_UID": str(os.getuid()), "PIPELINES_GID": str(os.getgid())}
def configure(directory, ntfy_url, bind_address, timezone=None, port=8081):
directory = Path(directory)
@@ -42,6 +48,7 @@ def configure(directory, ntfy_url, bind_address, timezone=None, port=8081):
settings = directory / ".env"
if settings.exists():
raise ValueError(".env exists; edit it rather than overwrite existing settings")
workspace_values = host_workspaces(directory)
secrets = directory / "secrets"
secrets.mkdir(mode=0o700, exist_ok=True)
token = secrets / "ntfy-token.txt"
@@ -53,7 +60,8 @@ def configure(directory, ntfy_url, bind_address, timezone=None, port=8081):
pass
with settings.open("x") as file:
file.write(f"TZ={timezone}\nNTFY_BASE_URL={ntfy_url.rstrip('/')}\n"
f"NTFY_BIND_ADDRESS={bind_address}\nPIPELINES_PORT={port}\n")
f"NTFY_BIND_ADDRESS={bind_address}\nPIPELINES_PORT={port}\n")
for key, value in workspace_values.items(): file.write(key+"="+value+"\n")
return timezone
+32
View File
@@ -197,6 +197,34 @@ exec pipelines provider-check --model "$1" --yes --json --timeout 90s
updated = updated.rstrip("\n")+"\n"+key+address+"\n"
path.write_text(updated)
def configure_workspaces(self):
home = str(Path.home().resolve())
path = self.directory/".env"
values = configure.host_workspaces(self.directory)
lines = path.read_text().splitlines(keepends=True)
text = "".join(key+"="+values[key]+"\n" if (key:=line.split("=",1)[0]) in values else line for line in lines)
for key,value in values.items():
if not any(line.startswith(key+"=") for line in lines): text=text.rstrip("\n")+"\n"+key+"="+value+"\n"
path.write_text(text)
print("Host repositories mounted at their original paths under " + home + "; server runs with host UID/GID. Stage tool/edit permissions still apply.")
def prepare_volume_ownership(self):
# Existing volumes may belong to the old image UID 10001. Change only
# ownership on container-managed data/auth volumes, never the host home.
resolved = json.loads(self.compose("config", "--format", "json", capture=True).stdout)
mounts = resolved.get("services", {}).get("pipelines", {}).get("volumes", [])
for target in ("/data", "/home/pipelines", "/workspace"):
if not any(mount.get("target") == target and mount.get("type") == "volume" for mount in mounts):
raise ValueError("Refusing ownership migration: " + target + " is not a managed Docker volume; no host directory ownership was changed")
self.compose("run", "--rm", "--no-deps", "-T", "--user", "0:0", "--entrypoint", "sh", "pipelines",
"-c", 'set -eu; chown -R "$PIPELINES_UID:$PIPELINES_GID" /data /home/pipelines /workspace')
def verify_workspace(self):
directory = str(self.directory.resolve())
self.compose("exec", "-T", "pipelines", "sh", "-c", 'set -eu; test -d "$1"; test -r "$1"; test -x "$1"; cd "$1"; test "$(pwd -P)" = "$1"',
"pipelines-workspace-check", directory, capture=True)
print("Verified: Docker server can enter the host installation repository at " + directory)
def verify(self):
# Read the token inside the container; never put it in argv or output.
probe = '''set -eu
@@ -275,17 +303,21 @@ printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FI
self.compose("version", capture=True)
self.settings(args)
self.publish_address()
self.configure_workspaces()
self.update_host_client()
# Validate the actual resolved Compose configuration before starting anything.
self.compose("config", "--quiet", capture=True)
self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy")
self.bootstrap_ntfy()
self.compose("build", "pipelines")
self.compose("stop", "--timeout", "60", "pipelines")
self.prepare_volume_ownership()
self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install")
self.authenticate(args.relogin)
self.validate_provider(args)
# Recreate to reload file secrets and environment, without deleting volumes.
self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines")
self.verify_workspace()
self.verify()
print("Docker setup complete. Use docker compose ps / logs to manage it; no OS user service was registered.")
+37
View File
@@ -51,6 +51,41 @@ class DockerSetupTests(unittest.TestCase):
with patch.object(setup.shutil,"which",return_value="/fixture/go"), self.assertRaises(ValueError):
setup.Setup(".",runner=runner).verify_host_endpoint("ws://127.0.0.1:8081/connect")
def test_workspace_mount_uses_host_home_uid_and_preserves_settings(self):
with tempfile.TemporaryDirectory() as directory:
path=Path(directory,".env");path.write_text("CUSTOM=keep\nPIPELINES_UID=10001\n")
setup.Setup(directory).configure_workspaces()
content=path.read_text()
self.assertIn("CUSTOM=keep\n",content)
self.assertIn("PIPELINES_HOST_HOME='"+str(Path.home().resolve())+"'",content)
self.assertIn("PIPELINES_UID="+str(os.getuid()),content)
self.assertIn("PIPELINES_GID="+str(os.getgid()),content)
setup.Setup(directory).configure_workspaces()
self.assertEqual(path.read_text(),content)
def test_ownership_migration_and_workspace_verification_are_scoped(self):
calls=[]
def runner(command,**kwargs):
calls.append(command)
config={"services":{"pipelines":{"volumes":[{"type":"volume","target":target} for target in ["/data","/home/pipelines","/workspace"]]}}}
return subprocess.CompletedProcess(command,0,stdout=setup.json.dumps(config) if "--format" in command else "",stderr="")
installer=setup.Setup(".",runner=runner)
installer.prepare_volume_ownership()
installer.verify_workspace()
self.assertIn("0:0",calls[1])
self.assertEqual(calls[1][-1], 'set -eu; chown -R "$PIPELINES_UID:$PIPELINES_GID" /data /home/pipelines /workspace')
self.assertEqual(calls[2][-1],str(Path(".").resolve()))
self.assertIn('cd "$1"',calls[2][-3])
def test_ownership_migration_never_chowns_host_bind_mounts(self):
calls=[]
def runner(command,**kwargs):
calls.append(command)
return subprocess.CompletedProcess(command,0,stdout='{"services":{"pipelines":{"volumes":[{"type":"bind","target":"/data"}]}}}',stderr="")
with self.assertRaises(ValueError): setup.Setup(".",runner=runner).prepare_volume_ownership()
self.assertEqual(len(calls),1)
self.assertFalse(any("chown" in part for part in calls[0]))
def test_validation_primes_its_own_service_and_passes_model_as_argument(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);(root/".env").write_text("CUSTOM=keep\n")
@@ -264,6 +299,8 @@ fi
out = '{"ntfy":{"health":"healthy","url":"http://192.168.1.20:8090"},"topic":"pipelines"}'
if args[:1] == ["port"]:
out = "127.0.0.1:8081\n"
if args == ["config", "--format", "json"]:
out = setup.json.dumps({"services":{"pipelines":{"volumes":[{"type":"volume","target":target} for target in ["/data","/home/pipelines","/workspace"]]}}})
if fail and (fail in args or (fail=="login" and "exec opencode auth login" in args[-1]) or (fail=="provider-check" and any("provider-check" in arg for arg in args))):
code, out = 9, "private provider data/token"
if fail == "host" and command[0] != "docker" and args[-2:] == ["status", "--json"]: