diff --git a/tools/packaging/static-build/cache_components.sh b/tools/packaging/static-build/cache_components.sh index 6cd25ec5ab..3310501d3b 100755 --- a/tools/packaging/static-build/cache_components.sh +++ b/tools/packaging/static-build/cache_components.sh @@ -11,34 +11,101 @@ set -o pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${script_dir}/../scripts/lib.sh" -source "${script_dir}/qemu/build-static-qemu-cc.sh" export KATA_BUILD_CC="${KATA_BUILD_CC:-}" export qemu_cc_tarball_name="kata-static-qemu-cc.tar.gz" cache_qemu_artifacts() { + source "${script_dir}/qemu/build-static-qemu-cc.sh" local current_qemu_version=$(get_from_kata_deps "assets.hypervisor.qemu.version") - create_qemu_cache_asset "${qemu_cc_tarball_name}" "${current_qemu_version}" + create_cache_asset "${qemu_cc_tarball_name}" "${current_qemu_version}" local qemu_sha=$(calc_qemu_files_sha256sum) echo "${current_qemu_version} ${qemu_sha}" > "latest" } -create_qemu_cache_asset() { +cache_clh_artifacts() { + local binary="cloud-hypervisor" + local binary_path="$(echo $script_dir | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,')" + echo "binary path $binary_path" + local current_cloud_hypervisor_version=$(get_from_kata_deps "assets.hypervisor.cloud_hypervisor.version") + local clh_binary_path="${binary_path}/tools/packaging/kata-deploy/local-build/build/cc-cloud-hypervisor/builddir/cloud-hypervisor" + if [ -f "${clh_binary_path}/cloud-hypervisor" ]; then + cp "${clh_binary_path}/${binary}" . + else + cloud_hypervisor_build_path="${binary_path}/cloud-hypervisor" + cp "${cloud_hypervisor_build_path}/${binary}" . + fi + create_cache_asset "${binary}" "${current_cloud_hypervisor_version}" + echo "${current_cloud_hypervisor_version}" > "latest" +} + +create_cache_asset() { local component_name="$1" local component_version="$2" - local qemu_cc_tarball_path=$(sudo find / -iname "${qemu_cc_tarball_name}") - info "qemu cc tarball_path ${qemu_cc_tarball_path}" - cp -a "${qemu_cc_tarball_path}" . + local verify_qemu=$(echo "${component_name}" | grep qemu || true) + local verify_clh=$(echo "${component_name}" | grep cloud || true) + + if [ ! -z "${verify_qemu}" ]; then + local qemu_cc_tarball_path=$(sudo find / -iname "${qemu_cc_tarball_name}") + info "qemu cc tarball_path ${qemu_cc_tarball_path}" + cp -a "${qemu_cc_tarball_path}" . + fi + sudo chown -R "${USER}:${USER}" . sha256sum "${component_name}" > "sha256sum-${component_name}" cat "sha256sum-${component_name}" } +help() { +echo "$(cat << EOF +Usage: $0 "[options]" + Description: + Builds the cache of several kata components. + Options: + -c Cloud hypervisor cache + -q Qemu cache + -h Shows help +EOF +)" +} + main() { + local cloud_hypervisor_component="${cloud_hypervisor_component:-}" + local qemu_component="${qemu_component:-}" + local OPTIND + while getopts ":cqh:" opt + do + case "$opt" in + c) + cloud_hypervisor_component="1" + ;; + q) + qemu_component="1" + ;; + h) + help + exit 0; + ;; + :) + echo "Missing argument for -$OPTARG"; + help + exit 1; + ;; + esac + done + shift $((OPTIND-1)) + + [[ -z "${cloud_hypervisor_component}" ]] && \ + [[ -z "${qemu_component}" ]] && \ + help && die "Must choose at least one option" + mkdir -p "${WORKSPACE}/artifacts" pushd "${WORKSPACE}/artifacts" echo "Artifacts:" - cache_qemu_artifacts + + [ "${cloud_hypervisor_component}" == "1" ] && cache_clh_artifacts + [ "${qemu_component}" == "1" ] && cache_qemu_artifacts + ls -la "${WORKSPACE}/artifacts/" popd sync diff --git a/tools/packaging/static-build/cloud-hypervisor/build-static-clh.sh b/tools/packaging/static-build/cloud-hypervisor/build-static-clh.sh index 0bee1ea041..1fda017402 100755 --- a/tools/packaging/static-build/cloud-hypervisor/build-static-clh.sh +++ b/tools/packaging/static-build/cloud-hypervisor/build-static-clh.sh @@ -23,6 +23,7 @@ cloud_hypervisor_repo="${cloud_hypervisor_repo:-}" cloud_hypervisor_version="${cloud_hypervisor_version:-}" cloud_hypervisor_pr="${cloud_hypervisor_pr:-}" cloud_hypervisor_pull_ref_branch="${cloud_hypervisor_pull_ref_branch:-main}" +cloud_hypervisor_latest_build_url="${jenkins_url}/job/kata-containers-2.0-clh-cc-$(uname -m)/${cached_artifacts_path}" if [ -z "$cloud_hypervisor_repo" ]; then info "Get cloud_hypervisor information from runtime versions.yaml" @@ -82,6 +83,40 @@ build_clh_from_source() { popd } +check_cached_cloud_hypervisor() { + local cached_cloud_hypervisor_version=$(curl -sfL "${cloud_hypervisor_latest_build_url}"/latest) || latest="none" + info "Current cloud hypervisor version: ${cloud_hypervisor_version}" + info "Cached cloud hypervisor version: ${cached_cloud_hypervisor_version}" + if [ "${cloud_hypervisor_version}" == "${cached_cloud_hypervisor_version}" ] && [ "${ARCH}" == "x86_64" ]; then + install_cached_cloud_hypervisor + else + build_clh_from_source + fi +} + +install_cached_cloud_hypervisor() { + local cached_path="$(echo ${script_dir} | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,')" + local clh_directory="${cached_path}/tools/packaging/kata-deploy/local-build/build/cc-cloud-hypervisor/builddir/cloud-hypervisor" + mkdir cloud-hypervisor + pushd cloud-hypervisor + local checksum_file="sha256sum-cloud-hypervisor" + info "Downloading the cloud hypervisor binary" + curl -fOL --progress-bar "${cloud_hypervisor_latest_build_url}/cloud-hypervisor" || return 1 + info "Checking cloud hypervisor binary checksum" + curl -fOL --progress-bar "${cloud_hypervisor_latest_build_url}/${checksum_file}" || return 1 + info "Verify checksum" + sudo sha256sum -c "${checksum_file}" || return 1 + chmod +x cloud-hypervisor + local clh_binary_path="${cached_path}/cloud-hypervisor" + if [ ! -d "${clh_binary_path}" ]; then + mkdir -p "${clh_binary_path}" + fi + if [ ! -f "${clh_binary_path}/cloud-hypervisor" ]; then + cp cloud-hypervisor "${clh_binary_path}" + fi + popd +} + if [ "${ARCH}" == "aarch64" ]; then info "aarch64 binaries are not distributed as part of the Cloud Hypervisor releases, forcing to build from source" force_build_from_source="true" @@ -94,8 +129,8 @@ fi if [ "${force_build_from_source}" == "true" ]; then info "Build cloud-hypervisor from source as it's been request via the force_build_from_source flag" - build_clh_from_source + check_cached_cloud_hypervisor else pull_clh_released_binary || - (info "Failed to pull cloud-hypervisor released binary, trying to build from source" && build_clh_from_source) + (info "Failed to pull cloud-hypervisor released binary, trying to build from source" && check_cached_cloud_hypervisor) fi