image.mk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright 2016 The Rook Authors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # remove default suffixes as we dont use them
  15. .SUFFIXES:
  16. override GOOS=linux
  17. # include the common make file
  18. SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
  19. include $(SELF_DIR)/../build/makelib/common.mk
  20. # the registry used for cached images
  21. CACHE_REGISTRY := cache
  22. ifeq ($(GOARCH),amd64)
  23. PLATFORM_ARCH = x86_64
  24. else ifeq ($(GOARCH),arm64)
  25. PLATFORM_ARCH = aarch64
  26. else
  27. $(error Unknown go architecture $(GOARCH))
  28. endif
  29. # if we are running inside the container get our own cid
  30. ifeq ($(UNAME_S),Linux)
  31. SELF_CID := $(shell cat /proc/self/cgroup | grep docker | grep -o -E '[0-9a-f]{64}' | head -n 1)
  32. endif
  33. CACHEBUST ?= 0
  34. ifeq ($(CACHEBUST),1)
  35. BUILD_ARGS += --no-cache
  36. endif
  37. V ?= 0
  38. ifeq ($(V),1)
  39. MAKEFLAGS += VERBOSE=1
  40. else
  41. MAKEFLAGS += --no-print-directory
  42. BUILD_ARGS ?= -q
  43. endif
  44. PULL ?= 1
  45. ifeq ($(PULL),1)
  46. BUILD_BASE_ARGS += --pull
  47. endif
  48. export PULL
  49. ifeq ($(origin IMAGE_OUTPUT_DIR),undefined)
  50. IMAGE_OUTPUT_DIR := $(OUTPUT_DIR)/images/$(PLATFORM)
  51. endif
  52. BUILD_BASE_ARGS += $(BUILD_ARGS)
  53. # =====================================================================================
  54. # Targets
  55. #
  56. .PHONY: all build publish clean
  57. all: build
  58. build: do.build ## Build images for the host platform.
  59. @$(MAKE) cache.images
  60. clean: clean.build ## Remove all images created from the current build.
  61. prune: cache.prune ## Prunes orphaned and cached images at the host level.
  62. clean.images:
  63. @for i in $(CLEAN_IMAGES); do \
  64. if [ -n "$$($(DOCKERCMD) images -q $$i)" ]; then \
  65. for c in $$($(DOCKERCMD) ps -a -q --no-trunc --filter=ancestor=$$i); do \
  66. if [ "$$c" != "$(SELF_CID)" ]; then \
  67. echo stopping and removing container $${c} referencing image $$i; \
  68. $(DOCKERCMD) stop $${c}; \
  69. $(DOCKERCMD) rm $${c}; \
  70. fi; \
  71. done; \
  72. echo cleaning image $$i; \
  73. $(DOCKERCMD) rmi $$i > /dev/null 2>&1 || true; \
  74. fi; \
  75. done
  76. # this will clean everything for this build
  77. clean.build:
  78. @echo === cleaning images for $(BUILD_REGISTRY)
  79. @$(MAKE) clean.images CLEAN_IMAGES="$(shell $(DOCKERCMD) images | grep -E '^$(BUILD_REGISTRY)/' | awk '{print $$1":"$$2}')"
  80. # =====================================================================================
  81. # Caching
  82. #
  83. # NOTE: in order to reduce built time, we maintain a cache
  84. # of already built images. This cache contains images that can be used to help speed
  85. # future docker build commands using docker's content addressable schemes. And also
  86. # to avoid running builds like ceph when the contents have not changed.
  87. # All cached images go in in a 'cache/' local registry and we follow an MRU caching
  88. # policy -- keeping images that have been referenced around and evicting images
  89. # that have to been referenced in a while (and according to a policy). Note we can
  90. # not rely on the image's .CreatedAt date since docker only updates then when the
  91. # image is created and not referenced. Instead we keep a date in the Tag.
  92. # prune images that are at least this many hours old
  93. PRUNE_HOURS ?= 48
  94. # prune keeps at least this many images regardless of how old they are
  95. PRUNE_KEEP ?= 24
  96. PRUNE_DRYRUN ?= 0
  97. CACHE_DATE_FORMAT := "%Y-%m-%d.%H%M%S"
  98. CACHE_PRUNE_DATE := $(shell export TZ="UTC+$(PRUNE_HOURS)"; date +"$(CACHE_DATE_FORMAT)")
  99. CACHE_TAG := $(shell date -u +"$(CACHE_DATE_FORMAT)")
  100. cache.lookup:
  101. @IMAGE_NAME=$${LOOKUP_IMAGE#*/} ;\
  102. if [ -n "$$($(DOCKERCMD) images -q $(LOOKUP_IMAGE))" ]; then exit 0; fi; \
  103. if [ -z "$$($(DOCKERCMD) images -q $(CACHE_REGISTRY)/$${IMAGE_NAME})" ]; then \
  104. $(MAKE) $(MISS_TARGET); \
  105. else \
  106. $(DOCKERCMD) tag $$($(DOCKERCMD) images -q $(CACHE_REGISTRY)/$${IMAGE_NAME}) $(LOOKUP_IMAGE); \
  107. fi;
  108. cache.images:
  109. @for i in $(CACHE_IMAGES); do \
  110. IMGID=$$($(DOCKERCMD) images -q $$i); \
  111. if [ -n "$$IMGID" ]; then \
  112. echo === caching image $$i; \
  113. CACHE_IMAGE=$(CACHE_REGISTRY)/$${i#*/}; \
  114. $(DOCKERCMD) tag $$i $${CACHE_IMAGE}:$(CACHE_TAG); \
  115. for r in $$($(DOCKERCMD) images --format "{{.ID}}#{{.Repository}}:{{.Tag}}" | grep $$IMGID | grep $(CACHE_REGISTRY)/ | grep -v $${CACHE_IMAGE}:$(CACHE_TAG)); do \
  116. $(DOCKERCMD) rmi $${r#*#} > /dev/null 2>&1 || true; \
  117. done; \
  118. fi; \
  119. done
  120. # prune removes old cached images
  121. cache.prune:
  122. @echo === pruning images older than $(PRUNE_HOURS) hours
  123. @echo === keeping a minimum of $(PRUNE_KEEP) images
  124. @EXPIRED=$$($(DOCKERCMD) images --format "{{.Tag}}#{{.Repository}}:{{.Tag}}" \
  125. | grep -E '$(CACHE_REGISTRY)/' \
  126. | sort -r \
  127. | awk -v i=0 -v cd="$(CACHE_PRUNE_DATE)" -F "#" '{if ($$1 <= cd && i >= $(PRUNE_KEEP)) print $$2; i++ }') &&\
  128. for i in $$EXPIRED; do \
  129. echo removing expired cache image $$i; \
  130. [ $(PRUNE_DRYRUN) = 1 ] || $(DOCKERCMD) rmi $$i > /dev/null 2>&1 || true; \
  131. done
  132. @for i in $$($(DOCKERCMD) images -q -f dangling=true); do \
  133. echo removing dangling image $$i; \
  134. $(DOCKERCMD) rmi $$i > /dev/null 2>&1 || true; \
  135. done
  136. # =====================================================================================
  137. # Debugging nukes all images
  138. #
  139. debug.nuke:
  140. @for c in $$($(DOCKERCMD) ps -a -q --no-trunc); do \
  141. if [ "$$c" != "$(SELF_CID)" ]; then \
  142. echo stopping and removing container $${c}; \
  143. $(DOCKERCMD) stop $${c}; \
  144. $(DOCKERCMD) rm $${c}; \
  145. fi; \
  146. done
  147. @for i in $$($(DOCKERCMD) images -q); do \
  148. echo removing image $$i; \
  149. $(DOCKERCMD) rmi -f $$i > /dev/null 2>&1; \
  150. done