| #!/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 [--type=TYPE] [--name=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. | |
| 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[@]}" | |
| config_type="devhost" | |
| port="8083" | |
| source_name="" | |
| while [[ $# -ne 0 ]]; do | |
| case "$1" in | |
| --port) | |
| port="$2" | |
| shift | |
| ;; | |
| --type) | |
| config_type="$2" | |
| shift | |
| ;; | |
| --name) | |
| source_name="$2" | |
| shift | |
| ;; | |
| *) | |
| echo >&2 "Unrecognized option: $1" | |
| usage | |
| exit 1 | |
| esac | |
| shift | |
| done | |
| if [[ -z "${source_name}" ]]; then | |
| source_name="${config_type}" | |
| fi | |
| local local_addr="$(fx-command-run shell 'echo $SSH_CONNECTION' | cut -d ' ' -f 1 | sed 's/%/%25/g')" | |
| if [[ $? -ne 0 || -z "${local_addr}" ]]; then | |
| echo >&2 "Unable to determine target's view of amber address. Is the target up?" | |
| exit 1 | |
| fi | |
| 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}]:${port}/config.json" | |
| if [[ "${config_type}" == "devhost" ]]; then | |
| repo_url="http://[${local_addr}]:${port}" | |
| elif [[ "${config_type}" == "localhost" ]]; then | |
| repo_url="http://127.0.0.1:${port}" | |
| else | |
| echo >&2 "Unknown config type. Valid options: devhost, localhost" | |
| exit 1 | |
| fi | |
| fx-command-run shell amberctl 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 | |
| return "$err" | |
| fi | |
| } | |
| main "$@" |