From 6bb80e19c06a3a19d303c7074ef647f7369ca2de Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Wed, 13 Nov 2024 14:20:00 -0800 Subject: [PATCH] Wrap new detection logic in simple tool --- toolkit/scripts/chroot.mk | 3 +- toolkit/scripts/tools.mk | 1 + .../tools/containercheck/containercheck.go | 33 +++++++++++++++++++ .../pkggen/worker/create_worker_chroot.sh | 9 ++--- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 toolkit/tools/containercheck/containercheck.go diff --git a/toolkit/scripts/chroot.mk b/toolkit/scripts/chroot.mk index ec461909ee4..e43965e9e31 100644 --- a/toolkit/scripts/chroot.mk +++ b/toolkit/scripts/chroot.mk @@ -34,6 +34,7 @@ worker_chroot_rpm_paths := $(shell sed -nr $(sed_regex_full_path) < $(WORKER_CHR worker_chroot_deps := \ $(WORKER_CHROOT_MANIFEST) \ $(worker_chroot_rpm_paths) \ + $(go-containercheck) \ $(PKGGEN_DIR)/worker/create_worker_chroot.sh ifeq ($(REFRESH_WORKER_CHROOT),y) @@ -41,7 +42,7 @@ $(chroot_worker): $(worker_chroot_deps) $(depend_REBUILD_TOOLCHAIN) $(depend_TOO else $(chroot_worker): endif - $(PKGGEN_DIR)/worker/create_worker_chroot.sh $(BUILD_DIR)/worker $(WORKER_CHROOT_MANIFEST) $(TOOLCHAIN_RPMS_DIR) $(LOGS_DIR) + $(PKGGEN_DIR)/worker/create_worker_chroot.sh $(BUILD_DIR)/worker $(WORKER_CHROOT_MANIFEST) $(TOOLCHAIN_RPMS_DIR) $(go-containercheck) $(LOGS_DIR) validate-chroot: $(go-validatechroot) $(chroot_worker) $(go-validatechroot) \ diff --git a/toolkit/scripts/tools.mk b/toolkit/scripts/tools.mk index a9ab0c0383b..35d427dfb31 100644 --- a/toolkit/scripts/tools.mk +++ b/toolkit/scripts/tools.mk @@ -31,6 +31,7 @@ endif go_tool_list = \ bldtracker \ boilerplate \ + containercheck \ depsearch \ downloader \ grapher \ diff --git a/toolkit/tools/containercheck/containercheck.go b/toolkit/tools/containercheck/containercheck.go new file mode 100644 index 00000000000..f96237b9068 --- /dev/null +++ b/toolkit/tools/containercheck/containercheck.go @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +// Returns true (exit code 0) if the current build is a container build, false (exit code 1) otherwise + +package main + +import ( + "os" + + "github.com/microsoft/azurelinux/toolkit/tools/internal/buildpipeline" + "github.com/microsoft/azurelinux/toolkit/tools/internal/exe" + "github.com/microsoft/azurelinux/toolkit/tools/internal/logger" + + "gopkg.in/alecthomas/kingpin.v2" +) + +var ( + app = kingpin.New("containercheck", "Returns true (0) if the current build is a container build, false (1) otherwise") + logFlags = exe.SetupLogFlags(app) +) + +func main() { + app.Version(exe.ToolkitVersion) + kingpin.MustParse(app.Parse(os.Args[1:])) + logger.InitBestEffort(logFlags) + + if buildpipeline.IsRegularBuild() { + os.Exit(1) + } else { + os.Exit(0) + } +} diff --git a/toolkit/tools/pkggen/worker/create_worker_chroot.sh b/toolkit/tools/pkggen/worker/create_worker_chroot.sh index 67ec391e39d..53b44695fe4 100755 --- a/toolkit/tools/pkggen/worker/create_worker_chroot.sh +++ b/toolkit/tools/pkggen/worker/create_worker_chroot.sh @@ -10,12 +10,13 @@ set -o pipefail # $3 path to find RPMs. May be in PATH//*.rpm # $4 path to log directory -[ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ] && [ -n "$4" ] || { echo "Usage: create_worker.sh <./worker_base_folder> <./path_to_rpms> <./log_dir>"; exit; } +[ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ] && [ -n "$4" ] && [ -n "$4" ] || { echo "Usage: create_worker.sh <./worker_base_folder> <./path_to_rpms> <./containercheck> <./log_dir>"; exit; } chroot_base=$1 packages=$2 rpm_path=$3 -log_path=$4 +container_check_tool=$4 +log_path=$5 chroot_name="worker_chroot" chroot_builder_folder=$chroot_base/$chroot_name @@ -121,8 +122,8 @@ HOME=$ORIGINAL_HOME # In case of Docker based build do not add the below folders into chroot tarball # otherwise safechroot will fail to "untar" the tarball -DOCKERCONTAINERONLY=/.dockerenv -if [[ -f "$DOCKERCONTAINERONLY" ]]; then +if $container_check_tool; then + echo "Removing /dev, /proc, /run, /sys from chroot tarball for container based build." | tee -a "$chroot_log" rm -rf "${chroot_base:?}/$chroot_name"/dev rm -rf "${chroot_base:?}/$chroot_name"/proc rm -rf "${chroot_base:?}/$chroot_name"/run