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 897273f
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 91 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.

59 changes: 59 additions & 0 deletions funds-manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# === 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
RUN cargo chef cook --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
RUN cargo build -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
COPY --from=builder /build/target/debug/funds-manager /bin/funds-manager

ENTRYPOINT ["/bin/funds-manager"]
CMD ["--help"]
97 changes: 50 additions & 47 deletions funds-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,62 +126,65 @@ impl Server {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
setup_system_logger(LevelFilter::INFO);
let cli = Cli::parse();

// Parse an AWS config
let config = aws_config::defaults(BehaviorVersion::latest())
.region(Region::new(DEFAULT_REGION))
.load()
.await;

// Build an Arbitrum client
let wallet = LocalWallet::from_str(&cli.arbitrum_private_key)?;
let conf = ArbitrumClientConfig {
darkpool_addr: cli.darkpool_address,
chain: cli.chain,
rpc_url: cli.rpc_url,
arb_priv_keys: vec![wallet],
block_polling_interval_ms: BLOCK_POLLING_INTERVAL_MS,
};
let client = ArbitrumClient::new(conf).await?;
let chain_id = client.chain_id().await.map_err(raw_err_str!("Error fetching chain ID: {}"))?;

// Build the indexer
let mut decryption_keys = vec![DecryptionKey::from_hex_str(&cli.relayer_decryption_key)?];
if let Some(protocol_key) = cli.protocol_decryption_key {
decryption_keys.push(DecryptionKey::from_hex_str(&protocol_key)?);
}

let relayer_client = RelayerClient::new(&cli.relayer_url, &cli.usdc_mint);
let server = Server {
chain_id,
chain: cli.chain,
relayer_client: relayer_client.clone(),
arbitrum_client: client.clone(),
decryption_keys,
db_url: cli.db_url,
aws_config: config,
};
// setup_system_logger(LevelFilter::INFO);
// let cli = Cli::parse();

// // Parse an AWS config
// let config = aws_config::defaults(BehaviorVersion::latest())
// .region(Region::new(DEFAULT_REGION))
// .load()
// .await;

// // Build an Arbitrum client
// let wallet = LocalWallet::from_str(&cli.arbitrum_private_key)?;
// let conf = ArbitrumClientConfig {
// darkpool_addr: cli.darkpool_address,
// chain: cli.chain,
// rpc_url: cli.rpc_url,
// arb_priv_keys: vec![wallet],
// block_polling_interval_ms: BLOCK_POLLING_INTERVAL_MS,
// };
// let client = ArbitrumClient::new(conf).await?;
// let chain_id = client.chain_id().await.map_err(raw_err_str!("Error fetching
// chain ID: {}"))?;

// // Build the indexer
// let mut decryption_keys =
// vec![DecryptionKey::from_hex_str(&cli.relayer_decryption_key)?];
// if let Some(protocol_key) = cli.protocol_decryption_key {
// decryption_keys.push(DecryptionKey::from_hex_str(&protocol_key)?);
// }

// let relayer_client = RelayerClient::new(&cli.relayer_url, &cli.usdc_mint);
// let server = Server {
// chain_id,
// chain: cli.chain,
// relayer_client: relayer_client.clone(),
// arbitrum_client: client.clone(),
// decryption_keys,
// db_url: cli.db_url,
// aws_config: config,
// };

// --- Routes --- //

let ping = warp::get()
.and(warp::path("ping"))
.map(|| warp::reply::with_status("PONG", warp::http::StatusCode::OK));

let index_fees = warp::post()
.and(warp::path("index-fees"))
.and(with_server(Arc::new(server.clone())))
.and_then(index_fees_handler);
// let index_fees = warp::post()
// .and(warp::path("index-fees"))
// .and(with_server(Arc::new(server.clone())))
// .and_then(index_fees_handler);

let redeem_fees = warp::post()
.and(warp::path("redeem-fees"))
.and(with_server(Arc::new(server.clone())))
.and_then(redeem_fees_handler);
// let redeem_fees = warp::post()
// .and(warp::path("redeem-fees"))
// .and(with_server(Arc::new(server.clone())))
// .and_then(redeem_fees_handler);

let routes = ping.or(index_fees).or(redeem_fees).recover(handle_rejection);
warp::serve(routes).run(([0, 0, 0, 0], cli.port)).await;
// let routes = ping.or(index_fees).or(redeem_fees).recover(handle_rejection);
// warp::serve(routes).run(([0, 0, 0, 0], cli.port)).await;
warp::serve(ping).run(([0, 0, 0, 0], 3000)).await;

Ok(())
}
Expand Down

0 comments on commit 897273f

Please sign in to comment.