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 python3 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/${PROJECT}/global/networks/default \ 75 --network-tier=PREMIUM \ 76 --metadata='startup-script-url=${GCE_STARTUP_SCRIPT},num-workers=${NUM_WORKERS_PER_VM},sandbox-img=${SANDBOX_IMG},worker-img=${WORKER_IMG},google-logging-enabled=true,enable-oslogin=TRUE' \ 77 --maintenance-policy=MIGRATE \ 78 --service-account=gce-ci-worker@${PROJECT}.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 --local-ssd=interface=NVME 87 touch $@ 88 89.PHONY: deploy-controller 90deploy-controller: 91 make -C controller deploy 92 93.PHONY: deploy-frontend 94deploy-frontend: 95 make -C frontend deploy 96 97.PHONY: restart-workers 98restart-workers: stop-workers start-workers 99 100define start-workers-for-region 101gcloud compute --project=${PROJECT} \ 102 instance-groups managed create ${GCE_GROUP_NAME}-$1 \ 103 --region=$1 \ 104 --base-instance-name=ci-$1 \ 105 --template=ci-worker-template \ 106 --size=1 107gcloud compute --quiet --project=$(PROJECT) \ 108 instance-groups managed set-autoscaling ${GCE_GROUP_NAME}-$1 \ 109 --region=$1 \ 110 --min-num-replicas=0 \ 111 --max-num-replicas=${MAX_VMS_PER_REGION} \ 112 --cool-down-period=1800 \ 113 --stackdriver-metric-filter="resource.type = \"global\"" \ 114 --update-stackdriver-metric="custom.googleapis.com/$(PROJECT)/ci_job_queue_len" \ 115 --stackdriver-metric-single-instance-assignment=${NUM_WORKERS_PER_VM} 116endef 117 118.PHONY: start-workers 119start-workers: .deps/gce-template 120 $(foreach region,$(GCE_REGIONS),$(call start-workers-for-region,$(region))) 121 122define stop-workers-for-region 123gcloud compute --quiet --project=${PROJECT} \ 124 instance-groups managed delete ${GCE_GROUP_NAME}-$1 --region=$1 || true 125 126endef 127 128.PHONY: stop-workers 129stop-workers: 130 $(foreach region,$(GCE_REGIONS),$(call stop-workers-for-region,$(region))) 131 132# These are for testing only, start an individual VM. Use start-group for 133# production. 134 135.PHONY: stop-worker-for-testing 136stop-worker-for-testing: 137 gcloud compute --quiet \ 138 --project ${PROJECT} \ 139 instances delete ${GCE_VM_NAME} \ 140 --zone us-central1-f 141 142.PHONY: start-worker-for-testing 143start-worker-for-testing: .deps/gce-template 144 gcloud compute --quiet \ 145 --project ${PROJECT} \ 146 instances create ${GCE_VM_NAME} \ 147 --zone us-central1-f \ 148 --source-instance-template=${GCE_TEMPLATE} 149 150# Debugging client to make OAuth2 authenticated requests manually. 151.PHONY: cli 152cli: 153 GOOGLE_APPLICATION_CREDENTIALS=test-credentials.json \ 154 python3 -i -c 'from common_utils import *; from config import *; \ 155 SCOPES += ["https://www.googleapis.com/auth/firebase.database", \ 156 "https://www.googleapis.com/auth/userinfo.email", \ 157 "https://www.googleapis.com/auth/datastore"]' 158