1 #!/bin/bash 2 3 # Common code to build a host image on GCE 4 5 # INTERNAL_extra_source may be set to a directory containing the source for 6 # extra package to build. 7 8 # INTERNAL_IP can be set to --internal-ip run on a GCE instance 9 # The instance will need --scope compute-rw 10 11 source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags" 12 13 DEFINE_string build_instance \ 14 "${USER}-build" "Instance name to create for the build" "i" 15 DEFINE_string build_user cuttlefish_crosvm_builder \ 16 "User name to use on GCE when doing the build" 17 DEFINE_string project "$(gcloud config get-value project)" "Project to use" "p" 18 DEFINE_string source_image_family debian-9 "Image familty to use as the base" \ 19 "s" 20 DEFINE_string source_image_project debian-cloud \ 21 "Project holding the base image" "m" 22 DEFINE_string zone "$(gcloud config get-value compute/zone)" "Zone to use" "z" 23 24 SSH_FLAGS=(${INTERNAL_IP}) 25 26 wait_for_instance() { 27 alive="" 28 while [[ -z "${alive}" ]]; do 29 sleep 5 30 alive="$(gcloud compute ssh "${SSH_FLAGS[@]}" "$@" -- uptime || true)" 31 done 32 } 33 34 main() { 35 set -o errexit 36 set -x 37 fail=0 38 source_files=(rebuild_gce.sh) 39 if [[ -z "${FLAGS_project}" ]]; then 40 echo Must specify project 1>&2 41 fail=1 42 fi 43 if [[ -z "${FLAGS_zone}" ]]; then 44 echo Must specify zone 1>&2 45 fail=1 46 fi 47 if [[ "${fail}" -ne 0 ]]; then 48 exit "${fail}" 49 fi 50 project_zone_flags=(--project="${FLAGS_project}" --zone="${FLAGS_zone}") 51 delete_instances=("${FLAGS_build_instance}") 52 gcloud compute instances delete -q \ 53 "${project_zone_flags[@]}" \ 54 "${delete_instances[@]}" || \ 55 echo Not running 56 gcloud compute instances create \ 57 "${project_zone_flags[@]}" \ 58 --machine-type=n1-standard-4 \ 59 --image-family="${FLAGS_source_image_family}" \ 60 --image-project="${FLAGS_source_image_project}" \ 61 "${FLAGS_build_instance}" 62 wait_for_instance "${FLAGS_build_instance}" 63 # beta for the --internal-ip flag that may be passed via SSH_FLAGS 64 gcloud beta compute scp "${SSH_FLAGS[@]}" \ 65 "${project_zone_flags[@]}" \ 66 "${source_files[@]}" \ 67 "${FLAGS_build_user}@${FLAGS_build_instance}:" 68 gcloud compute ssh "${SSH_FLAGS[@]}" \ 69 "${project_zone_flags[@]}" \ 70 "${FLAGS_build_user}@${FLAGS_build_instance}" -- \ 71 ./rebuild_gce.sh 72 gcloud beta compute scp --recurse "${SSH_FLAGS[@]}" \ 73 "${project_zone_flags[@]}" \ 74 "${FLAGS_build_user}@${FLAGS_build_instance}":x86_64 \ 75 "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm" 76 gcloud compute disks describe \ 77 "${project_zone_flags[@]}" "${FLAGS_build_instance}" | \ 78 grep ^sourceImage: > x86_64/builder_image.txt 79 exit 0 80 gcloud compute instances delete -q \ 81 "${project_zone_flags[@]}" \ 82 "${FLAGS_build_instance}" 83 } 84 85 FLAGS "$@" || exit 1 86 main "${FLAGS_ARGV[@]}" 87