File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/tops/__pycache__/mongo.cpython-310.pyc
Back
o ;j� � @ sj d Z ddlZddlZzddlZdZW n ey dZY nw dddddd�Zd d � Ze�e �Z dd� ZdS ) a Read tops data from a mongodb collection This module will load tops data from a mongo collection. It uses the node's id for lookups. Salt Master Mongo Configuration =============================== The module shares the same base mongo connection variables as :py:mod:`salt.returners.mongo_return`. These variables go in your master config file. * ``mongo.db`` - The mongo database to connect to. Defaults to ``'salt'``. * ``mongo.host`` - The mongo host to connect to. Supports replica sets by specifying all hosts in the set, comma-delimited. Defaults to ``'salt'``. * ``mongo.port`` - The port that the mongo database is running on. Defaults to ``27017``. * ``mongo.user`` - The username for connecting to mongo. Only required if you are using mongo authentication. Defaults to ``''``. * ``mongo.password`` - The password for connecting to mongo. Only required if you are using mongo authentication. Defaults to ``''``. Configuring the Mongo Tops Subsystem ==================================== .. code-block:: yaml master_tops: mongo: collection: tops id_field: _id re_replace: "" re_pattern: \.example\.com states_field: states environment_field: environment Module Documentation ==================== � NTF�salt� i�i )�mongo.db� mongo.host�mongo.password� mongo.port� mongo.userc C s t sdS dS )NF�mongo)�HAS_PYMONGO� r r �C/opt/saltstack/salt/lib/python3.10/site-packages/salt/tops/mongo.py�__virtual__@ s r c K s� t d }t d }t d d �dd�}t d d �dd�}t d d �d d �}t d d �dd �}t d d �dd �}t d d �dd�}t�d||� t�||�} t�dt d � | t d } t �d�}t �d�}|ry|ryt�d|� | �||� | d d } |r�t� ||| �} t�d|| � | | j || i||gd�}|r�||v r�||v r�|| }nd}t�d� ||| iS t�d|� i S )a{ Connect to a mongo database and read per-node tops data. Parameters: * `collection`: The mongodb collection to read data from. Defaults to ``'tops'``. * `id_field`: The field in the collection that represents an individual minion id. Defaults to ``'_id'``. * `re_pattern`: If your naming convention in the collection is shorter than the minion id, you can use this to trim the name. `re_pattern` will be used to match the name, and `re_replace` will be used to replace it. Backrefs are supported as they are in the Python standard library. If ``None``, no mangling of the name will be performed - the collection will be searched with the entire minion id. Defaults to ``None``. * `re_replace`: Use as the replacement value in node ids matched with `re_pattern`. Defaults to ''. Feel free to use backreferences here. * `states_field`: The name of the field providing a list of states. * `environment_field`: The name of the field providing the environment. Defaults to ``environment``. r r Zmaster_topsr � collectionZtops�id_fieldZ_id� re_patternr � re_replace�states_fieldZstates�environment_field�environmentz&connecting to %s:%s for mongo ext_topszusing database '%s'r r r zauthenticating as '%s'�opts�idz=ext_tops.mongo: looking up tops def for {'%s': '%s'} in mongo)Z projection�basez0ext_tops.mongo: found document, returning statesz2ext_tops.mongo: no document found in collection %s)�__opts__�get�log�info�pymongoZMongoClient�debugZauthenticate�re�subZfind_one)�kwargs�host�portr r r r r r �connZmdb�user�passwordZ minion_id�resultr r r r �topJ sL � �� r'