DOCKERHUB_USER=finboxio GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD) GIT_COMMIT := $(shell git rev-parse HEAD) GIT_REPO := $(shell git remote -v | grep origin | grep "(fetch)" | awk '{ print $$2 }') GIT_DIRTY := $(shell git status --porcelain | wc -l) VERSION := $(shell cat package.json | jq '.version' -r) VERSION_DIRTY := $(shell git log --pretty=format:%h v$(VERSION)..HEAD | wc -l) BUILD_COMMIT := $(shell if [ "$(GIT_DIRTY)" -gt "0" ]; then echo $(GIT_COMMIT)+dev; else echo $(GIT_COMMIT); fi) BUILD_VERSION := $(shell if [ "$(VERSION_DIRTY)" -gt "0" ] || [ "$(GIT_DIRTY)" -gt "0" ]; then echo $(VERSION)-dev; else echo $(VERSION); fi) DEBUG ?= "false" DEBUG_FLAG := $(shell if [ "$(DEBUG)" == "true" ]; then echo "-debug"; fi) APPLY_POST_PROCESSORS := $(shell if [ "$(VERSION_DIRTY)" -gt "0" ] || [ "$(GIT_DIRTY)" -gt "0" ]; \ then echo "del(.[\"post-processors\"] | .. | select(.type? == \"atlas\")) | del(.. | select(length? == 0))"; \ else echo "."; \ fi) version: @echo $(BUILD_VERSION) | tr -d '\r' | tr -d '\n' | tr -d ' ' containers: reaper router reporter @echo built containers at version $(BUILD_VERSION) reaper: @cd reaper \ && docker build -t $(DOCKERHUB_USER)/reaper:$(BUILD_VERSION) . \ && docker push $(DOCKERHUB_USER)/reaper:$(BUILD_VERSION) router: @cd router \ && docker build -t $(DOCKERHUB_USER)/router:$(BUILD_VERSION) . \ && docker push $(DOCKERHUB_USER)/router:$(BUILD_VERSION) reporter: @cd reporter \ && docker build -t $(DOCKERHUB_USER)/reporter:$(BUILD_VERSION) . \ && docker push $(DOCKERHUB_USER)/reporter:$(BUILD_VERSION) packer_cache: @mkdir ~/.packer_cache &> /dev/null || true images: image.master image.agent images.atlas: image.master.atlas image.agent.atlas image.master: packer_cache containers @echo "Building master image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="master" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -force $(DEBUG_FLAG) - image.master.aws: packer_cache containers @echo "Building master image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="master" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -only aws -force $(DEBUG_FLAG) - image.master.vbox: packer_cache containers @echo "Building master image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="master" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -only vbox -force $(DEBUG_FLAG) - image.master.atlas: containers @echo "Pushing master image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="master" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ > packer/.tmp.cfk.master.packer.json @packer push packer/.tmp.cfk.master.packer.json @rm packer/.tmp.cfk.master.packer.json image.agent: packer_cache containers @echo "Building agent image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="agent" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -force $(DEBUG_FLAG) - image.agent.aws: packer_cache containers @echo "Building agent image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="agent" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -only aws -force $(DEBUG_FLAG) - image.agent.vbox: packer_cache containers @echo "Building agent image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @export PACKER_CACHE_DIR=~/.packer_cache && cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="agent" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ | packer build -only vbox -force $(DEBUG_FLAG) - image.agent.atlas: containers @echo "Pushing agent image from $(GIT_BRANCH):$(GIT_COMMIT) of $(GIT_REPO)" @echo "Version $(BUILD_VERSION), Commit $(BUILD_COMMIT)" @cat packer/cfk.json \ | jq '.variables.version="${BUILD_VERSION}" \ | .variables.branch="${GIT_BRANCH}" \ | .variables.role="agent" \ | .variables.commit="${BUILD_COMMIT}" \ | .variables.repository="${GIT_REPO}" \ | ${APPLY_POST_PROCESSORS}' \ > packer/.tmp.cfk.agent.packer.json @packer push packer/.tmp.cfk.agent.packer.json @rm packer/.tmp.cfk.agent.packer.json tf.plan: @echo "Plan changes to terraform infrastructure" @cd terraform && terraform plan tf.apply: @echo "Applying changes to terraform infrastructure" @cd terraform && terraform apply tf.destroy-asg: @cd terraform && echo "Destroying old autoscaling groups" && terraform destroy -target=aws_autoscaling_group.cfk-master-asg -target=aws_autoscaling_group.cfk-agent-asg || true tf.upgrade: @echo "Starting rolling upgrade of AWS resources" @bin/upgrade .PHONY: reaper reporter router