blob: 887cce65bbb2f846bc3b90657b6a07482dd372c7 [file] [log] [blame]
#!/bin/bash
# Copyright 2025 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=Build
### Manage build ResultStore configuration. ResultStore is a service
### for publishing and sharing build and test results (not artifacts).
### Fuchsia's results can be found at go/fxbtx (ResultStore UI).
## usage:
## fx resultstore status
## Show if ResultStore uploading during fx build is enabled or disabled.
##
## fx resultstore enable
## Enable ResultStore uploading during fx build.
##
## fx resultstore disable
## Disable ResultStore uploading during fx build.
##
## ResultStore data will contain information about the users's environment,
## including $USER name, and working directory.
## ResultStore data however, is only visible internally to Googlers.
set -e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
source "${FUCHSIA_DIR}/tools/devshell/lib/style.sh" || exit $?
readonly RESULTSTORE_CONFIG="${FUCHSIA_DIR}/.fx/config/resultstore"
readonly rsproxy_wrap="${FUCHSIA_DIR}/prebuilt/rsclient/${HOST_PLATFORM}/bin/rsproxy-wrap.sh"
function usage {
fx-command-help
}
function styled-status {
local enabled="$1"
local resultstore_status
if [[ "$RESULTSTORE_ENABLED" -eq 1 ]]; then
resultstore_status="enabled"
color="green"
else
resultstore_status="disabled"
color="red"
fi
style::echo --bold "--${color}" "${resultstore_status}"
}
fx-standard-switches "$@"
set -- "${FX_ARGV[@]}"
if [[ $# -lt 1 ]]; then
fx-error Invalid syntax
fx-command-help
exit 1
fi
action="$1"
shift
action_args=("$@")
function resultstore-read-config {
RESULTSTORE_ENABLED=0
if [[ ! -f "${RESULTSTORE_CONFIG}" ]]; then
return 1
fi
source "${RESULTSTORE_CONFIG}"
return 0
}
function resultstore-write-config {
local enabled="$1"
local -r tempfile="${RESULTSTORE_CONFIG}.tmp"
cat > "${tempfile}" <<EOF
# Autogenerated config file for 'fx resultstore'.
# Run 'fx help resultstore' for more information.
RESULTSTORE_ENABLED=${enabled}
EOF
# Only rewrite the config file if content has changed
if ! cmp --silent "${tempfile}" "${RESULTSTORE_CONFIG}"
then mv -f "${tempfile}" "${RESULTSTORE_CONFIG}"
else rm -f "${tempfile}"
fi
}
# Read config in a way that exit code is preserved but doesn't exit because
# of "set -e" if config file doesn't exist
__successfully_read_config=0
resultstore-read-config || __successfully_read_config="$?"
case "$action" in
-h|--help)
usage
exit 0
;;
status)
echo "ResultStore uploading during builds is currently $(styled-status "$RESULTSTORE_ENABLED") for ${FUCHSIA_DIR}"
echo "To change it, run: fx resultstore <enable|disable>"
exit 0
;;
enable)
if [[ ! -x "${rsproxy_wrap}" ]]; then
fx-warn "ResultStore cannot be enabled because the 'rsclient' prebuilt package is missing."
fx-warn "Please run 'jiri sync' to download the required prebuilts."
exit 1
fi
if [[ "$RESULTSTORE_ENABLED" -eq 1 ]]; then
echo "ResultStore uploading during build is already enabled."
exit 0
fi
RESULTSTORE_ENABLED=1
resultstore-write-config "${RESULTSTORE_ENABLED}"
echo "ResultStore uploading during build is enabled at user's request"
;;
disable)
if [[ "$RESULTSTORE_ENABLED" -eq 0 ]]; then
if [[ "${__successfully_read_config}" -ne 0 ]]; then
# if the feature is already disabled but the config file does not exist
# or is inconsistent, we rewrite the file before exiting
resultstore-write-config "${RESULTSTORE_ENABLED}"
fi
echo "ResultStore uploading during build is already disabled."
exit 0
fi
RESULTSTORE_ENABLED=0
resultstore-write-config "${RESULTSTORE_ENABLED}"
echo "ResultStore uploading during build is disabled at user's request"
;;
*)
fx-error Invalid syntax
fx-command-help
exit 1
;;
esac
echo "ResultStore uploading during build is now $(styled-status "$RESULTSTORE_ENABLED") for ${FUCHSIA_DIR}"