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-stable-75-12105-77-0 \
81		--image-project=cos-cloud \
82		--boot-disk-size=200GB \
83		--boot-disk-type=pd-ssd \
84		--boot-disk-device-name=ci-worker-template
85	touch $@
86
87.PHONY: deploy-controller
88deploy-controller:
89	make -C controller deploy
90
91.PHONY: deploy-frontend
92deploy-frontend:
93	make -C frontend deploy
94
95.PHONY: stop-workers
96stop-workers:
97	gcloud compute --quiet --project=${PROJECT} \
98		instance-groups managed delete ${GCE_GROUP_NAME} --zone=${ZONE} || true
99
100# Fix the replicas to 2. Dynamic scaling causes too jobs to be aborted while
101# scaling down.
102.PHONY: start-workers
103start-workers: .deps/gce-template
104	gcloud beta compute --project=${PROJECT} \
105		instance-groups managed create ${GCE_GROUP_NAME} \
106		--zone=${ZONE} \
107		--base-instance-name=ci-worker-group \
108		--template=ci-worker-template \
109		--size=1
110	gcloud beta compute --quiet --project=${PROJECT} \
111		instance-groups managed set-autoscaling ${GCE_GROUP_NAME} \
112		--zone ${ZONE} \
113		--min-num-replicas "${NUM_VMS}" \
114		--max-num-replicas "${NUM_VMS}" \
115		--cool-down-period "1800" \
116		--stackdriver-metric-filter "resource.type = \"global\"" \
117		--update-stackdriver-metric "custom.googleapis.com/perfetto-ci/ci_job_queue_len" \
118		--stackdriver-metric-single-instance-assignment "10"
119
120.PHONY: restart-workers
121restart-workers: stop-workers start-workers
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 ${ZONE}
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 ${ZONE} \
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