fixed caches (#30667)

This commit is contained in:
Brian Coca 2017-10-19 14:23:22 -04:00 committed by GitHub
parent 0b8e38d022
commit 2ffe3c42bb
7 changed files with 27 additions and 22 deletions

View file

@ -26,7 +26,6 @@ from ansible.errors import AnsibleError
from ansible.inventory.group import Group from ansible.inventory.group import Group
from ansible.inventory.host import Host from ansible.inventory.host import Host
from ansible.module_utils.six import iteritems from ansible.module_utils.six import iteritems
from ansible.plugins.cache import FactCache
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars
from ansible.utils.path import basedir from ansible.utils.path import basedir
@ -62,9 +61,6 @@ class InventoryData(object):
self.add_group(group) self.add_group(group)
self.add_child('all', 'ungrouped') self.add_child('all', 'ungrouped')
# prime cache
self.cache = FactCache()
def serialize(self): def serialize(self):
data = dict() data = dict()
return data return data

View file

@ -24,6 +24,8 @@ import os
import re import re
import itertools import itertools
from copy import deepcopy
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError
from ansible.inventory.data import InventoryData from ansible.inventory.data import InventoryData
@ -256,7 +258,12 @@ class InventoryManager(object):
# initialize # initialize
if plugin.verify_file(source): if plugin.verify_file(source):
try: try:
plugin.parse(self._inventory, self._loader, source, cache=cache) # in case plugin fails 1/2 way we dont want partial inventory
inventory = deepcopy(self._inventory)
plugin.parse(inventory, self._loader, source, cache=cache)
# plugin worked! so lets use the more complete inventory
self._inventory = inventory
parsed = True parsed = True
display.vvv('Parsed %s inventory source with %s plugin' % (to_native(source), plugin_name)) display.vvv('Parsed %s inventory source with %s plugin' % (to_native(source), plugin_name))
break break

View file

@ -45,11 +45,11 @@ class BaseInventoryPlugin(object):
TYPE = 'generator' TYPE = 'generator'
def __init__(self, cache=None): def __init__(self):
self.inventory = None self.inventory = None
self.display = display self.display = display
self.cache = cache self._cache = {}
def parse(self, inventory, loader, path, cache=True): def parse(self, inventory, loader, path, cache=True):
''' Populates self.groups from the given data. Raises an error on any parse failure. ''' ''' Populates self.groups from the given data. Raises an error on any parse failure. '''
@ -160,10 +160,9 @@ class BaseFileInventoryPlugin(BaseInventoryPlugin):
TYPE = 'storage' TYPE = 'storage'
def __init__(self, cache=None): def __init__(self):
# file based inventories are always local so no need for cache super(BaseFileInventoryPlugin, self).__init__()
super(BaseFileInventoryPlugin, self).__init__(cache=None)
# Helper methods # Helper methods

View file

@ -58,6 +58,7 @@ from collections import MutableMapping
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
from ansible.plugins.cache import FactCache
from ansible.plugins.inventory import BaseInventoryPlugin from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars
@ -72,6 +73,8 @@ class InventoryModule(BaseInventoryPlugin):
super(InventoryModule, self).__init__() super(InventoryModule, self).__init__()
self._cache = FactCache()
def verify_file(self, path): def verify_file(self, path):
valid = False valid = False
@ -86,7 +89,7 @@ class InventoryModule(BaseInventoryPlugin):
def parse(self, inventory, loader, path, cache=False): def parse(self, inventory, loader, path, cache=False):
''' parses the inventory file ''' ''' parses the inventory file '''
super(InventoryModule, self).parse(inventory, loader, path, cache=True) super(InventoryModule, self).parse(inventory, loader, path, cache=cache)
try: try:
data = self.loader.load_from_file(path) data = self.loader.load_from_file(path)
@ -107,8 +110,8 @@ class InventoryModule(BaseInventoryPlugin):
# get available variables to templar # get available variables to templar
hostvars = inventory.hosts[host].get_vars() hostvars = inventory.hosts[host].get_vars()
if host in inventory.cache: # adds facts if cache is active if host in self._cache: # adds facts if cache is active
hostvars = combine_vars(hostvars, inventory.cache[host]) hostvars = combine_vars(hostvars, self._cache[host])
# create composite vars # create composite vars
self._set_composite_vars(data.get('compose'), hostvars, host, strict=strict) self._set_composite_vars(data.get('compose'), hostvars, host, strict=strict)

View file

@ -151,9 +151,9 @@ class InventoryModule(BaseInventoryPlugin):
self._config_data = {} self._config_data = {}
source_data = None source_data = None
if cache and cache_key in inventory.cache: if cache and cache_key in self._cache:
try: try:
source_data = inventory.cache[cache_key] source_data = self._cache[cache_key]
except KeyError: except KeyError:
pass pass
@ -189,7 +189,7 @@ class InventoryModule(BaseInventoryPlugin):
source_data = cloud_inventory.list_hosts( source_data = cloud_inventory.list_hosts(
expand=expand_hostvars, fail_on_cloud_config=fail_on_errors) expand=expand_hostvars, fail_on_cloud_config=fail_on_errors)
inventory.cache[cache_key] = source_data self._cache[cache_key] = source_data
self._populate_from_source(source_data) self._populate_from_source(source_data)

View file

@ -72,7 +72,7 @@ class InventoryModule(BaseInventoryPlugin):
try: try:
cache_key = self.get_cache_prefix(path) cache_key = self.get_cache_prefix(path)
if not cache or cache_key not in inventory.cache: if not cache or cache_key not in self._cache:
try: try:
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError as e: except OSError as e:
@ -93,11 +93,11 @@ class InventoryModule(BaseInventoryPlugin):
raise AnsibleError("Inventory {0} contained characters that cannot be interpreted as UTF-8: {1}".format(path, to_native(e))) raise AnsibleError("Inventory {0} contained characters that cannot be interpreted as UTF-8: {1}".format(path, to_native(e)))
try: try:
inventory.cache[cache_key] = self.loader.load(data, file_name=path) self._cache[cache_key] = self.loader.load(data, file_name=path)
except Exception as e: except Exception as e:
raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err)) raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err))
processed = inventory.cache[cache_key] processed = self._cache[cache_key]
if not isinstance(processed, Mapping): if not isinstance(processed, Mapping):
raise AnsibleError("failed to parse executable inventory script results from {0}: needs to be a json dict\n{1}".format(path, err)) raise AnsibleError("failed to parse executable inventory script results from {0}: needs to be a json dict\n{1}".format(path, err))

View file

@ -176,9 +176,9 @@ class InventoryModule(BaseInventoryPlugin):
raise AnsibleParserError("Incorrect plugin name in file: %s" % config_data.get('plugin', 'none found')) raise AnsibleParserError("Incorrect plugin name in file: %s" % config_data.get('plugin', 'none found'))
source_data = None source_data = None
if cache and cache_key in inventory.cache: if cache and cache_key in self._cache:
try: try:
source_data = inventory.cache[cache_key] source_data = self._cache[cache_key]
except KeyError: except KeyError:
pass pass
@ -203,6 +203,6 @@ class InventoryModule(BaseInventoryPlugin):
AnsibleParserError(to_native(e)) AnsibleParserError(to_native(e))
source_data = p.stdout.read() source_data = p.stdout.read()
inventory.cache[cache_key] = to_text(source_data, errors='surrogate_or_strict') self._cache[cache_key] = to_text(source_data, errors='surrogate_or_strict')
self._populate_from_source(source_data.splitlines(), config_data) self._populate_from_source(source_data.splitlines(), config_data)