| #!/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). | |
| ## --[no-]persist Enable or disable persistence of repo metadata. Disabled by default. | |
| ## | |
| ## 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 $? | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/updates.sh || exit $? | |
| fx-config-read | |
| function usage { | |
| fx-command-help add-update-source | |
| } | |
| function main { | |
| fx-standard-switches "$@" | |
| set -- "${FX_ARGV[@]}" | |
| addr="" | |
| port="" | |
| source_name="" | |
| persist="" | |
| while [[ $# -ne 0 ]]; do | |
| case "$1" in | |
| --addr) | |
| addr="$2" | |
| shift | |
| ;; | |
| --port) | |
| port="$2" | |
| shift | |
| ;; | |
| --name) | |
| source_name="$2" | |
| shift | |
| ;; | |
| --no-persist) | |
| ;; | |
| --persist) | |
| persist="-p" | |
| ;; | |
| *) | |
| fx-error "Unrecognized option: $1" | |
| usage | |
| exit 1 | |
| esac | |
| shift | |
| done | |
| if is_feature_enabled "legacy_serve"; then | |
| if [[ -z "${port}" ]]; then | |
| port="8083" | |
| fi | |
| if [[ -z "${source_name}" ]]; then | |
| if [[ -z "${addr}" ]]; then | |
| source_name="devhost" | |
| else | |
| source_name="${addr}" | |
| fi | |
| fi | |
| 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" | |
| add_repo_args=(-n "${source_name}") | |
| if [[ ! -z "${persist}" ]]; then | |
| add_repo_args+=( "${persist}" ) | |
| fi | |
| add_repo_args+=( "${config_url}" ) | |
| fx-command-run shell pkgctl repo add url "${add_repo_args[@]}" | |
| 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 | |
| rule="'{ | |
| \"version\": \"1\", | |
| \"content\": [ | |
| { | |
| \"host_match\": \"fuchsia.com\", | |
| \"host_replacement\": \"${source_name}\", | |
| \"path_prefix_match\": \"/\", | |
| \"path_prefix_replacement\": \"/\" | |
| } | |
| ] | |
| }'" | |
| fx-command-run shell pkgctl rule replace json $rule | |
| err=$? | |
| if [[ $err -ne 0 ]]; then | |
| fx-error "The repo has been set up, but there was an error setting the rewriting rule for ${source_name}: $rule" | |
| fx-error "To remove the repo, run \"fx shell pkgctl repo rm fuchsia-pkg://${source_name}\"." | |
| return $err | |
| fi | |
| else | |
| if [[ -z "${port}" ]]; then | |
| port="8083" | |
| fi | |
| actual_addr=$(fx-command-run ffx config get repository.server.listen | sed 's/\"//g') | |
| err=$? | |
| if [[ $err -ne 0 ]]; then | |
| fx-error "Unable to get the configured repository server address." | |
| return "$err" | |
| fi | |
| if [[ "$actual_addr" = "null" ]]; then | |
| fx-error "Repository server is currently disabled. To re-enable, run:" | |
| fx-error "$ ffx config set repository.server.listen \"${config_ip}:${port}\" && ffx doctor --restart-daemon" | |
| return 1 | |
| fi | |
| [[ $actual_addr =~ (.*):[0-9]+ ]] | |
| actual_ip=${BASH_REMATCH[1]} | |
| if [[ ! -z "${addr}" ]]; then | |
| if [[ $addr =~ : ]]; then | |
| expected_addr="[${addr}]:${port}" | |
| else | |
| expected_addr="${addr}:${port}" | |
| fi | |
| else | |
| expected_addr="${actual_ip}:${port}" | |
| fi | |
| if [[ "$expected_addr" != "$actual_addr" ]]; then | |
| fx-error "The repository server is configured to use \"${actual_addr}\", not \"${expected_addr}\"" | |
| fx-error "To switch to a different address, run:" | |
| fx-error "$ ffx config set repository.server.listen \"${expected_addr}\" && ffx doctor --restart-daemon" | |
| return 1 | |
| fi | |
| # Use the build directory's name by default. Note that package URLs are not | |
| # allowed to have underscores, so replace them with hyphens. | |
| if [[ -z "${source_name}" ]]; then | |
| source_name="$(ffx-default-repository-name)" | |
| fi | |
| register_args=() | |
| if [[ -z "{persist}" ]]; then | |
| register_args+=( --storage-type persistent ) | |
| fi | |
| ffx-register-repository "$source_name" "${register_args[@]}" || return $? | |
| fi | |
| } | |
| main "$@" |