From 59ef0901d355c52ce2b7d1370bfff24d2cfe0c32 Mon Sep 17 00:00:00 2001 From: Michael Marineau Date: Sat, 23 Nov 2013 20:31:46 -0800 Subject: [PATCH] add(sdk_util.sh): Port basic SDK download logic to a shell library. The current logic for downloading SDK tarballs is in cros_sdk and written in python which isn't super convenient for re-using in the rest of our shell scripts. This is a start of rewriting that logic into a re-usable library but does not yet replace the functionality in cros_sdk. --- sdk_lib/sdk_util.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 sdk_lib/sdk_util.sh diff --git a/sdk_lib/sdk_util.sh b/sdk_lib/sdk_util.sh new file mode 100644 index 0000000000..401b818d52 --- /dev/null +++ b/sdk_lib/sdk_util.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Copyright (c) 2013 The CoreOS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# common.sh must be properly sourced before this file. +[[ -n "${COREOS_SDK_VERSION}" ]] || exit 1 + +COREOS_SDK_ARCH="amd64" # We are unlikely to support anything else. +COREOS_SDK_TARBALL="coreos-sdk-${COREOS_SDK_ARCH}-${COREOS_SDK_VERSION}.tar.bz2" +COREOS_SDK_TARBALL_CACHE="${REPO_CACHE_DIR}/sdks" +COREOS_SDK_TARBALL_PATH="${COREOS_SDK_TARBALL_CACHE}/${COREOS_SDK_TARBALL}" +COREOS_SDK_URL_PREFIX="http://storage.core-os.net/coreos/sdk" +COREOS_SDK_URL="${COREOS_SDK_URL_PREFIX}/${COREOS_SDK_ARCH}/${COREOS_SDK_VERSION}/${COREOS_SDK_TARBALL}" + +# Download the current SDK tarball (if required) and verify digests/sig +sdk_download_tarball() { + if sdk_verify_digests; then + return 0 + fi + + info "Downloading ${COREOS_SDK_TARBALL}" + info "URL: ${COREOS_SDK_URL}" + local suffix + for suffix in "" ".DIGESTS"; do # TODO(marineam): download .asc + wget --tries=3 --timeout=30 --continue \ + -O "${COREOS_SDK_TARBALL_PATH}${suffix}" \ + "${COREOS_SDK_URL}${suffix}" \ + || die_notrace "SDK download failed!" + done + + sdk_verify_digests || die_notrace "SDK digest verification failed!" + sdk_clean_cache +} + +sdk_verify_digests() { + if [[ ! -f "${COREOS_SDK_TARBALL_PATH}" || \ + ! -f "${COREOS_SDK_TARBALL_PATH}.DIGESTS" ]]; then + return 1 + fi + + # TODO(marineam): Add gpg signature verification too. + + verify_digests "${COREOS_SDK_TARBALL_PATH}" || return 1 +} + +sdk_clean_cache() { + pushd "${COREOS_SDK_TARBALL_CACHE}" >/dev/null + local filename + for filename in *; do + if [[ "${filename}" == "${COREOS_SDK_TARBALL}"* ]]; then + continue + fi + info "Cleaning up ${filename}" + # Not a big deal if this fails + rm -f "${filename}" || true + done + popd >/dev/null +}