Skip to content

Agent Zero AI framework

feat: container process supervision with sueprvisord

Rafael Uzarowskifbcb579a9193
diff --git a/docker/run/Dockerfile b/docker/run/Dockerfile
index 33a9418ae..266a5bc9c 100644
--- a/docker/run/Dockerfile
+++ b/docker/run/Dockerfile
@@ -28,5 +28,7 @@ RUN bash /ins/post_install.sh $BRANCH
# Expose ports
EXPOSE 22 80
-# initialize runtime
-CMD ["/bin/bash", "-c", "/bin/bash /exe/initialize.sh $BRANCH"]
\ No newline at end of file
+RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh
+
+# initialize runtime and switch to supervisord
+CMD ["/exe/initialize.sh", "$BRANCH"]
diff --git a/docker/run/DockerfileKali b/docker/run/DockerfileKali
index f5808cdce..9752b3a98 100644
--- a/docker/run/DockerfileKali
+++ b/docker/run/DockerfileKali
@@ -29,5 +29,7 @@ RUN bash /ins/post_install.sh $BRANCH
# Expose ports
EXPOSE 22 80
-# initialize runtime
-CMD ["/bin/bash", "-c", "/bin/bash /exe/initialize.sh $BRANCH"]
\ No newline at end of file
+RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh
+
+# initialize runtime and switch to supervisord
+CMD ["/exe/initialize.sh", "$BRANCH"]
diff --git a/docker/run/fs/etc/supervisor/conf.d/supervisord.conf b/docker/run/fs/etc/supervisor/conf.d/supervisord.conf
new file mode 100644
index 000000000..5983744cf
--- /dev/null
+++ b/docker/run/fs/etc/supervisor/conf.d/supervisord.conf
@@ -0,0 +1,77 @@
+[supervisord]
+nodaemon=true
+user=root
+logfile=/dev/stdout
+logfile_maxbytes=0
+pidfile=/var/run/supervisord.pid
+exitcodes=0,2
+directory=/
+
+[rpcinterface:supervisor]
+supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
+
+[supervisorctl]
+serverurl=unix:///var/run/supervisor.sock
+
+[program:run_sshd]
+command=/usr/sbin/sshd -D
+environment=LC_ALL=en_US.UTF-8
+stopwaitsecs=1
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+autorestart=true
+startretries=3
+stopasgroup=true
+killasgroup=true
+
+[program:run_cron]
+command=/usr/sbin/cron -f
+environment=
+stopwaitsecs=1
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+autorestart=true
+startretries=3
+stopasgroup=true
+killasgroup=true
+
+[program:run_searxng]
+command=/exe/run_searxng.sh
+environment=SEARXNG_SETTINGS_PATH=/etc/searxng/settings.yml
+user=searxng
+directory=/usr/local/searxng/searxng-src
+stopwaitsecs=1
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+autorestart=true
+startretries=3
+stopasgroup=true
+killasgroup=true
+
+[program:run_ui]
+command=/exe/run_A0.sh
+environment=
+user=root
+stopwaitsecs=60
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+autorestart=true
+startretries=3
+stopasgroup=true
+killasgroup=true
+
+[eventlistener:the_listener]
+command=python3 /exe/supervisor_event_listener.py
+events=PROCESS_STATE_FATAL
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
diff --git a/docker/run/fs/exe/initialize.sh b/docker/run/fs/exe/initialize.sh
index 549515e2a..170a162b2 100644
--- a/docker/run/fs/exe/initialize.sh
+++ b/docker/run/fs/exe/initialize.sh
@@ -17,15 +17,4 @@ chmod 444 /root/.profile
# update package list to save time later
apt-get update > /dev/null 2>&1 &
-# Start SSH service in background
-/usr/sbin/sshd -D &
-
-# Start searxng server in background
-su - searxng -c "bash /exe/run_searxng.sh \"$@\"" &
-
-# Start A0 and restart on exit
-bash /exe/run_A0.sh "$@"
-if [ $? -ne 0 ]; then
- echo "A0 script exited with an error. Restarting container..."
- exit 1
-fi
\ No newline at end of file
+exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
diff --git a/docker/run/fs/exe/run_A0.sh b/docker/run/fs/exe/run_A0.sh
index 7631b4a41..41b4b1906 100644
--- a/docker/run/fs/exe/run_A0.sh
+++ b/docker/run/fs/exe/run_A0.sh
@@ -1,33 +1,19 @@
#!/bin/bash
-# Loop to restart the Python script when it finishes
-while true; do
+. "/ins/setup_venv.sh" "$@"
+. "/ins/copy_A0.sh" "$@"
- . "/ins/setup_venv.sh" "$@"
- . "/ins/copy_A0.sh" "$@"
+python /a0/prepare.py --dockerized=true
+python /a0/preload.py --dockerized=true
- python /a0/prepare.py --dockerized=true
- python /a0/preload.py --dockerized=true
-
- echo "Starting A0..."
- python /a0/run_ui.py \
- --dockerized=true \
- --port=80 \
- --host="0.0.0.0" \
- --code_exec_docker_enabled=false \
- --code_exec_ssh_enabled=true \
- # --code_exec_ssh_addr="localhost" \
- # --code_exec_ssh_port=22 \
- # --code_exec_ssh_user="root" \
- # --code_exec_ssh_pass="toor"
-
- # Check the exit status
- if [ $? -ne 0 ]; then
- echo "A0 script exited with an error. Restarting..."
- else
- echo "A0 script finished. Restarting..."
- fi
-
- # Optional: Add a small delay if needed to avoid rapid restarts
- sleep 1
-done
+echo "Starting A0..."
+exec python /a0/run_ui.py \
+ --dockerized=true \
+ --port=80 \
+ --host="0.0.0.0" \
+ --code_exec_docker_enabled=false \
+ --code_exec_ssh_enabled=true \
+ # --code_exec_ssh_addr="localhost" \
+ # --code_exec_ssh_port=22 \
+ # --code_exec_ssh_user="root" \
+ # --code_exec_ssh_pass="toor"
diff --git a/docker/run/fs/exe/run_searxng.sh b/docker/run/fs/exe/run_searxng.sh
index 880314f29..bb544186c 100644
--- a/docker/run/fs/exe/run_searxng.sh
+++ b/docker/run/fs/exe/run_searxng.sh
@@ -1,7 +1,10 @@
#!/bin/bash
# start webapp
-sudo -H -u searxng -i
cd /usr/local/searxng/searxng-src
export SEARXNG_SETTINGS_PATH="/etc/searxng/settings.yml"
-python searx/webapp.py
\ No newline at end of file
+
+# activate venv
+source "/usr/local/searxng/searx-pyenv/bin/activate"
+
+exec python /usr/local/searxng/searxng-src/searx/webapp.py
diff --git a/docker/run/fs/exe/supervisor_event_listener.py b/docker/run/fs/exe/supervisor_event_listener.py
new file mode 100644
index 000000000..17fcc76c2
--- /dev/null
+++ b/docker/run/fs/exe/supervisor_event_listener.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+import sys
+import os
+import logging
+import subprocess
+import time
+
+from supervisor.childutils import listener
+
+
+def main(args):
+ logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, format='%(asctime)s %(levelname)s %(filename)s: %(message)s')
+ logger = logging.getLogger("supervisord-watchdog")
+ debug_mode = True if 'DEBUG' in os.environ else False
+
+ while True:
+ logger.info("Listening for events...")
+ headers, body = listener.wait(sys.stdin, sys.stdout)
+ body = dict([pair.split(":") for pair in body.split(" ")])
+
+ logger.debug("Headers: %r", repr(headers))
+ logger.debug("Body: %r", repr(body))
+ logger.debug("Args: %r", repr(args))
+
+ if debug_mode:
+ continue
+
+ try:
+ if headers["eventname"] == "PROCESS_STATE_FATAL":
+ logger.info("Process entered FATAL state...")
+ if not args or body["processname"] in args:
+ logger.error("Killing off supervisord instance ...")
+ _ = subprocess.call(["/bin/kill", "-15", "1"], stdout=sys.stderr)
+ logger.info("Sent TERM signal to init process")
+ time.sleep(5)
+ logger.critical("Why am I still alive? Send KILL to all processes...")
+ _ = subprocess.call(["/bin/kill", "-9", "-1"], stdout=sys.stderr)
+ except Exception as e:
+ logger.critical("Unexpected Exception: %s", str(e))
+ listener.fail(sys.stdout)
+ exit(1)
+ else:
+ listener.ok(sys.stdout)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/docker/run/fs/ins/pre_install.sh b/docker/run/fs/ins/pre_install.sh
index c5d05bcc4..44221cc42 100644
--- a/docker/run/fs/ins/pre_install.sh
+++ b/docker/run/fs/ins/pre_install.sh
@@ -11,7 +11,9 @@ apt-get update && apt-get install -y \
curl \
wget \
git \
- ffmpeg
+ ffmpeg \
+ supervisor \
+ cron
# # Configure system alternatives so that /usr/bin/python3 points to Python 3.12
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1