masto-aio/terraform/Makefile

93 lines
2.3 KiB
Makefile
Raw Normal View History

2023-11-12 16:50:05 -08:00
include ../config.mk
default: terraform
# I hate sed too and I am so sorry for what I'm about to do
2024-01-20 12:58:49 -08:00
terraform: terraform-check *.tf terraform-options
terraform init || terraform init -upgrade
2023-11-12 16:50:05 -08:00
terraform apply
terraform output | sed \
-e 's/\(.*\) = /\U\1 = /' \
-e 's/"//g' \
-e '' \
-e 'h;s/.* = //;s/\([.]\)/\\\1/g;x;s/ = .*//;G;s/\n/ = /' \
-e 's/ \+$$//' \
> terraform.mk
terraform-check: .toolcheck pubkey
2023-11-12 16:50:05 -08:00
$(eval AWS_INSTANCE_PUBLIC_KEY := $(shell sed -e 's/\//\\\//g' pubkey))
#CONFIGVARS = AWS_INSTANCE_PUBLIC_KEY AWS_SINGLE_INSTANCE_TYPE
SEDLINE =
config.tf: sedline config.tf.in ../config.mk
2024-01-28 18:15:30 -08:00
@sed $(SEDLINE) config.tf.in > config.tf
2023-11-12 16:50:05 -08:00
#configvars: ../config.mk
# $(eval CONFIGVARS := $(shell grep '^[0-9A-Z]' ../config.mk | awk '{print $1}'))
# echo $(CONFIGVARS)
#sedline: configvars $(addprefix __sed_,$(CONFIGVARS))
sedline: $(addprefix __sed_,$(shell grep '^[0-9A-Z_]' ../config.mk | awk '{print $$1}'))
__sed_%:
$(eval SEDLINE := $$(SEDLINE) -e 's/{{$*}}/$($*)/')
CHECK_TOOLS = terraform aws
# this relies on your credentials file, so that if you edit it, it will run again
.toolcheck: ~/.aws/credentials
2023-11-12 16:50:05 -08:00
@echo
@echo "Checking applications..."
@ FAIL=""; \
for TOOL in $(CHECK_TOOLS); do \
which $${TOOL} >/dev/null || FAIL="$${FAIL} $${TOOL}"; \
done; \
if test -n "$${FAIL}"; then \
echo "ERROR: You are missing the following:$${FAIL}"; \
echo "Please make sure all necessary tools are installed and available in your path"; \
echo; \
exit 1; \
fi
@echo
@echo "Checking AWS configuration..."
aws iam get-user
touch .toolcheck
# ...but if you don't have a credentials file, that's ok, it just runs the check every time
~/.aws/credentials:
true
2023-11-12 16:50:05 -08:00
pubkey:
if test -n "$(AWS_INSTANCE_PUBLIC_KEY)"; then \
echo "$(AWS_INSTANCE_PUBLIC_KEY)" > pubkey; \
else \
ssh-keygen -t rsa -N "" -f privkey && mv privkey.pub pubkey; \
fi
2024-01-20 12:58:49 -08:00
# optional components for terraform
terraform-options: config.tf tf-namecheap
tf-namecheap:
if test "$(DNS_NAMECHEAP)" -eq 1; then \
for FILE in namecheap/*; do \
sed $(SEDLINE) $${FILE} > $$(basename "$${FILE}"); \
done; \
else \
for FILE in namecheap/*; do \
echo rm -fv $$(basename "$${FILE}"); \
done; \
fi
2023-11-12 16:50:05 -08:00
# clean doesn't touch tfstate because we're not insane
clean:
rm -f privkey pubkey
rm -rf .terraform*