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
11source "${ANDROID_BUILD_TOP}/external/shflags/shflags"
12
13DEFINE_string build_instance \
14  "${USER}-build" "Instance name to create for the build" "i"
15DEFINE_string build_project "$(gcloud config get-value project)" \
16  "Project to use for scratch"
17DEFINE_string build_zone "$(gcloud config get-value compute/zone)" \
18  "Zone to use for scratch resources"
19DEFINE_string dest_image "vsoc-host-scratch-${USER}" "Image to create" "o"
20DEFINE_string dest_family "" "Image family to add the image to" "f"
21DEFINE_string dest_project "$(gcloud config get-value project)" \
22  "Project to use for the new image" "p"
23DEFINE_string launch_instance "" \
24  "Name of the instance to launch with the new image" "l"
25DEFINE_string source_image_family "debian-10" \
26  "Image familty to use as the base" "s"
27DEFINE_string source_image_project debian-cloud \
28  "Project holding the base image" "m"
29DEFINE_string repository_url \
30  "https://github.com/google/android-cuttlefish.git" \
31  "URL to the repository with host changes" "u"
32DEFINE_string repository_branch main \
33  "Branch to check out" "b"
34
35
36SSH_FLAGS=(${INTERNAL_IP})
37
38fatal_echo() {
39  echo "$1"
40  exit 1
41}
42
43wait_for_instance() {
44  alive=""
45  while [[ -z "${alive}" ]]; do
46    sleep 5
47    alive="$(gcloud compute ssh "${SSH_FLAGS[@]}" "$@" -- uptime || true)"
48  done
49}
50
51package_source() {
52  local url="$1"
53  local branch="$2"
54  local repository_dir="${url/*\//}"
55  local debian_dir="$(basename "${repository_dir}" .git)"
56  if [[ $# -eq 4 ]]; then
57    debian_dir="${repository_dir}/$4"
58  fi
59  git clone "${url}" -b "${branch}"
60  dpkg-source -b "${debian_dir}"
61  rm -rf "${debian_dir}"
62}
63
64main() {
65  set -o errexit
66  set -x
67  PZ=(--project=${FLAGS_build_project} --zone=${FLAGS_build_zone})
68  if [[ -n "${FLAGS_dest_family}" ]]; then
69    dest_family_flag=("--family=${FLAGS_dest_family}")
70  else
71    dest_family_flag=()
72  fi
73  scratch_dir="$(mktemp -d)"
74  pushd "${scratch_dir}"
75  package_source "${FLAGS_repository_url}" "${FLAGS_repository_branch}" \
76    "cuttlefish-common_${FLAGS_version}"
77  popd
78  source_files=(
79    "${ANDROID_BUILD_TOP}/device/google/cuttlefish/tools/create_base_image_gce.sh"
80    ${scratch_dir}/*
81  )
82  if [[ -n "${INTERNAL_extra_source}" ]]; then
83    source_files+=("${INTERNAL_extra_source}"/*)
84  fi
85
86  delete_instances=("${FLAGS_build_instance}" "${FLAGS_dest_image}")
87  if [[ -n "${FLAGS_launch_instance}" ]]; then
88    delete_instances+=("${FLAGS_launch_instance}")
89  fi
90  gcloud compute instances delete -q \
91    "${PZ[@]}" "${delete_instances[@]}" || \
92      echo Not running
93  gcloud compute disks delete -q \
94    "${PZ[@]}" "${FLAGS_dest_image}" || echo No scratch disk
95  gcloud compute images delete -q \
96    --project="${FLAGS_build_project}" "${FLAGS_dest_image}" || echo Not respinning
97  gcloud compute disks create \
98    "${PZ[@]}" \
99    --image-family="${FLAGS_source_image_family}" \
100    --image-project="${FLAGS_source_image_project}" \
101    "${FLAGS_dest_image}"
102  local gpu_type="nvidia-tesla-p100-vws"
103  gcloud compute accelerator-types describe "${gpu_type}" "${PZ[@]}" || \
104    fatal_echo "Please use a zone with ${gpu_type} GPUs available."
105  gcloud compute instances create \
106    "${PZ[@]}" \
107    --machine-type=n1-standard-16 \
108    --image-family="${FLAGS_source_image_family}" \
109    --image-project="${FLAGS_source_image_project}" \
110    --boot-disk-size=200GiB \
111    --accelerator="type=${gpu_type},count=1" \
112    --maintenance-policy=TERMINATE \
113    "${FLAGS_build_instance}"
114  wait_for_instance "${PZ[@]}" "${FLAGS_build_instance}"
115  # Ubuntu tends to mount the wrong disk as root, so help it by waiting until
116  # it has booted before giving it access to the clean image disk
117  gcloud compute instances attach-disk \
118      "${PZ[@]}" \
119      "${FLAGS_build_instance}" --disk="${FLAGS_dest_image}"
120  # beta for the --internal-ip flag that may be passed via SSH_FLAGS
121  gcloud beta compute scp "${SSH_FLAGS[@]}" "${PZ[@]}" \
122    "${source_files[@]}" \
123    "${FLAGS_build_instance}:"
124  gcloud compute ssh "${SSH_FLAGS[@]}" \
125    "${PZ[@]}" "${FLAGS_build_instance}" -- \
126    ./create_base_image_gce.sh
127  gcloud compute instances delete -q \
128    "${PZ[@]}" "${FLAGS_build_instance}"
129  gcloud compute images create \
130    --project="${FLAGS_build_project}" \
131    --source-disk="${FLAGS_dest_image}" \
132    --source-disk-zone="${FLAGS_build_zone}" \
133    --licenses=https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx \
134    "${dest_family_flag[@]}" \
135    "${FLAGS_dest_image}"
136  gcloud compute disks delete -q "${PZ[@]}" \
137    "${FLAGS_dest_image}"
138  if [[ -n "${FLAGS_launch_instance}" ]]; then
139    gcloud compute instances create "${PZ[@]}" \
140      --image-project="${FLAGS_build_project}" \
141      --image="${FLAGS_dest_image}" \
142      --machine-type=n1-standard-4 \
143      --scopes storage-ro \
144      --accelerator="type=${gpu_type},count=1" \
145      --maintenance-policy=TERMINATE \
146      "${FLAGS_launch_instance}"
147  fi
148  cat <<EOF
149    echo Test and if this looks good, consider releasing it via:
150
151    gcloud compute images create \
152      --project="${FLAGS_dest_project}" \
153      --source-image="${FLAGS_dest_image}" \
154      --source-image-project="${FLAGS_build_project}" \
155      "${dest_family_flag[@]}" \
156      "${FLAGS_dest_image}"
157EOF
158}
159