# Bramfield Mutual's systems on AWS: build them, look around, and remove them, one command at a time.
#
#   make budget EMAIL=you@example.com   a $10 monthly cost budget that emails you at 50% and 100%. Run this first.
#                   (add TAG_ONLY=yes to count only the lab, on an account that already spends on other things)
#   make quota      your account's limit for standard machines in us-east-1, and what the lab needs (1 vCPU)
#   make up         build Bramfield's systems with Terraform (about 1 minute, then about 3 to 8 more inside the server)
#   make check      wait until every system answers, then print each one
#   make tunnel     reach the systems from your computer through Session Manager (ports 15432, 18080, 18081, 12222, 18180)
#   make untunnel   close those tunnels
#   make look PART=db|policy|queue|drops|docs|identity   look around one system with read-only access
#   make cost       what the running lab costs per hour, from the AWS Price List
#   make stop       stop the server between sessions (you then pay only for its disk)
#   make start      start it again (it stops itself again after 4 hours)
#   make destroy    delete everything, then check that nothing is left
#   make leftovers  list anything still tagged Project=fde-course-lab, and the things that are easy to forget
#
# Nothing accepts a connection from the internet: there is no SSH key and no open port.
# Author: Roni Das. Created: 2026-11-13.

SHELL := /bin/bash
.SHELLFLAGS := -o pipefail -c
REGION ?= us-east-1
export AWS_REGION := $(REGION)
export AWS_PAGER :=
TF := terraform -chdir=terraform
ID = $$($(TF) output -raw instance_id)

.PHONY: help budget quota up check tunnel untunnel look cost stop start destroy leftovers
help:
	@sed -n '2,17p' Makefile | sed 's/^# \{0,1\}//'

budget:
	@test -n "$(EMAIL)" || { echo "say where the alerts go: make budget EMAIL=you@example.com"; exit 2; }
	@TAG_ONLY=$(TAG_ONLY) bash bin/budget.sh "$(EMAIL)"

quota:
	@have=$$(aws service-quotas get-service-quota --service-code ec2 --quota-code L-1216C47A --query Quota.Value --output text); \
	echo "your limit for standard EC2 machines in $(REGION): $${have%.*} vCPU"; \
	echo "this lab needs 1 vCPU: one t2.small server"; \
	if [ "$${have%.*}" -ge 1 ]; then echo "enough: go ahead"; else echo "ask AWS for a higher limit first"; exit 1; fi

build/app.tgz: $(shell find app -type f)
	@mkdir -p build && COPYFILE_DISABLE=1 tar --exclude ".DS_Store" -czf build/app.tgz app

up: build/app.tgz
	@$(TF) init -input=false -upgrade >/dev/null && echo "terraform is ready"
	@$(TF) apply -input=false -auto-approve -compact-warnings > build/apply.log 2>&1 || { cat build/apply.log; echo "terraform apply failed: the full message is above"; exit 1; }
	@grep -E "Creation complete|Apply complete" build/apply.log | sed -E 's/ \[id=[^]]*\]//; s/ after [0-9]+s//'
	@echo "the server is starting. Bramfield's data takes about 8 more minutes to build: run make check"

check:
	@bash bin/check.sh "$(ID)"

tunnel:
	@bash bin/tunnel.sh "$(ID)"

untunnel:
	@bash bin/tunnel.sh stop

look:
	@PGPASSWORD=$$(aws ssm get-parameter --name /bramfield/readonly-password --with-decryption --query Parameter.Value --output text) \
	 LAB_PASSWORD=$$(aws ssm get-parameter --name /bramfield/lab-password --with-decryption --query Parameter.Value --output text) \
	 BUCKET=$$($(TF) output -raw bucket) uv run --quiet look_around.py $(PART)

cost:
	@uv run --quiet cost.py "$$($(TF) output -raw instance_type)"

stop:
	@aws ec2 stop-instances --instance-ids $(ID) --query 'StoppingInstances[0].CurrentState.Name' --output text
	@echo "stopping: you now pay only for the 20 GB disk, about 5 cents a day"

start:
	@aws ec2 start-instances --instance-ids $(ID) --query 'StartingInstances[0].CurrentState.Name' --output text
	@echo "starting: run make check in about 2 minutes. The server stops itself again after 4 hours."

destroy:
	@mkdir -p build
	@bash bin/tunnel.sh stop >/dev/null 2>&1 || true
	@$(TF) destroy -input=false -auto-approve -compact-warnings > build/destroy.log 2>&1 || { cat build/destroy.log; echo "terraform destroy failed: the full message is above"; exit 1; }
	@grep -E "Destruction complete|Destroy complete" build/destroy.log | sed -E 's/ after [0-9]+s//' | tail -4
	@bash bin/leftovers.sh

leftovers:
	@bash bin/leftovers.sh
