Guest User

Untitled

a guest
Dec 14th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. # Updated 10/17 Alec Cunningham
  2. # Required resources for kubernetes
  3. # kubectl create secret generic cloudsql-oauth-credentials --from-file=creds/db-access.json
  4. # kubectl create secret generic --from-literal=username=root --from-literal=password=root
  5. # kubectl create configmap settings --from-file=config/configmap.yaml
  6.  
  7. # Used for PROJECT_ID
  8. GCLOUD_PROJECT = container-builder-186003
  9. # GRC URL
  10. REPO := gcr.io/$(GCLOUD_PROJECT)/
  11. # Docker image name
  12. IMAGE = gke-tut
  13. # Latest commit SHA for tagging
  14. COMMIT := $(shell git log -1 --format=%h)
  15. # The final release artifact docker image
  16. COMMIT_IMAGE := $(REPO)$(IMAGE):$(COMMIT)
  17. # GKE cluster name
  18. CLUSTER := $(IMAGE)
  19. # Variable of all manifests in all $DIRS
  20. DIRS = $(wildcard manifests/*)
  21. DPLY = manifests/deployment.yaml
  22. OBJS = $(notdir $(DIRS))
  23. # Holds envsubst'd templates
  24. TMPL = .generated
  25. CLEAN = rm -rf $(TMP)
  26. # Output colors
  27. GREEN := $(shell tput -Txterm setaf 2)
  28. WHITE := $(shell tput -Txterm setaf 7)
  29. YELLOW := $(shell tput -Txterm setaf 3)
  30. RESET := $(shell tput -Txterm sgr0)
  31.  
  32. # Help script
  33. HELP_FUN = \
  34. %help; \
  35. while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
  36. print "usage: make [target]\n\n"; \
  37. print "see makefile for additional commands\n\n"; \
  38. for (sort keys %help) { \
  39. print "${WHITE}$$_:${RESET}\n"; \
  40. for (@{$$help{$$_}}) { $$sep = " " x (32 - length $$_->[0]); \
  41. print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
  42. }; print "\n"; }
  43.  
  44. help: ## Show help (same if no target is specified).
  45. @perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)
  46.  
  47. # Prints value of variable
  48. print-% : ; @echo $* = $($*)
  49.  
  50. .PHONY: all
  51. all:
  52. make help
  53.  
  54. .PHONY: authenticate
  55. authenticate: ## Authenticate with GCP and set kubectl context
  56. @echo You must be signed in for this to work!
  57. gcloud config set project $(GCLOUD_PROJECT)
  58. # Get kubectl context
  59. gcloud container clusters get-credentials $(CLUSTER)
  60.  
  61. .PHONY: source
  62. source: ## Grab env variables
  63. . .env
  64.  
  65. .PHONY: list.manifests
  66. list.manifests: ##@ List all manifests in $OBJS
  67. @echo $(OBJS);
  68.  
  69. .PHONY: build
  70. build: ##@docker Build base $IMAGE
  71. docker build -t $(IMAGE) .
  72.  
  73. .PHONY: tag
  74. tag: ##@docker Tag $IMAGE with 'latest' and $COMMIT
  75. docker tag $(IMAGE) $(COMMIT_IMAGE)
  76. docker tag $(IMAGE) $(REPO)$(IMAGE):latest
  77.  
  78. push: build tag ##@docker Push release image to GCP registry
  79. gcloud docker -- push $(COMMIT_IMAGE)
  80. gcloud docker -- push $(REPO)$(IMAGE):latest
  81.  
  82. template: template.reset ##@manifests Replace environment variables
  83. $(foreach var,$(OBJS),envsubst < manifests/$(var) > $(TMPL)/$(var);)
  84. echo manifests in $(TMPL)
  85.  
  86. .PHONY: template.reset
  87. template.reset: ##@manifests Reset .generated
  88. if [ -d $(TMPL) ]; then rm -Rf $(TMPL); fi
  89. mkdir $(TMPL) && cd $(TMPL) && touch $(OBJS)
  90.  
  91. update.deployment: template ##@manifests Only update manifests
  92. kubectl apply -f $(TMPL)
  93. $(CLEAN)
  94.  
  95. update.image: push ##@kubectl Set deployments image to $COMMIT
  96. $(foreach var,$(DPLY),kubectl set image deployment/$(var) \
  97. $(IMAGE)=$(COMMIT_IMAGE);)
  98. $(CLEAN)
  99.  
  100. .PHONY: update.config
  101. update.config: ##@kubectl Replace ini ConfigMap
  102. kubectl replace configmap gke-settings --from-file=$(CONFIG)
  103.  
  104. .PHONY: delete.deployment
  105. delete.deployment: ##@kubectl Delete all deployments
  106. $(foreach var,$(OBJS),kubectl delete deployment $(var);)
  107.  
  108. .PHONY: delete.service
  109. delete.service: ##@kubectl Delete all services
  110. $(foreach var,$(OBJS),kubectl delete service $(var);)
  111.  
  112. delete: delete.deployment delete.service ##@kubectl Delete all resources
  113. kubectl delete pods --all
Add Comment
Please, Sign In to add comment