Use sudo only when not already root

pull/58/head
Jeff Grafton 2018-10-15 12:42:08 -07:00
parent 9e4fa1493d
commit a88244598a
4 changed files with 14 additions and 4 deletions

View File

@ -27,6 +27,8 @@ ALL_ARCH = amd64 arm arm64 ppc64le s390x
TEMP_DIR:=$(shell mktemp -d)
QEMUVERSION=v2.9.1
SUDO=$(if $(filter 0,$(shell id -u)),,sudo)
# This option is for running docker manifest command
export DOCKER_CLI_EXPERIMENTAL := enabled
@ -80,7 +82,7 @@ ifeq ($(ARCH),amd64)
else
# When cross-building, only the placeholder "CROSS_BUILD_" should be removed
# Register /usr/bin/qemu-ARCH-static as the handler for non-x86 binaries in the kernel
sudo ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
$(SUDO) ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
curl -sSL https://github.com/multiarch/qemu-user-static/releases/download/$(QEMUVERSION)/x86_64_qemu-$(QEMUARCH)-static.tar.gz | tar -xz -C $(TEMP_DIR)
# Ensure we don't get surprised by umask settings
chmod 0755 $(TEMP_DIR)/qemu-$(QEMUARCH)-static

View File

@ -33,6 +33,8 @@ CNI_TARBALL=cni-plugins-$(ARCH)-$(CNI_VERSION).tgz
# This option is for running docker manifest command
export DOCKER_CLI_EXPERIMENTAL := enabled
SUDO=$(if $(filter 0,$(shell id -u)),,sudo)
.PHONY: all build push clean all-build all-push-images all-push push-manifest
all: all-push
@ -74,7 +76,7 @@ endif
ifneq ($(ARCH),amd64)
# Register /usr/bin/qemu-ARCH-static as the handler for non-x86 binaries in the kernel
sudo ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
$(SUDO) ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
endif
docker build --pull -t $(IMAGE)-$(ARCH):$(TAG) $(TEMP_DIR)
rm -rf $(TEMP_DIR)

View File

@ -26,13 +26,15 @@ BASEIMAGE?=k8s.gcr.io/debian-base-$(ARCH):0.3.2
# This option is for running docker manifest command
export DOCKER_CLI_EXPERIMENTAL := enabled
SUDO=$(if $(filter 0,$(shell id -u)),,sudo)
build:
cp ./* $(TEMP_DIR)
cd $(TEMP_DIR) && sed -i "s|BASEIMAGE|$(BASEIMAGE)|g" Dockerfile
ifneq ($(ARCH),amd64)
# Register /usr/bin/qemu-ARCH-static as the handler for non-x86 binaries in the kernel
sudo ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
$(SUDO) ../../third_party/multiarch/qemu-user-static/register/register.sh --reset
endif
docker build --pull -t $(IMAGE)-$(ARCH):$(TAG) $(TEMP_DIR)

View File

@ -84,7 +84,11 @@ build() {
${SED} -i "s|QEMUARCH|${QEMUARCHS[$arch]}|g" Dockerfile
# Register qemu-*-static for all supported processors except the current one
echo "Registering qemu-*-static binaries in the kernel"
sudo "${KUBE_ROOT}/third_party/multiarch/qemu-user-static/register/register.sh" --reset
local sudo=""
if [[ $(id -u) != 0 ]]; then
sudo=sudo
fi
"${sudo}" "${KUBE_ROOT}/third_party/multiarch/qemu-user-static/register/register.sh" --reset
curl -sSL https://github.com/multiarch/qemu-user-static/releases/download/${QEMUVERSION}/x86_64_qemu-${QEMUARCHS[$arch]}-static.tar.gz | tar -xz -C ${temp_dir}
# Ensure we don't get surprised by umask settings
chmod 0755 "${temp_dir}/qemu-${QEMUARCHS[$arch]}-static"