%PDF- %PDF-
Mini Shell

Mini Shell

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

�
���^c@@sIdZddlmZmZmZddlZddlZddlZddlm	Z	ddl
ZddlZddl
mZddlmZmZmZmZddlm	Z	ejd�Zejd�Zejd	�Zeje�Zyeed
<Wnek
riZnXdefd��YZd
�Z dded�Z!dS(ub&
Python renderer that includes a Pythonic Object based interface

:maintainer: Evan Borgstrom <evan@borgstrom.ca>

Let's take a look at how you use pyobjects in a state file. Here's a quick
example that ensures the ``/tmp`` directory is in the correct state.

.. code-block:: python
   :linenos:

    #!pyobjects

    File.managed("/tmp", user='root', group='root', mode='1777')

Nice and Pythonic!

By using the "shebang" syntax to switch to the pyobjects renderer we can now
write our state data using an object based interface that should feel at home
to python developers. You can import any module and do anything that you'd
like (with caution, importing sqlalchemy, django or other large frameworks has
not been tested yet). Using the pyobjects renderer is exactly the same as
using the built-in Python renderer with the exception that pyobjects provides
you with an object based interface for generating state data.

Creating state data
-------------------

Pyobjects takes care of creating an object for each of the available states on
the minion. Each state is represented by an object that is the CamelCase
version of its name (i.e. ``File``, ``Service``, ``User``, etc), and these
objects expose all of their available state functions (i.e. ``File.managed``,
``Service.running``, etc).

The name of the state is split based upon underscores (``_``), then each part
is capitalized and finally the parts are joined back together.

Some examples:

* ``postgres_user`` becomes ``PostgresUser``
* ``ssh_known_hosts`` becomes ``SshKnownHosts``

Context Managers and requisites
-------------------------------

How about something a little more complex. Here we're going to get into the
core of how to use pyobjects to write states.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Pkg.installed("nginx"):
        Service.running("nginx", enable=True)

        with Service("nginx", "watch_in"):
            File.managed("/etc/nginx/conf.d/mysite.conf",
                         owner='root', group='root', mode='0444',
                         source='salt://nginx/mysite.conf')


The objects that are returned from each of the magic method calls are setup to
be used a Python context managers (``with``) and when you use them as such all
declarations made within the scope will **automatically** use the enclosing
state as a requisite!

The above could have also been written use direct requisite statements as.

.. code-block:: python
   :linenos:

    #!pyobjects

    Pkg.installed("nginx")
    Service.running("nginx", enable=True, require=Pkg("nginx"))
    File.managed("/etc/nginx/conf.d/mysite.conf",
                 owner='root', group='root', mode='0444',
                 source='salt://nginx/mysite.conf',
                 watch_in=Service("nginx"))

You can use the direct requisite statement for referencing states that are
generated outside of the current file.

.. code-block:: python
   :linenos:

    #!pyobjects

    # some-other-package is defined in some other state file
    Pkg.installed("nginx", require=Pkg("some-other-package"))

The last thing that direct requisites provide is the ability to select which
of the SaltStack requisites you want to use (require, require_in, watch,
watch_in, use & use_in) when using the requisite as a context manager.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Service("my-service", "watch_in"):
        ...

The above example would cause all declarations inside the scope of the context
manager to automatically have their ``watch_in`` set to
``Service("my-service")``.

Including and Extending
-----------------------

To include other states use the ``include()`` function. It takes one name per
state to include.

To extend another state use the ``extend()`` function on the name when creating
a state.

.. code-block:: python
   :linenos:

    #!pyobjects

    include('http', 'ssh')

    Service.running(extend('apache'),
                    watch=[File('/etc/httpd/extra/httpd-vhosts.conf')])


Importing from other state files
--------------------------------

Like any Python project that grows you will likely reach a point where you want
to create reusability in your state tree and share objects between state files,
Map Data (described below) is a perfect example of this.

To facilitate this Python's ``import`` statement has been augmented to allow
for a special case when working with a Salt state tree. If you specify a Salt
url (``salt://...``) as the target for importing from then the pyobjects
renderer will take care of fetching the file for you, parsing it with all of
the pyobjects features available and then place the requested objects in the
global scope of the template being rendered.

This works for all types of import statements; ``import X``,
``from X import Y``, and ``from X import Y as Z``.

.. code-block:: python
   :linenos:

    #!pyobjects

    import salt://myfile.sls
    from salt://something/data.sls import Object
    from salt://something/data.sls import Object as Other


See the Map Data section for a more practical use.

Caveats:

* Imported objects are ALWAYS put into the global scope of your template,
  regardless of where your import statement is.


Salt object
-----------

In the spirit of the object interface for creating state data pyobjects also
provides a simple object interface to the ``__salt__`` object.

A function named ``salt`` exists in scope for your sls files and will dispatch
its attributes to the ``__salt__`` dictionary.

The following lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    ret = salt.cmd.run(bar)
    ret = __salt__['cmd.run'](bar)

Pillar, grain, mine & config data
---------------------------------

Pyobjects provides shortcut functions for calling ``pillar.get``,
``grains.get``, ``mine.get`` & ``config.get`` on the ``__salt__`` object. This
helps maintain the readability of your state files.

Each type of data can be access by a function of the same name: ``pillar()``,
``grains()``, ``mine()`` and ``config()``.

The following pairs of lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    value = pillar('foo:bar:baz', 'qux')
    value = __salt__['pillar.get']('foo:bar:baz', 'qux')

    value = grains('pkg:apache')
    value = __salt__['grains.get']('pkg:apache')

    value = mine('os:Fedora', 'network.interfaces', 'grain')
    value = __salt__['mine.get']('os:Fedora', 'network.interfaces', 'grain')

    value = config('foo:bar:baz', 'qux')
    value = __salt__['config.get']('foo:bar:baz', 'qux')


Map Data
--------

When building complex states or formulas you often need a way of building up a
map of data based on grain data. The most common use of this is tracking the
package and service name differences between distributions.

To build map data using pyobjects we provide a class named Map that you use to
build your own classes with inner classes for each set of values for the
different grain matches.

.. code-block:: python
   :linenos:

    #!pyobjects

    class Samba(Map):
        merge = 'samba:lookup'
        # NOTE: priority is new to 2017.7.0
        priority = ('os_family', 'os')

        class Ubuntu:
            __grain__ = 'os'
            service = 'smbd'

        class Debian:
            server = 'samba'
            client = 'samba-client'
            service = 'samba'

        class RHEL:
            __match__ = 'RedHat'
            server = 'samba'
            client = 'samba'
            service = 'smb'

.. note::
    By default, the ``os_family`` grain will be used as the target for
    matching. This can be overridden by specifying a ``__grain__`` attribute.

    If a ``__match__`` attribute is defined for a given class, then that value
    will be matched against the targeted grain, otherwise the class name's
    value will be be matched.

    Given the above example, the following is true:

    1. Minions with an ``os_family`` of **Debian** will be assigned the
       attributes defined in the **Debian** class.
    2. Minions with an ``os`` grain of **Ubuntu** will be assigned the
       attributes defined in the **Ubuntu** class.
    3. Minions with an ``os_family`` grain of **RedHat** will be assigned the
       attributes defined in the **RHEL** class.

    That said, sometimes a minion may match more than one class. For instance,
    in the above example, Ubuntu minions will match both the **Debian** and
    **Ubuntu** classes, since Ubuntu has an ``os_family`` grain of **Debian**
    and an ``os`` grain of **Ubuntu**. As of the 2017.7.0 release, the order is
    dictated by the order of declaration, with classes defined later overriding
    earlier ones. Additionally, 2017.7.0 adds support for explicitly defining
    the ordering using an optional attribute called ``priority``.

    Given the above example, ``os_family`` matches will be processed first,
    with ``os`` matches processed after. This would have the effect of
    assigning ``smbd`` as the ``service`` attribute on Ubuntu minions. If the
    ``priority`` item was not defined, or if the order of the items in the
    ``priority`` tuple were reversed, Ubuntu minions would have a ``service``
    attribute of ``samba``, since ``os_family`` matches would have been
    processed second.

To use this new data you can import it into your state file and then access
your attributes. To access the data in the map you simply access the attribute
name on the base class that is extending Map. Assuming the above Map was in the
file ``samba/map.sls``, you could do the following.

.. code-block:: python
   :linenos:

    #!pyobjects

    from salt://samba/map.sls import Samba

    with Pkg.installed("samba", names=[Samba.server, Samba.client]):
        Service.running("samba", name=Samba.service)

i(tabsolute_importtprint_functiontunicode_literalsN(tsix(tget_file_client(tRegistrytStateFactoryt
SaltObjecttMapu'^\s*from\s+(salt:\/\/.*)\s+import (.*)$u^\s*import\s+(salt:\/\/.*)$u^(.*) as (.*)$upyobjects_loadedtPyobjectsModulecB@s eZdZd�Zd�ZRS(u)This provides a wrapper for bare imports.cC@s||_||_dS(N(tnamet__dict__(tselfR
tattrs((s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pyt__init__Ks	cC@sdj|j�S(Nu<module '{0!s}' (pyobjects)>(tformatR
(R((s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pyt__repr__Os(t__name__t
__module__t__doc__RR(((s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pyR	Hs	c	C@s�i}tjjt�td<ttd<tjjt�}tjjtd|�}tjjt�}tjjt|||�}xnt	j
|�D]]\}}d|kr�q�n|jdd�\}}||kr�i||<n||||<q�W|td<dS(u9
    This loads our states into the salt __context__
    ugrainsupillartutilsu.iupyobjects_statesN(
tsalttloadertgrainst__opts__t
__pillar__Rtminion_modstserializerststatesRt	iteritemstsplitt__context__(	Rt
lazy_utilst
lazy_funcstlazy_serializerstlazy_statestkeytfunctmod_namet	func_name((s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pytload_statesSs$
	
ubaseuc@s�dtkrt�ni�itd6}x�tdD]�}i}djg|jd�D]}|j�^qV�}	djtd|�}
dj|	||
�}tj|||�||	�|	<q4Wt	j
�d<t	j�d<tt
_t
�d	<y_�jitt�d
6tdd6td
d6tdd6tdd6td6td6td6�Wntk
ranX|sl�Stt��tt	_����fd���|�\}}
�j|
�tt	_tj|��t	j�S(Nupyobjects_statesuStateFactoryuu_u','u0{0} = StateFactory('{1!s}', valid_funcs=['{2}'])uincludeuextenduMapusaltu
pillar.getupillaru
grains.getugrainsumine.getumineu
config.getuconfigu__salt__u
__pillar__u
__grains__c@s=g}t��}x|j�D]}|jd�}t}x�ttfD]�}|j|�}|shqGn|jd�j�}y|jd�j	d�}Wnt
k
r�d}nX�j|��}	|	s�t
dj|���ntjjj|	��}
�|
�\}}WdQXtj||�|dkrgtjjtjj|	��d}
t|
|�||
<n�x�|D]�}|j�}}tj|�}|dk	r�|jd�j�}|jd�j�}n||kr�t
dj||���n||||<qnWt}PqGW|s|j|�qqWdj|�|fS(	Nu
iiu,uCould not find the file '{0}'iu'{0}' was not found in '{1}'u
(tdictt	readlinestrstriptFalset	IMPORT_REtFROM_REtmatchtgrouptstripRt
IndexErrortNonet
cache_filetImportErrorRRRtfilestfopenRtexec_tostpathtsplitexttbasenameR	t
FROM_AS_REtTruetappendtjoin(ttemplatet
template_datattemplate_globalstlinetmatchedtREtmatchestimport_filetimportst
state_filetstate_fhtstate_contentst
state_globalstimport_nameR
talias(t_globalstclienttprocess_templatetsaltenv(s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pyRR�sR

"
(RR(RR@Rt
capitalizeRRR8Rtincludetmake_extendt__salt__RtupdateRRt
__grains__t	NameErrorRRR,tenabledR>t	salt_data(RARStslsR\tkwargstmod_globalstmodt
mod_localstpartt	mod_cameltvalid_funcstmod_cmdtfinal_templatet
final_globals((RPRQRRRSs</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pytrenderpsV

(	

	
	

	?
	("Rt
__future__RRRtloggingR9tretsalt.extRtsalt.utils.filesRtsalt.loadertsalt.fileclientRtsalt.utils.pyobjectsRRRRtcompileR.R-R=t	getLoggerRtlogR>RRZtobjectR	R(Rh(((s</usr/lib/python2.7/site-packages/salt/renderers/pyobjects.pyt<module>*s*"

	

Zerion Mini Shell 1.0