#!/usr/bin/env bash # Build and push the panoptiblog job image, point the ECS task definition at # that tag, and apply the infrastructure. With --run, start one task now and # wait for it to stop; --no-wait returns after printing the task ARN. # Otherwise the EventBridge rule starts one every day. The apply is # auto-approved by default; pass -auto-approve=false to review it first. # # The first run needs the site infrastructure applied first: infra/main.tf's # release_sha has no default, and scripts/deploy-site.sh is what writes it. set -euo pipefail repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)" if [[ -n "$(git -C "${repo_root}" status --porcelain --untracked-files=no)" ]]; then echo "deploy-panoptiblog: commit tracked changes first; the image tag must name the code it runs" >&2 exit 1 fi region="us-east-1" run_now="" wait_for_run=1 auto_approve=0 tofu_args=() for arg in "$@"; do case "${arg}" in --run) run_now=1 ;; --no-wait) wait_for_run=0 ;; -auto-approve|--auto-approve|-auto-approve=*|--auto-approve=*) auto_approve=1 tofu_args+=("${arg}") ;; *) tofu_args+=("${arg}") ;; esac done if (( auto_approve == 0 )); then tofu_args=(-auto-approve "${tofu_args[@]}") fi if ! identity="$(aws sts get-caller-identity --output text --query Arn 2>&1)"; then echo "deploy-panoptiblog: AWS credential check failed:" >&2 echo " ${identity}" >&2 exit 1 fi echo "deploy-panoptiblog: deploying as ${identity}" # The ECR repository is created by the first apply, so read its URL after one # exists rather than spelling an account id here. if ! repository="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_repository_url 2>/dev/null)"; then echo "deploy-panoptiblog: applying once to create the ECR repository" tofu -chdir="${repo_root}/infra" apply "${tofu_args[@]}" repository="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_repository_url)" fi registry="${repository%%/*}" # The tag is the commit, so the task definition names the code it runs rather # than a moving "latest" nobody can reproduce. tag="$(git -C "${repo_root}" rev-parse --short=12 HEAD)" aws ecr get-login-password --region "${region}" \ | docker login --username AWS --password-stdin "${registry}" >/dev/null docker build --platform linux/amd64 -t "${repository}:${tag}" \ -f "${repo_root}/scripts/panoptiblog/Dockerfile" "${repo_root}/scripts/panoptiblog" docker push "${repository}:${tag}" node "${repo_root}/scripts/patch-tfvars.mjs" \ "${repo_root}/infra/terraform.tfvars.json" panoptiblog_image_tag "${tag}" tofu -chdir="${repo_root}/infra" apply "${tofu_args[@]}" if [[ -n "${run_now}" ]]; then cluster="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_cluster)" task_definition="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_task_definition)" subnet="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_subnet)" security_group="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_security_group)" # Resolve the latest ACTIVE revision by family, then wait until its image # is the tag just built. A stale revision would silently run the previous # image for the whole crawl. task_family="$(aws ecs describe-task-definition \ --task-definition "${task_definition}" \ --region "${region}" \ --query 'taskDefinition.family' --output text)" for attempt in 1 2 3 4 5 6 7 8 9 10; do read -r latest_arn latest_image < <( aws ecs describe-task-definition \ --task-definition "${task_family}" \ --region "${region}" \ --query 'taskDefinition.[taskDefinitionArn,containerDefinitions[0].image]' \ --output text ) if [[ "${latest_image}" == *":${tag}" ]]; then break fi if (( attempt == 10 )); then echo "deploy-panoptiblog: latest task definition ${latest_arn} runs ${latest_image}, expected image tag ${tag}" >&2 exit 1 fi sleep 2 done task_definition="${latest_arn}" echo "deploy-panoptiblog: task definition ${latest_arn} image ${latest_image}" task_arn="$(aws ecs run-task \ --cluster "${cluster}" \ --task-definition "${task_definition}" \ --launch-type FARGATE \ --region "${region}" \ --network-configuration "awsvpcConfiguration={subnets=[${subnet}],securityGroups=[${security_group}],assignPublicIp=ENABLED}" \ --query 'tasks[0].taskArn' --output text)" if [[ -z "${task_arn}" || "${task_arn}" == "None" ]]; then echo "deploy-panoptiblog: run-task did not return a task ARN" >&2 exit 1 fi echo "deploy-panoptiblog: task ${task_arn}" if (( wait_for_run )); then started="$(date +%s)" while :; do read -r status exit_code < <( aws ecs describe-tasks \ --cluster "${cluster}" \ --tasks "${task_arn}" \ --region "${region}" \ --query 'tasks[0].[lastStatus,containers[0].exitCode]' \ --output text ) if [[ "${status}" == "STOPPED" ]]; then break fi if (( $(date +%s) - started > 18000 )); then echo "deploy-panoptiblog: task still ${status} after 300 minutes; not waiting further" >&2 exit 1 fi sleep 15 done elapsed=$(( $(date +%s) - started )) echo "deploy-panoptiblog: task ${status} in ${elapsed}s (exit ${exit_code:-unknown})" if [[ "${exit_code}" != "0" ]]; then exit 1 fi fi fi