%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/pillar/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/pillar/consul_pillar.pyo

�
���^c@@sdZddlmZmZmZddlZddlZddlmZddl	m
ZddlZ
ddlZ
y+ddlZeed�s�de_nWnek
r�dZnXdZeje�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�ZdS(u0
Use Consul K/V as a Pillar source with values parsed as YAML

:depends:  - python-consul

In order to use an consul server, a profile must be created in the master
configuration file:

.. code-block:: yaml

    my_consul_config:
      consul.host: 127.0.0.1
      consul.port: 8500
      consul.token: b6376760-a8bb-edd5-fcda-33bc13bfc556
      consul.scheme: http
      consul.consistency: default
      consul.dc: dev
      consul.verify: True

All parameters are optional.

The ``consul.token`` requires python-consul >= 0.4.7.

If you have a multi-datacenter Consul cluster you can map your ``pillarenv``s
to your data centers by providing a dictionary of mappings in ``consul.dc``
field:

.. code-block:: yaml

    my_consul_config:
      consul.dc:
        dev: us-east-1
        prod: us-west-1

In the example above we specifying static mapping between Pillar environments
and data centers: the data for ``dev`` and ``prod`` Pillar environments will
be fetched from ``us-east-1`` and ``us-west-1`` datacenter respectively.

In fact when ``consul.dc`` is set to dictionary keys are processed as regular
expressions (that can capture named parameters) and values are processed as
string templates as per PEP 3101.

.. code-block:: yaml

    my_consul_config:
      consul.dc:
        ^dev-.*$: dev-datacenter
        ^(?P<region>.*)-prod$: prod-datacenter-{region}

This example maps all Pillar environments starting with ``dev-`` to
``dev-datacenter`` whereas Pillar environment like ``eu-prod`` will be
mapped to ``prod-datacenter-eu``.

Before evaluation patterns are sorted by length in descending order.

If Pillar environment names correspond to data center names a single pattern
can be used:

.. code-block:: yaml

    my_consul_config:
      consul.dc:
        ^(?P<env>.*)$: '{env}'

After the profile is created, configure the external pillar system to use it.
Optionally, a root may be specified.

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config

    ext_pillar:
      - consul: my_consul_config root=salt

Using these configuration profiles, multiple consul sources may also be used:

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config
      - consul: my_other_consul_config

Either the ``minion_id``, or the ``role``, or the ``environment`` grain  may be used in the ``root``
path to expose minion-specific information stored in consul.

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config root=salt/%(minion_id)s
      - consul: my_consul_config root=salt/%(role)s
      - consul: my_consul_config root=salt/%(environment)s

Minion-specific values may override shared values when the minion-specific root
appears after the shared root:

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config root=salt-shared
      - consul: my_other_consul_config root=salt-private/%(minion_id)s

If using the ``role`` or ``environment`` grain in the consul key path, be sure to define it using
`/etc/salt/grains`, or similar:

.. code-block:: yaml

    role: my-minion-role
    environment: dev

It's possible to lock down where the pillar values are shared through minion
targeting. Note that double quotes ``"`` are required around the target value
and cannot be used inside the matching statement. See the section on Compound
Matchers for more examples.

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config root=salt target="L@salt.example.com and G@osarch:x86_64"

The data from Consul can be merged into a nested key in Pillar.

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config pillar_root=consul_data

By default, keys containing YAML data will be deserialized before being merged into Pillar.
This behavior can be disabled by setting ``expand_keys`` to ``false``.

.. code-block:: yaml

    ext_pillar:
      - consul: my_consul_config expand_keys=false

i(tabsolute_importtprint_functiontunicode_literalsN(tCommandExecutionError(tupdateu__version__u0.1uconsulcC@stdk	rtStS(u3
    Only return if python-consul is installed
    N(tconsultNonet__virtualname__tFalse(((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyt__virtual__�scC@s�i}|}tjd�}|j|�}|r�|jd�|d<|j|jd�d�}tjjjt	�}|j
|dd�}|d}	tjd|	�||	kr�iSntjd	�}
|
j|�}|r|jd�|d
<|j|jd�d�}n
d|d
<tjd�}|j|�}|rj|jd�|d<|j|jd�d�}n
d|d<tjd
�}|j|�}|r�|jd�|d<|j|jd�d�}n
d|d<tjdtj�}
|
j|�}|r%t|d<|j|jd�d�}n
t|d<tt	|d�}tddd�}tddd�}|d
ci|d6|d6|d6;<y�t||d
|d�}|drNtjd|d
|d�i}|}|djd�jd�}xZt|�D]C\}}|t|�dkr3|||<qi||<||}qWn|}Wn2tk
r�tjd|d|d
�i}nX|S(u#
    Check consul for all data
    utarget="(.*?)"iutargetiuucompounduminionsuTargeted minions: %ru
root=(\S*)urootupillar_root=(\S*)upillar_rootu(?:profile=)?(\S+)uprofileuexpand_keys=Falseuexpand_keysu
grains.geturoleuenvironmentu	minion_idu*Merging consul path %s/ into pillar at %s/u/u$No such key in consul profile %s: %sN(tretcompiletsearchtgrouptreplacetsalttutilstminionst	CkMinionst__opts__t
check_minionstlogtdebugRt
IGNORECASERtTruetget_connt__salt__t
fetch_treetrstriptsplitt	enumeratetlentKeyErrorterror(t	minion_idtpillartconftoptsttempt	target_retmatchtcheckert_resRtroot_retpillar_root_ret
profile_retexpand_keys_retclienttroletenvironmenttpillar_treetbranchtkeystitk((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyt
ext_pillar�sz













cC@s|jj|dt�S(u;
    Query consul for all keys/values within base path
    trecurse(tkvtgetR(R/tpath((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pytconsul_fetchsc	C@st||�\}}i}tjd�}tjd|�|dkrJ|Sx�t|�D]�}tjd|dd|d�}|dkr�tjd||�tjd|j|��n|j|�dkrWt	||j
d	�|d
|�}tjd|�qWqWW|S(
u�
    Grab data from consul, trim base path and remove any keys which
    are folders. Take the remaining data and send it to be formatted
    in such a way as to be used as pillar data.
    u/$uFetched items: %ru^u/?uuKeyukey/path - %s: %suhas_children? %ru/uValueuFetching subkeys for key: %rN(R<R
RRRRtreversedtsubRt
pillar_formatR(	R/R;texpand_keyst_titemstretthas_childrentitemtkey((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyRs!"cC@s�|dkr|S|r.tjjj|�}n|}|j�}i||6}|j�x|D]}i||6}q^Wt||�S(ui
    Perform data formatting to be used as pillar data and
    merge it with the current pillar data
    N(RRRtyamlt	safe_loadtpoptreverset
dict_merge(RCR4tvalueR@tpillar_valuetkeyvaluetpilR6((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyR?"s


c	C@sB|jdi�}|jdi�}i}|j|�|j|�|j|�|rl|j|i�}n|}i}x;|D]3}|jd�r||||jd�d<qqWd|kr�|jd�p�d}t|d|�|d<ntr2tjd	kr%|jd
�r%|jd
�ntj|�St	d��dS(
u5
    Return a client object for accessing consul
    upillarumasteruconsul.u.iudcu	pillarenvubaseu0.4.7utargetuw(unable to import consul, module most likely not installed. Download python-consul module and be sure to import consul)N(
R:Rt
startswithRt_resolve_datacenterRt__version__RItConsulR(	R%tprofiletopts_pillartopts_mastertopts_mergedR$tparamsRFt	pillarenv((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyR<s,



"
cC@stjd|�y|j�}Wn"tk
rDtjd|�|SXtjdt|��tjd|�t|dd��}xe|D]]\}}tj||�}|r�tjd|�|j|j	��}tjd|�|Sq�Wtjd	|d
j
d�|D���dS(
u�
    If ``dc`` is a string - return it as is.

    If it's a dict then sort it in descending order by key length and try
    to use keys as RegEx patterns to match against ``pillarenv``.
    The value for matched pattern should be a string (that can use
    ``str.format`` syntax togetehr with captured variables from pattern)
    pointing to targe data center to use.

    If none patterns matched return ``None`` which meanse us datacenter of
    conencted Consul agent.
    u(Resolving Consul datacenter based on: %suUsing pre-defined DC: '%s'u3Selecting DC based on pillarenv using %d pattern(s)uPillarenv set to '%s'RFcS@st|d�|dfS(Ni(R(tm((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyt<lambda>~suMatched pattern: '%s'uResolved datacenter: '%s'u3None of following patterns matched pillarenv=%s: %su, cs@s|]}t|�VqdS(N(trepr(t.0tx((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pys	<genexpr>�sN(RRRBtAttributeErrorRtsortedR
R(tformatt	groupdicttjoin(tdcRYtmappingstsorted_mappingstpatternttargetR(tresult((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyRQds&

(t__doc__t
__future__RRRtloggingR
tsalt.exceptionsRtsalt.utils.dictupdateRRKtsalt.utils.minionsRtsalt.utils.yamlRthasattrRRtImportErrorRRt	getLoggert__name__RR	R7R<RR?RRQ(((s=/usr/lib/python2.7/site-packages/salt/pillar/consul_pillar.pyt<module>�s,

		W				(

Zerion Mini Shell 1.0