diff --git a/README.md b/README.md index b8e7e7e8..7beb8e06 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,27 @@ These commands are intended to be run as part of build systems / deployment pipe They don't have interactive prompts, you can expect fewer changes to them, and backwards compatibility is much more important than with the interactive commands above. +## Environment variables + +`cfbs` respects the following environment variables: + +- `CFBS_GLOBAL_DIR`: Directory where `cfbs` stores global information, such as its cache of downloaded modules. + - **Default:** `~/.cfengine/cfbs/`. + - **Usage:** `CFBS_GLOBAL_DIR=/tmp/cfbs cfbs build`. + - **Note:** `cfbs` still uses the current working directory for finding and building a project (`./cfbs.json`, `./out/`, etc.). + +Additionally, `cfbs` runs some commands in a shell, utilizing a few programs / shell built-ins, which may be affected by environment variables: + +- `git` +- `tar` +- `unzip` +- `rsync` +- `mv` +- `cat` +- `cd` +- `rm` +- Commands / scripts specified in the `run` build step. + ## The cfbs.json format More advanced users and module authors may need to understand, write, or edit `cfbs.json` files. diff --git a/cfbs/utils.py b/cfbs/utils.py index 63a6f51d..43afb4f3 100644 --- a/cfbs/utils.py +++ b/cfbs/utils.py @@ -198,7 +198,17 @@ def cfengine_dir(subdir=None): def cfbs_dir(append=None) -> str: - return os.path.join(cfengine_dir("cfbs"), append if append else "") + directory = os.getenv("CFBS_GLOBAL_DIR") + if directory: + # Env var was set, make it absolute, + # same as in cfengine_dir() / path_append() above. + directory = os.path.abspath(os.path.expanduser(directory)) + else: + # Env var not set, use default. + directory = cfengine_dir("cfbs") # Already absolute + if not append: + return directory + return os.path.join(directory, append) class FetchError(Exception): diff --git a/tests/shell/038_global_dir.sh b/tests/shell/038_global_dir.sh new file mode 100644 index 00000000..40caffac --- /dev/null +++ b/tests/shell/038_global_dir.sh @@ -0,0 +1,58 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +rm -rf ./* + +# Try to be nice to the user - back up and restore their +# module cache (~/.cfengine/cfbs/downloads): + +cleanup_restore_backup() +{ + if [ -d ~/.cfengine/cfbs_backup ]; then + if [ -d ~/.cfengine/cfbs ]; then + # Should be okay to delete this - it's created by a bug in cfbs or this test + # the "real" data we care about is in downloads_backup + rm -rf ~/.cfengine/cfbs + fi + echo "Restoring backup" + mv ~/.cfengine/cfbs_backup ~/.cfengine/cfbs + fi +} + +if [ -d ~/.cfengine/cfbs ]; then # Global dir used by cfbs by default + if [ -d ~/.cfengine/cfbs_backup ]; then # Backup dir used by this test + echo "Warning: Removing previous backup in" ~/.cfengine/cfbs_backup + rm -rf ~/.cfengine/cfbs_backup + fi + # Setting the trap here, after determining that we need to backup and + # after potentially deleting an older backup, so we don't end up + # restoring a backup which was not created in this test run: + trap cleanup_restore_backup EXIT ERR SIGHUP SIGINT SIGQUIT SIGABRT + mv ~/.cfengine/cfbs ~/.cfengine/cfbs_backup +fi + +test ! -e ./out/cfbs_global/ +test ! -e ~/.cfengine/cfbs + +# CFBS_GLOBAL_DIR allows us to override ~/.cfengine/cfbs with +# another path, for example for situations where you want to run +# cfbs as root, but not having access to root's home directory: +CFBS_GLOBAL_DIR="./out/cfbs_global" cfbs --non-interactive init +CFBS_GLOBAL_DIR="./out/cfbs_global" cfbs download + +# Check that something was downloaded in the correct place: +ls ./out/cfbs_global/downloads/github.com/cfengine/masterfiles/* +# And nothing was downloaded or created in the wrong place: +test ! -e ~/.cfengine/cfbs + +# Test some other commands, just in case: +rm -rf "./out/cfbs_global" +CFBS_GLOBAL_DIR="./out/cfbs_global" cfbs status +CFBS_GLOBAL_DIR="./out/cfbs_global" cfbs download +CFBS_GLOBAL_DIR="./out/cfbs_global" cfbs build + +# Same checks as above: +ls ./out/cfbs_global/downloads/github.com/cfengine/masterfiles/* +test ! -e ~/.cfengine/cfbs diff --git a/tests/shell/all.sh b/tests/shell/all.sh index 778f4075..c6b55ec2 100644 --- a/tests/shell/all.sh +++ b/tests/shell/all.sh @@ -41,5 +41,6 @@ bash tests/shell/034_git_user_name_git_user_email.sh bash tests/shell/035_cfbs_build_compatibility_1.sh bash tests/shell/036_cfbs_build_compatibility_2.sh bash tests/shell/037_cfbs_validate.sh +bash tests/shell/038_global_dir.sh echo "All cfbs shell tests completed successfully!"