1# Copyright (C) 2019 The Android Open Source Project
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
15include $(shell python config.py makefile)
16
17override COMMON_DEPS := Makefile *.py
18override GCE_LOCAL_STARTUP_SCRIPT := worker/gce-startup-script.sh
19override SCRIPT_HASH := $(shell git hash-object ${GCE_LOCAL_STARTUP_SCRIPT} | cut -c 1-8)
20override GCE_STARTUP_SCRIPT := gs://perfetto/ci/worker-startup-script/${SCRIPT_HASH}
21BUILDER := docker
22
23.PHONY: help
24help:
25	@echo "build:              Builds the worker and sandbox containers"
26	@echo "build-worker:       Builds the worker container"
27	@echo "build-sandbox:      Builds the sandbox container"
28	@echo "push:               Pushes the containers to the registry"
29	@echo "deploy-controller:  Deploys and restarts the controller"
30	@echo "deploy-frontend:    Deploys and restarts the controller"
31	@echo "stop-workers:       Stops the whole workers GCE instance group"
32	@echo "start-workers:      Starts the whole workers GCE instance group"
33	@echo "restart-workers:    Restarts the whole workers GCE instance group"
34
35.PHONY: build
36build: build-worker build-sandbox
37
38.PHONY: build-worker
39build-worker: .deps/${BUILDER}-worker
40
41.PHONY: build-sandbox
42build-sandbox: .deps/${BUILDER}-sandbox
43
44.PHONY: push
45push: build
46	${BUILDER} push ${WORKER_IMG}
47	${BUILDER} push ${SANDBOX_IMG}
48
49.PHONY: clean
50clean:
51	rm -rf .deps
52
53.deps/${BUILDER}-worker: worker/* ${COMMON_DEPS}
54	mkdir -p worker/tmp
55	cp -a config.py common_utils.py worker/tmp/
56	${BUILDER} build --rm --force-rm -t ${WORKER_IMG} worker
57	rm -rf worker/tmp/
58	touch $@
59
60.deps/${BUILDER}-sandbox: sandbox/* ${COMMON_DEPS}
61	${BUILDER} build --rm --force-rm -t ${SANDBOX_IMG} sandbox
62	touch $@
63
64.deps/upload-startup-script: ${GCE_LOCAL_STARTUP_SCRIPT} ${COMMON_DEPS}
65	gsutil -q cp -a public-read ${GCE_LOCAL_STARTUP_SCRIPT} ${GCE_STARTUP_SCRIPT}
66	touch $@
67
68.deps/gce-template: ${COMMON_DEPS} .deps/upload-startup-script
69	gcloud compute --quiet --project=${PROJECT} \
70		instance-templates delete --quiet ${GCE_TEMPLATE} || true
71	gcloud compute --quiet --project=${PROJECT} \
72		instance-templates create ${GCE_TEMPLATE} \
73		--machine-type=${GCE_VM_TYPE} \
74		--network=projects/perfetto-ci/global/networks/default \
75		--network-tier=PREMIUM \
76		--metadata='startup-script-url=${GCE_STARTUP_SCRIPT},num-workers=${NUM_WORKERS_PER_VM},google-logging-enabled=true' \
77		--maintenance-policy=MIGRATE \
78		--service-account=gce-ci-worker@perfetto-ci.iam.gserviceaccount.com \
79		--scopes=${GCE_SCOPES} \
80		--image=cos-85-13310-1209-10 \
81		--image-project=cos-cloud \
82		--boot-disk-size=100GB \
83		--boot-disk-type=pd-ssd \
84		--boot-disk-device-name=ci-worker-template \
85		--local-ssd=interface=NVME
86	touch $@
87
88.PHONY: deploy-controller
89deploy-controller:
90	make -C controller deploy
91
92.PHONY: deploy-frontend
93deploy-frontend:
94	make -C frontend deploy
95
96.PHONY: restart-workers
97restart-workers: stop-workers start-workers
98
99define start-workers-for-zone
100gcloud compute --project=${PROJECT} \
101	instance-groups managed create ${GCE_GROUP_NAME}-$1 \
102	--zone=$1 \
103	--base-instance-name=ci-$1 \
104	--template=ci-worker-template \
105	--size=${NUM_VMS}
106
107endef
108
109.PHONY: start-workers
110start-workers: .deps/gce-template
111	$(foreach zone,$(GCE_ZONES),$(call start-workers-for-zone,$(zone)))
112
113define stop-workers-for-zone
114gcloud compute --quiet --project=${PROJECT} \
115	instance-groups managed delete ${GCE_GROUP_NAME}-$1 --zone=$1 || true
116
117endef
118
119.PHONY: stop-workers
120stop-workers:
121	$(foreach zone,$(GCE_ZONES),$(call stop-workers-for-zone,$(zone)))
122
123# These are for testing only, start an individual VM. Use start-group for
124# production.
125
126.PHONY: stop-worker-for-testing
127stop-worker-for-testing:
128	gcloud compute --quiet \
129		--project ${PROJECT} \
130		instances delete ${GCE_VM_NAME} \
131		--zone us-central1-f
132
133.PHONY: start-worker-for-testing
134start-worker-for-testing: .deps/gce-template
135	gcloud compute --quiet \
136		--project ${PROJECT} \
137		instances create ${GCE_VM_NAME} \
138		--zone us-central1-f \
139		--source-instance-template=${GCE_TEMPLATE}
140
141# Debugging client to make OAuth2 authenticated requests manually.
142.PHONY: cli
143cli:
144	GOOGLE_APPLICATION_CREDENTIALS=test-credentials.json \
145	python -i -c 'from common_utils import *; from config import *; \
146		SCOPES += ["https://www.googleapis.com/auth/firebase.database", \
147								"https://www.googleapis.com/auth/userinfo.email", \
148								"https://www.googleapis.com/auth/datastore"]'
149