Makefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. .PHONY: all cri deps static clean realclean client-lint client-test client-sync backend frontend shell lint ui-upload
  2. # If you can use Docker without being root, you can `make SUDO= <target>`
  3. SUDO=$(shell docker info >/dev/null 2>&1 || echo "sudo -E")
  4. DOCKERHUB_USER=weaveworks
  5. SCOPE_EXE=prog/scope
  6. SCOPE_EXPORT=scope.tar
  7. CLOUD_AGENT_EXPORT=cloud-agent.tar
  8. SCOPE_UI_BUILD_IMAGE=node:10.19
  9. SCOPE_BACKEND_BUILD_IMAGE=$(DOCKERHUB_USER)/scope-backend-build
  10. SCOPE_BACKEND_BUILD_UPTODATE=.scope_backend_build.uptodate
  11. SCOPE_VERSION=$(shell git rev-parse --short HEAD)
  12. GIT_REVISION=$(shell git rev-parse HEAD)
  13. WEAVENET_VERSION=2.8.1
  14. RUNSVINIT=vendor/github.com/peterbourgon/runsvinit/runsvinit
  15. CODECGEN_DIR=vendor/github.com/ugorji/go/codec/codecgen
  16. CODECGEN_UID=0
  17. GET_CODECGEN_DEPS=$(shell find $(1) -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name '*.codecgen.go' -not -name '*.generated.go')
  18. CODECGEN_TARGETS=report/report.codecgen.go render/detailed/detailed.codecgen.go
  19. RM=--rm
  20. RUN_FLAGS=-ti
  21. BUILD_IN_CONTAINER=true
  22. GO_ENV=GOGC=off
  23. GO_BUILD_TAGS='netgo osusergo unsafe'
  24. GO_BUILD_FLAGS=-mod vendor -ldflags "-extldflags \"-static\" -X main.version=$(SCOPE_VERSION) -s -w" -tags $(GO_BUILD_TAGS)
  25. ifeq ($(GOARCH),arm)
  26. GO_ENV+=CGO_ENABLED=1
  27. ARM_CC=CC=/usr/bin/arm-linux-gnueabihf-gcc
  28. else ifeq ($(GOARCH),s390x)
  29. GO_ENV+=CGO_ENABLED=1
  30. S390X_CC=CC=/usr/bin/s390x-linux-gnu-gcc
  31. endif
  32. GO=env $(GO_ENV) $(ARM_CC) $(S390X_CC) go
  33. NO_CROSS_COMP=unset GOOS GOARCH
  34. GO_HOST=$(NO_CROSS_COMP); env $(GO_ENV) go
  35. WITH_GO_HOST_ENV=$(NO_CROSS_COMP); $(GO_ENV)
  36. IMAGE_TAG=$(shell ./tools/image-tag)
  37. all: $(SCOPE_EXPORT)
  38. update-cri:
  39. curl https://raw.githubusercontent.com/kubernetes/kubernetes/master/pkg/kubelet/apis/cri/runtime/v1alpha2/api.proto > cri/runtime/api.proto
  40. protoc-gen-gofast:
  41. @go get -u -v github.com/gogo/protobuf/protoc-gen-gofast
  42. # Use cri target to download latest cri proto files and regenerate CRI runtime files.
  43. cri: update-cri protoc-gen-gofast
  44. @cd $(GOPATH)/src;protoc --proto_path=$(GOPATH)/src --gofast_out=plugins=grpc:. github.com/weaveworks/scope/cri/runtime/api.proto
  45. docker/weave:
  46. curl -L https://github.com/weaveworks/weave/releases/download/v$(WEAVENET_VERSION)/weave -o docker/weave
  47. chmod u+x docker/weave
  48. docker/weaveutil:
  49. $(SUDO) docker run --rm --entrypoint=cat weaveworks/weaveexec:$(WEAVENET_VERSION) /usr/bin/weaveutil > $@
  50. chmod +x $@
  51. docker/%: %
  52. cp $* docker/
  53. %.tar: docker/Dockerfile.%
  54. $(SUDO) docker build --build-arg=revision=$(GIT_REVISION) -t $(DOCKERHUB_USER)/$* -f $< docker/
  55. $(SUDO) docker tag $(DOCKERHUB_USER)/$* $(DOCKERHUB_USER)/$*:$(IMAGE_TAG)
  56. $(SUDO) docker save $(DOCKERHUB_USER)/$*:latest > $@
  57. $(CLOUD_AGENT_EXPORT): docker/Dockerfile.cloud-agent docker/$(SCOPE_EXE) docker/weave docker/weaveutil
  58. $(SCOPE_EXPORT): docker/Dockerfile.scope $(CLOUD_AGENT_EXPORT) docker/$(RUNSVINIT) docker/run-app docker/run-probe docker/entrypoint.sh
  59. $(RUNSVINIT): vendor/github.com/peterbourgon/runsvinit/*.go
  60. $(SCOPE_EXE): $(shell find ./ -path ./vendor -prune -o -type f -name '*.go') prog/staticui/staticui.go prog/externalui/externalui.go $(CODECGEN_TARGETS)
  61. report/report.codecgen.go: $(call GET_CODECGEN_DEPS,report/)
  62. render/detailed/detailed.codecgen.go: $(call GET_CODECGEN_DEPS,render/detailed/)
  63. static: prog/staticui/staticui.go prog/externalui/externalui.go
  64. prog/staticui/staticui.go: client/build/index.html
  65. prog/externalui/externalui.go: client/build-external/index.html
  66. ifeq ($(BUILD_IN_CONTAINER),true)
  67. $(SCOPE_EXE) $(RUNSVINIT) lint tests shell prog/staticui/staticui.go prog/externalui/externalui.go: $(SCOPE_BACKEND_BUILD_UPTODATE)
  68. @mkdir -p $(shell pwd)/.pkg
  69. $(SUDO) docker run $(RM) $(RUN_FLAGS) \
  70. -v $(shell pwd):/go/src/github.com/weaveworks/scope:delegated,z \
  71. -v $(shell pwd)/.pkg:/go/pkg:delegated,z \
  72. -v $(shell pwd)/.cache:/go/cache:delegated,z \
  73. -u $(shell id -u ${USER}):$(shell id -g ${USER}) \
  74. --net=host \
  75. -e HOME=/go/src/github.com/weaveworks/scope \
  76. -e GOARCH -e GOOS -e CIRCLECI -e CIRCLE_BUILD_NUM -e CIRCLE_NODE_TOTAL \
  77. -e CIRCLE_NODE_INDEX -e COVERDIR -e SLOW -e TESTDIRS \
  78. $(SCOPE_BACKEND_BUILD_IMAGE) SCOPE_VERSION=$(SCOPE_VERSION) CODECGEN_UID=$(CODECGEN_UID) $@
  79. else
  80. $(SCOPE_EXE):
  81. time $(GO) build $(GO_BUILD_FLAGS) -o $@ ./$(@D)
  82. CODECGEN_EXE=$(CODECGEN_DIR)/bin/codecgen_$(shell go env GOHOSTOS)_$(shell go env GOHOSTARCH)
  83. %.codecgen.go: $(CODECGEN_EXE)
  84. rm -f $@; $(GO_HOST) build $(GO_BUILD_FLAGS) ./$(@D) # workaround for https://github.com/ugorji/go/issues/145
  85. cd $(@D) && $(WITH_GO_HOST_ENV) GO111MODULE=off $(shell pwd)/$(CODECGEN_EXE) -d $(CODECGEN_UID) -rt $(GO_BUILD_TAGS) -u -o $(@F) $(notdir $(call GET_CODECGEN_DEPS,$(@D)))
  86. $(CODECGEN_EXE): $(CODECGEN_DIR)/*.go
  87. mkdir -p $(@D)
  88. $(GO_HOST) build $(GO_BUILD_FLAGS) -o $@ ./$(CODECGEN_DIR)
  89. $(RUNSVINIT):
  90. time $(GO) build $(GO_BUILD_FLAGS) -o $@ ./$(@D)
  91. shell:
  92. /bin/bash
  93. tests: $(CODECGEN_TARGETS) prog/staticui/staticui.go prog/externalui/externalui.go
  94. ./tools/test -no-go-get -tags $(GO_BUILD_TAGS)
  95. lint: prog/staticui/staticui.go prog/externalui/externalui.go
  96. ./tools/lint
  97. prog/staticui/staticui.go:
  98. mkdir -p prog/staticui
  99. $(NO_CROSS_COMP); go run -mod vendor github.com/mjibson/esc -o $@ -pkg staticui -prefix client/build client/build
  100. prog/externalui/externalui.go:
  101. mkdir -p prog/externalui
  102. $(NO_CROSS_COMP); go run -mod vendor github.com/mjibson/esc -o $@ -pkg externalui -prefix client/build-external -include '\.html$$' client/build-external
  103. endif
  104. ifeq ($(BUILD_IN_CONTAINER),true)
  105. SCOPE_UI_TOOLCHAIN=.cache/build_node_modules
  106. SCOPE_UI_TOOLCHAIN_UPTODATE=$(SCOPE_UI_TOOLCHAIN)/.uptodate
  107. SCOPE_UI_BUILD_CMD=$(SUDO) docker run $(RM) $(RUN_FLAGS) \
  108. -v $(shell pwd)/.cache:/home/weave/scope/.cache:delegated,z \
  109. -v $(shell pwd)/client:/home/weave/scope/client:delegated,z \
  110. -v $(shell pwd)/$(SCOPE_UI_TOOLCHAIN):/home/weave/scope/client/node_modules:delegated,z \
  111. -w /home/weave/scope/client \
  112. -e HOME=/home/weave/scope/client \
  113. -e NPM_CONFIG_LOGLEVEL=warn \
  114. -e NPM_CONFIG_PROGRESS=false \
  115. -e XDG_CACHE_HOME=/home/weave/scope/.cache \
  116. -u $(shell id -u ${USER}):$(shell id -g ${USER})
  117. $(SCOPE_UI_TOOLCHAIN_UPTODATE): client/yarn.lock $(SCOPE_UI_BUILD_UPTODATE)
  118. mkdir -p $(SCOPE_UI_TOOLCHAIN) client/node_modules
  119. if test "true" != "$(SCOPE_SKIP_UI_ASSETS)"; then \
  120. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn install; \
  121. fi
  122. touch $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  123. client/build/index.html: $(shell find client/app -type f) $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  124. mkdir -p client/build
  125. if test "true" != "$(SCOPE_SKIP_UI_ASSETS)"; then \
  126. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn run build; \
  127. fi
  128. client/build-external/index.html: $(shell find client/app -type f) $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  129. mkdir -p client/build-external
  130. if test "true" != "$(SCOPE_SKIP_UI_ASSETS)"; then \
  131. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn run build-external; \
  132. fi
  133. client-test: $(shell find client/app/scripts -type f) $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  134. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn test
  135. client-lint: $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  136. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn run lint
  137. client-start: $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  138. $(SCOPE_UI_BUILD_CMD) --net=host \
  139. -e WEBPACK_SERVER_HOST \
  140. $(SCOPE_UI_BUILD_IMAGE) yarn start
  141. client/bundle/weave-scope.tgz: $(shell find client/app -type f) $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  142. $(SCOPE_UI_BUILD_CMD) $(SCOPE_UI_BUILD_IMAGE) yarn run bundle
  143. else
  144. SCOPE_UI_TOOLCHAIN=client/node_modules
  145. SCOPE_UI_TOOLCHAIN_UPTODATE=$(SCOPE_UI_TOOLCHAIN)/.uptodate
  146. $(SCOPE_UI_TOOLCHAIN_UPTODATE): client/yarn.lock
  147. if test "true" = "$(SCOPE_SKIP_UI_ASSETS)"; then mkdir -p $(SCOPE_UI_TOOLCHAIN); else cd client && yarn install; fi
  148. touch $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  149. client/build/index.html: $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  150. mkdir -p client/build
  151. if test "true" != "$(SCOPE_SKIP_UI_ASSETS)"; then cd client && yarn run build; fi
  152. client/build-external/index.html: $(SCOPE_UI_TOOLCHAIN_UPTODATE)
  153. mkdir -p client/build-external
  154. if test "true" != "$(SCOPE_SKIP_UI_ASSETS)"; then cd client && yarn run build-external; fi
  155. endif
  156. $(SCOPE_BACKEND_BUILD_UPTODATE): backend/*
  157. $(SUDO) docker build -t $(SCOPE_BACKEND_BUILD_IMAGE) backend
  158. $(SUDO) docker tag $(SCOPE_BACKEND_BUILD_IMAGE) $(SCOPE_BACKEND_BUILD_IMAGE):$(IMAGE_TAG)
  159. touch $@
  160. # Run aws CLI from a container image so we don't have to install Python, etc.
  161. AWS_COMMAND=docker run $(RM) $(RUN_FLAGS) \
  162. -e AWS_ACCESS_KEY_ID=$$UI_BUCKET_KEY_ID \
  163. -e AWS_SECRET_ACCESS_KEY=$$UI_BUCKET_KEY_SECRET \
  164. -v $(shell pwd):/scope \
  165. amazon/aws-cli:2.1.35
  166. ui-upload: client/build-external/index.html
  167. $(AWS_COMMAND) s3 cp /scope/client/build-external/ s3://static.weave.works/scope-ui/ --recursive --exclude '*.html'
  168. ui-pkg-upload: client/bundle/weave-scope.tgz
  169. $(AWS_COMMAND) s3 cp /scope/client/bundle/weave-scope.tgz s3://weaveworks-js-modules/weave-scope/$(shell echo $(SCOPE_VERSION))/weave-scope.tgz
  170. # We don't rmi images here; rm'ing the .uptodate files is enough to
  171. # get the build images rebuilt, and rm'ing the scope exe is enough to
  172. # get the main images rebuilt.
  173. #
  174. # rmi'ng images is desirable sometimes. Invoke `realclean` for that.
  175. clean:
  176. $(GO) clean ./...
  177. rm -rf $(SCOPE_EXPORT) $(SCOPE_UI_TOOLCHAIN_UPTODATE) $(SCOPE_BACKEND_BUILD_UPTODATE) \
  178. $(SCOPE_EXE) $(RUNSVINIT) prog/staticui/staticui.go prog/externalui/externalui.go client/build/*.js client/build-external/*.js docker/weave .pkg \
  179. $(CODECGEN_TARGETS) $(CODECGEN_DIR)/bin
  180. clean-codecgen:
  181. rm -rf $(CODECGEN_TARGETS) $(CODECGEN_DIR)/bin
  182. # clean + rmi
  183. #
  184. # Removal of the main images ensures that a subsequent build rebuilds
  185. # all their layers, in particular layers installing packages.
  186. # Crucially, we also remove the *base* images, so their latest
  187. # versions will be pulled.
  188. #
  189. # Doing this is important for release builds.
  190. realclean: clean
  191. rm -rf $(SCOPE_UI_TOOLCHAIN)
  192. $(SUDO) docker rmi -f $(SCOPE_BACKEND_BUILD_IMAGE) \
  193. $(DOCKERHUB_USER)/scope $(DOCKERHUB_USER)/cloud-agent \
  194. $(DOCKERHUB_USER)/scope:$(IMAGE_TAG) $(DOCKERHUB_USER)/cloud-agent:$(IMAGE_TAG) \
  195. weaveworks/weaveexec:$(WEAVENET_VERSION) \
  196. ubuntu:bionic alpine:3.11 node:6.9.0 2>/dev/null || true
  197. # Dependencies are intentionally build without enforcing any tags
  198. # since they are build on the host
  199. deps:
  200. $(GO) get -u -f \
  201. github.com/mattn/goveralls \
  202. github.com/2opremio/trifles/wscat