diff --git a/aws/ssm/documents/deploy_eliza.yml b/aws/ssm/documents/deploy_eliza.yml new file mode 100644 index 00000000000..714dec27811 --- /dev/null +++ b/aws/ssm/documents/deploy_eliza.yml @@ -0,0 +1,124 @@ +--- +schemaVersion: "2.2" +description: "SSM document to update Agent Docker container on EC2 with character configuration from SSM" +parameters: + ImageParameterName: + description: "SSM parameter name for Docker image" + type: "String" + allowedPattern: "^[a-zA-Z0-9/_-]+$" + CharacterParameterName: + description: "SSM parameter name for character configuration" + type: "String" + allowedPattern: "^[a-zA-Z0-9/_-]+$" + ContainerMemory: + description: "Memory limit for container (in MB)" + type: "String" + default: "512" + allowedPattern: "^[0-9]+$" +mainSteps: +- inputs: + runCommand: + - "#!/bin/bash" + - "set -e" + - "" + - "# Validate AWS CLI is installed" + - "if ! command -v aws &> /dev/null; then" + - " echo \"AWS CLI is not installed\" >&2" + - " exit 1" + - "fi" + - "" + - "# Validate Docker is running" + - "if ! docker info &> /dev/null; then" + - " echo \"Docker daemon is not running\" >&2" + - " exit 1" + - "fi" + - "" + - "# Source environment variables" + - "if [ ! -f /var/run/agent/secrets/env ]; then" + - " echo \"Environment file not found\" >&2" + - " exit 1" + - "fi" + - "source /var/run/agent/secrets/env" + - "" + - "# Create required directories" + - "mkdir -p /opt/agent/characters" + - "" + - "# Get parameters from SSM" + - "echo \"Fetching parameters from SSM...\"" + - "IMAGE_NAME=$(aws ssm get-parameter --name {{ ImageParameterName }} --query\ + \ \"Parameter.Value\" --output text)" + - "if [ $? -ne 0 ]; then" + - " echo \"Failed to fetch image parameter\" >&2" + - " exit 1" + - "fi" + - "" + - "CHARACTER_DATA=$(aws ssm get-parameter --name {{ CharacterParameterName }}\ + \ --with-decryption --query \"Parameter.Value\" --output text)" + - "if [ $? -ne 0 ]; then" + - " echo \"Failed to fetch character data\" >&2" + - " exit 1" + - "fi" + - "" + - "# Validate JSON data" + - "echo \"$CHARACTER_DATA\" | jq empty" + - "if [ $? -ne 0 ]; then" + - " echo \"Invalid character JSON configuration\" >&2" + - " exit 1" + - "fi" + - "" + - "# Write character data to file" + - "echo \"$CHARACTER_DATA\" > /opt/agent/characters/eliza.character.json" + - "chmod 600 /opt/agent/characters/eliza.character.json" + - "" + - "# Create required volumes if they don't exist" + - "docker volume create tokenizer || true" + - "" + - "# Stop and remove existing container" + - "echo \"Stopping existing container...\"" + - "docker stop agent-docker.service 2>/dev/null || true" + - "docker rm agent-docker.service 2>/dev/null || true" + - "" + - "# Start new container" + - "echo \"Starting new container...\"" + - "docker run -d \\\n" + - " -p 3000:3000 \\\n" + - " -v tokenizer:/app/node_modules/@anush008/tokenizers/ \\\n" + - " -v tokenizer:/app/node_modules/fastembed/node_modules/.pnpm/@anush008+tokenizers@https+++codeload.github.com+meta-introspector+arm64-tokenizers+tar.gz+98_s2457qj3pe4ojcbckddasgzfvu/node_modules/@anush008/\ + \ \\\n" + - " --mount type=bind,source=/opt/agent,target=/opt/agent \\\n" + - " --mount type=bind,source=/opt/agent/characters/,target=/app/agent/characters/\ + \ \\\n" + - " --env-file /var/run/agent/secrets/env \\\n" + - " --memory={{ ContainerMemory }}m \\\n" + - " --memory-swap={{ ContainerMemory }}m \\\n" + - " --health-cmd=\"curl -f http://localhost:3000/health || exit 1\" \\\n" + - " --health-interval=30s \\\n" + - " --health-timeout=10s \\\n" + - " --health-retries=3 \\\n" + - " --rm \\\n" + - " --name \"agent-docker.service\" \\\n" + - " --entrypoint /opt/agent/docker-entrypoint-strace2.sh \\\n" + - " ${AGENT_IMAGE:-$IMAGE_NAME}" + - "" + - "# Wait for container to be healthy" + - "echo \"Waiting for container to be healthy...\"" + - "timeout 60 bash -c 'until docker ps --filter \"name=agent-docker.service\"\ + \ --filter \"health=healthy\" | grep agent-docker.service; do sleep 2; done'" + - "" + - "echo \"Container update completed successfully\"" + name: "UpdateDockerContainer" + action: "aws:runShellScript" +- inputs: + runCommand: + - "#!/bin/bash" + - "if [ $? -ne 0 ]; then" + - " echo \"Container update failed at $(date)\" | tee -a /var/log/container_update_errors.log" + - " echo \"Container logs:\" | tee -a /var/log/container_update_errors.log" + - " docker logs agent-docker.service 2>&1 | tee -a /var/log/container_update_errors.log" + - " echo \"System information:\" | tee -a /var/log/container_update_errors.log" + - " docker info | tee -a /var/log/container_update_errors.log" + - " df -h | tee -a /var/log/container_update_errors.log" + - " exit 1" + - "fi" + name: "CaptureErrors" + action: "aws:runShellScript"