blob: f9982a5e2869c531855715201523593342dcccfe [file] [log] [blame]
Greg Hartmanc8d55bd2018-07-11 16:48:46 -07001#!/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
Greg Hartman94258012019-03-21 20:36:12 -07008# INTERNAL_IP can be set to --internal-ip run on a GCE instance
9# The instance will need --scope compute-rw
10
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070011source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
12
13DEFINE_string build_instance \
14 "${USER}-build" "Instance name to create for the build" "i"
Greg Hartmance71dc12019-03-29 17:31:25 -070015DEFINE_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"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070019DEFINE_string dest_image "vsoc-host-scratch-${USER}" "Image to create" "o"
20DEFINE_string dest_family "" "Image family to add the image to" "f"
Greg Hartmance71dc12019-03-29 17:31:25 -070021DEFINE_string dest_project "$(gcloud config get-value project)" \
22 "Project to use for the new image" "p"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070023DEFINE_string launch_instance "" \
24 "Name of the instance to launch with the new image" "l"
Jason Macnak3cedcc52019-12-05 09:31:54 -080025DEFINE_string source_image_family "debian-10" \
26 "Image familty to use as the base" "s"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070027DEFINE_string source_image_project debian-cloud \
28 "Project holding the base image" "m"
29DEFINE_string repository_url \
Jason Macnak3cedcc52019-12-05 09:31:54 -080030 "https://github.com/google/android-cuttlefish.git" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070031 "URL to the repository with host changes" "u"
32DEFINE_string repository_branch master \
33 "Branch to check out" "b"
Greg Hartman20580ae2018-09-28 18:26:59 -070034DEFINE_string variant master \
35 "Variant to build: generally master or stable"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070036
Greg Hartmance71dc12019-03-29 17:31:25 -070037
Greg Hartman94258012019-03-21 20:36:12 -070038SSH_FLAGS=(${INTERNAL_IP})
39
Jason Macnaked76aed2020-01-21 16:19:34 -080040fatal_echo() {
41 echo "$1"
42 exit 1
43}
44
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070045wait_for_instance() {
46 alive=""
47 while [[ -z "${alive}" ]]; do
48 sleep 5
Greg Hartman94258012019-03-21 20:36:12 -070049 alive="$(gcloud compute ssh "${SSH_FLAGS[@]}" "$@" -- uptime || true)"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070050 done
51}
52
53package_source() {
54 local url="$1"
55 local branch="$2"
56 local repository_dir="${url/*\//}"
57 local debian_dir="$(basename "${repository_dir}" .git)"
58 if [[ $# -eq 4 ]]; then
59 debian_dir="${repository_dir}/$4"
60 fi
61 git clone "${url}" -b "${branch}"
62 dpkg-source -b "${debian_dir}"
63 rm -rf "${debian_dir}"
64}
65
66main() {
67 set -o errexit
68 set -x
Greg Hartmance71dc12019-03-29 17:31:25 -070069 PZ=(--project=${FLAGS_build_project} --zone=${FLAGS_build_zone})
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070070 if [[ -n "${FLAGS_dest_family}" ]]; then
71 dest_family_flag=("--family=${FLAGS_dest_family}")
72 else
73 dest_family_flag=()
74 fi
75 scratch_dir="$(mktemp -d)"
76 pushd "${scratch_dir}"
77 package_source "${FLAGS_repository_url}" "${FLAGS_repository_branch}" \
78 "cuttlefish-common_${FLAGS_version}"
79 popd
Greg Hartman20580ae2018-09-28 18:26:59 -070080 source_files=(
Ram Muthiah6fbd6c82019-12-10 15:58:27 -080081 "${ANDROID_BUILD_TOP}/device/google/cuttlefish/tools/create_base_image_gce.sh"
Greg Hartman20580ae2018-09-28 18:26:59 -070082 ${scratch_dir}/*
83 )
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070084 if [[ -n "${INTERNAL_extra_source}" ]]; then
Greg Hartman20580ae2018-09-28 18:26:59 -070085 source_files+=("${INTERNAL_extra_source}"/*)
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070086 fi
87
88 delete_instances=("${FLAGS_build_instance}" "${FLAGS_dest_image}")
89 if [[ -n "${FLAGS_launch_instance}" ]]; then
90 delete_instances+=("${FLAGS_launch_instance}")
91 fi
92 gcloud compute instances delete -q \
Greg Hartmance71dc12019-03-29 17:31:25 -070093 "${PZ[@]}" "${delete_instances[@]}" || \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070094 echo Not running
95 gcloud compute disks delete -q \
Greg Hartmance71dc12019-03-29 17:31:25 -070096 "${PZ[@]}" "${FLAGS_dest_image}" || echo No scratch disk
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070097 gcloud compute images delete -q \
Greg Hartmance71dc12019-03-29 17:31:25 -070098 --project="${FLAGS_build_project}" "${FLAGS_dest_image}" || echo Not respinning
Greg Hartmanc8d55bd2018-07-11 16:48:46 -070099 gcloud compute disks create \
Greg Hartmance71dc12019-03-29 17:31:25 -0700100 "${PZ[@]}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700101 --image-family="${FLAGS_source_image_family}" \
102 --image-project="${FLAGS_source_image_project}" \
103 "${FLAGS_dest_image}"
Jason Macnaked76aed2020-01-21 16:19:34 -0800104 local gpu_type="nvidia-tesla-p100-vws"
105 gcloud compute accelerator-types describe "${gpu_type}" "${PZ[@]}" || \
106 fatal_echo "Please use a zone with ${gpu_type} GPUs available."
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700107 gcloud compute instances create \
Greg Hartmance71dc12019-03-29 17:31:25 -0700108 "${PZ[@]}" \
Greg Hartman20580ae2018-09-28 18:26:59 -0700109 --machine-type=n1-standard-16 \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700110 --image-family="${FLAGS_source_image_family}" \
111 --image-project="${FLAGS_source_image_project}" \
Greg Hartman20580ae2018-09-28 18:26:59 -0700112 --boot-disk-size=200GiB \
Jason Macnaked76aed2020-01-21 16:19:34 -0800113 --accelerator="type=${gpu_type},count=1" \
Jason Macnak3cedcc52019-12-05 09:31:54 -0800114 --maintenance-policy=TERMINATE \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700115 "${FLAGS_build_instance}"
Greg Hartmance71dc12019-03-29 17:31:25 -0700116 wait_for_instance "${PZ[@]}" "${FLAGS_build_instance}"
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700117 # Ubuntu tends to mount the wrong disk as root, so help it by waiting until
118 # it has booted before giving it access to the clean image disk
119 gcloud compute instances attach-disk \
Greg Hartmance71dc12019-03-29 17:31:25 -0700120 "${PZ[@]}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700121 "${FLAGS_build_instance}" --disk="${FLAGS_dest_image}"
Greg Hartman94258012019-03-21 20:36:12 -0700122 # beta for the --internal-ip flag that may be passed via SSH_FLAGS
Greg Hartmance71dc12019-03-29 17:31:25 -0700123 gcloud beta compute scp "${SSH_FLAGS[@]}" "${PZ[@]}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700124 "${source_files[@]}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700125 "${FLAGS_build_instance}:"
Greg Hartman94258012019-03-21 20:36:12 -0700126 gcloud compute ssh "${SSH_FLAGS[@]}" \
Greg Hartmance71dc12019-03-29 17:31:25 -0700127 "${PZ[@]}" "${FLAGS_build_instance}" -- \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700128 ./create_base_image_gce.sh
129 gcloud compute instances delete -q \
Greg Hartmance71dc12019-03-29 17:31:25 -0700130 "${PZ[@]}" "${FLAGS_build_instance}"
131 gcloud compute images create \
132 --project="${FLAGS_build_project}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700133 --source-disk="${FLAGS_dest_image}" \
Greg Hartmance71dc12019-03-29 17:31:25 -0700134 --source-disk-zone="${FLAGS_build_zone}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700135 --licenses=https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx \
136 "${dest_family_flag[@]}" \
137 "${FLAGS_dest_image}"
Greg Hartmance71dc12019-03-29 17:31:25 -0700138 gcloud compute disks delete -q "${PZ[@]}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700139 "${FLAGS_dest_image}"
140 if [[ -n "${FLAGS_launch_instance}" ]]; then
Greg Hartmance71dc12019-03-29 17:31:25 -0700141 gcloud compute instances create "${PZ[@]}" \
142 --image-project="${FLAGS_build_project}" \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700143 --image="${FLAGS_dest_image}" \
Greg Hartman20580ae2018-09-28 18:26:59 -0700144 --machine-type=n1-standard-4 \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700145 --scopes storage-ro \
Jason Macnaked76aed2020-01-21 16:19:34 -0800146 --accelerator="type=${gpu_type},count=1" \
Jason Macnak3cedcc52019-12-05 09:31:54 -0800147 --maintenance-policy=TERMINATE \
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700148 "${FLAGS_launch_instance}"
149 fi
Greg Hartmance71dc12019-03-29 17:31:25 -0700150 cat <<EOF
151 echo Test and if this looks good, consider releasing it via:
152
153 gcloud compute images create \
154 --project="${FLAGS_dest_project}" \
155 --source-image="${FLAGS_dest_image}" \
156 --source-image-project="${FLAGS_build_project}" \
157 "${dest_family_flag[@]}" \
158 "${FLAGS_dest_image}"
159EOF
Greg Hartmanc8d55bd2018-07-11 16:48:46 -0700160}