scripts/setup-docker-compose.sh
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

SERVER_ENV_FILE=.env.server
DB_ENV_FILE=.env.db
DB_NAME=vivaria
TEST_DB_NAME=test
DB_USER=vivaria
DB_PASSWORD="$(openssl rand -base64 32)"
DB_READONLY_USER=vivariaro
DB_READONLY_PASSWORD="$(openssl rand -base64 32)"

echo "ACCESS_TOKEN_SECRET_KEY=$(openssl rand -base64 32)" > "${SERVER_ENV_FILE}"

echo "ACCESS_TOKEN=$(openssl rand -base64 32)" >> "${SERVER_ENV_FILE}"
echo "ID_TOKEN=$(openssl rand -base64 32)" >> "${SERVER_ENV_FILE}"

echo "AGENT_CPU_COUNT=1" >> "${SERVER_ENV_FILE}"
echo "AGENT_RAM_GB=4" >> "${SERVER_ENV_FILE}"

if [ "$(uname -m)" = "arm64" ]; then
  echo "DOCKER_BUILD_PLATFORM=linux/arm64" >> "${SERVER_ENV_FILE}"
fi

echo "PGDATABASE=${DB_NAME}" >> "${SERVER_ENV_FILE}"
echo "TEST_PGDATABASE=${TEST_DB_NAME}" >> "${SERVER_ENV_FILE}"
echo "PGUSER=${DB_USER}" >> "${SERVER_ENV_FILE}"
echo "PGPASSWORD=${DB_PASSWORD}" >> "${SERVER_ENV_FILE}"

echo "PG_READONLY_USER=${DB_READONLY_USER}" >> "${SERVER_ENV_FILE}"
echo "PG_READONLY_PASSWORD=${DB_READONLY_PASSWORD}" >> "${SERVER_ENV_FILE}"

echo "TASK_ENVIRONMENT_STORAGE_GB=-1" >> "${SERVER_ENV_FILE}"

echo "POSTGRES_DB=${DB_NAME}" > "${DB_ENV_FILE}"
echo "TEST_POSTGRES_DB=${TEST_DB_NAME}" >> "${DB_ENV_FILE}"
echo "POSTGRES_USER=${DB_USER}" >> "${DB_ENV_FILE}"
echo "POSTGRES_PASSWORD=${DB_PASSWORD}" >> "${DB_ENV_FILE}"
echo "PG_READONLY_USER=${DB_READONLY_USER}" >> "${DB_ENV_FILE}"
echo "PG_READONLY_PASSWORD=${DB_READONLY_PASSWORD}" >> "${DB_ENV_FILE}"

# Adds a proxy for MacOS to connect with the containers
if [[ "$(uname)" == "Darwin" ]]; then
    if [[ -s docker-compose.override.yml ]]; then
        echo "Error: docker-compose.override.yml already exists and is not empty." >&2
        exit 1
    fi
    cat > docker-compose.override.yml << EOF
services:
  proxy:
    image: quay.io/panubo/sshd
    ports:
      - "2222:22"
    volumes:
      - "\${SSH_PUBLIC_KEY_PATH-~/.ssh/id_rsa.pub}:/etc/authorized_keys/agent:ro"
    environment:
      TCP_FORWARDING: true
      SSH_USERS: "agent:48:48"
    networks:
      - full-internet
      - no-internet

EOF
fi
