Last active
June 11, 2018 07:00
-
-
Save echo-devnull/ddd9f57178b676b2f8873358e6753cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATE := $(shell date) | |
# The "?=" in this command assigns "acceptance" only when "ENVIRONMENT" has not | |
# already been set on the commandline or environment variables. Jenkins will have | |
# this set to "production" so it will use production values. | |
ENVIRONMENT ?= acc | |
NAMESPACE ?= k8snamespace | |
.ONESHELL: | |
.PHONY: secrets status namespace configmap service deploy autoscale delete syncelasticsearch | |
.DEFAULT_GOAL := deploy | |
## This function is here to find the version / color of kubernetes cluster | |
## we currently use. The api.live..... dns name is manually changed to point to | |
## the correct kubernetes cluster. | |
## And it loads the environment variables for the current deploy | |
define context | |
. ./env/$(1).env; | |
## This command does a dns lookup to see which context is the current live kubernetes network | |
## And then exports a variable that is filled with the context name. | |
export CONTEXT=$(shell host api.live.$(1).k8s.domain | head -n1 | awk -F'.' '{print $$7"."$$8"."$$9"."$$10"."$$11}') | |
export DOCKER_TAG=$(shell curl --silent http://jenkinslink:8080/job/Elect%20Build/lastSuccessfulBuild/api/json | jq '.number') | |
endef | |
reset: delete deploy | |
changedefaultcontext: | |
@echo "## Status" | |
$(call context,${ENVIRONMENT}) | |
kubectl config set-context $$CONTEXT | |
kubectl config use-context $$CONTEXT | |
echo "----" | |
status: | |
@echo "## Status" | |
$(call context,${ENVIRONMENT}) | |
kubectl --context=$$CONTEXT --namespace ${NAMESPACE} get deployment | |
kubectl --context=$$CONTEXT --namespace ${NAMESPACE} get services -o wide | |
kubectl --context=$$CONTEXT --namespace=${NAMESPACE} rollout status deployment/nginx | |
kubectl --context=$$CONTEXT --namespace=${NAMESPACE} rollout status deployment/php-fpm | |
echo "----" | |
secrets: | |
@echo "## Creating the Secrets" | |
$(call context,${ENVIRONMENT}) | |
kubectl --namespace ${NAMESPACE} \ | |
create secret generic env-symfony \ | |
--dry-run \ | |
-o yaml \ | |
--from-file=configmaps/${ENVIRONMENT}/env/ | \ | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT apply -f - | |
echo "----" | |
namespace: | |
@echo "## Naming the namespace" | |
$(call context,${ENVIRONMENT}) | |
envsubst < ./namespace.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
cronjobs: | |
@echo "## Creating the cronjobs" | |
$(call context,${ENVIRONMENT}) | |
envsubst < ./cron-saved-searches.yml | kubectl --context=$$CONTEXT apply -f - | |
envsubst < ./cron-flirtmails.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
dbmigrate: | |
@echo "## Migrating the unmigrated database" | |
$(call context,${ENVIRONMENT}) | |
kubectl delete --context=$$CONTEXT --namespace=${NAMESPACE} job dbmigrate || true | |
envsubst < ./job-dbmigrate.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
syncelasticsearch: | |
@echo "## Syncing the ElasticSearches" | |
$(call context,${ENVIRONMENT}) | |
kubectl delete --context=$$CONTEXT --namespace=${NAMESPACE} job synces || true | |
envsubst < ./job-synces.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
configmap: | |
@echo "## Configuring the configmap(s)" | |
$(call context,${ENVIRONMENT}) | |
kubectl --namespace ${NAMESPACE} \ | |
create configmap nginx \ | |
--dry-run \ | |
-o yaml \ | |
--from-file=configmaps/${ENVIRONMENT}/nginx | \ | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT apply -f - | |
kubectl --namespace ${NAMESPACE} \ | |
create configmap php-fpm \ | |
--dry-run \ | |
-o yaml \ | |
--from-file=configmaps/${ENVIRONMENT}/fpm | \ | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT apply -f - | |
echo "----" | |
reload: changedefaultcontext configmap secrets | |
@echo "## Reloading to load the new load(s)" | |
$(call context,${ENVIRONMENT}) | |
kubectl --namespace ${NAMESPACE} \ | |
patch deployment nginx \ | |
--context=$$CONTEXT \ | |
-p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}" | |
kubectl --namespace ${NAMESPACE} \ | |
patch deployment php-fpm \ | |
--context=$$CONTEXT \ | |
-p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}" | |
service: | |
@echo "## Servicing the service(s)" | |
$(call context,${ENVIRONMENT}) | |
envsubst < ./service.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
deploy: changedefaultcontext namespace secrets configmap service autoscale cronjobs dbmigrate | |
@echo "## Deploying the deploy(s)" | |
$(call context,${ENVIRONMENT}) | |
envsubst < ./deploy-fpm.yml | kubectl --context=$$CONTEXT apply -f - | |
envsubst < ./deploy-nginx.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
autoscale: deploy | |
@echo "## Scaling the Autoscaler(s)" | |
$(call context,${ENVIRONMENT}) | |
envsubst < ./hpa.yml | kubectl --context=$$CONTEXT apply -f - | |
echo "----" | |
# Not used much, but it could be handy to sometimes completely eradicate the important bits | |
# and start over. | |
delete: | |
@echo "## Deleting all the things" | |
$(call context,${ENVIRONMENT}) | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete cronjob savedsearches-mail || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete cronjob flirtmails || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete deployment nginx || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete deployment php-fpm || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete configmap nginx || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete configmap php-fpm || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete secret env-symfony || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete hpa nginx || echo "already deleted" && true | |
kubectl --namespace ${NAMESPACE} --context=$$CONTEXT delete hpa php-fpm || echo "already deleted" && true | |
@echo "----" | |
release: | |
@echo "## Enter commit message:" | |
$(call context,${ENVIRONMENT}) | |
@read REPLY; \ | |
echo "${DATE} - $$REPLY" >> CHANGELOG; \ | |
git add --all; \ | |
git commit -m "$$REPLY"; \ | |
git pull | |
git push |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment