Skip to content
Open
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
3 changes: 3 additions & 0 deletions cf_remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
exit_success,
expand_list_from_file,
is_file_string,
migrate_config_paths,
)
from cf_remote.utils import strip_user, read_json, is_package_url, cache
from cf_remote.packages import Releases
Expand Down Expand Up @@ -685,6 +686,8 @@ def main() -> int:

The only thing we want to do here is call _main() and handle exceptions (errors).
"""
migrate_config_paths()

if os.getenv("CFBACKTRACE") == "1":
r = _main()
assert type(r) is int
Expand Down
10 changes: 5 additions & 5 deletions cf_remote/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def path_append(dir, subdir):
return dir if not subdir else os.path.join(dir, subdir)


def cfengine_dir(subdir=None):
def cfengine_dir(subdir=None, in_cache=False):
override_dir = os.getenv("CF_REMOTE_DIR")

if override_dir:
Expand All @@ -23,19 +23,19 @@ def cfengine_dir(subdir=None):

return path_append(override_dir, subdir)

return path_append("~/.cfengine/", subdir)
return path_append("~/.%s/cfengine/" % ("cache" if in_cache else "config"), subdir)


def cf_remote_dir(subdir=None):
return path_append(cfengine_dir("cf-remote"), subdir)
def cf_remote_dir(subdir=None, in_cache=False):
return path_append(cfengine_dir("cf-remote", in_cache=in_cache), subdir)


def cf_remote_file(fname=None):
return path_append(cfengine_dir("cf-remote"), fname)


def cf_remote_packages_dir(subdir=None):
return path_append(cf_remote_dir("packages"), subdir)
return path_append(cf_remote_dir("packages", in_cache=True), subdir)


CLOUD_CONFIG_FNAME = "cloud_config.json"
Expand Down
2 changes: 1 addition & 1 deletion cf_remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def install_host(
trust_keys=None,
insecure=False,
demo_salt=None,
demo_sha=None,
demo_sha=None
):
data = get_info(host, connection=connection)
if show_info:
Expand Down
2 changes: 1 addition & 1 deletion cf_remote/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def spawn_vm_in_vagrant(
vagrantdir = cf_remote_dir(os.path.join("vagrant", name))
os.makedirs(vagrantdir, exist_ok=True)

# Copy Vagrantfile to .cfengine/cf-remote/vagrant
# Copy Vagrantfile to ~/.config/cfengine/cf-remote/vagrant
vagrantfile = join(dirname(__file__), "Vagrantfile")
copy_file(vagrantfile, os.path.join(vagrantdir, "Vagrantfile"))

Expand Down
43 changes: 43 additions & 0 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,46 @@ def has_unescaped_character(string, char):
return True
previous = current
return False


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
Comment on lines +345 to +348
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's debatable, but I think I would run the migration for the default paths even if a specific dir is specified. If they have something in the default location, they probably want that moved in case the old location will stop working in the future, for example.


old_dir = os.path.expanduser("~/.cfengine/cf-remote/")
conf_dir = os.path.expanduser("~/.config/cfengine/cf-remote/")
cache_dir = os.path.expanduser("~/.cache/cfengine/cf-remote/")
if os.path.exists(conf_dir) and os.path.exists(cache_dir):
return # Migration has already occured
if not os.path.exists(os.path.dirname(old_dir)):
return # nothing to migrate
shutil.copytree(
old_dir,
conf_dir,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("json", "packages"),
)
print("config-files has been moved to '%s'" % conf_dir)
Copy link
Copy Markdown
Member

@olehermanse olehermanse Jun 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("config-files has been moved to '%s'" % conf_dir)
print("Migration: config files have been moved to '%s'" % conf_dir)


shutil.copytree(
os.path.join(old_dir, "json"),
os.path.join(cache_dir, "json"),
dirs_exist_ok=True,
)
shutil.copytree(
os.path.join(old_dir, "packages"),
os.path.join(cache_dir, "packages"),
dirs_exist_ok=True,
)
print("cache-files has been moved to '%s'" % cache_dir)
Copy link
Copy Markdown
Member

@olehermanse olehermanse Jun 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("cache-files has been moved to '%s'" % cache_dir)
print("Migration: cache files have been moved to '%s'" % cache_dir)


choice = input("Remove 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.")
Comment on lines +377 to +384
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't prompt for input like this. cf-remote could be running in a script or similar. I think this migration should be safe to run without prompts, if the copies are successful, the old dir should be removed to avoid confusion / duplication.

2 changes: 1 addition & 1 deletion cf_remote/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_json(url):
data = json.loads(r.read().decode(), object_pairs_hook=OrderedDict)

filename = os.path.basename(url)
dir = cf_remote_dir("json")
dir = cf_remote_dir("json", in_cache=True)
path = os.path.join(dir, filename)
log.debug("Saving '{}' to '{}'".format(url, path))
write_json(path, data)
Expand Down
Loading