[re2o_lookup] Add support for json, yaml, pickle and memcached cache plugins.

certbot_on_virtu
_pollion 2020-05-03 15:49:58 +02:00
parent f73b136b1e
commit c0e02b29ba
2 changed files with 55 additions and 18 deletions

View File

@ -49,6 +49,11 @@ use_cpasswords = True
# Specify cache plugin for re2o API. By default, cache nothing # Specify cache plugin for re2o API. By default, cache nothing
cache = jsonfile cache = jsonfile
# Only used for memcached plugin
# List of connection information for the memcached DBs
# Default is ['127.0.0.1:11211']
# memcached_connection = ['127.0.0.1:11211']
# Time in second before the cache expired. 0 means never expire cache. # Time in second before the cache expired. 0 means never expire cache.
# Default is 24 hours. # Default is 24 hours.
timeout = 86400 timeout = 86400

View File

@ -374,6 +374,27 @@ class LookupModule(LookupBase):
return config.getint(section, key) return config.getint(section, key)
else: else:
return config.get(section, key) return config.get(section, key)
else:
return default
def _manage_cachedir(self, cachedir=None, plugin=None):
try:
self._uri = cachedir / plugin
except Exception:
raise AnsibleError("Undefined specification for cache plugin")
display.vvv("Cache directory is {}".format(self._uri))
if not self._uri.exists():
# Creates Ansible cache directory with right permissions
# if it doesn't exist yet.
display.vvv("Cache directory doesn't exist. Creating it.")
try:
self._uri.mkdir(mode=0o700, parents=True)
except Exception as e:
raise AnsibleError("""Unable to create {dir}.
Original error was : {err}""".format(dir=self._uri,
err=to_native(e)))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -410,29 +431,36 @@ class LookupModule(LookupBase):
if self._cache_plugin is not None: if self._cache_plugin is not None:
display.vvv("Using {} as cache plugin".format(self._cache_plugin)) display.vvv("Using {} as cache plugin".format(self._cache_plugin))
cachedir = Path.home() / ".cache/ansible/re2oapi"
if self._cache_plugin == 'jsonfile': if self._cache_plugin == 'jsonfile':
self._cachedir = Path.home() / ".cache/Ansible/re2oapi" self._manage_cachedir(cachedir=cachedir, plugin='json')
display.vvv("Cache directory is {}".format(self._cachedir)) elif self._cache_plugin == 'yaml':
if not self._cachedir.exists(): self._manage_cachedir(cachedir=cachedir, plugin='yaml')
# Creates Ansible cache directory with right permissions elif self._cache_plugin == 'pickle':
# if it doesn't exist yet. self._manage_cachedir(cachedir=cachedir, plugin='pickle')
display.vvv("Cache directory doesn't exist. Creating it.") elif self._cache_plugin == 'memcached':
try: # requires packages python3-memcache and memcached
self._cachedir.mkdir(mode=0o700, parents=True) display.vvvv("Please make sure you have installed packages"
except Exception as e: "python3-memcache and memcached"
raise AnsibleError("""Unable to create {dir}. )
Original error was : {err}""" self._uri = self._readconfig(key='memcached_connection',
.format(dir=self._cachedir, default=['127.0.0.1:11211'],
err=to_native(e)))
self._cache = cache_loader.get('jsonfile',
_uri=self._cachedir,
_timeout=self._timeout,
) )
else: else:
raise AnsibleError("Cache plugin {} not supported" raise AnsibleError("Cache plugin {} not supported"
.format(self._cache_plugin)) .format(self._cache_plugin))
self._cache = cache_loader.get(self._cache_plugin,
_uri=self._uri,
_timeout=self._timeout,
)
self._cachetoken = cache_loader.get(self._cache_plugin,
_uri=self._uri,
_timeout=self._timeouttoken,
)
def run(self, terms, variables=None, api_hostname=None, api_username=None, def run(self, terms, variables=None, api_hostname=None, api_username=None,
api_password=None, use_tls=True): api_password=None, use_tls=True):
@ -546,7 +574,7 @@ class LookupModule(LookupBase):
zones_name = [zone["name"][1:] for zone in zones] zones_name = [zone["name"][1:] for zone in zones]
display.vvv("Storing dnszones in cache.") display.vvv("Storing dnszones in cache.")
self._set_cache('dnszones', zones_name) self._set_cache('dnszones', zones_name)
display.vvv('\n')
return zones_name return zones_name
def _getreverse(self, api_client): def _getreverse(self, api_client):
@ -615,6 +643,7 @@ class LookupModule(LookupBase):
display.vvv("Storing dns reverse zones in cache.") display.vvv("Storing dns reverse zones in cache.")
self._set_cache('dnsreverse', list(set(res))) self._set_cache('dnsreverse', list(set(res)))
display.vvv('\n')
return res return res
def _rawquery(self, api_client, endpoint): def _rawquery(self, api_client, endpoint):
@ -629,6 +658,8 @@ class LookupModule(LookupBase):
res = api_client.list(endpoint) res = api_client.list(endpoint)
display.vvv("Storing result in cache.") display.vvv("Storing result in cache.")
self._set_cache(endpoint.replace('/', '_'), res) self._set_cache(endpoint.replace('/', '_'), res)
display.vvv('\n')
return res return res
def _get_role(self, api_client, role_name): def _get_role(self, api_client, role_name):
@ -655,4 +686,5 @@ class LookupModule(LookupBase):
display.vvv("Storing {} in cache.".format(role_name)) display.vvv("Storing {} in cache.".format(role_name))
self._set_cache(role_name, res) self._set_cache(role_name, res)
display.vvv('\n')
return res return res