GIT_COMMIT ?= $(shell git rev-parse --verify HEAD)

build-docker-image:
	env GOOS=linux \
		go build \
			-o build/primeshttp/primeshttp \
			cmd/primeshttp/main.go

	docker build -t "primes:$(GIT_COMMIT)" .
	docker tag "primes:$(GIT_COMMIT)" "primes:latest"
	docker tag "primes:$(GIT_COMMIT)" "799890034725.dkr.ecr.us-west-2.amazonaws.com/primes:latest"
	docker tag "primes:$(GIT_COMMIT)" "799890034725.dkr.ecr.us-west-2.amazonaws.com/primes:$(GIT_COMMIT)"

push-docker-image:
	docker push "799890034725.dkr.ecr.us-west-2.amazonaws.com/primes:latest"
	docker push "799890034725.dkr.ecr.us-west-2.amazonaws.com/primes:$(GIT_COMMIT)"

authenticate-docker:
	aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 799890034725.dkr.ecr.us-west-2.amazonaws.com

update-aws-credentials:
	ada credentials update \
		--account 799890034725 \
		--role admin \
		--once

build-lambda:
	env GOOS=linux \
		go build \
			-o build/primeslambda/primeslambda \
			cmd/primeslambda/main.go
	zip -j "./build/primeslambdazip/primeslambda_$(GIT_COMMIT).zip" ./build/primeslambda/primeslambda

upload-lambda:
	aws s3 cp \
		"./build/primeslambdazip/primeslambda_$(GIT_COMMIT).zip" \
		"s3://twitch-riantoi-primes/lambda/primeslambda_$(GIT_COMMIT).zip"

# ECS Cluster Cloudformation Stack

create-ecs-cluster-stack:
	aws cloudformation create-stack \
		--stack-name ecscluster \
		--template-body file://cloudformation/ecs-cluster.template.yml \
		--capabilities CAPABILITY_IAM

update-ecs-cluster-stack:
	aws cloudformation update-stack \
		--stack-name ecscluster \
		--template-body file://cloudformation/ecs-cluster.template.yml \
		--capabilities CAPABILITY_IAM

# ECS Service Cloudformation Stack

create-ecs-service-stack:
	aws cloudformation create-stack \
		--stack-name ecsservice \
		--template-body file://cloudformation/ecs-service.template.yml \
		--capabilities CAPABILITY_IAM

update-ecs-service-stack:
	aws cloudformation update-stack \
		--stack-name ecsservice \
		--template-body file://cloudformation/ecs-service.template.yml \
		--capabilities CAPABILITY_IAM

# EC2 Instance Cloudformation Stack

create-ec2-stack:
	aws cloudformation create-stack \
		--stack-name ec2 \
		--template-body file://cloudformation/ec2.template.yml \
		--capabilities CAPABILITY_IAM

update-ec2-stack:
	aws cloudformation update-stack \
		--stack-name ec2 \
		--template-body file://cloudformation/ec2.template.yml \
		--capabilities CAPABILITY_IAM

# Lambda Cloudformation Stack

create-lambda-stack:
	aws cloudformation create-stack \
		--stack-name lambda \
		--template-body file://cloudformation/lambda.template.yml \
		--capabilities CAPABILITY_IAM

update-lambda-stack:
	aws cloudformation update-stack \
		--stack-name lambda \
		--template-body file://cloudformation/lambda.template.yml \
		--capabilities CAPABILITY_IAM

