From 05428a64240714040537074371ec08364255ea51 Mon Sep 17 00:00:00 2001 From: Nitesh Konkar Date: Tue, 24 Sep 2019 23:31:45 +0530 Subject: [PATCH 1/2] rootfs: Install yq if not found instead of error When building rootfs, yq is needed to parse the version from versions.yaml file. If yq is not found, it fails. In this PR, we install yq if not found. Fixes: #363 Signed-off-by: Nitesh Konkar --- scripts/install-yq.sh | 72 +++++++++++++++++++++++++++++++++++++++++++ scripts/lib.sh | 5 ++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 scripts/install-yq.sh diff --git a/scripts/install-yq.sh b/scripts/install-yq.sh new file mode 100644 index 0000000000..f2bd8e6044 --- /dev/null +++ b/scripts/install-yq.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2019 IBM +# +# SPDX-License-Identifier: Apache-2.0 +# + +# If we fail for any reason a message will be displayed +die() { + msg="$*" + echo "ERROR: $msg" >&2 + exit 1 +} + +# Install the yq yaml query package from the mikefarah github repo +# Install via binary download, as we may not have golang installed at this point +function install_yq() { + GOPATH=${GOPATH:-${HOME}/go} + local yq_path="${GOPATH}/bin/yq" + local yq_pkg="github.com/mikefarah/yq" + [ -x "${GOPATH}/bin/yq" ] && return + + read -r -a sysInfo <<< "$(uname -sm)" + + case "${sysInfo[0]}" in + "Linux" | "Darwin") + goos="${sysInfo[0],}" + ;; + "*") + die "OS ${sysInfo[0]} not supported" + ;; + esac + + case "${sysInfo[1]}" in + "aarch64") + goarch=arm64 + ;; + "ppc64le") + goarch=ppc64le + ;; + "x86_64") + goarch=amd64 + ;; + "s390x") + goarch=s390x + ;; + "*") + die "Arch ${sysInfo[1]} not supported" + ;; + esac + + mkdir -p "${GOPATH}/bin" + + # Check curl + if ! command -v "curl" >/dev/null; then + die "Please install curl" + fi + + local yq_version=2.3.0 + + local yq_url="https://${yq_pkg}/releases/download/${yq_version}/yq_${goos}_${goarch}" + curl -o "${yq_path}" -LSsf ${yq_url} + [ $? -ne 0 ] && die "Download ${yq_url} failed" + chmod +x ${yq_path} + + if ! command -v "${yq_path}" >/dev/null; then + die "Cannot not get ${yq_path} executable" + fi +} + +install_yq + diff --git a/scripts/lib.sh b/scripts/lib.sh index 0691563d94..14be92cb3d 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -12,6 +12,7 @@ GO_RUNTIME_PKG=${GO_RUNTIME_PKG:-github.com/kata-containers/runtime} # Give preference to variable set by CI KATA_BRANCH=${branch:-} KATA_BRANCH=${KATA_BRANCH:-master} +yq_file="${script_dir}/../scripts/install-yq.sh" error() { @@ -273,7 +274,9 @@ detect_go_version() { info "Detecting agent 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)" + if [ -z "$yq" ]; then + source "$yq_file" + fi local runtimeRevision="" From 5982e487749e4264ca0c550eaf02d4a5b90e44f1 Mon Sep 17 00:00:00 2001 From: Nitesh Konkar Date: Mon, 23 Sep 2019 18:46:02 +0530 Subject: [PATCH 2/2] lib.sh: Fix curl error when using curl+yq When you curl versions.yaml file and pipe into yq, sometimes the piped program closes the read pipe before the previous program is finished leading to "curl: (23) Failed writing body (1337 != 1371)". As a workaround we pipe the stream through double "tac", an intermediary program that always reads the whole page before feeding it to the next program. Fixes: #363 Signed-off-by: Nitesh Konkar --- scripts/lib.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/lib.sh b/scripts/lib.sh index 14be92cb3d..218eef5515 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -289,7 +289,7 @@ detect_go_version() typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeRevision}/versions.yaml" info "Getting golang version from ${runtimeVersionsURL}" # This may fail if we are a kata bump. - if GO_VERSION="$(curl -fsSL "$runtimeVersionsURL" | $yq r - "languages.golang.version")"; then + if GO_VERSION="$(curl -fsSL "$runtimeVersionsURL" | tac | tac | $yq r - "languages.golang.version")"; then [ "$GO_VERSION" != "null" ] return 0 fi @@ -301,7 +301,7 @@ detect_go_version() info "There is not runtime repository in filesystem (${kata_runtime_pkg_dir})" local runtime_versions_url="https://raw.githubusercontent.com/kata-containers/runtime/${KATA_BRANCH}/versions.yaml" info "Get versions file from ${runtime_versions_url}" - GO_VERSION="$(curl -fsSL "${runtime_versions_url}" | $yq r - "languages.golang.version")" + GO_VERSION="$(curl -fsSL "${runtime_versions_url}" | tac | tac | $yq r - "languages.golang.version")" if [ "$?" == "0" ] && [ "$GO_VERSION" != "null" ]; then return 0 fi