aboutsummaryrefslogtreecommitdiff
path: root/rebuild.sh
blob: b08fa5a29de14654e36a57b67f3ed4069cec1557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/bash

# Common code to build a host image on GCE

# INTERNAL_extra_source may be set to a directory containing the source for
# extra package to build.

# INTERNAL_IP can be set to --internal-ip run on a GCE instance
# The instance will need --scope compute-rw

if [ -z "${ANDROID_BUILD_TOP}" ]; then
  echo "ANDROID_BUILD_TOP is not set, did you forget to lunch?" && exit 1
fi

source "${ANDROID_BUILD_TOP}/external/shflags/shflags"
DIR="${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm"

# ARM-board options

DEFINE_boolean arm false "Build on an ARM board"
DEFINE_string arm_instance "" "IP address or DNS name of an ARM system to do the secondary build"
DEFINE_string arm_user "vsoc-01" "User to invoke on the ARM system"

# Docker options

DEFINE_boolean docker false "Build inside docker"
DEFINE_boolean docker_persistent true "Build inside a privileged, persistent container (faster for iterative development)"
DEFINE_string docker_arch "$(uname -m)" "Target architectre"
DEFINE_boolean docker_build_image true "When --noreuse is specified, this flag controls building the docker image (else we assume it was built and reuse it)"
DEFINE_string docker_image "docker_vmm" "Name of docker image to build"
DEFINE_string docker_container "docker_vmm" "Name of docker container to create"
DEFINE_string docker_source "" "Path to sources checked out using manifest"
DEFINE_string docker_working "" "Path to working directory"
DEFINE_string docker_output "" "Output directory (when --docker is specified)"
DEFINE_string docker_user "${USER}" "Docker-container user"
DEFINE_string docker_uid "${UID}" "Docker-container user ID"

# GCE options

DEFINE_boolean gce false "Build on a GCE instance"
DEFINE_string gce_arch "$(uname -m)" "Target architecture"
DEFINE_string gce_project "$(gcloud config get-value project)" "Project to use" "p"
DEFINE_string gce_instance "${USER}-build" "Instance name to create for the build" "i"
DEFINE_string gce_user cuttlefish_crosvm_builder "User name to use on GCE when doing the build"
DEFINE_integer gce_vcpus 4 "Instance size (vcpus) to create"
DEFINE_string gce_zone "$(gcloud config get-value compute/zone)" "Zone to use" "z"

# Common options

DEFINE_string manifest "" "Path to custom manifest to use for the build"
DEFINE_boolean reuse false "Set to true to reuse a previously-set-up instance."
DEFINE_boolean reuse_resync false "Reuse a previously-set-up instance, but clean and re-sync the sources. Overrides --reuse if both are specified."

set -e

SSH_FLAGS=(${INTERNAL_IP})

wait_for_instance() {
  alive=""
  while [[ -z "${alive}" ]]; do
    sleep 5
    alive="$(gcloud compute ssh "${SSH_FLAGS[@]}" "$@" -- uptime || true)"
  done
}

check_common_docker_options() {
  if [[ -z "${FLAGS_docker_image}" ]]; then
    echo Option --docker_image must not be empty 1>&1
    fail=1
  fi
  if [[ -z "${FLAGS_docker_container}" ]]; then
    echo Options --docker_container must not be empty 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_docker_user}" ]]; then
    echo Options --docker_user must not be empty 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_docker_uid}" ]]; then
    echo Options --docker_uid must not be empty 1>&2
    fail=1
  fi
  # Volume mapping are specified only when a container is created.  With
  # --reuse, an already-created persistent container is reused, which implies
  # that we cannot change the volume maps.  For non-persistent containers, we
  # use docker run, which creates and runs the continer in one step; in that
  # case, we must pass the same values for --docker_source and --docker_output
  # that we passed when we ran the non-persistent continer the first time.
  if [[ ${_reuse} -eq 1 && ${FLAGS_docker_persistent} -eq ${FLAGS_TRUE} ]]; then
    if [ -n "${FLAGS_docker_source}" ]; then
      echo Option --docker_source may not be specified with --reuse and --docker_persistent 1>&2
      fail=1
    fi
    if [ -n "${FLAGS_docker_working}" ]; then
      echo Option --docker_working may not be specified with --reuse and --docker_persistent 1>&2
      fail=1
    fi
    if [ -n "${FLAGS_docker_output}" ]; then
      echo Option --docker_output may not be specified with --reuse and --docker_persistent 1>&2
      fail=1
    fi
  fi
  if [[ "${fail}" -ne 0 ]]; then
    exit "${fail}"
  fi
}

build_locally_using_docker() {
  check_common_docker_options
  case "${FLAGS_docker_arch}" in
    aarch64) ;;
    x86_64) ;;
    *) echo Invalid value ${FLAGS_docker_arch} for --docker_arch 1>&2
      fail=1
      ;;
  esac
  if [[ "${fail}" -ne 0 ]]; then
    exit "${fail}"
  fi
  local -i _persistent=0
  if [[ ${FLAGS_docker_persistent} -eq ${FLAGS_TRUE} ]]; then
    _persistent=1
  fi

  local -i _build_image=0
  if [[ ${FLAGS_docker_build_image} -eq ${FLAGS_TRUE} ]]; then
    _build_image=1
  fi

  local _docker_output=""
  if [ -z "${FLAGS_docker_output}" ]; then
    _docker_output="${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm/${FLAGS_docker_arch}-linux-gnu"
  else
    _docker_output="${FLAGS_docker_output}"
  fi

  local _temp="$(mktemp -d)"
  rsync -avR "${relative_source_files[@]/#/${DIR}/./}" "${_temp}"
  if [ -n "${custom_manifest}" ]; then
    cp "${custom_manifest}" "${_temp}"/custom.xml
  else
    touch "${_temp}"/custom.xml
  fi

  ${DIR}/rebuild-docker.sh "${FLAGS_docker_image}" \
                     "${FLAGS_docker_container}" \
                     "${FLAGS_docker_arch}" \
                     "${FLAGS_docker_user}" \
                     "${FLAGS_docker_uid}" \
                     "${_persistent}" \
                     "x${FLAGS_docker_source}" \
                     "x${FLAGS_docker_working}" \
                     "x${_docker_output}" \
                     "${_reuse}" \
                     "${_build_image}" \
                     "${_temp}/Dockerfile" \
                     "${_temp}" \
                     "${#docker_flags[@]}" "${docker_flags[@]}" \
                     "${#_prepare_source[@]}" "${_prepare_source[@]}"

  rm -rf "${_temp}"
}

function build_on_gce() {
  check_common_docker_options
  if [[ "${FLAGS_gce_arch}" != "${FLAGS_docker_arch}" ]]; then
    echo Docker arch must match gce arch 1>&2
    fail=1
  fi
  local _image_family=""
  local _machine_type=""
  case "${FLAGS_gce_arch}" in
    aarch64)
      _image_family=debian-11-arm64
      _machine_type=t2a-standard
      ;;
    x86_64)
      _image_family=debian-11
      _machine_type=n1-standard
      ;;
    *) echo Invalid value ${FLAGS_gce_arch} for --gce_arch 1>&2
      fail=1
      ;;
  esac
  if [[ -z "${FLAGS_gce_instance}" ]]; then
    echo Must specify instance 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_gce_project}" ]]; then
    echo Must specify project 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_gce_zone}" ]]; then
    echo Must specify zone 1>&2
    fail=1
  fi
  if [[ "${fail}" -ne 0 ]]; then
    exit "${fail}"
  fi
  project_zone_flags=(--project="${FLAGS_gce_project}" --zone="${FLAGS_gce_zone}")
  if [ ${_reuse} -eq 0 ]; then
    delete_instances=("${FLAGS_gce_instance}")
    gcloud compute instances delete -q \
      "${delete_instances[@]}" \
      "${project_zone_flags[@]}" || \
        echo Instance does not exist
    gcloud compute images delete -q \
      "${delete_instances[@]/%/-image}" \
      --project "${FLAGS_gce_project}" || \
        echo Image does not exist
    gcloud compute disks delete -q \
      "${delete_instances[@]/%/-disk}" \
      "${project_zone_flags[@]}" || \
        echo Disk does not exist

    gcloud compute disks create \
      "${delete_instances[@]/%/-disk}" \
      "${project_zone_flags[@]}" \
      --image-project="debian-cloud" \
      --image-family="${_image_family}"
    gcloud compute images create \
      "${delete_instances[@]/%/-image}" \
      --source-disk "${delete_instances[@]/%/-disk}" \
      --project "${FLAGS_gce_project}" --source-disk-zone "${FLAGS_gce_zone}"
    gcloud compute instances create \
      "${delete_instances[@]}" \
      "${project_zone_flags[@]}" \
      --image "${delete_instances[@]/%/-image}" \
      --boot-disk-size=200GB \
      --machine-type="${_machine_type}-${FLAGS_gce_vcpus}" \
      --network-interface=nic-type=GVNIC

    wait_for_instance "${FLAGS_gce_instance}" "${project_zone_flags[@]}"

    # install docker
    gcloud beta compute ssh "${SSH_FLAGS[@]}" \
        "${project_zone_flags[@]}" \
        "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
        'curl --retry 10 --retry-all-errors -fsSL https://get.docker.com | /bin/bash'
    gcloud beta compute ssh "${SSH_FLAGS[@]}" \
        "${project_zone_flags[@]}" \
        "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
      sudo usermod -aG docker "${FLAGS_gce_user}"

    # beta for the --internal-ip flag that may be passed via SSH_FLAGS

    gcloud beta compute ssh "${SSH_FLAGS[@]}" \
        "${project_zone_flags[@]}" \
        "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
        mkdir -p '$PWD/docker $PWD/docker/source $PWD/docker/working $PWD/docker/output'

    tar czv -C "${DIR}" -f - "${relative_source_files[@]}" | \
      gcloud beta compute ssh "${SSH_FLAGS[@]}" \
          "${project_zone_flags[@]}" \
          "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
          'tar xzv -C ~/docker -f -'
    if [ -n "${custom_manifest}" ]; then
      gcloud beta compute scp "${SSH_FLAGS[@]}" \
        "${project_zone_flags[@]}" \
        "${custom_manifest}" \
        "${FLAGS_gce_user}@${FLAGS_gce_instance}:~/docker/custom.xml"
    else
      gcloud beta compute ssh "${SSH_FLAGS[@]}" \
        "${project_zone_flags[@]}" \
        "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
        "touch ~/docker/custom.xml"
    fi
  fi

  local _status=$(gcloud compute instances list \
                  --project="${FLAGS_gce_project}" \
                  --zones="${FLAGS_gce_zone}" \
                  --filter="name=('${FLAGS_gce_instance}')" \
                  --format=flattened | awk '/status:/ {print $2}')
  if [ "${_status}" != "RUNNING" ] ; then
    echo "Instance ${FLAGS_gce_instance} is not running."
    exit 1;
  fi

  local -i _persistent=0
  if [[ ${FLAGS_docker_persistent} -eq ${FLAGS_TRUE} ]]; then
    _persistent=1
  fi
  local -i _build_image=0
  if [[ ${FLAGS_docker_build_image} -eq ${FLAGS_TRUE} ]]; then
    _build_image=1
  fi
  gcloud beta compute ssh "${SSH_FLAGS[@]}" \
      "${project_zone_flags[@]}" \
      "${FLAGS_gce_user}@${FLAGS_gce_instance}" -- \
      ./docker/rebuild-docker.sh "${FLAGS_docker_image}" \
                       "${FLAGS_docker_container}" \
                       "${FLAGS_docker_arch}" \
                       '${USER}' \
                       '${UID}' \
                       "${_persistent}" \
                       'x$PWD/docker/source' \
                       'x$PWD/docker/working' \
                       'x$PWD/docker/output' \
                       "${_reuse}" \
                       "${_build_image}" \
                       '~/docker/Dockerfile' \
                       '~/docker/' \
                       "${#docker_flags[@]}" "${docker_flags[@]}" \
                       "${#_prepare_source[@]}" "${_prepare_source[@]}"

  gcloud beta compute ssh "${SSH_FLAGS[@]}" \
      "${project_zone_flags[@]}" \
      "${FLAGS_gce_user}@${FLAGS_gce_instance}" --command \
      'tar czv -C $PWD/docker/output -f - $(find $PWD/docker/output -printf "%P\n")' | \
    tar xzv -C ${DIR}/${FLAGS_docker_arch}-linux-gnu -f -

  gcloud compute disks describe \
    "${project_zone_flags[@]}" "${FLAGS_gce_instance}" | \
      grep ^sourceImage: > "${DIR}"/x86_64-linux-gnu/builder_image.txt
}

function build_on_arm_board() {
  check_common_docker_options
  if [[ "${FLAGS_docker_arch}" != "aarch64" ]]; then
    echo ARM board supports building only aarch64 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_arm_instance}" ]]; then
    echo Must specify IP address of ARM board 1>&2
    fail=1
  fi
  if [[ -z "${FLAGS_arm_user}" ]]; then
    echo Must specify a user account on ARM board 1>&2
    fail=1
  fi
  if [[ "${fail}" -ne 0 ]]; then
    exit "${fail}"
  fi
  if [[ "${_reuse}" -eq 0 ]]; then
    ssh -t "${FLAGS_arm_user}@${FLAGS_arm_instance}" -- \
      rm -rf '$PWD/docker'
  fi
  rsync -avR -e ssh \
    "${relative_source_files[@]/#/${DIR}/./}" \
    "${FLAGS_arm_user}@${FLAGS_arm_instance}:~/docker/"

  if [ -n "${custom_manifest}" ]; then
    scp "${custom_manifest}" "${FLAGS_arm_user}@${FLAGS_arm_instance}":~/docker/custom.xml
  else
    ssh -t "${FLAGS_arm_user}@${FLAGS_arm_instance}" -- \
      "touch ~/docker/custom.xml"
  fi

  local -i _persistent=0
  if [[ ${FLAGS_docker_persistent} -eq ${FLAGS_TRUE} ]]; then
    _persistent=1
  fi
  local -i _build_image=0
  if [[ ${FLAGS_docker_build_image} -eq ${FLAGS_TRUE} ]]; then
    _build_image=1
  fi
  ssh -t "${FLAGS_arm_user}@${FLAGS_arm_instance}" -- \
    mkdir -p '$PWD/docker/source' '$PWD/docker/working' '$PWD/docker/output'
  ssh -t "${FLAGS_arm_user}@${FLAGS_arm_instance}" -- \
    ./docker/rebuild-docker.sh "${FLAGS_docker_image}" \
                     "${FLAGS_docker_container}" \
                     "${FLAGS_docker_arch}" \
                     '${USER}' \
                     '${UID}' \
                     "${_persistent}" \
                     'x$PWD/docker/source' \
                     'x$PWD/docker/working' \
                     'x$PWD/docker/output' \
                     "${_reuse}" \
                     "${_build_image}" \
                     '~/docker/Dockerfile' \
                     '~/docker/' \
                     "${#docker_flags[@]}" "${docker_flags[@]}" \
                     "${#_prepare_source[@]}" "${_prepare_source[@]}"

  rsync -avR -e ssh "${FLAGS_arm_user}@${FLAGS_arm_instance}":docker/output/./ \
    "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm/${FLAGS_docker_arch}-linux-gnu"
}

main() {
  set -o errexit
  set -x
  fail=0
  relative_source_files=("rebuild-docker.sh"
     "rebuild-internal.sh"
     "Dockerfile"
     "manifest.xml"
     ".dockerignore")
  # These must match the definitions in the Dockerfile
  docker_flags=("-eSOURCE_DIR=/source" "-eWORKING_DIR=/working" "-eOUTPUT_DIR=/output" "-eTOOLS_DIR=/static/tools")

  if [[ $(( $((${FLAGS_gce}==${FLAGS_TRUE})) + $((${FLAGS_arm}==${FLAGS_TRUE})) + $((${FLAGS_docker}==${FLAGS_TRUE})) )) > 1 ]]; then
    echo You may specify only one of --gce, --docker, or --arm 1>&2
    exit 2
  fi

  if [[ -n "${FLAGS_manifest}" ]]; then
    if [[ ! -f "${FLAGS_manifest}" ]]; then
      echo custom manifest not found: ${FLAGS_manifest} 1>&1
      exit 2
    fi
    custom_manifest="${FLAGS_manifest}"
    docker_flags+=("-eCUSTOM_MANIFEST=/static/custom.xml")
  else
    custom_manifest="${DIR}/manifest.xml"
    docker_flags+=("-eCUSTOM_MANIFEST=/static/manifest.xml")
  fi
  local -a _prepare_source=(setup_env fetch_source);
  local -i _reuse=0
  if [[ ${FLAGS_reuse} -eq ${FLAGS_TRUE} ]]; then
    # neither install packages, nor sync sources; skip to building them
    _prepare_source=(setup_env)
    # unless you're setting up a non-persistent container and --docker_source is
    # the empty string; in this case, --reuse implies --reuse_resync
    if [[ "${FLAGS_docker_persistent}" -eq ${FLAGS_FALSE} && \
          -z "${FLAGS_docker_source}" ]]; then
      _prepare_source+=(resync_source)
    fi
    _reuse=1
  fi
  if [[ ${FLAGS_reuse_resync} -eq ${FLAGS_TRUE} ]]; then
    # do not install packages but clean and sync sources afresh
    _prepare_source=(setup_env resync_source);
    _reuse=1
  fi
  if [[ ${FLAGS_gce} -eq ${FLAGS_TRUE} ]]; then
    build_on_gce
    exit 0
    gcloud compute instances delete -q \
      "${project_zone_flags[@]}" \
      "${FLAGS_gce_instance}"
  fi
  if [[ ${FLAGS_arm} -eq ${FLAGS_TRUE} ]]; then
    build_on_arm_board
    exit 0
  fi
  if [[ ${FLAGS_docker} -eq ${FLAGS_TRUE} ]]; then
    build_locally_using_docker
    exit 0
  fi
}

FLAGS "$@" || exit 1
main "${FLAGS_ARGV[@]}"