Introduce KAS_BUILD_DIR environment variable

This variable can override default build path `${KAS_WORK_DIR}/build`.

Signed-off-by: Peter Hatina <peter@hatina.eu>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Peter Hatina 2021-03-09 21:40:50 +01:00 committed by Jan Kiszka
parent 24c13a491b
commit f32290d425
6 changed files with 57 additions and 6 deletions

View File

@ -15,6 +15,8 @@ Environment variables
+=======================+=====================================================+
| ``KAS_WORK_DIR`` | The path of the kas work directory, current work |
| | directory is the default. |
| ``KAS_BUILD_DIR`` | The path build directory, ``${KAS_WORK_DIR}/build`` |
| | is the default. |
+-----------------------+-----------------------------------------------------+
| ``KAS_REPO_REF_DIR`` | The path to the repository reference directory. |
| | Repositories in this directory are used as |

View File

@ -93,6 +93,12 @@ else
KAS_WORK_DIR="$(pwd)"
fi
if [ -n "${KAS_BUILD_DIR}" ]; then
KAS_BUILD_DIR=$(readlink -f "${KAS_BUILD_DIR}")
else
KAS_BUILD_DIR="${KAS_WORK_DIR}/build"
fi
KAS_CONTAINER_ENGINE="${KAS_CONTAINER_ENGINE:-${KAS_DOCKER_ENGINE}}"
if [ -z "${KAS_CONTAINER_ENGINE}" ]; then
# Try to auto-detect a container engine
@ -183,17 +189,16 @@ while [ $# -gt 0 ]; do
;;
clean)
[ $# -eq 1 ] || usage
KAS_CLEAN_DIR=build/tmp
if [ -n "${KAS_ISAR_ARGS}" ]; then
set_container_image_var
# SC2086: Double quote to prevent globbing and word splitting.
# shellcheck disable=2086
trace ${KAS_CONTAINER_COMMAND} run -v "${KAS_WORK_DIR}":/work:rw \
--workdir=/work --rm ${KAS_ISAR_ARGS} \
trace ${KAS_CONTAINER_COMMAND} run -v "${KAS_BUILD_DIR}":/build:rw \
--workdir=/build --rm ${KAS_ISAR_ARGS} \
${KAS_CONTAINER_IMAGE} \
sudo rm -rf ${KAS_CLEAN_DIR}
sudo rm -rf tmp
else
trace rm -rf "${KAS_WORK_DIR}/${KAS_CLEAN_DIR}"
trace rm -rf "${KAS_BUILD_DIR}/tmp"
fi
exit 0
;;
@ -276,6 +281,7 @@ KAS_REPO_DIR=$(git -C "${KAS_FILE_DIR}" rev-parse --show-toplevel 2>/dev/null) \
KAS_FILES=/repo/"$(echo "${KAS_FILES}" | sed 's|'"${KAS_REPO_DIR}"'/||g;s|:|:/repo/|g')"
trace mkdir -p "${KAS_WORK_DIR}"
trace mkdir -p "${KAS_BUILD_DIR}"
if [ "$(id -u)" -eq 0 ] && [ "${KAS_ALLOW_ROOT}" != "yes" ] ; then
echo "Error: Running as root - may break certain recipes."
@ -286,6 +292,8 @@ fi
set -- "$@" -v "${KAS_REPO_DIR}":/repo:ro \
-v "${KAS_WORK_DIR}":/work:rw --workdir=/work \
-v "${KAS_BUILD_DIR}":/build:rw \
-e KAS_BUILD_DIR=/build \
-e USER_ID="$(id -u)" -e GROUP_ID="$(id -g)" --rm
if [ -n "${KAS_SSH_DIR}" ] ; then

View File

@ -72,6 +72,9 @@ class Context:
"""
def __init__(self, args):
self.__kas_work_dir = os.environ.get('KAS_WORK_DIR', os.getcwd())
self.__kas_build_dir = os.environ.get('KAS_BUILD_DIR',
os.path.join(self.__kas_work_dir,
'build'))
self.__kas_repo_ref_dir = os.environ.get('KAS_REPO_REF_DIR', None)
self.setup_initial_environ()
self.config = None
@ -110,7 +113,7 @@ class Context:
"""
The path to the build directory
"""
return os.path.join(self.__kas_work_dir, 'build')
return self.__kas_build_dir
@property
def kas_work_dir(self):

View File

@ -0,0 +1,30 @@
import os
import shutil
from kas import kas
def test_build_dir_is_placed_inside_work_dir_by_default(changedir, tmpdir):
conf_dir = str(tmpdir.mkdir('test_env_variables'))
shutil.rmtree(conf_dir, ignore_errors=True)
shutil.copytree('tests/test_environment_variables', conf_dir)
os.chdir(conf_dir)
kas.kas(['checkout', 'test.yml'])
assert(os.path.exists(os.path.join(os.getcwd(), 'build', 'conf')))
def test_build_dir_can_be_specified_by_environment_variable(changedir, tmpdir):
conf_dir = str(tmpdir.mkdir('test_env_variables'))
build_dir = str(tmpdir.mkdir('test_build_dir'))
shutil.rmtree(conf_dir, ignore_errors=True)
shutil.copytree('tests/test_environment_variables', conf_dir)
shutil.rmtree(build_dir, ignore_errors=True)
os.chdir(conf_dir)
os.environ['KAS_BUILD_DIR'] = build_dir
kas.kas(['checkout', 'test.yml'])
del os.environ['KAS_BUILD_DIR']
assert(os.path.exists(os.path.join(build_dir, 'conf')))

View File

@ -0,0 +1,3 @@
#!/bin/sh
true

View File

@ -0,0 +1,5 @@
header:
version: 10
repos:
this: