From 9e329c40c5a11654969cd3867baffd1625d813de Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Thu, 31 Aug 2017 12:35:16 +0200 Subject: [PATCH] Introduce cleanup-after-build.sh to clean all docker volumes generated during rpm building --- hack/cleanup-after-build.sh | 8 +++++++ hack/lib/build/environment.sh | 6 +++-- hack/lib/cleanup.sh | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100755 hack/cleanup-after-build.sh diff --git a/hack/cleanup-after-build.sh b/hack/cleanup-after-build.sh new file mode 100755 index 000000000000..c5e03dc63ec1 --- /dev/null +++ b/hack/cleanup-after-build.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# This script cleans up resources allocated during image building + +source "$(dirname "${BASH_SOURCE}")/lib/init.sh" + +# delete all docker volumes used to build images to free space +os::cleanup::buildvolumes diff --git a/hack/lib/build/environment.sh b/hack/lib/build/environment.sh index 2c95eba0767c..c5f10669a638 100644 --- a/hack/lib/build/environment.sh +++ b/hack/lib/build/environment.sh @@ -230,8 +230,10 @@ function os::build::environment::run() { local volume local tmp_volume - volume="$( os::build::environment::volume_name "origin-build" "${commit}" "${OS_BUILD_ENV_REUSE_VOLUME:-}" )" - tmp_volume="$( os::build::environment::volume_name "origin-build-tmp" "${commit}" "${OS_BUILD_ENV_REUSE_TMP_VOLUME:-}" )" + export OS_BUILD_ENV_VOLUME_PREFIX="origin-build" + + volume="$( os::build::environment::volume_name "${OS_BUILD_ENV_VOLUME_PREFIX}" "${commit}" "${OS_BUILD_ENV_REUSE_VOLUME:-}" )" + tmp_volume="$( os::build::environment::volume_name "${OS_BUILD_ENV_VOLUME_PREFIX}-tmp" "${commit}" "${OS_BUILD_ENV_REUSE_TMP_VOLUME:-}" )" export OS_BUILD_ENV_VOLUME="${volume}" export OS_BUILD_ENV_TMP_VOLUME="${tmp_volume}" diff --git a/hack/lib/cleanup.sh b/hack/lib/cleanup.sh index 4ef6928c895d..5c3c549512e7 100644 --- a/hack/lib/cleanup.sh +++ b/hack/lib/cleanup.sh @@ -339,3 +339,44 @@ function os::cleanup::processes() { done } readonly -f os::cleanup::processes + +# os::cleanup::docker::volumes clens all docker volumes with a given prefix. +# +# Globals: +# None +# Arguments: +# - 1: docker volume prefix +# Returns: +# None +function os::cleanup::docker::volumes() { + local prefix=${1} + local volumes="$(docker volume ls -q | grep ${prefix})" + echo $volumes + if [[ "${volumes}" != "" ]]; then + docker volume rm ${volumes} + fi +} +readonly -f os::cleanup::docker::volumes + +# os::cleanup::buildvolumes clens all volumes with origin-build prefix +# +# Globals: +# None +# Arguments: +# None +# Returns: +# None +function os::cleanup::buildvolumes() { + local result=1 + if os::util::find::system_binary 'imagebuilder' >/dev/null; then + os::log::warning "volumes cleaning not implemented for imagebuilder" + result=0 + else + os::log::warning "Unable to locate 'imagebuilder' on PATH, falling back to Docker" + if os::cleanup::docker::volumes "origin-build"; then + result=0 + fi + fi + + return "${result}" +}