blob: 4b86f4ad775da610a475c8c298a312dc2810e8c2 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
### start the update server and attach to a running fuchsia device
## usage: fx serve-updates [-v] [-d <device>]
## -v verbose (do not suppress amber-srv output)
## -d <device> to specify a specific target device
##
## -v must come before -d.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh
fx-config-read
# Flag order restrictions exist because we're using the get-device-addr helper
# from vars.sh which needs -d to appear in $1 in order to work.
verbose=false
if [[ "$1" == "-v" ]]; then
verbose=true
shift
fi
# Recover from present situation by finding any already-running amber-srv
amber_srv_pid=$(pgrep amber-srv)
cleanup() {
if [[ -n "${amber_srv_pid}" ]]; then
if kill -0 "${amber_srv_pid}" 2> /dev/null; then
kill -TERM "${amber_srv_pid}" 2> /dev/null
wait "${amber_srv_pid}" 2> /dev/null
fi
fi
}
trap cleanup EXIT
log() {
# This format matches bootserver so that `fx serve` ui is easier to read.
echo "$(date '+%Y-%m-%d %H:%M:%S') [serve-updates] $@"
}
if [[ -z "${amber_srv_pid}" ]]; then
if [[ "${verbose}" = true ]]; then
${FUCHSIA_BUILD_DIR}/host_x64/amber-srv -d ${FUCHSIA_BUILD_DIR} &
else
${FUCHSIA_BUILD_DIR}/host_x64/amber-srv -d ${FUCHSIA_BUILD_DIR} > /dev/null &
fi
amber_srv_pid=$!
fi
# Allow a little slack for amber-srv to startup, that way the first kill -0 will catch a dead amber-srv.
sleep 0.1
if ! kill -0 "${amber_srv_pid}" 2> /dev/null; then
log "Amber Server died, exiting"
wait
exit $?
fi
device_addr=
update_device_addr() {
device_addr=$(get-device-addr "$@")
if [[ -z "${device_addr}" ]]; then
device_addr="$(fx-command-run netaddr --fuchsia 2>/dev/null)"
fi
}
# State is used to prevent too much output
state="discover"
while true; do
if ! kill -0 "${amber_srv_pid}" 2> /dev/null; then
log "Amber Server died, exiting"
amber_srv_pid=
exit 1
fi
update_device_addr "$@"
if [[ -z "${device_addr}" ]]; then
if [[ "${state}" != "discover1" ]]; then
log "No device found, waiting..."
state="discover1"
fi
sleep 1
continue
fi
if [[ "$state" == "discover" || "$state" == "discover1" ]]; then
log "SSH Tunnel connecting to ${device_addr}..."
if fx-command-run ssh -f -o LogLevel=ERROR -o ExitOnForwardFailure=yes -R 8083:localhost:8083 "${device_addr}" "echo -n"; then
if [[ "$state" != "ready" ]]; then
log "Ready to push packages!"
state="ready"
fi
fi
sleep 1
fi
if [[ "$state" == "ready" ]]; then
if fx-command-run ssh -O check "${device_addr}" 2> /dev/null; then
sleep 1
else
log "SSH Tunnel lost, restarting..."
state="discover"
fi
fi
done
# See EXIT trap above for cleanup that occurs