Skip to content

Commit

Permalink
Added CFBS_GLOBAL_DIR environment variable
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
  • Loading branch information
olehermanse committed Jul 9, 2024
1 parent 9f93b06 commit 4643b69
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion cfbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
58 changes: 58 additions & 0 deletions tests/shell/038_global_dir.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

0 comments on commit 4643b69

Please sign in to comment.