gtat-tech-career-kickstarte.../setup_utils/setup_dev_machine.sh

81 lines
2.5 KiB
Bash

#!/bin/bash
#
# setup_dev_machine.sh — Run from the UAT instance to finish setting up dev machines.
#
# Discovers all dev instances via AWS tags and clones the template repo on each.
# For AWS deployments, provision_aws.py handles instance creation, SSH keys,
# env vars, and the deploy script via user-data. This script handles the
# remaining steps that require network connectivity between UAT and dev
# (i.e. cloning the template repo onto the dev machines).
#
# Prerequisites (set on UAT via user-data):
# CK_TEMPLATE_REPO_URL — git URL for the template repo
# CK_PROJECT_TAG — project tag used to discover instances
# CK_AWS_REGION — AWS region where instances are running
#
set -e
# ---------------------------------------------------------------------------
if [ -z "${CK_TEMPLATE_REPO_URL:-}" ]; then
echo "CK_TEMPLATE_REPO_URL is not set, skipping template repo clone."
exit 0
fi
if [ -z "${CK_PROJECT_TAG:-}" ]; then
echo "Error: CK_PROJECT_TAG is not set. Cannot discover dev instances."
exit 1
fi
if [ -z "${CK_AWS_REGION:-}" ]; then
echo "Error: CK_AWS_REGION is not set. Cannot query AWS API."
exit 1
fi
# Discover all running dev instances by project tag (exclude UAT)
echo "Discovering dev instances (Project=$CK_PROJECT_TAG, Region=$CK_AWS_REGION)..."
DEV_INSTANCES=$(aws ec2 describe-instances \
--region "$CK_AWS_REGION" \
--filters \
"Name=tag:Project,Values=$CK_PROJECT_TAG" \
"Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[?Tags[?Key==`Role` && Value!=`uat`]].[Tags[?Key==`Role`].Value | [0], PrivateIpAddress]' \
--output text)
if [ -z "$DEV_INSTANCES" ]; then
echo "No dev instances found. Nothing to do."
exit 0
fi
echo "Found dev instances:"
echo "$DEV_INSTANCES"
echo "---"
FAILED=0
while IFS=$'\t' read -r DEV_NAME DEV_IP; do
echo ""
echo "=== Setting up $DEV_NAME ($DEV_IP) ==="
if ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$DEV_NAME@$DEV_IP" '
git clone '"'$CK_TEMPLATE_REPO_URL'"' ~/optiver-career-kickstarter
cd ~/optiver-career-kickstarter
uv sync
git checkout -b team-$(whoami)
'; then
echo "Template repo cloned on $DEV_NAME ($DEV_IP)"
else
echo "ERROR: Failed to set up $DEV_NAME ($DEV_IP)"
FAILED=$((FAILED + 1))
fi
done <<< "$DEV_INSTANCES"
echo ""
echo "=== Done ==="
if [ "$FAILED" -gt 0 ]; then
echo "$FAILED dev machine(s) failed setup."
exit 1
fi
echo "All dev machines set up successfully."