Refactor deploy_termina script to add building of packages and image

This CL switches to use common.sh to provide 'die' and shflags over getops.
It also adds building packages and image to deploy_termin with '-p' and '-i'.

There is also a small patch to allow the user to use "-d=<ip>" or "-d <ip>" to
specify the device ip (which would previously fail to deploy) and makes
deploying optional (for testing build without a device).

Also fixes a bug in usage of "${IMAGE}" vs "${image}".

BUG=None
TEST=./deploy_termina -d device_ip
Change-Id: I57c44c350262789f57af0a2fb70610379f38e066
Reviewed-on: https://chromium-review.googlesource.com/1192663
Commit-Ready: Josh Pratt <jopra@chromium.org>
Tested-by: Josh Pratt <jopra@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/contrib/deploy_termina b/contrib/deploy_termina
index 2c6f24b..7a69b70 100755
--- a/contrib/deploy_termina
+++ b/contrib/deploy_termina
@@ -8,70 +8,86 @@
 # Pushes the lastest local build of tatl to your test device.
 # Usage: ./deploy_termina.sh device_ip
 
-IMAGE="/mnt/host/source/src/build/images/tatl/latest/chromiumos_test_image.bin"
+# Loads script libraries.
+CONTRIB_DIR=$(dirname "$(readlink -f "$0")")
+. "${CONTRIB_DIR}/common.sh" || exit 1
+
 STATEFUL_PARTITION="/mnt/stateful_partition/"
 RUN_PATH="/run/imageloader/cros-termina/99999.0.0"
-TEST_SSH_KEY="/mnt/host/source/chromite/ssh_keys/testing_rsa"
+TEST_SSH_KEY="${GCLIENT_ROOT}/chromite/ssh_keys/testing_rsa"
 
-die() {
-  echo "$0: error: $*" >&2
-  exit 1
-}
-
-usage() {
-  echo "Usage: $0 -d=device_ip_addr [-v]" >&2
-  exit 1
-}
+# Define flags.
+DEFINE_string 'board' 'tatl' 'Specify the board to build for.' 'b'
+DEFINE_boolean 'build_packages' false 'Builds packages.' 'p'
+DEFINE_boolean 'build_image' false 'Builds the image.' 'i'
+DEFINE_string 'device_ip_addr' '' 'The ip of the device to deploy to.' 'd'
+DEFINE_boolean 'verbose' false 'Turns on verbose logging.' 'v'
 
 main() {
-  local device_ip_addr=""
   local install_script=""
   local out_dir=""
-  local verbose="false"
-  while getopts vd: name; do
-    case "${name}" in
-      d) device_ip_addr="${OPTARG}" ;;
-      v) verbose="true" ;;
-      ?) usage ;;
-    esac
-  done
-  if [[ -z "${device_ip_addr}" ]]; then
-    die "Missing -d flag"
+
+  # The script will only work inside the chroot.
+  assert_inside_chroot
+
+  if [[ ${FLAGS_build_packages} -eq ${FLAGS_TRUE} ]]; then
+    if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
+      echo "Building packages..." >&2
+    fi
+    ./build_packages --board="${FLAGS_board}" --nowithautotest ||
+      die "Failed to build packages."
   fi
 
-  if [[ ${verbose} == "true" ]]; then
-    echo "Pushing termina to '${device_ip_addr}'" >&2
+  if [[ ${FLAGS_build_image} -eq ${FLAGS_TRUE} ]]; then
+    if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
+      echo "Building image..." >&2
+    fi
+    ./build_image --board="${FLAGS_board}" test ||
+      die "Failed to build image"
   fi
 
-  if [[ ${verbose} == "true" ]]; then
+  if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
     echo "Repacking the termina image..." >&2
   fi
 
-  out_dir="$(mktemp -d)/tatl"
-  ./termina_build_image --image "${IMAGE}" -t --output "${out_dir}"
+  out_dir="$(mktemp -d)/${FLAGS_board}"
+  image_dir="${GCLIENT_ROOT}/src/build/images/${FLAGS_board}/"
+  image="${image_dir}/latest/chromiumos_test_image.bin"
+  ./termina_build_image --image "${image}" -t --output "${out_dir}"
 
-  if [[ ${verbose} == "true" ]]; then
-    echo "Copying image to device:" >&2
-  fi
-  rsync -a "${out_dir}" -e "ssh -i ${TEST_SSH_KEY}" \
-    "root@${device_ip_addr}:${STATEFUL_PARTITION}" \
-    || die "Failed to copy to device."
-  rm -rf "${out_dir}"
+  if [[ -n "${FLAGS_device_ip_addr}" ]]; then
+    # Remove '=' if the user typed -d=<ip> rather than -d <ip>.
+    FLAGS_device_ip_addr=$(echo "${FLAGS_device_ip_addr}" | sed "s/=//g")
 
-  if [[ ${verbose} == "true" ]]; then
-    echo "Install image onto to device:" >&2
-  fi
-  read -r -d '' install_script <<EOF
-  dbus-send --system --type=method_call --print-reply \
-    --dest=org.chromium.ComponentUpdaterService \
-    /org/chromium/ComponentUpdaterService \
-    org.chromium.ComponentUpdaterService.LoadComponent 'string:cros-termina' &&
-  mkdir -p "${RUN_PATH}" &&
-  mount --bind "${STATEFUL_PARTITION}/tatl" "${RUN_PATH}" &&
-  restart vm_concierge
+    if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
+      echo "Copying image to device:" >&2
+    fi
+    rsync -a "${out_dir}" -e "ssh -i ${TEST_SSH_KEY}" \
+      "root@${FLAGS_device_ip_addr}:${STATEFUL_PARTITION}" \
+      || die "Failed to copy to device."
+    rm -rf "${out_dir}"
+
+    if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
+      echo "Install image onto to device:" >&2
+    fi
+    read -r -d '' install_script <<EOF
+    dbus-send --system --type=method_call --print-reply \
+      --dest=org.chromium.ComponentUpdaterService \
+      /org/chromium/ComponentUpdaterService \
+      org.chromium.ComponentUpdaterService.LoadComponent \
+      'string:cros-termina' &&
+    mkdir -p "${RUN_PATH}" &&
+    mount --bind "${STATEFUL_PARTITION}/${FLAGS_board}" "${RUN_PATH}" &&
+    restart vm_cicerone
+    restart vm_concierge
 EOF
-  ssh -i "${TEST_SSH_KEY}" "root@${device_ip_addr}" "${install_script}" \
-    || die "Failed to deploy to device."
+    ssh -i "${TEST_SSH_KEY}" "root@${FLAGS_device_ip_addr}" \
+      "${install_script}" \
+      || die "Failed to deploy to device."
+  fi
 }
 
+# Parse the command-line.
+FLAGS "$@" || exit $?
+eval set -- "${FLAGS_ARGV}"
 main "$@"