blob: 47849544b8c20dee266bfec7e0b2c249d18c2ddb [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.
#### CATEGORY=Software delivery
### register dev host as target's update source
## usage: fx add-update-source [--addr ADDR] [--port PORT] [--name NAME]
##
## Configure target device to use a new update source.
##
## --name NAME Name the generated update source config NAME.
## --addr ADDR Specify the package server address explicitly.
## --port PORT The port at ADDR on which the package server is running (default: 8083).
##
## ADDR:
## Any IPv4 / IPv6 address or publicly reachable DNS name.
## Note: ADDR must be the address as-routable by the target device.
## If an link-local IPv6 address is provided, it must include a
## valid scope for the target (e.g. %ethp0014).
## If no ADDR is provided, the default address is assumed to be the
## address of the host machine as seen by the target from an SSH
## connection to the target device.
##
## NAME:
## If no name is supplied, the name defaults to "devhost", otherwise
## it contains the address given.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
fx-config-read
function usage {
fx-command-help add-update-source
}
function main {
fx-standard-switches "$@"
set -- "${FX_ARGV[@]}"
addr=""
port="8083"
source_name="devhost"
while [[ $# -ne 0 ]]; do
case "$1" in
--addr)
addr="$2"
if [[ "${source_name}" == "devhost" ]]; then
source_name="${addr}"
fi
shift
;;
--port)
port="$2"
shift
;;
--name)
source_name="$2"
shift
;;
*)
fx-error "Unrecognized option: $1"
usage
exit 1
esac
shift
done
if [[ -z "${addr}" ]]; then
addr=$(fx-command-run shell 'echo $SSH_CONNECTION' | cut -d ' ' -f 1)
if [[ $? -ne 0 || -z "${addr}" ]]; then
fx-error "unable to determine host address as seen from the target. Is the target up?"
exit 1
fi
fi
addr="$(echo "${addr}" | sed 's/%/%25/g')"
# A poor mans heuristic for "is an ipv6 address", URL encase escape
# the address.
if [[ "${addr}" =~ : ]]; then
addr="[${addr}]"
fi
config_url="http://${addr}:${port}/config.json"
fx-command-run shell amberctl add_src \
-n "${source_name}" \
-f "${config_url}"
err=$?
if [[ $err -ne 0 ]]; then
fx-error "Unable to register update source."
if [[ $err -eq 2 ]]; then
# The GET request failed.
fx-error " - Is 'fx serve' or 'fx serve-updates' running?"
fx-error " - Can the target reach ${config_url} ?"
fi
return "$err"
fi
}
main "$@"