blob: f9277944000f4c3af2a128d66d6acabb7f53c1e7 [file] [log] [blame]
#!/usr/bin/env bash
# Copyright 2017 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.
set -eo pipefail; [[ "${TRACE}" ]] && set -x
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly CIPD_CLIENT_VERSION="${CIPD_CLIENT_VERSION:-$(cat ${SCRIPT_DIR}/.cipd_version)}"
readonly CIPD_CLIENT_URL="${CIPD_CLIENT_URL:-https://chrome-infra-packages.appspot.com}"
declare HOST_PLATFORM="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${HOST_PLATFORM}" in
linux)
readonly HOST_PLATFORM="linux"
;;
darwin)
readonly HOST_PLATFORM="mac"
;;
*)
echo "Unknown operating system."
exit 1
esac
HOST_ARCH="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${HOST_ARCH}" in
x86_64|amd64)
readonly HOST_ARCH="amd64"
;;
arm*)
readonly HOST_ARCH="${HOST_ARCH}"
;;
*86)
readonly HOST_ARCH=386
;;
*)
echo "Unknown machine architecture."
exit 1
esac
readonly URL="${CIPD_CLIENT_URL}/client?platform=${HOST_PLATFORM}-${HOST_ARCH}&version=${CIPD_CLIENT_VERSION}"
readonly CLIENT="${SCRIPT_DIR}/.cipd_client"
readonly USER_AGENT="buildtools/$(git -C ${SCRIPT_DIR} rev-parse HEAD 2>/dev/null || echo "???")"
# clean_bootstrap bootstraps the client from scratch using 'curl'.
function clean_bootstrap() {
echo "Bootstrapping cipd client for ${HOST_PLATFORM}-${HOST_ARCH}..."
local CLIENT_TMP="$(mktemp -p "${SCRIPT_DIR}" 2>/dev/null || mktemp "${SCRIPT_DIR}/.cipd_client.XXXXXXX")"
if hash curl 2> /dev/null ; then
curl -f --progress-bar "${URL}" -A "${USER_AGENT}" -L -o "${CLIENT_TMP}"
chmod +x "${CLIENT_TMP}"
else
echo "The \`curl\` command is missing, download ${URL} as ${CLIENT}, set +x, and then re-run this command" 1>&2
exit 1
fi
mv "${CLIENT_TMP}" "${CLIENT}"
}
# self_update asks the existing client to update itself, if necessary.
function self_update() {
"${CLIENT}" selfupdate -version "${CIPD_CLIENT_VERSION}" -service-url "${CIPD_CLIENT_URL}"
}
if [[ ! -x "${CLIENT}" ]]; then
clean_bootstrap
fi
export CIPD_HTTP_USER_AGENT_PREFIX="${USER_AGENT}"
if ! self_update ; then
echo -n "CIPD selfupdate failed. " 1>&2
echo "Trying to bootstrap the CIPD client from scratch... " 1>&2
clean_bootstrap
if ! self_update ; then # need to run it again to setup .cipd_version file
echo -n "Bootstrap from scratch failed, something is seriously broken " 1>&2
echo "run \`CIPD_HTTP_USER_AGENT_PREFIX=${USER_AGENT}/manual ${CLIENT} selfupdate -version '${CIPD_CLIENT_VERSION}'\` to diagnose if this is repeating." 1>&2
echo "" 1>&2
fi
fi
exec "${CLIENT}" "${@}"