blob: 247b69d2757ab4c2a580927f37bd9f14dbecd0c1 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Based on http://go/termina-care-and-feeding
#
# Pushes the lastest local build of tatl to your test device.
# Usage: ./deploy_termina.sh device_ip
# 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="${GCLIENT_ROOT}/chromite/ssh_keys/testing_rsa"
SSH_OPTS=(
"-i" "${TEST_SSH_KEY}"
"-o" "ConnectTimeout=10"
"-o" "StrictHostKeyChecking=no"
"-o" "UserKnownHostsFile=/dev/null"
)
# 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_boolean 'test_image' true 'Use a testing termina image instead of a base image.' 't'
DEFINE_string 'device_ip_addr' '' 'The ip of the device to deploy to.' 'd'
DEFINE_integer 'device_port' 22 'The ssh port number of the device to deploy to.' 'P'
DEFINE_boolean 'verbose' false 'Turns on verbose logging.' 'v'
main() {
local install_script=""
local out_dir=""
# The script will only work inside the chroot.
assert_inside_chroot
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 [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
echo "Detecting device architecture..." >&2
fi
uname_m=$(ssh "${SSH_OPTS[@]}" "-p" "${FLAGS_device_port}" \
"root@${FLAGS_device_ip_addr}" "uname -m")
case "${uname_m}" in
x86_64) FLAGS_board='tatl' ;;
aarch64) FLAGS_board='tael' ;;
*) die "Unknown target uname -m ${uname_m}" ;;
esac
if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
echo "Using board ${FLAGS_board} based on device architecture ${uname_m}." >&2
fi
fi
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 [[ ${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 [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
echo "Repacking the termina image..." >&2
fi
out_dir="$(mktemp -d)/${FLAGS_board}"
image_dir="${GCLIENT_ROOT}/src/build/images/${FLAGS_board}/"
if [[ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]]; then
image="${image_dir}/latest/chromiumos_test_image.bin"
else
image="${image_dir}/latest/chromiumos_base_image.bin"
fi
termina_build_image="${GCLIENT_ROOT}/src/platform/container-guest-tools/termina/termina_build_image.py"
sudo "${termina_build_image}" "${image}" "${out_dir}"
if [[ -n "${FLAGS_device_ip_addr}" ]]; then
if [[ ${FLAGS_verbose} -eq ${FLAGS_TRUE} ]]; then
echo "Copying image to device:" >&2
fi
rsync -a "${out_dir}" -e "ssh ${SSH_OPTS[*]} -p ${FLAGS_device_port} " \
"root@${FLAGS_device_ip_addr}:${STATEFUL_PARTITION}" \
|| die "Failed to copy to device."
sudo 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 "${SSH_OPTS[@]}" "-p" "${FLAGS_device_port}" \
"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 "$@"