Disallow running more than one instance of opercode

We don't want to let people run more than one instance of opencode
in one project directory, this will lead to chaos and then
they interfear with each other in weird ways like when one
stopps it crashes the other, etc.
This commit is contained in:
Jeena 2026-01-22 01:39:44 +09:00
parent 941140581c
commit 582038e009

View file

@ -1,12 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import hashlib import hashlib
import logging
import os import os
import subprocess import subprocess
import sys import sys
import time import time
from pathlib import Path from pathlib import Path
logging.basicConfig(
level=logging.INFO,
format='%(message)s',
stream=sys.stderr
)
logger = logging.getLogger(__name__)
class OpenCodeContainer: class OpenCodeContainer:
IMAGE = "opencode-container:latest" IMAGE = "opencode-container:latest"
@ -45,6 +53,14 @@ class OpenCodeContainer:
# Ensure container home directory exists # Ensure container home directory exists
self.container_home_path.mkdir(parents=True, exist_ok=True) self.container_home_path.mkdir(parents=True, exist_ok=True)
# Check if this project already has a running container
if self.container_exists() and self.container_running():
logger.error(f"Project '{self.project_path.name}' already has a running OpenCode container.")
logger.error(f"Container name: {self.container_name}")
logger.error("Wait for the current instance to finish or manually stop it with:")
logger.error(f" docker stop {self.container_name}")
sys.exit(1)
# Pre-create project directory structure to prevent root-owned directories # Pre-create project directory structure to prevent root-owned directories
try: try:
relative_path = self.project_path.relative_to(Path.home()) relative_path = self.project_path.relative_to(Path.home())
@ -60,7 +76,7 @@ class OpenCodeContainer:
self.create_container() self.create_container()
if not self.start_container(): if not self.start_container():
print("Recreating container due to failed start") logger.warning("Recreating container due to failed start")
self.remove_container() self.remove_container()
self.create_container() self.create_container()
if not self.start_container(): if not self.start_container():
@ -83,7 +99,7 @@ class OpenCodeContainer:
).returncode == 0 ).returncode == 0
def build_image(self) -> None: def build_image(self) -> None:
print(f"Building image '{self.IMAGE}' with user {self.host_username} ({self.host_uid}:{self.host_gid})") logger.info(f"Building image '{self.IMAGE}' with user {self.host_username} ({self.host_uid}:{self.host_gid})")
self._run([ self._run([
"docker", "build", "docker", "build",
"--build-arg", f"USERNAME={self.host_username}", "--build-arg", f"USERNAME={self.host_username}",
@ -98,7 +114,7 @@ class OpenCodeContainer:
# ========================= # =========================
def create_container(self) -> None: def create_container(self) -> None:
print(f"Creating container '{self.container_name}'") logger.info(f"Creating container '{self.container_name}'")
self._run([ self._run([
"docker", "create", "docker", "create",