Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented 'prefix' for file based cache #69872

Merged
merged 8 commits into from Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/add_prefix_to_cache.yml
@@ -0,0 +1,2 @@
bugfixes:
- added 'unimplemented' prefix to file based caching
18 changes: 13 additions & 5 deletions lib/ansible/plugins/cache/__init__.py
Expand Up @@ -137,6 +137,14 @@ def validate_cache_connection(self):
raise AnsibleError("error in '%s' cache, configured path (%s) does not have necessary permissions (rwx), disabling plugin" % (
self.plugin_name, self._cache_dir))

def _get_cache_file_name(self, key):
prefix = self.get_option('_prefix')
if prefix:
cachefile = "%s/%s%s" % (self._cache_dir, prefix, key)
else:
cachefile = "%s/%s" % (self._cache_dir, key)
bcoca marked this conversation as resolved.
Show resolved Hide resolved
return cachefile

def get(self, key):
""" This checks the in memory cache first as the fact was not expired at 'gather time'
and it would be problematic if the key did expire after some long running tasks and
Expand All @@ -147,7 +155,7 @@ def get(self, key):
if self.has_expired(key) or key == "":
raise KeyError

cachefile = "%s/%s" % (self._cache_dir, key)
cachefile = self._get_cache_file_name(key)
try:
value = self._load(cachefile)
self._cache[key] = value
Expand All @@ -169,7 +177,7 @@ def set(self, key, value):

self._cache[key] = value

cachefile = "%s/%s" % (self._cache_dir, key)
cachefile = self._get_cache_file_name(key)
try:
self._dump(value, cachefile)
except (OSError, IOError) as e:
Expand All @@ -180,7 +188,7 @@ def has_expired(self, key):
if self._timeout == 0:
return False

cachefile = "%s/%s" % (self._cache_dir, key)
cachefile = self._get_cache_file_name(key)
try:
st = os.stat(cachefile)
except (OSError, IOError) as e:
Expand All @@ -205,7 +213,7 @@ def keys(self):
return keys

def contains(self, key):
cachefile = "%s/%s" % (self._cache_dir, key)
cachefile = self._get_cache_file_name(key)

if key in self._cache:
return True
Expand All @@ -227,7 +235,7 @@ def delete(self, key):
except KeyError:
pass
try:
os.remove("%s/%s" % (self._cache_dir, key))
os.remove(self._get_cache_file_name(key))
except (OSError, IOError):
pass # TODO: only pass on non existing?

Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/collections/cache.statichost.yml
Expand Up @@ -4,3 +4,4 @@ hostname: cache_host_a
cache_plugin: testns.content_adj.custom_jsonfile
cache: yes
cache_connection: inventory_cache
cache_prefix: 'prefix_'
5 changes: 5 additions & 0 deletions test/integration/targets/collections/runme.sh
Expand Up @@ -66,6 +66,11 @@ fi

CACHEFILE="$(find ./inventory_cache -type f ! -path './inventory_cache/.keep')"

if [[ $CACHEFILE != ./inventory_cache/prefix_* ]]; then
echo "Unexpected cache file"
exit 1
fi

# Check the cache for the expected hosts

if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then
Expand Down
9 changes: 8 additions & 1 deletion test/integration/targets/set_fact/runme.sh
Expand Up @@ -13,9 +13,16 @@ ANSIBLE_INJECT_FACT_VARS=1 ansible-playbook -i inventory incremental.yml
ansible-playbook -i inventory nowarn_clean_facts.yml | grep '[WARNING]: Removed restricted key from module data: ansible_ssh_common_args' && exit 1

# test cached feature
export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}"
export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ANSIBLE_CACHE_PLUGIN_PREFIX=prefix_
ansible-playbook -i inventory "$@" set_fact_cached_1.yml
ansible-playbook -i inventory "$@" set_fact_cached_2.yml

# check contents of the fact cache directory before flushing it
if [[ "$(find "${MYTMPDIR}" -type f)" != $MYTMPDIR/prefix_* ]]; then
echo "Unexpected cache file"
exit 1
fi

ansible-playbook -i inventory --flush-cache "$@" set_fact_no_cache.yml

# Test boolean conversions in set_fact
Expand Down