agentcabin 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentbox/__init__.py +8 -0
- agentbox/assets/Dockerfile +76 -0
- agentbox/assets/base.list +32 -0
- agentbox/assets/rootfs/etc/ssh/sshd_config.d/10-agentbox.conf +9 -0
- agentbox/assets/rootfs/usr/local/bin/entrypoint.sh +232 -0
- agentbox/assets/scripts/add_apt_repos.sh +95 -0
- agentbox/assets/scripts/apt_update.sh +27 -0
- agentbox/assets/scripts/create_user.sh +59 -0
- agentbox/assets/scripts/install_base.sh +36 -0
- agentbox/assets/scripts/install_firefox.sh +28 -0
- agentbox/assets/scripts/install_packages.sh +15 -0
- agentbox/assets/scripts/install_pkg_list.sh +41 -0
- agentbox/assets/scripts/install_tools.sh +39 -0
- agentbox/assets/scripts/save_defaults.sh +15 -0
- agentbox/assets/scripts/set_apt_mirror.sh +30 -0
- agentbox/assets/vnc/xstartup +16 -0
- agentbox/cli.py +543 -0
- agentbox/config.py +681 -0
- agentbox/errors.py +2 -0
- agentbox/podman.py +721 -0
- agentbox/templates/agentbox.toml +449 -0
- agentcabin-0.2.0.dist-info/METADATA +380 -0
- agentcabin-0.2.0.dist-info/RECORD +25 -0
- agentcabin-0.2.0.dist-info/WHEEL +4 -0
- agentcabin-0.2.0.dist-info/entry_points.txt +2 -0
agentbox/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""agentbox: build and run containers for AI coding agents (SSH, VNC desktop, Firefox) with rootless podman."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
__version__ = version("agentcabin")
|
|
7
|
+
except PackageNotFoundError: # running from a source tree without installation
|
|
8
|
+
__version__ = "0+unknown"
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# agentbox image: a container with SSH, a VNC desktop (XFCE), Firefox and the tools of agentbox.toml.
|
|
2
|
+
# Built by `agentbox build`: BASE_IMAGE, PACKAGES and APT_REPOS come from the user's agentbox.toml;
|
|
3
|
+
# the build context is the package's assets/ directory plus tools/ (the [[tools]] scripts, generated).
|
|
4
|
+
|
|
5
|
+
ARG BASE_IMAGE=docker.io/library/ubuntu:26.04
|
|
6
|
+
FROM ${BASE_IMAGE}
|
|
7
|
+
|
|
8
|
+
ENV DEBIAN_FRONTEND=noninteractive \
|
|
9
|
+
LANG=en_US.UTF-8 \
|
|
10
|
+
LC_ALL=en_US.UTF-8 \
|
|
11
|
+
NPM_CONFIG_UPDATE_NOTIFIER=false \
|
|
12
|
+
NPM_CONFIG_FUND=false
|
|
13
|
+
|
|
14
|
+
# RUN instructions use /bin/sh: podman builds OCI images, which ignore the SHELL instruction.
|
|
15
|
+
# Keep RUN lines POSIX; anything that needs bash goes into a script run with `bash script.sh`.
|
|
16
|
+
|
|
17
|
+
# Build args are declared right before the step that uses them, so that changing
|
|
18
|
+
# one only invalidates the build cache from that step on.
|
|
19
|
+
|
|
20
|
+
# 0. apt mirror ([build] apt_mirror in the config; empty = the base image's default sources)
|
|
21
|
+
ARG APT_MIRROR=""
|
|
22
|
+
COPY scripts/set_apt_mirror.sh /opt/agentbox/scripts/
|
|
23
|
+
RUN bash /opt/agentbox/scripts/set_apt_mirror.sh
|
|
24
|
+
|
|
25
|
+
# 1. Base packages: openssh-server / tigervnc / XFCE desktop / sudo
|
|
26
|
+
COPY base.list /opt/agentbox/base.list
|
|
27
|
+
COPY scripts/apt_update.sh scripts/install_pkg_list.sh scripts/install_base.sh /opt/agentbox/scripts/
|
|
28
|
+
RUN bash /opt/agentbox/scripts/install_base.sh /opt/agentbox/base.list
|
|
29
|
+
|
|
30
|
+
# 2. Firefox from Mozilla's APT repository (the distribution's package is a snap wrapper)
|
|
31
|
+
COPY scripts/install_firefox.sh /opt/agentbox/scripts/
|
|
32
|
+
RUN bash /opt/agentbox/scripts/install_firefox.sh
|
|
33
|
+
|
|
34
|
+
# 3. Create the user (UID/GID match the host, for userns keep-id)
|
|
35
|
+
ARG CONTAINER_USER=agent
|
|
36
|
+
ARG USER_UID=1000
|
|
37
|
+
ARG USER_GID=1000
|
|
38
|
+
COPY scripts/create_user.sh /opt/agentbox/scripts/
|
|
39
|
+
RUN bash /opt/agentbox/scripts/create_user.sh
|
|
40
|
+
|
|
41
|
+
# 4. Third-party apt repositories ([[apt_repos]]) and extra packages (packages) from agentbox.toml
|
|
42
|
+
ARG APT_REPOS=""
|
|
43
|
+
ARG PACKAGES=""
|
|
44
|
+
COPY scripts/add_apt_repos.sh scripts/install_packages.sh /opt/agentbox/scripts/
|
|
45
|
+
RUN bash /opt/agentbox/scripts/add_apt_repos.sh \
|
|
46
|
+
&& bash /opt/agentbox/scripts/install_packages.sh ${PACKAGES}
|
|
47
|
+
|
|
48
|
+
# 5. [[tools]] with run_as = "root", then those with run_as = "user" (in config order)
|
|
49
|
+
COPY scripts/install_tools.sh /opt/agentbox/scripts/
|
|
50
|
+
COPY tools/root/ /opt/agentbox/tools/root/
|
|
51
|
+
RUN bash /opt/agentbox/scripts/install_tools.sh root /opt/agentbox/tools/root
|
|
52
|
+
COPY tools/user/ /opt/agentbox/tools/user/
|
|
53
|
+
RUN bash /opt/agentbox/scripts/install_tools.sh user /opt/agentbox/tools/user
|
|
54
|
+
|
|
55
|
+
# 6. Defaults for ~/.claude and ~/.codex (they are usually bind-mounted, which hides the image's copy)
|
|
56
|
+
COPY scripts/save_defaults.sh /opt/agentbox/scripts/
|
|
57
|
+
RUN bash /opt/agentbox/scripts/save_defaults.sh
|
|
58
|
+
|
|
59
|
+
# 7. Runtime files: sshd config, entrypoint, VNC xstartup
|
|
60
|
+
COPY rootfs/ /
|
|
61
|
+
COPY vnc/xstartup /opt/agentbox/vnc/xstartup
|
|
62
|
+
RUN chmod 755 /usr/local/bin/entrypoint.sh /opt/agentbox/vnc/xstartup \
|
|
63
|
+
&& chmod 644 /etc/ssh/sshd_config.d/10-agentbox.conf \
|
|
64
|
+
&& mkdir -p /etc/agentbox \
|
|
65
|
+
&& echo "AGENTBOX_USER=${CONTAINER_USER}" > /etc/agentbox/container.env \
|
|
66
|
+
&& chmod 644 /etc/agentbox/container.env
|
|
67
|
+
|
|
68
|
+
LABEL agentbox.user="${CONTAINER_USER}" \
|
|
69
|
+
agentbox.uid="${USER_UID}" \
|
|
70
|
+
agentbox.gid="${USER_GID}"
|
|
71
|
+
|
|
72
|
+
WORKDIR /home/${CONTAINER_USER}
|
|
73
|
+
|
|
74
|
+
EXPOSE 22 5901
|
|
75
|
+
|
|
76
|
+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Base apt packages of the agentbox image (installed by scripts/install_base.sh): the system, SSH, VNC
|
|
2
|
+
# and the desktop. Extra packages come from `packages` in agentbox.toml.
|
|
3
|
+
# A trailing "?" marks an optional package: installed only if the OS release has it.
|
|
4
|
+
|
|
5
|
+
# ---- basic tools ----
|
|
6
|
+
ca-certificates
|
|
7
|
+
curl
|
|
8
|
+
sudo
|
|
9
|
+
locales
|
|
10
|
+
tzdata
|
|
11
|
+
procps
|
|
12
|
+
iproute2
|
|
13
|
+
iputils-ping
|
|
14
|
+
less
|
|
15
|
+
nano
|
|
16
|
+
rsync
|
|
17
|
+
|
|
18
|
+
# ---- ssh / vnc / desktop ----
|
|
19
|
+
openssh-server
|
|
20
|
+
tigervnc-standalone-server
|
|
21
|
+
tigervnc-common
|
|
22
|
+
# xauth: used by vncserver; not pulled in with --no-install-recommends
|
|
23
|
+
xauth
|
|
24
|
+
# vncpasswd: separate package since TigerVNC 1.11; in tigervnc-common on Ubuntu 20.04
|
|
25
|
+
tigervnc-tools?
|
|
26
|
+
xfce4
|
|
27
|
+
xfce4-terminal
|
|
28
|
+
dbus-x11
|
|
29
|
+
x11-xserver-utils
|
|
30
|
+
x11-utils
|
|
31
|
+
xfonts-base
|
|
32
|
+
fonts-dejavu
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# agentbox: password login (the container user's password), no root login
|
|
2
|
+
PasswordAuthentication yes
|
|
3
|
+
PubkeyAuthentication yes
|
|
4
|
+
PermitRootLogin no
|
|
5
|
+
PermitEmptyPasswords no
|
|
6
|
+
UsePAM yes
|
|
7
|
+
X11Forwarding yes
|
|
8
|
+
PrintMotd no
|
|
9
|
+
AcceptEnv LANG LC_* TERM COLORTERM ANTHROPIC_API_KEY OPENAI_API_KEY
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Container entrypoint (runs as root, PID 1):
|
|
3
|
+
# 1. Set the user password from CONTAINER_PWD and the VNC password from VNC_PASSWORD
|
|
4
|
+
# 2. Start sshd (22) and vncserver :1 (5901); in host network mode on the ports and the address
|
|
5
|
+
# given by AGENTBOX_SSH_PORT / AGENTBOX_VNC_PORT / AGENTBOX_BIND
|
|
6
|
+
# 3. Stay in the foreground, restart sshd / vncserver if they stop, clean up on stop signals
|
|
7
|
+
#
|
|
8
|
+
# The services are independent: a failing VNC server must not take SSH down with it,
|
|
9
|
+
# so errors after the setup are logged instead of ending the script.
|
|
10
|
+
set -uo pipefail
|
|
11
|
+
|
|
12
|
+
log() { echo "[entrypoint] $(date '+%H:%M:%S') $*"; }
|
|
13
|
+
die() { log "ERROR: $*"; exit 1; }
|
|
14
|
+
|
|
15
|
+
# shellcheck disable=SC1091
|
|
16
|
+
[[ -r /etc/agentbox/container.env ]] && . /etc/agentbox/container.env
|
|
17
|
+
[[ -n "${AGENTBOX_USER:-}" ]] || die "AGENTBOX_USER is not set in /etc/agentbox/container.env"
|
|
18
|
+
user="${AGENTBOX_USER}"
|
|
19
|
+
group="$(id -gn "${user}")" || die "user ${user} does not exist in the image"
|
|
20
|
+
home_dir="/home/${user}"
|
|
21
|
+
|
|
22
|
+
[[ -n "${CONTAINER_PWD:-}" ]] || die "CONTAINER_PWD is not set"
|
|
23
|
+
VNC_PASSWORD="${VNC_PASSWORD:-${CONTAINER_PWD}}"
|
|
24
|
+
VNC_GEOMETRY="${VNC_GEOMETRY:-1920x1080}"
|
|
25
|
+
if [[ ! "${VNC_GEOMETRY}" =~ ^[0-9]+x[0-9]+$ ]]; then
|
|
26
|
+
log "Invalid VNC_GEOMETRY: ${VNC_GEOMETRY}, using 1920x1080"
|
|
27
|
+
VNC_GEOMETRY=1920x1080
|
|
28
|
+
fi
|
|
29
|
+
|
|
30
|
+
# ---- Listening address (network.mode = "host") ----
|
|
31
|
+
# With the host's network the services listen on the host's ports themselves. agentbox then sets the
|
|
32
|
+
# ports and the address of the management NIC, so nothing is opened on the other (fast) NICs.
|
|
33
|
+
# Without them (pasta networking) sshd and VNC use their default ports on every address of the container.
|
|
34
|
+
ssh_port="${AGENTBOX_SSH_PORT:-}"
|
|
35
|
+
vnc_port="${AGENTBOX_VNC_PORT:-}"
|
|
36
|
+
bind_addr="${AGENTBOX_BIND:-}"
|
|
37
|
+
[[ -z "${ssh_port}" || "${ssh_port}" =~ ^[0-9]+$ ]] || die "invalid AGENTBOX_SSH_PORT: ${ssh_port}"
|
|
38
|
+
[[ -z "${vnc_port}" || "${vnc_port}" =~ ^[0-9]+$ ]] || die "invalid AGENTBOX_VNC_PORT: ${vnc_port}"
|
|
39
|
+
[[ -z "${bind_addr}" || "${bind_addr}" =~ ^[0-9A-Fa-f:.]+$ ]] || die "invalid AGENTBOX_BIND: ${bind_addr}"
|
|
40
|
+
[[ "${bind_addr}" == "0.0.0.0" || "${bind_addr}" == "::" ]] && bind_addr=""
|
|
41
|
+
|
|
42
|
+
# ---- Host name ----
|
|
43
|
+
# vncserver runs `hostname -f` and refuses to start when the container's own name does not resolve.
|
|
44
|
+
# With the host's network (network.mode = "host") podman uses the host's /etc/hosts, which does not
|
|
45
|
+
# know the name given to the container.
|
|
46
|
+
host_name="$(hostname)"
|
|
47
|
+
if [[ -n "${host_name}" ]] && ! getent hosts "${host_name}" >/dev/null 2>&1; then
|
|
48
|
+
log "Adding ${host_name} to /etc/hosts"
|
|
49
|
+
echo "127.0.1.1 ${host_name}" >> /etc/hosts || log "Warning: could not write /etc/hosts; vncserver may not start"
|
|
50
|
+
fi
|
|
51
|
+
|
|
52
|
+
# ---- Password (needed for SSH and sudo) ----
|
|
53
|
+
log "Setting password for user ${user}"
|
|
54
|
+
printf '%s:%s\n' "${user}" "${CONTAINER_PWD}" | chpasswd || die "chpasswd failed"
|
|
55
|
+
unset CONTAINER_PWD
|
|
56
|
+
|
|
57
|
+
# ---- Agent config ----
|
|
58
|
+
# Claude Code and Codex keep their login, settings and plugins in ~/.claude and ~/.codex, which are
|
|
59
|
+
# usually bind mounts and so start out empty. Copy in the defaults the image was built with; files that
|
|
60
|
+
# already exist are never overwritten, so the user's own settings win.
|
|
61
|
+
defaults=/usr/local/share/agentbox/defaults
|
|
62
|
+
for name in .claude .codex; do
|
|
63
|
+
if [[ -d "${defaults}/${name}" ]] && command -v rsync >/dev/null; then
|
|
64
|
+
install -d -o "${user}" -g "${group}" "${home_dir}/${name}"
|
|
65
|
+
runuser -u "${user}" -- rsync -a --ignore-existing "${defaults}/${name}/" "${home_dir}/${name}/" \
|
|
66
|
+
|| log "Warning: could not copy the default ${name} config"
|
|
67
|
+
fi
|
|
68
|
+
done
|
|
69
|
+
|
|
70
|
+
# ---- sshd ----
|
|
71
|
+
# A non-interactive `ssh host command` reads neither ~/.profile nor ~/.bashrc; tools installed for the
|
|
72
|
+
# user (Claude Code, Codex, ...) live in ~/.local/bin, so put it on PATH here.
|
|
73
|
+
cat > /etc/ssh/sshd_config.d/20-agentbox-path.conf <<CONF
|
|
74
|
+
SetEnv PATH=${home_dir}/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
|
|
75
|
+
CONF
|
|
76
|
+
|
|
77
|
+
if [[ -n "${ssh_port}" || -n "${bind_addr}" ]]; then
|
|
78
|
+
{
|
|
79
|
+
[[ -n "${ssh_port}" ]] && echo "Port ${ssh_port}"
|
|
80
|
+
[[ -n "${bind_addr}" ]] && echo "ListenAddress ${bind_addr}"
|
|
81
|
+
} > /etc/ssh/sshd_config.d/30-agentbox-listen.conf
|
|
82
|
+
fi
|
|
83
|
+
|
|
84
|
+
sshd_running() { pgrep -x sshd >/dev/null; }
|
|
85
|
+
|
|
86
|
+
start_sshd() {
|
|
87
|
+
ssh-keygen -A >/dev/null 2>&1
|
|
88
|
+
mkdir -p /run/sshd
|
|
89
|
+
chmod 755 /run/sshd
|
|
90
|
+
log "Starting sshd"
|
|
91
|
+
if ! /usr/sbin/sshd; then
|
|
92
|
+
log "ERROR: sshd failed to start; config check:"
|
|
93
|
+
/usr/sbin/sshd -t 2>&1 | sed 's/^/ /'
|
|
94
|
+
return 1
|
|
95
|
+
fi
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
# ---- VNC ----
|
|
99
|
+
# Debian/Ubuntu name the commands vncpasswd/vncserver (alternatives) or tigervncpasswd/tigervncserver
|
|
100
|
+
find_cmd() {
|
|
101
|
+
local c
|
|
102
|
+
for c in "$@"; do
|
|
103
|
+
command -v "${c}" && return 0
|
|
104
|
+
done
|
|
105
|
+
return 1
|
|
106
|
+
}
|
|
107
|
+
vncpasswd_cmd="$(find_cmd vncpasswd tigervncpasswd)" || vncpasswd_cmd=""
|
|
108
|
+
vncserver_cmd="$(find_cmd vncserver tigervncserver)" || vncserver_cmd=""
|
|
109
|
+
|
|
110
|
+
# VNC files are written to ~/.vnc. TigerVNC >= 1.14 (e.g. Ubuntu 26.04) keeps them in
|
|
111
|
+
# ~/.config/tigervnc instead: on start it moves ~/.vnc there and leaves a ~/.vnc symlink.
|
|
112
|
+
# The move fails if ~/.config does not exist, so setup_vnc() creates it (see
|
|
113
|
+
# /usr/share/perl5/TigerVNC/Config.pm). Older versions just use ~/.vnc.
|
|
114
|
+
vnc_dir="${home_dir}/.vnc"
|
|
115
|
+
vnc_log_dirs=("${home_dir}/.local/state/tigervnc" "${home_dir}/.config/tigervnc" "${home_dir}/.vnc")
|
|
116
|
+
|
|
117
|
+
# Switch to the ~/.config/tigervnc layout: create the XDG directories owned by the user and
|
|
118
|
+
# move our files out of ~/.vnc ourselves, so that vncserver does not try (and fail) to migrate it
|
|
119
|
+
use_xdg_dir() {
|
|
120
|
+
local new="${home_dir}/.config/tigervnc" f
|
|
121
|
+
install -d -o "${user}" -g "${group}" "${home_dir}/.config" "${home_dir}/.local" "${home_dir}/.local/state"
|
|
122
|
+
install -d -m 700 -o "${user}" -g "${group}" "${new}"
|
|
123
|
+
if [[ -d "${home_dir}/.vnc" ]]; then
|
|
124
|
+
for f in passwd xstartup; do
|
|
125
|
+
if [[ -f "${home_dir}/.vnc/${f}" && ! -f "${new}/${f}" ]]; then
|
|
126
|
+
mv "${home_dir}/.vnc/${f}" "${new}/${f}"
|
|
127
|
+
fi
|
|
128
|
+
done
|
|
129
|
+
rm -rf "${home_dir}/.vnc"
|
|
130
|
+
fi
|
|
131
|
+
vnc_dir="${new}"
|
|
132
|
+
vnc_log_dirs=("${home_dir}/.local/state/tigervnc" "${new}")
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setup_vnc() {
|
|
136
|
+
[[ -n "${vncpasswd_cmd}" ]] || { log "ERROR: vncpasswd not found in the image"; return 1; }
|
|
137
|
+
log "Setting VNC password (vncserver: ${vncserver_cmd:-<not found>})"
|
|
138
|
+
# Parent directories for TigerVNC >= 1.14 (~/.config/tigervnc, ~/.local/state/tigervnc)
|
|
139
|
+
install -d -o "${user}" -g "${group}" "${home_dir}/.config" "${home_dir}/.local" "${home_dir}/.local/state"
|
|
140
|
+
install -d -m 700 -o "${user}" -g "${group}" "${vnc_dir}"
|
|
141
|
+
printf '%s\n' "${VNC_PASSWORD}" | "${vncpasswd_cmd}" -f > "${vnc_dir}/passwd" || return 1
|
|
142
|
+
chown "${user}:${group}" "${vnc_dir}/passwd"
|
|
143
|
+
chmod 600 "${vnc_dir}/passwd"
|
|
144
|
+
if [[ ! -f "${vnc_dir}/xstartup" ]]; then
|
|
145
|
+
install -m 755 -o "${user}" -g "${group}" /opt/agentbox/vnc/xstartup "${vnc_dir}/xstartup"
|
|
146
|
+
fi
|
|
147
|
+
# X server socket directory: must exist, be owned by root and world-writable with the sticky bit
|
|
148
|
+
install -d -m 1777 -o root -g root /tmp/.X11-unix
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
vnc_running() { pgrep -f 'X(tigervnc|vnc) :1( |$)' >/dev/null; }
|
|
152
|
+
|
|
153
|
+
start_vnc() {
|
|
154
|
+
[[ -n "${vncserver_cmd}" ]] || { log "ERROR: vncserver not found in the image"; return 1; }
|
|
155
|
+
rm -f /tmp/.X1-lock /tmp/.X11-unix/X1
|
|
156
|
+
log "Starting vncserver :1 (${VNC_GEOMETRY})"
|
|
157
|
+
local out rc
|
|
158
|
+
local vnc_net=""
|
|
159
|
+
[[ -n "${vnc_port}" ]] && vnc_net+=" -rfbport ${vnc_port}"
|
|
160
|
+
[[ -n "${bind_addr}" ]] && vnc_net+=" -interface ${bind_addr}"
|
|
161
|
+
out="$(su - "${user}" -c "${vncserver_cmd} :1 -geometry ${VNC_GEOMETRY} -depth 24 -localhost no -SecurityTypes VncAuth${vnc_net}" 2>&1)"
|
|
162
|
+
rc=$?
|
|
163
|
+
[[ -n "${out}" ]] && printf '%s\n' "${out}"
|
|
164
|
+
# Safety net: if TigerVNC still cannot migrate ~/.vnc to ~/.config/tigervnc,
|
|
165
|
+
# move our files there ourselves and try once more.
|
|
166
|
+
if (( rc != 0 )) && [[ "${out}" == *"Could not migrate"* && "${vnc_dir}" == "${home_dir}/.vnc" ]]; then
|
|
167
|
+
log "vncserver uses ~/.config/tigervnc: moving the VNC files there and retrying"
|
|
168
|
+
use_xdg_dir
|
|
169
|
+
start_vnc
|
|
170
|
+
return
|
|
171
|
+
fi
|
|
172
|
+
if (( rc != 0 )); then
|
|
173
|
+
log "ERROR: vncserver failed to start"
|
|
174
|
+
show_vnc_log
|
|
175
|
+
return 1
|
|
176
|
+
fi
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
show_vnc_log() {
|
|
180
|
+
local d f
|
|
181
|
+
for d in "${vnc_log_dirs[@]}"; do
|
|
182
|
+
for f in "${d}"/*:1.log; do
|
|
183
|
+
[[ -r "${f}" ]] || continue
|
|
184
|
+
log "Last lines of ${f}:"
|
|
185
|
+
tail -n 20 "${f}" | sed 's/^/ /'
|
|
186
|
+
done
|
|
187
|
+
done
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
start_sshd || true
|
|
191
|
+
vnc_ok=1
|
|
192
|
+
setup_vnc || { vnc_ok=0; log "ERROR: VNC setup failed, VNC will not be available"; }
|
|
193
|
+
unset VNC_PASSWORD
|
|
194
|
+
(( vnc_ok )) && { start_vnc || true; }
|
|
195
|
+
|
|
196
|
+
cleanup() {
|
|
197
|
+
log "Stopping services"
|
|
198
|
+
[[ -n "${vncserver_cmd}" ]] && su - "${user}" -c "${vncserver_cmd} -kill :1" >/dev/null 2>&1
|
|
199
|
+
pkill -TERM -x sshd 2>/dev/null
|
|
200
|
+
exit 0
|
|
201
|
+
}
|
|
202
|
+
trap cleanup TERM INT
|
|
203
|
+
|
|
204
|
+
if sshd_running; then log "sshd is running"; else log "ERROR: sshd is not running"; fi
|
|
205
|
+
if (( vnc_ok )); then
|
|
206
|
+
if vnc_running; then log "vncserver :1 is running"; else log "ERROR: vncserver :1 is not running"; fi
|
|
207
|
+
fi
|
|
208
|
+
log "Container ready"
|
|
209
|
+
|
|
210
|
+
# ---- Keep the services running ----
|
|
211
|
+
# Restart a service that stopped (e.g. the VNC server exits when its desktop session ends).
|
|
212
|
+
# Retries back off so that a service that keeps failing does not flood the log.
|
|
213
|
+
sshd_delay=10
|
|
214
|
+
vnc_delay=10
|
|
215
|
+
sshd_next=0
|
|
216
|
+
vnc_next=0
|
|
217
|
+
while true; do
|
|
218
|
+
sleep 10 &
|
|
219
|
+
wait $!
|
|
220
|
+
now=$(date +%s)
|
|
221
|
+
if ! sshd_running && (( now >= sshd_next )); then
|
|
222
|
+
log "sshd is not running, restarting it"
|
|
223
|
+
if start_sshd; then sshd_delay=10; else sshd_delay=$(( sshd_delay < 300 ? sshd_delay * 2 : 300 )); fi
|
|
224
|
+
sshd_next=$(( now + sshd_delay ))
|
|
225
|
+
fi
|
|
226
|
+
if (( vnc_ok )) && ! vnc_running && (( now >= vnc_next )); then
|
|
227
|
+
log "vncserver :1 is not running, restarting it"
|
|
228
|
+
show_vnc_log
|
|
229
|
+
if start_vnc; then vnc_delay=10; else vnc_delay=$(( vnc_delay < 300 ? vnc_delay * 2 : 300 )); fi
|
|
230
|
+
vnc_next=$(( now + vnc_delay ))
|
|
231
|
+
fi
|
|
232
|
+
done
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Add the third-party apt repositories of [[apt_repos]] in agentbox.toml.
|
|
3
|
+
#
|
|
4
|
+
# APT_REPOS: space-separated entries "name|key|url|suite|components|fingerprint", written by
|
|
5
|
+
# agentbox (AptRepo.build_arg); suite empty = the OS codename, ending in "/" = flat
|
|
6
|
+
# repository; components comma-separated; fingerprint optional (40 hex digits).
|
|
7
|
+
# APT_ROOT: prefix for /etc (tests only)
|
|
8
|
+
#
|
|
9
|
+
# For each repository: download the signing key to /etc/apt/keyrings/<name>.asc (ASCII-armored)
|
|
10
|
+
# or <name>.gpg (binary), check its fingerprint if one is given, and write
|
|
11
|
+
# /etc/apt/sources.list.d/<name>.sources. The packages are installed by install_packages.sh.
|
|
12
|
+
set -euo pipefail
|
|
13
|
+
|
|
14
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
15
|
+
|
|
16
|
+
read -r -a repos <<< "${APT_REPOS:-}"
|
|
17
|
+
if (( ${#repos[@]} == 0 )); then
|
|
18
|
+
echo "No extra apt repositories"
|
|
19
|
+
exit 0
|
|
20
|
+
fi
|
|
21
|
+
|
|
22
|
+
root="${APT_ROOT:-}"
|
|
23
|
+
keyrings="${root}/etc/apt/keyrings"
|
|
24
|
+
sources="${root}/etc/apt/sources.list.d"
|
|
25
|
+
install -d -m 0755 "${keyrings}" "${sources}"
|
|
26
|
+
# shellcheck disable=SC1091
|
|
27
|
+
codename="$(. "${root}/etc/os-release" && echo "${VERSION_CODENAME:-}")"
|
|
28
|
+
|
|
29
|
+
tmp="$(mktemp -d)"
|
|
30
|
+
trap 'rm -rf "${tmp}"' EXIT
|
|
31
|
+
|
|
32
|
+
# gpg is only needed to check fingerprints; the base image does not have it
|
|
33
|
+
ensure_gpg() {
|
|
34
|
+
command -v gpg >/dev/null && return
|
|
35
|
+
echo "Installing gnupg to check key fingerprints"
|
|
36
|
+
bash "$(dirname "$0")/apt_update.sh"
|
|
37
|
+
apt-get install -y --no-install-recommends gnupg
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
# Fingerprints of the primary keys in a key file, one per line
|
|
41
|
+
key_fingerprints() {
|
|
42
|
+
GNUPGHOME="${tmp}/gnupg" gpg --batch --show-keys --with-colons "$1" 2>/dev/null \
|
|
43
|
+
| awk -F: '$1 == "pub" { primary = 1; next } $1 == "sub" { primary = 0 } $1 == "fpr" && primary { print $10; primary = 0 }'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for entry in "${repos[@]}"; do
|
|
47
|
+
IFS='|' read -r name key url suite components fingerprint <<< "${entry}"
|
|
48
|
+
echo "==> apt repository ${name}: ${url}"
|
|
49
|
+
|
|
50
|
+
if ! curl -fsSL --retry 3 -o "${tmp}/key" "${key}"; then
|
|
51
|
+
echo "Error: apt repository ${name}: cannot download the signing key ${key}" >&2
|
|
52
|
+
exit 1
|
|
53
|
+
fi
|
|
54
|
+
if grep -q -- "-----BEGIN PGP PUBLIC KEY BLOCK-----" "${tmp}/key"; then
|
|
55
|
+
keyfile="/etc/apt/keyrings/${name}.asc"
|
|
56
|
+
else
|
|
57
|
+
keyfile="/etc/apt/keyrings/${name}.gpg"
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
if [[ -n "${fingerprint}" ]]; then
|
|
61
|
+
ensure_gpg
|
|
62
|
+
install -d -m 0700 "${tmp}/gnupg"
|
|
63
|
+
found="$(key_fingerprints "${tmp}/key")"
|
|
64
|
+
if [[ -z "${found}" ]]; then
|
|
65
|
+
echo "Error: apt repository ${name}: ${key} is not an OpenPGP key" >&2
|
|
66
|
+
exit 1
|
|
67
|
+
fi
|
|
68
|
+
# Every key in the file must be the expected one: apt trusts all keys of a keyring
|
|
69
|
+
if [[ "$(sort -u <<< "${found}")" != "${fingerprint}" ]]; then
|
|
70
|
+
echo "Error: apt repository ${name}: fingerprint mismatch for ${key}" >&2
|
|
71
|
+
echo " expected: ${fingerprint}" >&2
|
|
72
|
+
echo " found: $(tr '\n' ' ' <<< "${found}")" >&2
|
|
73
|
+
exit 1
|
|
74
|
+
fi
|
|
75
|
+
echo " key fingerprint ${fingerprint} OK"
|
|
76
|
+
fi
|
|
77
|
+
|
|
78
|
+
install -m 0644 "${tmp}/key" "${root}${keyfile}"
|
|
79
|
+
|
|
80
|
+
suite="${suite:-${codename}}"
|
|
81
|
+
if [[ -z "${suite}" ]]; then
|
|
82
|
+
echo "Error: apt repository ${name}: no suite given and the OS codename is unknown" >&2
|
|
83
|
+
exit 1
|
|
84
|
+
fi
|
|
85
|
+
{
|
|
86
|
+
echo "# [[apt_repos]] ${name} in agentbox.toml"
|
|
87
|
+
echo "Types: deb"
|
|
88
|
+
echo "URIs: ${url}"
|
|
89
|
+
echo "Suites: ${suite}"
|
|
90
|
+
[[ "${suite}" == */ ]] || echo "Components: ${components//,/ }"
|
|
91
|
+
echo "Signed-By: ${keyfile}"
|
|
92
|
+
} > "${sources}/${name}.sources"
|
|
93
|
+
chmod 0644 "${sources}/${name}.sources"
|
|
94
|
+
sed 's/^/ /' "${sources}/${name}.sources"
|
|
95
|
+
done
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# apt-get update that fails when any package index could not be downloaded, with retries.
|
|
3
|
+
#
|
|
4
|
+
# apt-get update only warns ("W: Failed to fetch ...") and exits 0 when a mirror is unreachable;
|
|
5
|
+
# the next apt-get install then fails with misleading "has no installation candidate" errors.
|
|
6
|
+
# Attempts: APT_UPDATE_ATTEMPTS (default 5), waiting 10 s, 20 s, ... between them.
|
|
7
|
+
set -uo pipefail
|
|
8
|
+
|
|
9
|
+
attempts="${APT_UPDATE_ATTEMPTS:-5}"
|
|
10
|
+
# The output is shown live (tee) and kept in a temporary file to look for fetch failures
|
|
11
|
+
log="$(mktemp)"
|
|
12
|
+
trap 'rm -f "${log}"' EXIT
|
|
13
|
+
for (( i = 1; i <= attempts; i++ )); do
|
|
14
|
+
echo "apt-get update (attempt ${i}/${attempts})"
|
|
15
|
+
apt-get update -o Acquire::Retries=3 2>&1 | tee "${log}"
|
|
16
|
+
rc=${PIPESTATUS[0]}
|
|
17
|
+
if (( rc == 0 )) && ! grep -qE '^(W|E): (Failed to fetch|Some index files failed)' "${log}"; then
|
|
18
|
+
exit 0
|
|
19
|
+
fi
|
|
20
|
+
if (( i < attempts )); then
|
|
21
|
+
echo "apt-get update could not download all package indexes (attempt ${i}/${attempts}), retrying in $(( i * 10 )) s" >&2
|
|
22
|
+
sleep $(( i * 10 ))
|
|
23
|
+
fi
|
|
24
|
+
done
|
|
25
|
+
echo "Error: apt-get update could not download the package indexes after ${attempts} attempts;" \
|
|
26
|
+
"check the network connection or the apt mirror, then run the build again" >&2
|
|
27
|
+
exit 1
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Create the container user: UID/GID match the host user, member of the sudo group.
|
|
3
|
+
# The password is not set at build time; the entrypoint sets it from CONTAINER_PWD at startup.
|
|
4
|
+
set -euo pipefail
|
|
5
|
+
|
|
6
|
+
: "${CONTAINER_USER:?CONTAINER_USER is not set}"
|
|
7
|
+
: "${USER_UID:?USER_UID is not set}"
|
|
8
|
+
: "${USER_GID:?USER_GID is not set}"
|
|
9
|
+
|
|
10
|
+
# If another user in the base image owns the UID (e.g. the ubuntu user in ubuntu:24.04+), remove it.
|
|
11
|
+
# Do this before handling the group: userdel may also delete that user's group.
|
|
12
|
+
if existing="$(getent passwd "${USER_UID}" | cut -d: -f1)" && [[ -n "${existing}" ]] \
|
|
13
|
+
&& [[ "${existing}" != "${CONTAINER_USER}" ]]; then
|
|
14
|
+
echo "UID ${USER_UID} is used by ${existing}, removing that user"
|
|
15
|
+
userdel -r "${existing}" 2>/dev/null || userdel "${existing}"
|
|
16
|
+
fi
|
|
17
|
+
|
|
18
|
+
# Group: reuse the GID if it already exists, otherwise create it
|
|
19
|
+
if getent group "${USER_GID}" >/dev/null; then
|
|
20
|
+
group_name="$(getent group "${USER_GID}" | cut -d: -f1)"
|
|
21
|
+
else
|
|
22
|
+
groupadd -g "${USER_GID}" "${CONTAINER_USER}"
|
|
23
|
+
group_name="${CONTAINER_USER}"
|
|
24
|
+
fi
|
|
25
|
+
|
|
26
|
+
useradd -m -u "${USER_UID}" -g "${group_name}" -G sudo -s /bin/bash "${CONTAINER_USER}"
|
|
27
|
+
|
|
28
|
+
home_dir="/home/${CONTAINER_USER}"
|
|
29
|
+
# The VNC directory is created by the entrypoint: ~/.vnc or ~/.config/tigervnc, depending on the TigerVNC version.
|
|
30
|
+
chown -R "${USER_UID}:${USER_GID}" "${home_dir}"
|
|
31
|
+
|
|
32
|
+
# ---- Shell configuration ----
|
|
33
|
+
# ~/.bashrc normally comes from /etc/skel; write a minimal one if the base image has none
|
|
34
|
+
if [[ ! -f "${home_dir}/.bashrc" ]]; then
|
|
35
|
+
cat > "${home_dir}/.bashrc" <<'EOF'
|
|
36
|
+
# ~/.bashrc: executed by bash for interactive shells
|
|
37
|
+
case $- in *i*) ;; *) return ;; esac
|
|
38
|
+
HISTCONTROL=ignoreboth
|
|
39
|
+
shopt -s histappend checkwinsize
|
|
40
|
+
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
|
|
41
|
+
alias ls='ls --color=auto'
|
|
42
|
+
alias grep='grep --color=auto'
|
|
43
|
+
alias ll='ls -alF'
|
|
44
|
+
EOF
|
|
45
|
+
fi
|
|
46
|
+
# Coloured prompt whenever the terminal supports colours (not only for TERM=*-256color)
|
|
47
|
+
sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' "${home_dir}/.bashrc"
|
|
48
|
+
|
|
49
|
+
# Login shells (SSH, `agentcabin shell`) read ~/.profile: make sure it loads ~/.bashrc
|
|
50
|
+
if ! grep -qs '\.bashrc' "${home_dir}/.profile"; then
|
|
51
|
+
cat >> "${home_dir}/.profile" <<'EOF'
|
|
52
|
+
|
|
53
|
+
# Load ~/.bashrc in login shells too
|
|
54
|
+
if [ -n "$BASH_VERSION" ] && [ -f "$HOME/.bashrc" ]; then
|
|
55
|
+
. "$HOME/.bashrc"
|
|
56
|
+
fi
|
|
57
|
+
EOF
|
|
58
|
+
fi
|
|
59
|
+
chown "${USER_UID}:${USER_GID}" "${home_dir}/.bashrc" "${home_dir}/.profile"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Install the base system packages (the list given as argument) and set up the locale and sshd.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
pkg_list="${1:-/opt/agentbox/base.list}"
|
|
6
|
+
bash "$(dirname "${BASH_SOURCE[0]}")/install_pkg_list.sh" "${pkg_list}"
|
|
7
|
+
|
|
8
|
+
# The entrypoint needs these; fail the build instead of starting a container without VNC.
|
|
9
|
+
# Debian/Ubuntu name them vncpasswd/vncserver (alternatives) or tigervncpasswd/tigervncserver.
|
|
10
|
+
for cmds in "vncpasswd tigervncpasswd" "vncserver tigervncserver"; do
|
|
11
|
+
found=""
|
|
12
|
+
for c in ${cmds}; do
|
|
13
|
+
command -v "${c}" >/dev/null && found="${c}" && break
|
|
14
|
+
done
|
|
15
|
+
if [[ -z "${found}" ]]; then
|
|
16
|
+
echo "Error: none of ${cmds} found after installing ${pkg_list}" >&2
|
|
17
|
+
echo " (vncpasswd is in tigervnc-tools on Ubuntu 22.04+, tigervnc-common on 20.04)" >&2
|
|
18
|
+
exit 1
|
|
19
|
+
fi
|
|
20
|
+
done
|
|
21
|
+
|
|
22
|
+
# locale: English by default, Chinese available for the desktop session
|
|
23
|
+
sed -i -E 's/^# ?(en_US\.UTF-8|zh_CN\.UTF-8)/\1/' /etc/locale.gen
|
|
24
|
+
locale-gen
|
|
25
|
+
update-locale LANG=en_US.UTF-8
|
|
26
|
+
|
|
27
|
+
# sshd: no auditd in the container, make pam_loginuid optional to avoid login failures
|
|
28
|
+
mkdir -p /run/sshd
|
|
29
|
+
sed -i 's/^\(session\s\+\)required\(\s\+pam_loginuid.so\)/\1optional\2/' /etc/pam.d/sshd
|
|
30
|
+
|
|
31
|
+
# Disable PackageKit's apt notification hooks: packages are managed through the CLI, and PolicyKit
|
|
32
|
+
# activation can time out inside the container.
|
|
33
|
+
if [[ -f /etc/apt/apt.conf.d/20packagekit ]]; then
|
|
34
|
+
mkdir -p /etc/apt/disabled-hooks
|
|
35
|
+
mv /etc/apt/apt.conf.d/20packagekit /etc/apt/disabled-hooks/
|
|
36
|
+
fi
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Install Firefox from Mozilla's APT repository (https://packages.mozilla.org).
|
|
3
|
+
# The distribution's own "firefox" package is a snap wrapper on Ubuntu 22.04+, and snaps
|
|
4
|
+
# do not run in containers; Mozilla's repository provides a real .deb for Ubuntu and Debian.
|
|
5
|
+
set -euo pipefail
|
|
6
|
+
|
|
7
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
8
|
+
|
|
9
|
+
install -d -m 0755 /etc/apt/keyrings
|
|
10
|
+
curl -fsSL --retry 3 https://packages.mozilla.org/apt/repo-signing-key.gpg \
|
|
11
|
+
-o /etc/apt/keyrings/packages.mozilla.org.asc
|
|
12
|
+
|
|
13
|
+
echo "deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main" \
|
|
14
|
+
> /etc/apt/sources.list.d/mozilla.list
|
|
15
|
+
|
|
16
|
+
# Prefer Mozilla's firefox over the distribution's snap wrapper
|
|
17
|
+
cat > /etc/apt/preferences.d/mozilla <<'PREF'
|
|
18
|
+
Package: *
|
|
19
|
+
Pin: origin packages.mozilla.org
|
|
20
|
+
Pin-Priority: 1000
|
|
21
|
+
PREF
|
|
22
|
+
|
|
23
|
+
bash "$(dirname "$0")/apt_update.sh"
|
|
24
|
+
apt-get install -y --no-install-recommends firefox
|
|
25
|
+
apt-get clean
|
|
26
|
+
rm -rf /var/lib/apt/lists/*
|
|
27
|
+
|
|
28
|
+
firefox --version
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Install the extra apt packages from `packages` in agentbox.toml (passed as arguments).
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
if (( $# == 0 )); then
|
|
6
|
+
echo "No extra packages"
|
|
7
|
+
exit 0
|
|
8
|
+
fi
|
|
9
|
+
|
|
10
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
11
|
+
echo "Installing extra packages: $*"
|
|
12
|
+
bash "$(dirname "$0")/apt_update.sh"
|
|
13
|
+
apt-get install -y --no-install-recommends "$@"
|
|
14
|
+
apt-get clean
|
|
15
|
+
rm -rf /var/lib/apt/lists/*
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Install the apt packages listed in a package list file:
|
|
3
|
+
# install_pkg_list.sh LIST
|
|
4
|
+
# One package per line, text after # is ignored. A trailing "?" marks an optional
|
|
5
|
+
# package: installed only if this OS release has it. An empty list installs nothing.
|
|
6
|
+
set -euo pipefail
|
|
7
|
+
|
|
8
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
9
|
+
|
|
10
|
+
pkg_list="${1:?usage: install_pkg_list.sh LIST}"
|
|
11
|
+
[[ -r "${pkg_list}" ]] || { echo "Error: package list not found: ${pkg_list}" >&2; exit 1; }
|
|
12
|
+
mapfile -t entries < <(sed -e 's/#.*//' -e 's/[[:space:]]//g' -e '/^$/d' "${pkg_list}")
|
|
13
|
+
if (( ${#entries[@]} == 0 )); then
|
|
14
|
+
echo "No packages in ${pkg_list}"
|
|
15
|
+
exit 0
|
|
16
|
+
fi
|
|
17
|
+
|
|
18
|
+
bash "$(dirname "$0")/apt_update.sh"
|
|
19
|
+
|
|
20
|
+
packages=()
|
|
21
|
+
for entry in "${entries[@]}"; do
|
|
22
|
+
if [[ "${entry}" == *"?" ]]; then
|
|
23
|
+
pkg="${entry%"?"}"
|
|
24
|
+
# Check for an installable version: `apt-cache show` also succeeds for a purely
|
|
25
|
+
# virtual package (e.g. libasound2 on Ubuntu 24.04+, provided by libasound2t64),
|
|
26
|
+
# which `apt-get install` then rejects. Such packages report "Candidate: (none)".
|
|
27
|
+
candidate="$(apt-cache policy "${pkg}" 2>/dev/null | awk '/Candidate:/ {print $2}')"
|
|
28
|
+
if [[ -n "${candidate}" && "${candidate}" != "(none)" ]]; then
|
|
29
|
+
packages+=("${pkg}")
|
|
30
|
+
else
|
|
31
|
+
echo "Optional package ${pkg} is not available on this OS release, skipping"
|
|
32
|
+
fi
|
|
33
|
+
else
|
|
34
|
+
packages+=("${entry}")
|
|
35
|
+
fi
|
|
36
|
+
done
|
|
37
|
+
|
|
38
|
+
echo "Installing ${#packages[@]} packages from ${pkg_list}"
|
|
39
|
+
apt-get install -y --no-install-recommends "${packages[@]}"
|
|
40
|
+
apt-get clean
|
|
41
|
+
rm -rf /var/lib/apt/lists/*
|