Skip to content

Commit

Permalink
bin, funds-manager: Add deployment and upgrade scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Jul 19, 2024
1 parent ec4216b commit 75ccf24
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 44 deletions.
39 changes: 39 additions & 0 deletions bin/build_and_push.sh
Original file line number Diff line number Diff line change
@@ -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 <path_to_dockerfile> --ecr-repo <ecr_repository> [--environment <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
49 changes: 49 additions & 0 deletions bin/deploy.sh
Original file line number Diff line number Diff line change
@@ -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 <env> --resource <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"
10 changes: 0 additions & 10 deletions dealer/bin/build_and_push.sh

This file was deleted.

34 changes: 0 additions & 34 deletions dealer/bin/deploy.sh

This file was deleted.

54 changes: 54 additions & 0 deletions funds-manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit 75ccf24

Please sign in to comment.