Skip to content

Commit 782b5a6

Browse files
author
yolo h8cker 93
committed
Scripts to interact with aws
1 parent b7d9c57 commit 782b5a6

3 files changed

Lines changed: 120 additions & 15 deletions

File tree

docker/aws/AWSCodeClash.Dockerfile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ RUN apt update && apt install -y \
77
git \
88
curl \
99
unzip \
10+
iptables \
11+
ca-certificates \
12+
gnupg \
13+
lsb-release \
1014
&& rm -rf /var/lib/apt/lists/*
1115

12-
# Install Docker
13-
RUN curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
16+
# Install Docker with proper setup for Docker-in-Docker
17+
RUN curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh \
18+
&& usermod -aG docker root
1419

1520
# Install AWS CLI
1621
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
@@ -26,14 +31,22 @@ ARG GITHUB_TOKEN
2631
RUN git clone https://klieret:${GITHUB_TOKEN}@github.com/emagedoc/CodeClash.git . \
2732
&& python3 -m venv .venv \
2833
&& . .venv/bin/activate \
29-
&& pip install -e .
34+
&& pip install -e '.[dev]'
3035

3136
# Set ulimit for open files
3237
RUN echo "* soft nofile 65536" >> /etc/security/limits.conf \
3338
&& echo "* hard nofile 65536" >> /etc/security/limits.conf
3439

40+
# Create Docker directory and set permissions
41+
RUN mkdir -p /var/lib/docker && chmod 755 /var/lib/docker
42+
43+
# Set build timestamp as environment variable
44+
ARG BUILD_TIMESTAMP
45+
ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
46+
3547
# Entry script
3648
COPY entrypoint.sh /entrypoint.sh
3749
RUN chmod +x /entrypoint.sh
3850

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

docker/aws/build_and_push_aws.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Configuration
5+
ECR_REGISTRY="039984708918.dkr.ecr.us-east-1.amazonaws.com"
6+
ECR_REPOSITORY="codeclash"
7+
IMAGE_TAG="latest"
8+
REGION="us-east-1"
9+
DOCKERFILE_PATH="AWSCodeClash.Dockerfile"
10+
DOCKER_CONTEXT="."
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
YELLOW='\033[1;33m'
16+
NC='\033[0m' # No Color
17+
18+
echo -e "${GREEN}Starting AWS ECR build and push process...${NC}"
19+
20+
# Check if required tools are installed
21+
command -v aws >/dev/null 2>&1 || { echo -e "${RED}Error: AWS CLI is required but not installed.${NC}" >&2; exit 1; }
22+
command -v docker >/dev/null 2>&1 || { echo -e "${RED}Error: Docker is required but not installed.${NC}" >&2; exit 1; }
23+
24+
# Check if GITHUB_TOKEN is set (required for the Dockerfile)
25+
if [ -z "${GITHUB_TOKEN:-}" ]; then
26+
echo -e "${RED}Error: GITHUB_TOKEN environment variable is required for building this container.${NC}"
27+
echo "Please set it with: export GITHUB_TOKEN=your_token_here"
28+
exit 1
29+
fi
30+
31+
# Build the Docker image
32+
echo -e "${YELLOW}Building Docker image...${NC}"
33+
FULL_IMAGE_NAME="$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
34+
35+
# Authenticate with ECR
36+
echo -e "${YELLOW}Authenticating with AWS ECR...${NC}"
37+
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
38+
39+
docker build \
40+
--build-arg GITHUB_TOKEN="$GITHUB_TOKEN" \
41+
--build-arg BUILD_TIMESTAMP="$(date -u '+%Y-%m-%d %H:%M:%S UTC')" \
42+
--platform linux/amd64 \
43+
-f "$DOCKERFILE_PATH" \
44+
-t "$FULL_IMAGE_NAME" \
45+
"$DOCKER_CONTEXT"
46+
47+
# Push the image to ECR
48+
echo -e "${YELLOW}Pushing image to ECR...${NC}"
49+
docker push "$FULL_IMAGE_NAME"
50+
51+
echo -e "${GREEN}Successfully built and pushed image: $FULL_IMAGE_NAME${NC}"
52+
echo -e "${GREEN}You can now use this image in your AWS services.${NC}"
53+
echo ""
54+
echo -e "${YELLOW}IMPORTANT: When running this container on AWS Batch or other services,${NC}"
55+
echo -e "${YELLOW}make sure to enable privileged mode for Docker-in-Docker functionality.${NC}"
56+
echo -e "${YELLOW}For AWS Batch, add 'privileged: true' to your job definition.${NC}"

docker/aws/entrypoint.sh

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,54 @@
11
#!/bin/bash
2-
set -e
32

4-
# Start Docker daemon (if needed)
5-
dockerd &
6-
#
7-
# # Activate venv
3+
set -euo pipefail
4+
5+
echo "📅 Container built at: $BUILD_TIMESTAMP"
6+
7+
echo "Git pull"
8+
git pull
9+
10+
# Function to sync logs on exit
11+
cleanup() {
12+
local exit_code=$?
13+
echo "Syncing logs to S3..."
14+
aws s3 sync logs/ s3://codeclash/logs/ || echo "Warning: Failed to sync logs to S3"
15+
exit $exit_code
16+
}
17+
18+
# Set trap to always sync logs on exit (normal exit, signals, errors)
19+
trap cleanup EXIT
20+
21+
# Start Docker daemon with proper configuration for AWS Batch
22+
echo "Starting Docker daemon..."
23+
# Use vfs storage driver to avoid overlay permission issues
24+
dockerd --storage-driver=vfs --iptables=false --ip-masq=false --bridge=none &
25+
26+
# Wait for Docker daemon to be ready
27+
echo "Waiting for Docker daemon to start..."
28+
for i in {1..30}; do
29+
if docker info >/dev/null 2>&1; then
30+
echo "Docker daemon is ready!"
31+
break
32+
fi
33+
if [ $i -eq 30 ]; then
34+
echo "ERROR: Docker daemon failed to start after 30 seconds"
35+
exit 1
36+
fi
37+
sleep 1
38+
done
39+
40+
# Smoke test
41+
docker run hello-world
42+
43+
# Activate venv
844
source .venv/bin/activate
9-
#
10-
# # Create logs directory and sync from S3
45+
46+
# Create logs directory
1147
mkdir -p logs
12-
aws s3 sync s3://codeclash/logs/ logs/
13-
#
14-
# # Set ulimit
48+
# aws s3 sync s3://codeclash/logs/ logs/
49+
50+
# Set ulimit for number of open files, relevant for matrix
1551
ulimit -n 65536
16-
#
17-
# # Execute the command passed to container
52+
53+
# Execute the command passed to container
1854
exec "$@"

0 commit comments

Comments
 (0)