implemented 'prefix' for file based cache (#69872)

* implemented 'prefix' for file based cache

Co-authored-by: s-hertel <shertel@redhat.com>
pull/70011/head
Brian Coca 6 years ago committed by GitHub
parent ad37218200
commit ebb22655e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- added 'unimplemented' prefix to file based caching

@ -138,6 +138,14 @@ class BaseFileCacheModule(BaseCacheModule):
raise AnsibleError("error in '%s' cache, configured path (%s) does not have necessary permissions (rwx), disabling plugin" % ( raise AnsibleError("error in '%s' cache, configured path (%s) does not have necessary permissions (rwx), disabling plugin" % (
self.plugin_name, self._cache_dir)) 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)
return cachefile
def get(self, key): def get(self, key):
""" This checks the in memory cache first as the fact was not expired at 'gather time' """ 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 and it would be problematic if the key did expire after some long running tasks and
@ -148,7 +156,7 @@ class BaseFileCacheModule(BaseCacheModule):
if self.has_expired(key) or key == "": if self.has_expired(key) or key == "":
raise KeyError raise KeyError
cachefile = "%s/%s" % (self._cache_dir, key) cachefile = self._get_cache_file_name(key)
try: try:
value = self._load(cachefile) value = self._load(cachefile)
self._cache[key] = value self._cache[key] = value
@ -170,7 +178,7 @@ class BaseFileCacheModule(BaseCacheModule):
self._cache[key] = value self._cache[key] = value
cachefile = "%s/%s" % (self._cache_dir, key) cachefile = self._get_cache_file_name(key)
try: try:
self._dump(value, cachefile) self._dump(value, cachefile)
except (OSError, IOError) as e: except (OSError, IOError) as e:
@ -181,7 +189,7 @@ class BaseFileCacheModule(BaseCacheModule):
if self._timeout == 0: if self._timeout == 0:
return False return False
cachefile = "%s/%s" % (self._cache_dir, key) cachefile = self._get_cache_file_name(key)
try: try:
st = os.stat(cachefile) st = os.stat(cachefile)
except (OSError, IOError) as e: except (OSError, IOError) as e:
@ -206,7 +214,7 @@ class BaseFileCacheModule(BaseCacheModule):
return keys return keys
def contains(self, key): def contains(self, key):
cachefile = "%s/%s" % (self._cache_dir, key) cachefile = self._get_cache_file_name(key)
if key in self._cache: if key in self._cache:
return True return True
@ -228,7 +236,7 @@ class BaseFileCacheModule(BaseCacheModule):
except KeyError: except KeyError:
pass pass
try: try:
os.remove("%s/%s" % (self._cache_dir, key)) os.remove(self._get_cache_file_name(key))
except (OSError, IOError): except (OSError, IOError):
pass # TODO: only pass on non existing? pass # TODO: only pass on non existing?

@ -4,3 +4,4 @@ hostname: cache_host_a
cache_plugin: testns.content_adj.custom_jsonfile cache_plugin: testns.content_adj.custom_jsonfile
cache: yes cache: yes
cache_connection: inventory_cache cache_connection: inventory_cache
cache_prefix: 'prefix_'

@ -66,6 +66,11 @@ fi
CACHEFILE="$(find ./inventory_cache -type f ! -path './inventory_cache/.keep')" 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 # Check the cache for the expected hosts
if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then

@ -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 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 # 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_1.yml
ansible-playbook -i inventory "$@" set_fact_cached_2.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 ansible-playbook -i inventory --flush-cache "$@" set_fact_no_cache.yml
# Test boolean conversions in set_fact # Test boolean conversions in set_fact

Loading…
Cancel
Save