| #!/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>] [--direct] |
| ## -v verbose (do not suppress amber-srv output) |
| ## -d <device> to specify a specific target device |
| ## --no-direct configure target to use an SSH tunnel as transport (deprecated) |
| |
| 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 |
| device="" |
| device_arg="" |
| direct=true |
| while (($#)); do |
| case "$1" in |
| -v|--verbose) |
| verbose=true |
| ;; |
| -d|--device) |
| device="$2" |
| device_arg="-d $2" |
| shift |
| ;; |
| --no-direct) |
| direct=false |
| ;; |
| --direct) |
| direct=true |
| ;; |
| *) |
| echo "Unrecognized option: $1" |
| usage |
| exit 1 |
| ;; |
| esac |
| shift |
| done |
| |
| # 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="$(fx-command-run netaddr --nowait --timeout=1000 --fuchsia "$@" 2>/dev/null)" |
| } |
| |
| # 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 $device_arg |
| 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 |
| log "SSH Tunnel connected" |
| state="config" |
| else |
| sleep 1 |
| fi |
| fi |
| |
| if [[ "$state" == "config" ]]; then |
| if $direct; then |
| log "Registering devhost as update source" |
| if fx-command-run add-update-source --remove-localhost $device_arg; then |
| log "Ready to push packages!" |
| state="ready" |
| fi |
| else |
| log "Ready to push packages!" |
| state="ready" |
| 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 |