Skip to content

Commit 6f74370

Browse files
author
yolo h8cker 93
committed
Ref(aws): Cleaner & easier to update way with bootstrap script
1 parent d7c9ef8 commit 6f74370

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ done
5555
# Smoke test
5656
docker run hello-world
5757

58+
# Pull images from ECR so we don't have to build them
5859
DOCKER_REGISTRY="039984708918.dkr.ecr.us-east-1.amazonaws.com"
5960
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $DOCKER_REGISTRY
6061

@@ -63,9 +64,6 @@ for game in battlesnake; do
6364
docker tag $DOCKER_REGISTRY/codeclash/$game codeclash/$game
6465
done
6566

66-
# Activate venv
67-
source .venv/bin/activate
68-
6967
# Create logs directory
7068
mkdir -p logs
7169
# aws s3 sync s3://codeclash/logs/ logs/

aws/run_job.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import argparse
1515
import json
16+
import shlex
17+
import subprocess
1618
import sys
1719
import time
1820
from typing import Any
@@ -24,6 +26,18 @@
2426
logger = get_logger("launch", emoji="🚀")
2527

2628

29+
def get_current_git_branch() -> str:
30+
"""Get the current git branch name."""
31+
try:
32+
result = subprocess.run(
33+
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
34+
)
35+
return result.stdout.strip()
36+
except subprocess.CalledProcessError as e:
37+
logger.error(f"Failed to get git branch: {e}")
38+
raise
39+
40+
2741
class AWSBatchJobLauncher:
2842
def __init__(self, job_definition_name: str = "codeclash-yolo-test", job_queue: str = "codeclash-test-queue"):
2943
self.batch_client = boto3.client("batch")
@@ -54,21 +68,26 @@ def submit_job(self, command: list[str], job_name: str | None = None) -> str:
5468
timestamp = int(time.time())
5569
job_name = f"codeclash-{cmd_name}-{timestamp}"
5670

71+
# Get current git branch and prepend it to the command
72+
current_branch = get_current_git_branch()
73+
full_command = [current_branch] + command
74+
5775
# Get the latest job definition
5876
job_definition_arn = self.get_latest_job_definition_arn()
5977

6078
response = self.batch_client.submit_job(
6179
jobName=job_name,
6280
jobQueue=self.job_queue,
6381
jobDefinition=job_definition_arn,
64-
containerOverrides={"command": command},
82+
containerOverrides={"command": full_command},
6583
)
6684

6785
job_id = response["jobId"]
6886
logger.info("Job submitted successfully!")
6987
logger.info(f"Job ID: {job_id}")
7088
logger.info(f"Job Name: {job_name}")
71-
logger.info(f"Command: {' '.join(command)}")
89+
logger.info(f"Original Command: {shlex.join(command)}")
90+
logger.info(f"Full Command (passed to container entrypoint/bootstrap.sh): {shlex.join(full_command)}")
7291
logger.info(f"To retrieve logs later, run: python get_job_log.py {job_id}")
7392

7493
return job_id
@@ -132,12 +151,6 @@ def main():
132151
parser = argparse.ArgumentParser(
133152
description="Submit jobs to AWS Batch",
134153
formatter_class=argparse.RawDescriptionHelpFormatter,
135-
epilog="""
136-
Examples:
137-
%(prog)s -- main.py configs/test/battlesnake_pvp_test.yaml
138-
%(prog)s --job-name my-test -- main.py @battlesnake_pvp_test.yaml --suffix test-run
139-
%(prog)s --wait --show-logs -- python -m pytest tests/
140-
""",
141154
)
142155
parser.add_argument("--job-name", help="Custom job name (auto-generated if not specified)")
143156
parser.add_argument(

aws/setup/AWSCodeClash.Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ RUN mkdir -p /var/lib/docker /var/run/docker \
4848
ARG BUILD_TIMESTAMP
4949
ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
5050

51-
# Entry script
52-
COPY entrypoint.sh /entrypoint.sh
53-
RUN chmod +x /entrypoint.sh
51+
# Entry script - replace the existing COPY and ENTRYPOINT lines
52+
COPY bootstrap.sh /bootstrap.sh
53+
RUN chmod +x /bootstrap.sh
5454

5555
# Note: Container must be run with --privileged flag for Docker-in-Docker
56-
ENTRYPOINT ["/entrypoint.sh"]
56+
ENTRYPOINT ["/bootstrap.sh"]

aws/setup/bootstrap.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
echo "📅 Container built at: $BUILD_TIMESTAMP"
6+
echo "🔄 Updating repository..."
7+
8+
# First parameter is the branch name, rest are the command to execute
9+
BRANCH_NAME="$1"
10+
shift # Remove first argument, leaving the rest for exec
11+
12+
echo "🌿 Checking out branch: $BRANCH_NAME"
13+
git fetch
14+
git checkout "$BRANCH_NAME"
15+
git pull origin "$BRANCH_NAME"
16+
17+
source .venv/bin/activate
18+
19+
# Execute the remaining command arguments
20+
exec "$@"

0 commit comments

Comments
 (0)