From e770e2ad1bbc41f38aa9397989698ec29a065cc0 Mon Sep 17 00:00:00 2001 From: Marco Vedovati Date: Fri, 17 May 2019 19:42:12 +0200 Subject: [PATCH] rootfs: enforce minimum Go version when building locally When building locally (without Docker), the Go version installed on the system, needed to build the agent, must satisfy the minimum Go version requirement specified in runtime/versions.yaml. Signed-off-by: Marco Vedovati --- rootfs-builder/rootfs.sh | 56 +++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 55c41e8b89..fa8c68a682 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -250,11 +250,46 @@ detect_go_version() typeset -r yq=$(command -v yq || command -v ${GOPATH}/bin/yq) [ -z "$yq" ] && die "'yq' application not found (needed to parsing minimum Go version required)" - typeset -r runtimeVersion="${AGENT_VERSION:-master}" - typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeVersion}/versions.yaml" + local runtimeRevision= - GO_VERSION=$(curl -sSL "${runtimeVersionsURL}" | $yq r - languages.golang.version) - info "Detected Go version: $GO_VERSION" + if [ "${AGENT_VERSION:-master}" == "master" ]; then + # This matches both AGENT_VERSION == "" and AGENT_VERSION == "master" + runtimeRevision="master" + else + # Detect runtime revision by fetching the agent's VERSION file + runtimeRevision="$(curl -fsSL https://raw.githubusercontent.com/kata-containers/agent/${AGENT_VERSION:-master}/VERSION)" + [ -z "$runtimeRevision" ] && die "Could not detect the agent version for the given AGENT_VERSION='${AGENT_VERSION:-master}'" + fi + + typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeRevision}/versions.yaml" + GO_VERSION="$(curl -fsSL "$runtimeVersionsURL" | $yq r - "languages.golang.version")" + + [ "$?" == "0" ] && [ "$GO_VERSION" != "null" ] +} + +# Compares two SEMVER-style versions passed as arguments, up to the MINOR version +# number. +# Returns a zero exit code if the version specified by the first argument is +# older OR equal than / to the version in the second argument, non-zero exit +# code otherwise. +compare_versions() +{ + typeset -i -a v1=($(echo "$1" | awk 'BEGIN {FS = "."} {print $1" "$2}')) + typeset -i -a v2=($(echo "$2" | awk 'BEGIN {FS = "."} {print $1" "$2}')) + + # Sanity check: first version can't be all zero + [ "${v1[0]}" -eq "0" ] && \ + [ "${v1[1]}" -eq "0" ] && \ + die "Failed to parse version number" + + # Major + [ "${v1[0]}" -gt "${v2[0]}" ] && { false; return; } + + # Minor + [ "${v1[0]}" -eq "${v2[0]}" ] && \ + [ "${v1[1]}" -gt "${v2[1]}" ] && { false; return; } + + true } while getopts a:hlo:r:t: opt @@ -321,9 +356,18 @@ fi mkdir -p ${ROOTFS_DIR} -detect_go_version +detect_go_version || + die "Could not detect the required Go version for AGENT_VERSION='${AGENT_VERSION:-master}'." -if [ -n "${USE_DOCKER}" ] ; then +echo "Required Go version: $GO_VERSION" + +if [ -z "${USE_DOCKER}" ] ; then + #Generate an error if the local Go version is too old + foundVersion=$(go version | sed -E "s/^.+([0-9]+\.[0-9]+\.[0-9]+).*$/\1/g") + + compare_versions "$GO_VERSION" $foundVersion || \ + die "Your Go version $foundVersion is older than the minimum expected Go version $GO_VERSION" +else image_name="${distro}-rootfs-osbuilder" generate_dockerfile "${distro_config_dir}"