Skip to content

Commit 8b82cad

Browse files
Add Android container environment for runner
This commit introduces a new Docker-based runner environment utilizing Redroid and Termux to run snippets within a full Android OS. The following files were added: - src/runner/Dockerfile.android: Defines the Docker image based on Redroid, responsible for setting up the base Android system and Termux. - src/runner/entrypoint.sh: Orchestrates the container startup, launching Android's init process and the setup script. - src/runner/setup_runner.sh: Handles the one-time setup of Termux, installation of necessary interpreters (Node.js, Python, Ruby, PHP, Java, R, etc.), Node.js dependencies, and global npm packages. It then launches the runner.js application. This new environment provides enhanced isolation and a more realistic Android execution context. The existing src/runner/Dockerfile and docker-compose.yml remain unchanged, allowing the Android runner to be an alternative environment rather than a direct replacement for the current setup.
1 parent b5797bb commit 8b82cad

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/runner/Dockerfile.android

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM redroid/redroid:13.0.0-latest
2+
3+
# Temporarily install wget during the build phase to download the Termux APK.
4+
RUN apk add --no-cache wget
5+
6+
# Download the Termux APK from F-Droid.
7+
# The official F-Droid repository for Termux is https://f-droid.org/en/packages/com.termux/
8+
# We need to find the direct APK link from a mirror or the main site.
9+
# For this example, I'll use a placeholder URL. This needs to be updated with the actual APK URL.
10+
# A common source for Termux APKs is a mirror like termux.net or directly from F-Droid's repo.
11+
# Let's assume we've found a direct link to version 0.118.0 (a common version).
12+
# IMPORTANT: The specific version and download URL should be verified and updated.
13+
RUN wget -O /tmp/termux.apk https://f-droid.org/repo/com.termux_118.apk
14+
15+
# Set the working directory for the runner application.
16+
WORKDIR /app
17+
18+
# Copy the runner's source code into the /app directory.
19+
# This includes runner.js, package.json, and any other necessary files.
20+
COPY runner.js .
21+
COPY package.json .
22+
COPY package-lock.json .
23+
24+
# Copy the orchestration scripts into the image.
25+
# These scripts will manage the setup and execution within the Android environment.
26+
COPY entrypoint.sh /entrypoint.sh
27+
COPY setup_runner.sh /setup_runner.sh
28+
29+
# Ensure the scripts are executable
30+
RUN chmod +x /entrypoint.sh /setup_runner.sh
31+
32+
# Define the entrypoint.sh script as the entry point that will run when the container starts.
33+
ENTRYPOINT ["/entrypoint.sh"]

src/runner/entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/system/bin/sh
2+
3+
echo "Starting Android init..."
4+
# Launch Android init in the background.
5+
# This allows the script to continue while the OS boots.
6+
/init &
7+
8+
echo "Executing setup_runner.sh..."
9+
# Execute the main setup script.
10+
# This script handles Termux installation, package setup, and starts the runner app.
11+
sh /setup_runner.sh
12+
13+
# Keep the container alive.
14+
# This simple loop prevents the container from exiting prematurely.
15+
# A more robust solution might involve monitoring the runner.js process.
16+
echo "Entrypoint finished. Keeping container alive."
17+
while true; do
18+
sleep 60
19+
done

src/runner/setup_runner.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/system/bin/sh
2+
3+
echo "Setup script started."
4+
5+
# --- Android Boot Verification ---
6+
echo "Waiting for Android system to boot completely..."
7+
# Loop until the 'sys.boot_completed' property is '1'
8+
while [ "$(getprop sys.boot_completed)" != "1" ]; do
9+
sleep 5 # Wait for 5 seconds before checking again
10+
echo "Still waiting for boot to complete..."
11+
done
12+
echo "Android system booted successfully."
13+
14+
# --- One-Time Installation Logic ---
15+
FLAG_FILE="/data/termux_setup_completed.flag"
16+
17+
if [ ! -f "$FLAG_FILE" ]; then
18+
echo "First boot detected. Starting Termux setup and package installation..."
19+
20+
# Install Termux APK
21+
echo "Installing Termux from /tmp/termux.apk..."
22+
pm install /tmp/termux.apk
23+
# It might take a moment for the installation to complete.
24+
sleep 10
25+
26+
# Start Termux once to allow it to configure its internal environment.
27+
# We need to run a Termux command. 'termux-setup-storage' is a common one.
28+
# Or, more simply, just try to run 'bash' via termux-exec to ensure its environment is set up.
29+
echo "Initializing Termux environment..."
30+
termux-wake-lock # Acquire wake lock
31+
# The following command attempts to start a bash session within Termux,
32+
# which should trigger its initial setup if needed.
33+
# We pipe 'exit' to it to close the session immediately after.
34+
echo "exit" | termux-exec bash
35+
sleep 5 # Give Termux a moment to set itself up
36+
37+
# Update Termux repositories
38+
echo "Updating Termux repositories..."
39+
termux-exec pkg update -y
40+
termux-exec pkg upgrade -y
41+
42+
# Install required interpreters and tools
43+
echo "Installing packages via pkg..."
44+
termux-exec pkg install -y nodejs-lts python ruby php openjdk-17 r-base lua54 gawk tcl expect bash zsh tcsh curl gnupg ca-certificates git vim # Added vim as a generally useful tool
45+
46+
# Change directory to /app
47+
echo "Changing directory to /app..."
48+
# This cd is for the current script's execution context.
49+
# termux-exec will also need its CWD set for npm commands.
50+
51+
# Run npm install for local dependencies using Termux's Node.js/npm
52+
echo "Installing Node.js dependencies in /app..."
53+
termux-exec sh -c 'cd /app && npm install --production'
54+
55+
# Install global npm packages
56+
echo "Installing global npm packages (typescript, ts-node, taylored)..."
57+
termux-exec npm install -g typescript ts-node taylored
58+
59+
# Create the flag file to indicate setup completion
60+
echo "Creating flag file: $FLAG_FILE"
61+
touch "$FLAG_FILE"
62+
termux-wake-unlock # Release wake lock
63+
echo "Termux setup and package installation complete."
64+
else
65+
echo "Termux setup already completed (flag file found). Skipping installation."
66+
fi
67+
68+
# --- Application Execution ---
69+
echo "Executing the runner application: node /app/runner.js"
70+
# Ensure Termux environment is active for the node execution
71+
# The actual command to run Node.js applications in Termux is via 'node' directly
72+
# if the environment (like PATH) is correctly set up by termux-exec.
73+
# 'termux-exec' sets up the environment for the command it runs.
74+
termux-exec node /app/runner.js
75+
76+
echo "setup_runner.sh finished."

0 commit comments

Comments
 (0)