|
1 | 1 | ---
|
2 | 2 | schemaVersion: "2.2"
|
3 |
| -description: "SSM document to update Docker container on EC2" |
| 3 | +description: "SSM document to update Agent Docker container on EC2 with character configuration from SSM" |
4 | 4 | parameters:
|
5 | 5 | ImageParameterName:
|
6 | 6 | description: "SSM parameter name for Docker image"
|
7 | 7 | type: "String"
|
8 |
| - ConfigParameterName: |
9 |
| - description: "SSM parameter name for app configuration" |
| 8 | + allowedPattern: "^[a-zA-Z0-9/_-]+$" |
| 9 | + CharacterParameterName: |
| 10 | + description: "SSM parameter name for character configuration" |
10 | 11 | type: "String"
|
| 12 | + allowedPattern: "^[a-zA-Z0-9/_-]+$" |
| 13 | + ContainerMemory: |
| 14 | + description: "Memory limit for container (in MB)" |
| 15 | + type: "String" |
| 16 | + default: "512" |
| 17 | + allowedPattern: "^[0-9]+$" |
11 | 18 | mainSteps:
|
12 | 19 | - inputs:
|
13 | 20 | runCommand:
|
14 | 21 | - "#!/bin/bash"
|
15 | 22 | - "set -e"
|
| 23 | + - "" |
| 24 | + - "# Validate AWS CLI is installed" |
| 25 | + - "if ! command -v aws &> /dev/null; then" |
| 26 | + - " echo \"AWS CLI is not installed\" >&2" |
| 27 | + - " exit 1" |
| 28 | + - "fi" |
| 29 | + - "" |
| 30 | + - "# Validate Docker is running" |
| 31 | + - "if ! docker info &> /dev/null; then" |
| 32 | + - " echo \"Docker daemon is not running\" >&2" |
| 33 | + - " exit 1" |
| 34 | + - "fi" |
| 35 | + - "" |
| 36 | + - "# Source environment variables" |
| 37 | + - "if [ ! -f /var/run/agent/secrets/env ]; then" |
| 38 | + - " echo \"Environment file not found\" >&2" |
| 39 | + - " exit 1" |
| 40 | + - "fi" |
| 41 | + - "source /var/run/agent/secrets/env" |
| 42 | + - "" |
| 43 | + - "# Create required directories" |
| 44 | + - "mkdir -p /opt/agent/characters" |
| 45 | + - "" |
| 46 | + - "# Get parameters from SSM" |
| 47 | + - "echo \"Fetching parameters from SSM...\"" |
16 | 48 | - "IMAGE_NAME=$(aws ssm get-parameter --name {{ ImageParameterName }} --query\
|
17 | 49 | \ \"Parameter.Value\" --output text)"
|
18 |
| - - "CONFIG=$(aws ssm get-parameter --name {{ ConfigParameterName }} --with-decryption\ |
19 |
| - \ --query \"Parameter.Value\" --output text)" |
20 |
| - - "echo \"$CONFIG\" > /tmp/app_config.json" |
21 |
| - - "docker pull $IMAGE_NAME" |
22 |
| - - "docker stop app_container || true" |
23 |
| - - "docker rm app_container || true" |
24 |
| - - "docker run -d --name app_container -v /tmp/app_config.json:/app/config.json\ |
25 |
| - \ $IMAGE_NAME" |
| 50 | + - "if [ $? -ne 0 ]; then" |
| 51 | + - " echo \"Failed to fetch image parameter\" >&2" |
| 52 | + - " exit 1" |
| 53 | + - "fi" |
| 54 | + - "" |
| 55 | + - "CHARACTER_DATA=$(aws ssm get-parameter --name {{ CharacterParameterName }}\ |
| 56 | + \ --with-decryption --query \"Parameter.Value\" --output text)" |
| 57 | + - "if [ $? -ne 0 ]; then" |
| 58 | + - " echo \"Failed to fetch character data\" >&2" |
| 59 | + - " exit 1" |
| 60 | + - "fi" |
| 61 | + - "" |
| 62 | + - "# Validate JSON data" |
| 63 | + - "echo \"$CHARACTER_DATA\" | jq empty" |
| 64 | + - "if [ $? -ne 0 ]; then" |
| 65 | + - " echo \"Invalid character JSON configuration\" >&2" |
| 66 | + - " exit 1" |
| 67 | + - "fi" |
| 68 | + - "" |
| 69 | + - "# Write character data to file" |
| 70 | + - "echo \"$CHARACTER_DATA\" > /opt/agent/characters/eliza.character.json" |
| 71 | + - "chmod 600 /opt/agent/characters/eliza.character.json" |
| 72 | + - "" |
| 73 | + - "# Create required volumes if they don't exist" |
| 74 | + - "docker volume create tokenizer || true" |
| 75 | + - "" |
| 76 | + - "# Stop and remove existing container" |
| 77 | + - "echo \"Stopping existing container...\"" |
| 78 | + - "docker stop agent-docker.service 2>/dev/null || true" |
| 79 | + - "docker rm agent-docker.service 2>/dev/null || true" |
| 80 | + - "" |
| 81 | + - "# Start new container" |
| 82 | + - "echo \"Starting new container...\"" |
| 83 | + - "docker run -d \\\n" |
| 84 | + - " -p 3000:3000 \\\n" |
| 85 | + - " -v tokenizer:/app/node_modules/@anush008/tokenizers/ \\\n" |
| 86 | + - " -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/\ |
| 87 | + \ \\\n" |
| 88 | + - " --mount type=bind,source=/opt/agent,target=/opt/agent \\\n" |
| 89 | + - " --mount type=bind,source=/opt/agent/characters/,target=/app/agent/characters/\ |
| 90 | + \ \\\n" |
| 91 | + - " --env-file /var/run/agent/secrets/env \\\n" |
| 92 | + - " --memory={{ ContainerMemory }}m \\\n" |
| 93 | + - " --memory-swap={{ ContainerMemory }}m \\\n" |
| 94 | + - " --health-cmd=\"curl -f http://localhost:3000/health || exit 1\" \\\n" |
| 95 | + - " --health-interval=30s \\\n" |
| 96 | + - " --health-timeout=10s \\\n" |
| 97 | + - " --health-retries=3 \\\n" |
| 98 | + - " --rm \\\n" |
| 99 | + - " --name \"agent-docker.service\" \\\n" |
| 100 | + - " --entrypoint /opt/agent/docker-entrypoint-strace2.sh \\\n" |
| 101 | + - " ${AGENT_IMAGE:-$IMAGE_NAME}" |
| 102 | + - "" |
| 103 | + - "# Wait for container to be healthy" |
| 104 | + - "echo \"Waiting for container to be healthy...\"" |
| 105 | + - "timeout 60 bash -c 'until docker ps --filter \"name=agent-docker.service\"\ |
| 106 | + \ --filter \"health=healthy\" | grep agent-docker.service; do sleep 2; done'" |
| 107 | + - "" |
| 108 | + - "echo \"Container update completed successfully\"" |
26 | 109 | name: "UpdateDockerContainer"
|
27 | 110 | action: "aws:runShellScript"
|
28 | 111 | - inputs:
|
29 | 112 | runCommand:
|
30 | 113 | - "#!/bin/bash"
|
31 | 114 | - "if [ $? -ne 0 ]; then"
|
32 |
| - - " echo \"Error occurred during container update\" >> /var/log/container_update_errors.log" |
33 |
| - - " docker logs app_container >> /var/log/container_update_errors.log" |
| 115 | + - " echo \"Container update failed at $(date)\" | tee -a /var/log/container_update_errors.log" |
| 116 | + - " echo \"Container logs:\" | tee -a /var/log/container_update_errors.log" |
| 117 | + - " docker logs agent-docker.service 2>&1 | tee -a /var/log/container_update_errors.log" |
| 118 | + - " echo \"System information:\" | tee -a /var/log/container_update_errors.log" |
| 119 | + - " docker info | tee -a /var/log/container_update_errors.log" |
| 120 | + - " df -h | tee -a /var/log/container_update_errors.log" |
| 121 | + - " exit 1" |
34 | 122 | - "fi"
|
35 | 123 | name: "CaptureErrors"
|
36 | 124 | action: "aws:runShellScript"
|
0 commit comments