�
�Udac @` s� d d l m Z m Z m Z e Z i d d 6d g d 6d d 6Z d Z d d l Z d d
l m
Z
d d l m Z
d d l m Z d d
l m Z d d l m Z y d d l Z Wn e k
r� e d � � n Xe � Z d e f d � � YZ d S( i ( t absolute_importt divisiont print_functions 1.1t metadata_versiont previewt statust communityt supported_bys�
cache: mongodb
short_description: Use MongoDB for caching
description:
- This cache uses per host records saved in MongoDB.
version_added: "2.5"
requirements:
- pymongo>=3
options:
_uri:
description:
- MongoDB Connection String URI
required: False
env:
- name: ANSIBLE_CACHE_PLUGIN_CONNECTION
ini:
- key: fact_caching_connection
section: defaults
_prefix:
description: User defined prefix to use when creating the DB entries
default: ansible_facts
env:
- name: ANSIBLE_CACHE_PLUGIN_PREFIX
ini:
- key: fact_caching_prefix
section: defaults
_timeout:
default: 86400
description: Expiration timeout in seconds for the cache plugin data
env:
- name: ANSIBLE_CACHE_PLUGIN_TIMEOUT
ini:
- key: fact_caching_timeout
section: defaults
type: integer
N( t contextmanager( t constants( t AnsibleError( t BaseCacheModule( t Displays^ The 'pymongo' python module is required for the mongodb fact cache, 'pip install pymongo>=3.0't CacheModulec B` s� e Z d Z d � Z d � Z e d � � Z d � Z d � Z d � Z d � Z
d � Z d � Z d
� Z
d � Z d � Z d
� Z RS( s-
A caching module backed by mongodb.
c O` s� yY t t | � j | | � | j d � | _ t | j d � � | _ | j d � | _ WnN t k
r� t j
d d d �t j | _ t t j
� | _ t j | _ n Xi | _ t | _ d S( Nt _urit _timeoutt _prefixsT Rather than importing CacheModules directly, use ansible.plugins.loader.cache_loadert versions 2.12( t superR
t __init__t
get_optiont _connectiont intR R t KeyErrort displayt
deprecatedt Ct CACHE_PLUGIN_CONNECTIONt CACHE_PLUGIN_TIMEOUTt CACHE_PLUGIN_PREFIXt _cachet Falset _managed_indexes( t selft argst kwargs( ( sA /usr/lib/python2.7/site-packages/ansible/plugins/cache/mongodb.pyR D s
c C` s} | j } | rl | d k rl y | j d d d d | �Wqy t j j k
rh | j d � | j | � SXn
| j d � d S( s�
This function manages indexes on the mongo collection.
We only do this once, at run time based on _managed_indexes,
rather than per connection instantiation as that would be overkill
i t datet namet ttlt expireAfterSecondsN( R t create_indext pymongot errorst OperationFailuret
drop_indext _manage_indexes( R! t
collectionR ( ( sA /usr/lib/python2.7/site-packages/ansible/plugins/cache/mongodb.pyR- T s
c c` s� t j | j � } y | j � } Wn! t j j k
rE | d } n X| d } | j sr | j | � t | _ n | V| j � d S( s
This is a context manager for opening and closing mongo connections as needed. This exists as to not create a global
connection, due to pymongo not being fork safe (http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe)
t ansiblet cacheN(
R) t MongoClientR t get_default_databaseR* t ConfigurationErrorR R- t Truet close( R! t mongot dbR. ( ( sA /usr/lib/python2.7/site-packages/ansible/plugins/cache/mongodb.pyt _collectioni s
c C` s d | j | f S( Ns %s%s( R ( R! t key( ( sA /usr/lib/python2.7/site-packages/ansible/plugins/cache/mongodb.pyt _make_key� s c C` sg | | j k rW | j � �&