From 917e868f6589abe92cc336f45d33f7a029c8f46b Mon Sep 17 00:00:00 2001 From: Josh Drake Date: Thu, 3 Jul 2014 14:02:26 -0500 Subject: [PATCH] Implement fact cache flushing alongside a command-line argument to invoke it. --- bin/ansible-playbook | 7 ++++++- lib/ansible/cache/__init__.py | 8 +++++--- lib/ansible/cache/base.py | 3 +++ lib/ansible/cache/file.py | 10 +++++++--- lib/ansible/cache/memcached.py | 4 ++++ lib/ansible/cache/memory.py | 3 +++ lib/ansible/cache/redis.py | 4 ++++ 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/bin/ansible-playbook b/bin/ansible-playbook index 3d05d8e29c8..3a5e399b23b 100755 --- a/bin/ansible-playbook +++ b/bin/ansible-playbook @@ -83,6 +83,8 @@ def main(args): help="start the playbook at the task matching this name") parser.add_option('--force-handlers', dest='force_handlers', action='store_true', help="run handlers even if a task fails") + parser.add_option('--flush-cache', dest='flush_cache', action='store_true', + help="flush to fact cache") options, args = parser.parse_args(args) @@ -191,6 +193,10 @@ def main(args): force_handlers=options.force_handlers ) + if options.flush_cache: + display(callbacks.banner("FLUSHING FACT CACHE")) + pb.SETUP_CACHE.flush() + if options.listhosts or options.listtasks or options.syntax: print '' print 'playbook: %s' % playbook @@ -313,4 +319,3 @@ if __name__ == "__main__": except KeyboardInterrupt, ke: display("ERROR: interrupted", color='red', stderr=True) sys.exit(1) - diff --git a/lib/ansible/cache/__init__.py b/lib/ansible/cache/__init__.py index d4b9fece49a..4100861c149 100644 --- a/lib/ansible/cache/__init__.py +++ b/lib/ansible/cache/__init__.py @@ -50,10 +50,12 @@ class FactCache(MutableMapping): return len(self._plugin.keys()) def copy(self): - """ - Return a primitive copy of the keys and values from the cache. - """ + """ Return a primitive copy of the keys and values from the cache. """ return dict([(k, v) for (k, v) in self.iteritems()]) def keys(self): return self._plugin.keys() + + def flush(self): + """ Flush the fact cache of all keys. """ + self._plugin.flush() diff --git a/lib/ansible/cache/base.py b/lib/ansible/cache/base.py index cce4d4de707..694ac47a7b2 100644 --- a/lib/ansible/cache/base.py +++ b/lib/ansible/cache/base.py @@ -13,3 +13,6 @@ class BaseCacheModule(object): def delete(self, key): raise NotImplementedError("Subclasses of {} must implement the '{}' method".format(self.__class__.__name__, self.__name__)) + + def flush(self): + raise NotImplementedError("Subclasses of {} must implement the '{}' method".format(self.__class__.__name__, self.__name__)) diff --git a/lib/ansible/cache/file.py b/lib/ansible/cache/file.py index 96c0d1f9acb..3171ee76f5e 100644 --- a/lib/ansible/cache/file.py +++ b/lib/ansible/cache/file.py @@ -47,13 +47,13 @@ class CacheModule(MemoryCacheModule): def set(self, *args, **kwargs): super(CacheModule, self).set(*args, **kwargs) - self.flush() + self.fsync() def delete(self, *args, **kwargs): super(CacheModule, self).delete(*args, **kwargs) - self.flush() + self.fsync() - def flush(self): + def fsync(self): temp = tempfile.TemporaryFile('r+b') try: @@ -63,3 +63,7 @@ class CacheModule(MemoryCacheModule): shutil.copyfileobj(temp, f) finally: temp.close() + + def flush(self): + super(CacheModule, self).flush() + self.fsync() diff --git a/lib/ansible/cache/memcached.py b/lib/ansible/cache/memcached.py index 59b3820f5df..213e3a24832 100644 --- a/lib/ansible/cache/memcached.py +++ b/lib/ansible/cache/memcached.py @@ -110,3 +110,7 @@ class CacheModule(BaseCacheModule): def delete(self, key): self._cache.delete(self._make_key(key)) self._keys.discard(key) + + def flush(self): + for key in self.keys(): + self.delete(key) diff --git a/lib/ansible/cache/memory.py b/lib/ansible/cache/memory.py index f4bf149a2fb..f948de1b7eb 100644 --- a/lib/ansible/cache/memory.py +++ b/lib/ansible/cache/memory.py @@ -35,3 +35,6 @@ class CacheModule(object): def delete(self, key): del self._cache[key] + + def flush(self): + self._cache = {} diff --git a/lib/ansible/cache/redis.py b/lib/ansible/cache/redis.py index 3891bce6169..0641fb337be 100644 --- a/lib/ansible/cache/redis.py +++ b/lib/ansible/cache/redis.py @@ -106,3 +106,7 @@ class CacheModule(BaseCacheModule): def delete(self, key): self._cache.delete(self._make_key(key)) self._cache.zrem(self._keys_set, key) + + def flush(self): + for key in self.keys(): + self.delete(key)