Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ They don't have interactive prompts, you can expect fewer changes to them, and b
`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/`.
- **Default:** `~/.cache/cfenginge/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.).

Expand Down
2 changes: 1 addition & 1 deletion cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def validate_command(paths=None, index_arg=None):

def _download_dependencies(config: CFBSConfig, redownload=False, ignore_versions=False):
# TODO: This function should be split in 2:
# 1. Code for downloading things into ~/.cfengine
# 1. Code for downloading things into ~/.cache/cfengine
# 2. Code for copying things into ./out
print("\nModules:")
counter = 1
Expand Down
2 changes: 1 addition & 1 deletion cfbs/internal_file_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

The functions here are quite "contained", they don't rely on the
global config (read and writen to cfbs.json), just their parameters
and what is on the file system (in ~/.cfengine and ./out).
and what is on the file system (in ~/.cache/cfengine and ./out).
"""

import os
Expand Down
2 changes: 2 additions & 0 deletions cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
is_cfbs_repo,
CFBSProgrammerError,
CFBSNetworkError,
migrate_config_paths,
)
from cfbs.cfbs_config import CFBSConfig
from cfbs import commands
Expand Down Expand Up @@ -61,6 +62,7 @@ def _main() -> int:

This function is wrapped by main() which catches exceptions.
"""
migrate_config_paths()
args = get_args()
init_logging(args.loglevel)
if args.manual:
Expand Down
35 changes: 34 additions & 1 deletion cfbs/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import difflib
import logging as log
import shutil
import os
import re
import sys
Expand Down Expand Up @@ -434,7 +436,7 @@ def are_paths_equal(path_a, path_b) -> bool:


def cfengine_dir(subdir=None):
CFENGINE_DIR = "~/.cfengine/"
CFENGINE_DIR = "~/.cache/cfengine/"
cfengine_dir_abspath = os.path.abspath(os.path.expanduser(CFENGINE_DIR))

return path_append(cfengine_dir_abspath, subdir)
Expand Down Expand Up @@ -675,3 +677,34 @@ def most_relevant_version(
highest_lower = highest_version(lower_other_versions)
assert highest_lower is not None
return highest_lower


def migrate_config_paths():
override_dir = os.getenv("CF_REMOTE_DIR")
# Set manually by user, assume they want to keep it like that
if override_dir:
return

old_dir = os.path.expanduser("~/.cfengine/cfbs/")
new_dir = os.path.expanduser("~/.cache/cfengine/cfbs/")
if not os.path.exists(os.path.dirname(old_dir)):
return # nothing to migrate
if os.path.exists(new_dir):
log.warning(
" cached data found both in ~/.cfengine [deprecated] and ~/.cache/cfengine [new directory] "
)
return # Migration has already occured
shutil.copytree(
old_dir,
new_dir,
)
print("CFBS related files has been moved to '%s'" % new_dir)

choice = input("Remove old directory %s ? [y/N]" % old_dir).strip().lower() or "n"
if choice in "yes":
shutil.rmtree(old_dir)
print("%s has been removed" % old_dir)
return
if choice in "no":
return
print("Unknown input.")
2 changes: 1 addition & 1 deletion tests/shell/003_download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rm -rf .git
cfbs --non-interactive init
cfbs download

ls ~/.cfengine/cfbs/downloads/*
ls ~/.cache/cfengine/cfbs/downloads/*
4 changes: 2 additions & 2 deletions tests/shell/013_add_url_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ grep scl_dmidecode_example out/masterfiles/scl_example.json
# so let's test that it works after deleting the things which
# will not be on the next machine.
# Notably, cfbs add will download some things which can be reused in
# cfbs build (git clones / zip downloads in ~/.cfengine/cfbs)
# cfbs build (git clones / zip downloads in ~/.cache/cfengine/cfbs)

rm -rf out/
rm -rf ~/.cfengine/cfbs
rm -rf ~/.cache/cfengine/cfbs

cfbs build
ls out/masterfiles/lib/scl/
Expand Down
4 changes: 2 additions & 2 deletions tests/shell/035_cfbs_build_compatibility_1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ grep 'bundle common inventory' out/masterfiles/promises.cf

# Once more, but let's do download and build as separate steps:
rm -rf out/
rm -rf ~/.cfengine/cfbs
rm -rf ~/.cache/cfengine/cfbs

cfbs download

Expand All @@ -59,7 +59,7 @@ grep 'bundle common inventory' out/masterfiles/promises.cf

# Finally, let's see validation working if we fix the module:
rm -rf out/
rm -rf ~/.cfengine/cfbs
rm -rf ~/.cache/cfengine/cfbs

echo '{
"name": "backwards-compatibility-test-1",
Expand Down
2 changes: 1 addition & 1 deletion tests/shell/036_cfbs_build_compatibility_2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ grep '$(paths.systemctl) list-units --type=service --state=running' out/masterfi

# Once more, but let's do download and build as separate steps:
rm -rf out/
rm -rf ~/.cfengine/cfbs
rm -rf ~/.cache/cfengine/cfbs

cfbs download

Expand Down
28 changes: 14 additions & 14 deletions tests/shell/038_global_dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ cd ./tmp/
rm -rf ./*

# Try to be nice to the user - back up and restore their
# module cache (~/.cfengine/cfbs/downloads):
# module cache (~/.cache/cfengine/cfbs/downloads):

cleanup_restore_backup()
{
if [ -d ~/.cfengine/cfbs_backup ]; then
if [ -d ~/.cfengine/cfbs ]; then
if [ -d ~/.cache/cfengine/cfbs_backup ]; then
if [ -d ~/.cache/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
rm -rf ~/.cache/cfengine/cfbs
fi
echo "Restoring backup"
mv ~/.cfengine/cfbs_backup ~/.cfengine/cfbs
mv ~/.cache/cfengine/cfbs_backup ~/.cache/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
if [ -d ~/.cache/cfengine/cfbs ]; then # Global dir used by cfbs by default
if [ -d ~/.cache/cfengine/cfbs_backup ]; then # Backup dir used by this test
echo "Warning: Removing previous backup in" ~/.cache/cfengine/cfbs_backup
rm -rf ~/.cache/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
mv ~/.cache/cfengine/cfbs ~/.cache/cfengine/cfbs_backup
fi

test ! -e ./out/cfbs_global/
test ! -e ~/.cfengine/cfbs
test ! -e ~/.cache/cfengine/cfbs

# CFBS_GLOBAL_DIR allows us to override ~/.cfengine/cfbs with
# CFBS_GLOBAL_DIR allows us to override ~/.cache/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
Expand All @@ -45,7 +45,7 @@ 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 ! -e ~/.cache/cfengine/cfbs

# Test some other commands, just in case:
rm -rf "./out/cfbs_global"
Expand All @@ -55,4 +55,4 @@ 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
test ! -e ~/.cache/cfengine/cfbs
Loading