From 5f06d7e04c036e289b6c71a7005fd37aaca5b178 Mon Sep 17 00:00:00 2001 From: retoor Date: Wed, 11 Jun 2025 01:21:21 +0200 Subject: [PATCH] Update. --- DockerfileUbuntu | 6 +++++ src/snek/service/container.py | 2 +- src/snek/system/docker.py | 37 ++++++++++++++++++--------- terminal/.welcome.txt | 47 +++++++++++++++++++++++++++++++++++ terminal/entry | 26 +++++++++++++++++++ 5 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 terminal/.welcome.txt create mode 100755 terminal/entry diff --git a/DockerfileUbuntu b/DockerfileUbuntu index 161cdf0..4faea80 100644 --- a/DockerfileUbuntu +++ b/DockerfileUbuntu @@ -10,3 +10,9 @@ RUN chmod +x r RUN mv r /usr/local/bin/r +RUN echo 'root:root' | chpasswd + +COPY ./terminal /opt/bootstrap +COPY ./terminal /opt/snek +RUN cp -r ./root /opt/bootrap/root +COPY ./terminal/entry /usr/local/bin/entry diff --git a/src/snek/service/container.py b/src/snek/service/container.py index 0a00f27..cae55ba 100644 --- a/src/snek/service/container.py +++ b/src/snek/service/container.py @@ -84,7 +84,7 @@ class ContainerService(BaseService): "./" + str(await self.services.channel.get_home_folder(channel_uid)) + ":" - + "/home/ubuntu" + + "/root" ], ) return await self.compose.get_instance(name) diff --git a/src/snek/system/docker.py b/src/snek/system/docker.py index a67bcba..c390b94 100644 --- a/src/snek/system/docker.py +++ b/src/snek/system/docker.py @@ -66,7 +66,9 @@ class ComposeFileManager: "context": ".", "dockerfile": "DockerfileUbuntu", }, - "user":"ubuntu" + "user":"root", + "working_dir": "/home/retoor/projects/snek", + "environment": [f"SNEK_UID={name}"], } service["command"] = command or "tail -f /dev/null" if cpus or memory: @@ -165,14 +167,13 @@ class ComposeFileManager: stdout = b'' stderr = str(ex).encode() await self.event_handler(name,"stdout",stdout or stderr) - if proc.returncode != 0: - raise RuntimeError(f"Failed to stop {name}: {stderr.decode()}") + print("Return code", proc.returncode) if stdout: - await self.event_handler(name,"stdout",stdout) - return stdout.decode(errors="ignore") + await self.event_handler(name,"stdout",stdout or b'') + return stdout and stdout.decode(errors="ignore") or "" - await self.event_handler(name,"stdout",stderr) - return stderr.decode(errors="ignore") + await self.event_handler(name,"stdout",stderr or b'') + return stderr and stderr.decode(errors="ignore") or "" async def start(self, name): """Asynchronously start a container by doing 'docker compose up -d [name]'.""" @@ -187,15 +188,27 @@ class ComposeFileManager: proc = await asyncio.create_subprocess_exec( "docker", "compose", "-f", self.compose_path, "up", name, "-d", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, stdin=asyncio.subprocess.PIPE, ) - stdout,stderr = await proc.communicate() - if proc.returncode != 0: - print(f"Failed to start {name}: {stderr.decode(errors='ignore')}") - return False + stdout, stderr = None, None + while proc.returncode is None: + try: + stdout, stderr = await proc.communicate() + except Exception as ex: + stdout = b'' + stderr = str(ex).encode() + if stdout: + print(stdout.decode(errors="ignore")) + if stderr: + print(stderr.decode(errors="ignore")) + await self.event_handler(name,"stdout",stdout or stderr) + print("Return code", proc.returncode) + master, slave = pty.openpty() proc = await asyncio.create_subprocess_exec( - "docker", "compose", "-f", self.compose_path, "exec", name, "/bin/bash", + "docker", "compose", "-f", self.compose_path, "exec", name, "/usr/local/bin/entry", stdin=slave, stdout=slave, stderr=slave, diff --git a/terminal/.welcome.txt b/terminal/.welcome.txt new file mode 100644 index 0000000..d0dbd44 --- /dev/null +++ b/terminal/.welcome.txt @@ -0,0 +1,47 @@ +👋 Welcome to your custom Ubuntu development environment! + +This container is pre-configured with a rich set of tools to help you hit the ground running: + +🔧 Development Libraries & Tools: + +Full support for building and debugging C/C++ and Python code + +Libraries for SSL, readline, SQLite, zlib, bz2, ffi, lzma, and JSON-C + +Valgrind for memory debugging and profiling + +🐍 Python: + +Python 3 with pip and virtual environment support (venv) + +🦀 Rust: + +Rust installed with the nightly toolchain via rustup + +🛠 Command-line Essentials: + +vim, tmux, htop, git, curl, wget, xterm, ack, and more + +💬 Networking & Communication: + +irssi for IRC and lynx for browsing from the terminal + +📦 Custom Tool Installed: + +Latest version of AI vibe coding tool r is installed from the +retoor.molodetz.nl repository. + +To get started, open a new terminal and type "r" to launch the tool. + +Configure ~/.rcontext.txt to describe it's behavior. It can be a friend +or a whatever you like. + +🔐 Credentials: + +Default root password is set to: root (please change it if needed) + +🗂 Custom Terminal Files: + +Files from ./terminal/ have been copied to your home directory (/root/) + +Enjoy hacking in your personalized command-line workspace! diff --git a/terminal/entry b/terminal/entry new file mode 100755 index 0000000..da922e0 --- /dev/null +++ b/terminal/entry @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import os +import sys +import pathlib + +import os + +os.chdir("/root") + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +sys.path.insert(0, "/root/bin") +sys.path.insert(0, "/root/bin/local/bin") + +if not pathlib.Path(".welcome.txt").exists(): + os.system("python3 -m venv --prompt '' .venv") + os.system("cp -r /opt/bootstrap/root/.* /root") + os.system("cp /opt/bootstrap/.welcome.txt /root/.welcome.txt") + pathlib.Path(".bashrc").write_text(pathlib.Path(".bashrc").read_text() + "\n" + "source .venv/bin/activate") +os.environ["SNEK"] = "1" + +if pathlib.Path(".welcome.txt").exists(): + with open(".welcome.txt") as f: + print(f.read()) + +os.system("bash")