1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Name cache file by relevant regions - they might change between calls.

Also, use old school string interpolation so that the plugin is compatible with
Python < 2.7.
This commit is contained in:
Tom Berger 2014-10-30 17:03:18 +01:00 committed by Abhijit Menon-Sen
parent cd1125aac2
commit 07a4076d12

View file

@ -339,23 +339,25 @@ def _list_into_cache(regions):
if hostvars: if hostvars:
groups['_meta'] = {'hostvars': hostvars} groups['_meta'] = {'hostvars': hostvars}
with open(get_cache_file_path(), 'w') as cache_file: with open(get_cache_file_path(regions), 'w') as cache_file:
json.dump(groups, cache_file) json.dump(groups, cache_file)
def get_cache_file_path(): def get_cache_file_path(regions):
regions_str = '.'.join([reg.strip().lower() for reg in regions])
return os.path.join(gettempdir(), return os.path.join(gettempdir(),
'ansible-rax-{}.cache'.format(pyrax.identity.username)) 'ansible-rax-%s-%s.cache' % (
pyrax.identity.username, regions_str))
def _list(regions, refresh_cache=True): def _list(regions, refresh_cache=True):
if (not os.path.exists(get_cache_file_path()) or if (not os.path.exists(get_cache_file_path(regions)) or
refresh_cache or refresh_cache or
(time() - os.stat(get_cache_file_path())[-1]) > 600): (time() - os.stat(get_cache_file_path(regions))[-1]) > 600):
# Cache file doesn't exist or older than 10m or refresh cache requested # Cache file doesn't exist or older than 10m or refresh cache requested
_list_into_cache(regions) _list_into_cache(regions)
with open(get_cache_file_path(), 'r') as cache_file: with open(get_cache_file_path(regions), 'r') as cache_file:
groups = json.load(cache_file) groups = json.load(cache_file)
print(json.dumps(groups, sort_keys=True, indent=4)) print(json.dumps(groups, sort_keys=True, indent=4))