From 3ff07245476a01dce14998aa60f87a59627688ff Mon Sep 17 00:00:00 2001 From: Mike DuPont Date: Mon, 17 Feb 2025 06:12:15 -0500 Subject: [PATCH 1/2] Create deploy_eliza.yml --- aws/ssm/documents/deploy_eliza.yml | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 aws/ssm/documents/deploy_eliza.yml diff --git a/aws/ssm/documents/deploy_eliza.yml b/aws/ssm/documents/deploy_eliza.yml new file mode 100644 index 00000000000..d2da8ef2cd7 --- /dev/null +++ b/aws/ssm/documents/deploy_eliza.yml @@ -0,0 +1,36 @@ +--- +schemaVersion: "2.2" +description: "SSM document to update Docker container on EC2" +parameters: + ImageParameterName: + description: "SSM parameter name for Docker image" + type: "String" + ConfigParameterName: + description: "SSM parameter name for app configuration" + type: "String" +mainSteps: +- inputs: + runCommand: + - "#!/bin/bash" + - "set -e" + - "IMAGE_NAME=$(aws ssm get-parameter --name {{ ImageParameterName }} --query\ + \ \"Parameter.Value\" --output text)" + - "CONFIG=$(aws ssm get-parameter --name {{ ConfigParameterName }} --with-decryption\ + \ --query \"Parameter.Value\" --output text)" + - "echo \"$CONFIG\" > /tmp/app_config.json" + - "docker pull $IMAGE_NAME" + - "docker stop app_container || true" + - "docker rm app_container || true" + - "docker run -d --name app_container -v /tmp/app_config.json:/app/config.json\ + \ $IMAGE_NAME" + name: "UpdateDockerContainer" + action: "aws:runShellScript" +- inputs: + runCommand: + - "#!/bin/bash" + - "if [ $? -ne 0 ]; then" + - " echo \"Error occurred during container update\" >> /var/log/container_update_errors.log" + - " docker logs app_container >> /var/log/container_update_errors.log" + - "fi" + name: "CaptureErrors" + action: "aws:runShellScript" From 01f3d7eb5402645a41f5b26f598b6ea5231dd012 Mon Sep 17 00:00:00 2001 From: Mike DuPont Date: Mon, 17 Feb 2025 07:40:05 -0500 Subject: [PATCH 2/2] Update deploy_eliza.yml --- aws/ssm/documents/deploy_eliza.yml | 114 +++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 13 deletions(-) diff --git a/aws/ssm/documents/deploy_eliza.yml b/aws/ssm/documents/deploy_eliza.yml index d2da8ef2cd7..714dec27811 100644 --- a/aws/ssm/documents/deploy_eliza.yml +++ b/aws/ssm/documents/deploy_eliza.yml @@ -1,36 +1,124 @@ --- schemaVersion: "2.2" -description: "SSM document to update Docker container on EC2" +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" - ConfigParameterName: - description: "SSM parameter name for app configuration" + 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)" - - "CONFIG=$(aws ssm get-parameter --name {{ ConfigParameterName }} --with-decryption\ - \ --query \"Parameter.Value\" --output text)" - - "echo \"$CONFIG\" > /tmp/app_config.json" - - "docker pull $IMAGE_NAME" - - "docker stop app_container || true" - - "docker rm app_container || true" - - "docker run -d --name app_container -v /tmp/app_config.json:/app/config.json\ - \ $IMAGE_NAME" + - "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 \"Error occurred during container update\" >> /var/log/container_update_errors.log" - - " docker logs app_container >> /var/log/container_update_errors.log" + - " 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"