diff --git a/bin/build_and_push.sh b/bin/build_and_push.sh new file mode 100755 index 0000000..80f7d4e --- /dev/null +++ b/bin/build_and_push.sh @@ -0,0 +1,39 @@ +#!/bin/sh +REGION=us-east-2 +ENVIRONMENT=testnet +ECR_REGISTRY=377928551571.dkr.ecr.us-east-2.amazonaws.com + +# Parse command line arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --dockerfile) DOCKERFILE="$2"; shift ;; + --ecr-repo) ECR_REPO="$2"; shift ;; + --environment) ENVIRONMENT="$2"; shift ;; + *) echo "Unknown parameter: $1"; exit 1 ;; + esac + shift +done + +# Check if required arguments are provided +if [ -z "$DOCKERFILE" ] || [ -z "$ECR_REPO" ]; then + echo "Usage: $0 --dockerfile --ecr-repo [--environment ]" + exit 1 +fi + +ECR_URL="$ECR_REGISTRY/$ECR_REPO" +IMAGE_NAME=$ECR_REPO + +# Get the current commit hash +COMMIT_HASH=$(git rev-parse --short HEAD) + +# Build the Docker image +docker build -t $IMAGE_NAME:latest -f "$DOCKERFILE" . + +# Login to ECR +aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ECR_REGISTRY + +# Tag and push the image with latest and commit hash +docker tag $IMAGE_NAME:latest $ECR_URL:latest +docker tag $IMAGE_NAME:latest $ECR_URL:$COMMIT_HASH +docker push $ECR_URL:latest +docker push $ECR_URL:$COMMIT_HASH \ No newline at end of file diff --git a/bin/deploy.sh b/bin/deploy.sh new file mode 100755 index 0000000..cedb5d8 --- /dev/null +++ b/bin/deploy.sh @@ -0,0 +1,49 @@ +#!/bin/sh +set -e + +REGION=us-east-2 +ECR_REGISTRY=377928551571.dkr.ecr.us-east-2.amazonaws.com + +# Parse command line arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --environment) ENVIRONMENT="$2"; shift ;; + --resource) RESOURCE="$2"; shift ;; + *) echo "Unknown parameter: $1"; exit 1 ;; + esac + shift +done + +# Check if required arguments are provided +if [ -z "$ENVIRONMENT" ] || [ -z "$RESOURCE" ]; then + echo "Usage: $0 --environment --resource " + exit 1 +fi + +# Derive values from environment and resource +CLUSTER_NAME="$ENVIRONMENT-$RESOURCE-cluster" +SERVICE_NAME="$ENVIRONMENT-$RESOURCE-service" +TASK_DEF_NAME="$ENVIRONMENT-$RESOURCE-task-def" +ECR_REPO="$RESOURCE-$ENVIRONMENT" + +# Fetch the latest image URI from ECR +ECR_URL="$ECR_REGISTRY/$ECR_REPO" +IMAGE_URI=$(aws ecr describe-images --repository-name $ECR_REPO --region $REGION --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text) +FULL_IMAGE_URI="$ECR_URL:$IMAGE_URI" +echo "Using image URI: $FULL_IMAGE_URI" + +# Fetch the existing definition of the task and create a new revision with the updated URI +TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition $TASK_DEF_NAME --region $REGION --query 'taskDefinition') +NEW_TASK_DEF=$(echo $TASK_DEFINITION | \ + jq --arg IMAGE_URI "$FULL_IMAGE_URI" '.containerDefinitions[0].image = $IMAGE_URI' | \ + jq 'del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy)' | \ + jq -c) + +# Register the new task definition +NEW_TASK_INFO=$(aws ecs register-task-definition --cli-input-json "$NEW_TASK_DEF" --region $REGION) +NEW_REVISION=$(echo $NEW_TASK_INFO | jq -r '.taskDefinition.revision') +echo "Created new task revision: $NEW_REVISION" + +# Update the ECS cluster to the new revision +aws ecs update-service --cluster $CLUSTER_NAME --service $SERVICE_NAME --task-definition $TASK_DEF_NAME:$NEW_REVISION --region $REGION >/dev/null 2>&1 +echo "ECS cluster updated to new revision" \ No newline at end of file diff --git a/dealer/bin/build_and_push.sh b/dealer/bin/build_and_push.sh deleted file mode 100755 index 6056a67..0000000 --- a/dealer/bin/build_and_push.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh -REGION=ca-central-1 -ENVIRONMENT=${1:-staging} -ECR_URL=377928551571.dkr.ecr.ca-central-1.amazonaws.com/renegade-dealer-$ENVIRONMENT - -docker build -t dealer:latest . -aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ECR_URL - -docker tag dealer:latest $ECR_URL:latest -docker push $ECR_URL:latest diff --git a/dealer/bin/deploy.sh b/dealer/bin/deploy.sh deleted file mode 100755 index 6ae831f..0000000 --- a/dealer/bin/deploy.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh -REGION=ca-central-1 -ENVIRONMENT=${1:-staging} -CLUSTER_NAME=$ENVIRONMENT-renegade-dealer-cluster -SERVICE_NAME=$ENVIRONMENT-renegade-dealer-service -TASK_FAMILY=$ENVIRONMENT-renegade-dealer-task-def -ECR_URL=377928551571.dkr.ecr.ca-central-1.amazonaws.com/renegade-dealer-$ENVIRONMENT - -# Fetch the latest image URI from ECR -IMAGE_URI=$(aws ecr describe-images --repository-name renegade-dealer-$ENVIRONMENT --region $REGION --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text) -FULL_IMAGE_URI="$ECR_URL:$IMAGE_URI" -echo "Using image URI: $FULL_IMAGE_URI" - -# Fetch the existing definition of the task and create a new revision with the updated URI -TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition $TASK_FAMILY --region $REGION --query 'taskDefinition') -NEW_TASK_DEF=$(echo $TASK_DEFINITION | \ - jq --arg IMAGE_URI "$FULL_IMAGE_URI" '.containerDefinitions[0].image = $IMAGE_URI' | \ - jq 'del(.taskDefinitionArn)' | \ - jq 'del(.revision)' | \ - jq 'del(.status)' | \ - jq 'del(.requiresAttributes)' | \ - jq 'del(.compatibilities)' | \ - jq 'del(.registeredAt)' | \ - jq 'del(.registeredBy)' | \ - jq -c) - -# Register the new task definition -NEW_TASK_INFO=$(aws ecs register-task-definition --cli-input-json "$NEW_TASK_DEF" --region $REGION) -NEW_REVISION=$(echo $NEW_TASK_INFO | jq -r '.taskDefinition.revision') -echo "Created new task revision: $NEW_REVISION" - -# Update the ECS cluster to the new revision -aws ecs update-service --cluster $CLUSTER_NAME --service $SERVICE_NAME --task-definition $TASK_FAMILY:$NEW_REVISION --region $REGION >/dev/null 2>&1 -echo "ECS cluster updated to new revision" diff --git a/funds-manager/Dockerfile b/funds-manager/Dockerfile new file mode 100644 index 0000000..6ab9657 --- /dev/null +++ b/funds-manager/Dockerfile @@ -0,0 +1,54 @@ +# === Chef === # +FROM --platform=arm64 rust:latest AS chef + +# Create a build dir and add local dependencies +WORKDIR /build + +COPY ./rust-toolchain ./rust-toolchain +RUN cat rust-toolchain | xargs rustup toolchain install + +# Install cargo-chef +RUN cargo install cargo-chef + +# === Sources === # +FROM chef AS sources +WORKDIR /build +COPY ./Cargo.toml ./Cargo.lock ./ +COPY ./funds-manager ./funds-manager + +# === Builder === # +# Pull the sources into their own layer +FROM chef AS builder +COPY --from=sources /build /build +WORKDIR /build + +# Install protoc, openssl, and pkg-config +RUN apt-get update && \ + apt-get install -y pkg-config libssl-dev libclang-dev libpq-dev + +# Update Cargo.toml to include only "funds-manager" in workspace members +RUN sed -i '/members[[:space:]]*=[[:space:]]*\[/,/\]/c\members = ["funds-manager"]' Cargo.toml +RUN cargo chef prepare --recipe-path recipe.json --bin funds-manager + +# Build only the dependencies to cache them in this layer +RUN cargo chef cook --release --recipe-path recipe.json + +# Disable compiler warnings and enable backtraces for panic debugging +ENV RUSTFLAGS=-Awarnings +ENV RUST_BACKTRACE=1 + +COPY --from=sources /build/funds-manager /build/funds-manager +WORKDIR /build + +RUN cargo build --release -p funds-manager + +# === Release stage === # +FROM --platform=arm64 debian:bookworm-slim +RUN apt-get update && \ + apt-get install -y libssl-dev ca-certificates libpq-dev + +# Copy the binary from the build stage +COPY --from=builder /build/target/release/funds-manager /bin/funds-manager + +ENTRYPOINT ["/bin/funds-manager"] +CMD ["--help"] \ No newline at end of file