Commit de4c9203 by John Jarvis

separate cache for --tags-only option

parent e559a000
...@@ -162,8 +162,13 @@ class Ec2Inventory(object): ...@@ -162,8 +162,13 @@ class Ec2Inventory(object):
def is_cache_valid(self): def is_cache_valid(self):
''' Determines if the cache files have expired, or if it is still valid ''' ''' Determines if the cache files have expired, or if it is still valid '''
if os.path.isfile(self.cache_path_cache): if self.args.tags_only:
mod_time = os.path.getmtime(self.cache_path_cache) to_check = self.cache_path_tags
else:
to_check = self.cache_path_cache
if os.path.isfile(to_check):
mod_time = os.path.getmtime(to_check)
current_time = time() current_time = time()
if (mod_time + self.cache_max_age) > current_time: if (mod_time + self.cache_max_age) > current_time:
if os.path.isfile(self.cache_path_index): if os.path.isfile(self.cache_path_index):
...@@ -214,6 +219,7 @@ class Ec2Inventory(object): ...@@ -214,6 +219,7 @@ class Ec2Inventory(object):
# Cache related # Cache related
cache_path = config.get('ec2', 'cache_path') cache_path = config.get('ec2', 'cache_path')
self.cache_path_cache = cache_path + "/ansible-ec2.cache" self.cache_path_cache = cache_path + "/ansible-ec2.cache"
self.cache_path_tags = cache_path + "/ansible-ec2.tags.cache"
self.cache_path_index = cache_path + "/ansible-ec2.index" self.cache_path_index = cache_path + "/ansible-ec2.index"
self.cache_max_age = config.getint('ec2', 'cache_max_age') self.cache_max_age = config.getint('ec2', 'cache_max_age')
...@@ -248,9 +254,12 @@ class Ec2Inventory(object): ...@@ -248,9 +254,12 @@ class Ec2Inventory(object):
self.get_instances_by_region(region) self.get_instances_by_region(region)
self.get_rds_instances_by_region(region) self.get_rds_instances_by_region(region)
self.write_to_cache(self.inventory, self.cache_path_cache) if self.args.tags_only:
self.write_to_cache(self.index, self.cache_path_index) self.write_to_cache(self.inventory, self.cache_path_tags)
else:
self.write_to_cache(self.inventory, self.cache_path_cache)
self.write_to_cache(self.index, self.cache_path_index)
def get_instances_by_region(self, region): def get_instances_by_region(self, region):
''' Makes an AWS EC2 API call to the list of instances in a particular ''' Makes an AWS EC2 API call to the list of instances in a particular
...@@ -542,8 +551,10 @@ class Ec2Inventory(object): ...@@ -542,8 +551,10 @@ class Ec2Inventory(object):
def get_inventory_from_cache(self): def get_inventory_from_cache(self):
''' Reads the inventory from the cache file and returns it as a JSON ''' Reads the inventory from the cache file and returns it as a JSON
object ''' object '''
if self.args.tags_only:
cache = open(self.cache_path_cache, 'r') cache = open(self.cache_path_tags, 'r')
else:
cache = open(self.cache_path_cache, 'r')
json_inventory = cache.read() json_inventory = cache.read()
return json_inventory return json_inventory
...@@ -557,7 +568,11 @@ class Ec2Inventory(object): ...@@ -557,7 +568,11 @@ class Ec2Inventory(object):
def write_to_cache(self, data, filename): def write_to_cache(self, data, filename):
''' Writes data in JSON format to a file ''' '''
Writes data in JSON format to a file
Never write the data to cache if the
--tags-only option was used
'''
json_data = self.json_format_dict(data, True) json_data = self.json_format_dict(data, True)
cache = open(filename, 'w') cache = open(filename, 'w')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment