blob: 486e7f343eccf4e6b5985f4ad5237c2652b23403 [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]
## -v verbose (do not suppress `pm serve` output)
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh
fx-config-read
function usage {
fx-command-help serve-updates
}
fx-standard-switches "$@"
set -- "${FX_ARGV[@]}"
verbose=false
very_verbose=false
while (($#)); do
case "$1" in
-v|--verbose)
if $verbose; then
very_verbose=true
else
verbose=true
fi
;;
-vv)
verbose=true
very_verbose=true
;;
*)
echo "Unrecognized option: $1"
usage
exit 1
;;
esac
shift
done
pm_srv_pid=
cleanup() {
if [[ -n "${pm_srv_pid}" ]]; then
if kill -0 "${pm_srv_pid}" 2> /dev/null; then
kill -TERM "${pm_srv_pid}" 2> /dev/null
wait "${pm_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] $@"
}
log_verbose() {
if [[ "$very_verbose" == true ]]; then
log "$@"
fi
}
if [[ -z "${pm_srv_pid}" ]]; then
serve_flags=()
if [[ "${verbose}" != true ]]; then
serve_flags+=("-q")
fi
${FUCHSIA_BUILD_DIR}/host_x64/pm serve -d ${FUCHSIA_BUILD_DIR}/amber-files/repository ${serve_flags[@]} &
pm_srv_pid=$!
fi
# Allow a little slack for pm serve to startup, that way the first kill -0 will catch a dead server
sleep 0.1
if ! kill -0 "${pm_srv_pid}" 2> /dev/null; then
log "Server died, exiting"
wait
exit $?
fi
device_addr=$(get-fuchsia-device-addr)
update_device_addr() {
device_addr="$(get-fuchsia-device-addr 2>/dev/null)"
}
if [[ "$(uname -s)" = "Darwin" ]]; then
ping_device() {
# Darwin's "ping" doesn't recognize IPv6 addresses and doesn't accept a
# timeout parameter, which appears to default to 10 seconds.
ping6 -c 1 "$1" >/dev/null
}
else
ping_device() {
ping -c 1 -W 1 "$1" >/dev/null
}
fi
# State is used to prevent too much output
state="discover"
while true; do
if ! kill -0 "${pm_srv_pid}" 2> /dev/null; then
log "Server died, exiting"
pm_srv_pid=
exit 1
fi
if [[ "$state" == "discover" || "$state" == "discover1" ]]; then
log_verbose "Updating device address (direct) with \"$device\""
update_device_addr
if [[ -z "${device_addr}" ]]; then
if [[ "${state}" != "discover1" ]]; then
log "No device found, waiting..."
state="discover1"
fi
sleep 1
continue
fi
log_verbose "Ping $device_addr"
if ping_device $device_addr; then
log "Device up"
state="config"
fi
sleep 1
fi
if [[ "$state" == "config" ]]; then
log "Registering devhost as update source"
if fx-command-run add-update-source --disable-source=localhost $device_arg; then
log "Ready to push packages!"
state="ready"
else
log "Device lost while configuring update source"
state="discover"
fi
sleep 1
fi
if [[ "$state" == "ready" ]]; then
log_verbose "Ping $device_addr"
if ping_device $device_addr; then
sleep 1
else
log "Device lost"
state="discover"
fi
fi
done
# See EXIT trap above for cleanup that occurs