APP = es-backup

BUILD_NUMBER  ?= 1
PROJECT       ?= es-backup
PROJECT_GROUP ?= marketing

BASE_AWS_PROFILE    = gdb-infra-dev
BASE_AWS_ACCOUNT_ID = 483193324480
BASE_AWS_REGION     = us-east-1
BASE_ECR_REGISTRY   = $(BASE_AWS_ACCOUNT_ID).dkr.ecr.$(BASE_AWS_REGION).amazonaws.com

AWS_PROFILE    ?= gdb-mct-dev
AWS_ACCOUNT_ID ?= 925412343888
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= marketing/es-backup
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

REVISION  ?= $(shell git rev-parse --short=8 HEAD)
BUILDTIME ?= $(shell date -u +"%Y%m%d%H%M%S")
VERSION   ?= build-$(BUILDTIME).$(REVISION)
SEMVER    ?= $(shell cat VERSION )

## -
## help - this message
.PHONY: help
help: Makefile
	@echo "Application: ${APP}\n"
	@echo "Run command:\n  make <target>\n"
	@grep -E -h '^## .*' $(MAKEFILE_LIST) | sed -n 's/^##//p'  | column -t -s '-' |  sed -e 's/^/ /'

## -
## Dcoker commands:

##   docker/login/base - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login/base
docker/login/base:
	@aws ecr get-login \
		--profile ${BASE_AWS_PROFILE} \
		--no-include-email \
		--region ${BASE_AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(BASE_ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${BASE_AWS_PROFILE} \
			--region ${BASE_AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(BASE_ECR_REGISTRY)

##   docker/login - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login
docker/login:
	@aws ecr get-login \
		--profile ${AWS_PROFILE} \
		--no-include-email \
		--region ${AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${AWS_PROFILE} \
			--region ${AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(ECR_REGISTRY)

##   docker/base/image/pull - pull base image from ECR
docker/base/image/pull: docker/login/base
	docker pull $(BASE_ECR_REGISTRY)/python:3.8-alpine3.13-runner
	docker pull $(BASE_ECR_REGISTRY)/python:3.8-alpine3.13-builder

##   docker/image/build - build lambda package for each environment and put all of them into one docker container
.PHONY: docker/image/build
docker/image/build: docker/login/base
	@echo "Building docker image"
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--target main \
		-t $(ECR_REGISTRY_URI):$(VERSION) \
		-t $(ECR_REGISTRY_URI):$(REVISION) .
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--target build-test \
		-t $(TEST_IMAGE) .

##   docker/image/push - push an image to a registry
.PHONY: docker/image/push
docker/image/push: docker/login
	@echo "Pushing docker image"
	docker push $(ECR_REGISTRY_URI):$(VERSION)
	docker push $(ECR_REGISTRY_URI):$(REVISION)

## -
## ElasticSearch commands:

##   remote/register_snapshot_repo - register ElasticSearch snapshot repository
.PHONY: remote/register_snapshot_repo
remote/register_snapshot_repo: docker/base/image/pull
	docker-compose up --build register-snapshot-repository-s3

##   remote/take_snapshot - take ElasticSearch cluster snapshot
.PHONY: remote/take_snapshot
remote/take_snapshot:
	docker-compose up --build take-snapshot

##   remote/restore_snapshot - restore ElasticSearch cluster snapshot
.PHONY: remote/restore_snapshot
remote/restore_snapshot:
	docker-compose up --build restore-snapshot

##   local/register_snapshot_repo - register ElasticSearch snapshot repository
.PHONY: local/register_snapshot_repo
local/register_snapshot_repo:
	docker-compose up --build register-snapshot-repository-local

##   local/restore_snapshot - restore ElasticSearch cluster snapshot
.PHONY: local/restore_snapshot
local/restore_snapshot:
	docker-compose up --build restore-snapshot-local

##   s3/copy/local - copy ElasticSearch snapshot
.PHONY: s3/copy/local
s3/copy/local:
	AWS_PROFILE=gdb-mct-dev aws s3 sync --delete s3://dev-marketing-es-manual-snapshots/ es-repo/

##   s3/copy/remote - copy ElasticSearch snapshot
.PHONY: s3/copy/remote
s3/copy/remote:
	AWS_PROFILE=gdb-infra-dev aws s3 sync --delete es-repo/ s3://dev-infra-decibel-test-data/elasticsearch/

## -
## PostgreSQL commands:

##   pg_dump/% - create plain text backup of the database
.PHONY: pg_dump/%
pg_dump/%:
	docker-compose up pg_dump_$*

