| #!/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. |
| |
| ### register dev host as target's update source |
| |
| ## usage: fx add-update-source [--type=TYPE] [--name=NAME] [--disable-source=NAME] |
| ## |
| ## Configure target device to use a new update source. |
| ## |
| ## When --type is devhost (the default), configure the target to use the dev |
| ## host's address as seen from the target fuchsia device. |
| ## |
| ## When --type is localhost, configure the target to use 127.0.0.1 as its |
| ## update source. |
| ## |
| ## --name=NAME Name the generated update source config NAME. Defaults to the config type. |
| ## --disable-source=NAME Disable the update source with NAME after adding the new update source. |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh |
| fx-config-read |
| |
| function usage { |
| fx-command-help add-update-source |
| } |
| |
| function main { |
| fx-standard-switches "$@" |
| set -- "${FX_ARGV[@]}" |
| |
| config_type="devhost" |
| source_name="" |
| disable_source="" |
| while [[ $# -ne 0 ]]; do |
| case "$1" in |
| --type) |
| config_type="$2" |
| shift |
| ;; |
| --name) |
| source_name="$2" |
| shift |
| ;; |
| --disable-source) |
| disable_source="$2" |
| shift |
| ;; |
| --remove-localhost) |
| # Legacy option, equivalent to --disable-source=localhost |
| disable_source="localhost" |
| ;; |
| *) |
| echo >&2 "Unrecognized option: $1" |
| usage |
| exit 1 |
| esac |
| shift |
| done |
| |
| if [[ -z "${source_name}" ]]; then |
| source_name="${config_type}" |
| fi |
| |
| if ! [[ -x "$(command -v shasum)" ]]; then |
| echo >&2 "'shasum' binary not found" |
| exit 1 |
| fi |
| |
| local local_addr="$(fx-command-run netaddr --local "$(get-device-name)")" |
| if [[ $? -ne 0 || -z "${local_addr}" ]]; then |
| echo >&2 "Unable to determine host's IP. Is the target up?" |
| exit 1 |
| fi |
| # Strip interface name suffix. |
| local_addr="${local_addr%%%*}" |
| |
| repository_dir="${FUCHSIA_BUILD_DIR}/amber-files/repository" |
| if [[ ! -d "${repository_dir}" ]]; then |
| echo >&2 "Amber repository does not exist. Please build first." |
| exit 1 |
| fi |
| |
| config_url="http://[${local_addr}]:8083/config.json" |
| |
| if [[ "${config_type}" == "devhost" ]]; then |
| repo_url="http://[${local_addr}]:8083" |
| elif [[ "${config_type}" == "localhost" ]]; then |
| repo_url="http://127.0.0.1:8083" |
| else |
| echo >&2 "Unknown config type. Valid options: devhost, localhost" |
| exit 1 |
| fi |
| |
| fx-command-run shell $device_arg amber_ctl add_src \ |
| -n "${source_name}" \ |
| -f "${config_url}" |
| err=$? |
| |
| if [[ $err -ne 0 ]]; then |
| echo >&2 "Unable to register update source." |
| if [[ $err -eq 2 ]]; then |
| # The GET request failed. |
| echo >&2 " - Is 'fx serve' or 'fx serve-updates' running?" |
| echo >&2 " - Can the target reach the development host on tcp port 8083?" |
| fi |
| exit 1 |
| fi |
| |
| if [[ -n "${disable_source}" ]]; then |
| # Best effort, don't show status or fail on error |
| fx-command-run shell amber_ctl disable_src -n "${disable_source}" >/dev/null 2>/dev/null || true |
| fi |
| } |
| |
| main "$@" |