| #!/bin/bash |
| # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| GS_MIRROR_URL='gs://chromeos-localmirror' |
| HTTP_MIRROR_URL='http://commondatastorage.googleapis.com/chromeos-localmirror' |
| BUNDLE_PATH='distfiles/chameleon-bundle' |
| GS_BUNDLE_URL="${GS_MIRROR_URL}/${BUNDLE_PATH}" |
| HTTP_BUNDLE_URL="${HTTP_MIRROR_URL}/${BUNDLE_PATH}" |
| LATEST='LATEST' |
| bundle_path='' |
| bundle_name='' |
| |
| die() { |
| if [[ -n "$1" ]]; then |
| echo "ERROR: $1" >&2 |
| fi |
| echo "Usage: $0 PATH_TO_BUNDLE" >&2 |
| exit 1 |
| } |
| |
| # If the bundle name is wbs_xxxx, let LATEST represent 'LATEST_WBS'. |
| # Otherwise, LATEST represents 'LATEST' for a chameleon bundle. |
| update_var_LATEST() { |
| [[ ${bundle_name} == wbs* ]] && LATEST='LATEST_WBS' |
| echo "The LATEST file is ${LATEST}" |
| } |
| |
| # Check the existence of the bundle and update LATEST if it is a wbs bundle. |
| check_bundle() { |
| [[ -f "${bundle_path}" ]] || die 'Invalid bundle file' |
| bundle_name="$(basename ${bundle_path})" |
| |
| # Update the LATEST variable if the bundle is a wbs bundle. |
| update_var_LATEST |
| } |
| |
| # Upload the bundle and enable public-read access. |
| upload_bundle() { |
| gsutil cp -a public-read "${bundle_path}" "${GS_BUNDLE_URL}" |
| wget --spider -q "${HTTP_BUNDLE_URL}/${bundle_name}" || |
| die "Failed to access ${bundle_name} from GS" |
| } |
| |
| # Update the LATEST file pointing to the bundle and enable public-read access. |
| update_cloud_LATEST() { |
| local latest_file="$(mktemp ${LATEST}.XXXXX)" |
| echo "${bundle_name}" > "${latest_file}" |
| gsutil cp -a public-read "${latest_file}" "${GS_BUNDLE_URL}/${LATEST}" |
| rm -f "${latest_file}" |
| |
| wget --spider -q "${HTTP_BUNDLE_URL}/${LATEST}" || |
| die "Failed to access ${LATEST} file from GS" |
| |
| local latest_bundle=$(wget -qO - "${HTTP_BUNDLE_URL}/${LATEST}") |
| [[ "${latest_bundle}" = "${bundle_name}" ]] || |
| die "Failed to update ${LATEST} file" |
| echo "The ${LATEST} is updated to ${latest_bundle}" |
| } |
| |
| # Get the latest WBS bundle name. |
| get_latest_wbs_bundle_name() { |
| local latest_bundle=$(wget -qO - "${HTTP_BUNDLE_URL}/LATEST_WBS") |
| echo "${latest_bundle}" |
| } |
| |
| # Download the specified file $1 to the path $2. |
| download_cloud_file() { |
| wget -q -P "$2" "${HTTP_BUNDLE_URL}/$1" || |
| die "Failed to access $1 file from GS" |
| } |