%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/netapi/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyo

�
���^c@@sLdZddlmZmZmZddlZddlZddlZddlZddl	Zddl
Zidd6dd6dd	6d
d6dd
6dd6dd6ZdZej
e�Zd�Zdefd��YZd�Zd�Zd�Zdd�Zd�Zd�Zd�Zd�Zd�Zd�Zed krHe�ndS(!u�
A minimalist REST API for Salt
==============================

This ``rest_wsgi`` module provides a no-frills REST interface for sending
commands to the Salt master. There are no dependencies.

Extra care must be taken when deploying this module into production. Please
read this documentation in entirety.

All authentication is done through Salt's :ref:`external auth <acl-eauth>`
system.

Usage
=====

* All requests must be sent to the root URL (``/``).
* All requests must be sent as a POST request with JSON content in the request
  body.
* All responses are in JSON.

.. seealso:: :py:mod:`rest_cherrypy <salt.netapi.rest_cherrypy.app>`

    The :py:mod:`rest_cherrypy <salt.netapi.rest_cherrypy.app>` module is
    more full-featured, production-ready, and has builtin security features.

Deployment
==========

The ``rest_wsgi`` netapi module is a standard Python WSGI app. It can be
deployed one of two ways.

Using a WSGI-compliant web server
---------------------------------

This module may be run via any WSGI-compliant production server such as Apache
with mod_wsgi or Nginx with FastCGI.

It is strongly recommended that this app be used with a server that supports
HTTPS encryption since raw Salt authentication credentials must be sent with
every request. Any apps that access Salt through this interface will need to
manually manage authentication credentials (either username and password or a
Salt token). Tread carefully.

:program:`salt-api` using a development-only server
---------------------------------------------------

If run directly via the salt-api daemon it uses the `wsgiref.simple_server()`__
that ships in the Python standard library. This is a single-threaded server
that is intended for testing and development. **This server does not use
encryption**; please note that raw Salt authentication credentials must be sent
with every HTTP request.

**Running this module via salt-api is not recommended!**

In order to start this module via the ``salt-api`` daemon the following must be
put into the Salt master config::

    rest_wsgi:
        port: 8001

.. __: http://docs.python.org/2/library/wsgiref.html#module-wsgiref.simple_server

Usage examples
==============

.. http:post:: /

    **Example request** for a basic ``test.ping``::

        % curl -sS -i \
                -H 'Content-Type: application/json' \
                -d '[{"eauth":"pam","username":"saltdev","password":"saltdev","client":"local","tgt":"*","fun":"test.ping"}]' localhost:8001

    **Example response**:

    .. code-block:: http

        HTTP/1.0 200 OK
        Content-Length: 89
        Content-Type: application/json

        {"return": [{"ms--4": true, "ms--3": true, "ms--2": true, "ms--1": true, "ms--0": true}]}

    **Example request** for an asynchronous ``test.ping``::

        % curl -sS -i \
                -H 'Content-Type: application/json' \
                -d '[{"eauth":"pam","username":"saltdev","password":"saltdev","client":"local_async","tgt":"*","fun":"test.ping"}]' localhost:8001

    **Example response**:

    .. code-block:: http

        HTTP/1.0 200 OK
        Content-Length: 103
        Content-Type: application/json

        {"return": [{"jid": "20130412192112593739", "minions": ["ms--4", "ms--3", "ms--2", "ms--1", "ms--0"]}]}

    **Example request** for looking up a job ID::

        % curl -sS -i \
                -H 'Content-Type: application/json' \
                -d '[{"eauth":"pam","username":"saltdev","password":"saltdev","client":"runner","fun":"jobs.lookup_jid","jid":"20130412192112593739"}]' localhost:8001

    **Example response**:

    .. code-block:: http

        HTTP/1.0 200 OK
        Content-Length: 89
        Content-Type: application/json

        {"return": [{"ms--4": true, "ms--3": true, "ms--2": true, "ms--1": true, "ms--0": true}]}

:form lowstate: A list of lowstate data appropriate for the
    :ref:`client <client-apis>` interface you are calling.
:status 200: success
:status 401: authentication required

i(tabsolute_importtprint_functiontunicode_literalsNu200 OKi�u400 BAD REQUESTi�u401 UNAUTHORIZEDi�u
404 NOT FOUNDi�u405 METHOD NOT ALLOWEDi�u406 NOT ACCEPTABLEi�u500 INTERNAL SERVER ERRORi�u	rest_wsgicC@s&tjti�}d|kr"tStS(Nuport(t__opts__tgett__virtualname__tFalse(tmod_opts((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyt__virtual__�st	HTTPErrorcB@seZdZd�ZRS(uM
    A custom exception that can take action based on an HTTP error code
    cC@s)||_tj|dj||��dS(Nu{0}: {1}(tcodet	Exceptiont__init__tformat(tselfR
tmessage((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyR�s	(t__name__t
__module__t__doc__R(((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyR	�scC@sUytj|�Wn=tk
rP}|jtjkrJtjj|�rJqQ�nXdS(u?
    mkdir -p
    http://stackoverflow.com/a/600612/127816
    N(tostmakedirstOSErrorterrnotEEXISTtpathtisdir(Rtexc((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytmkdir_p�s$cC@sA|jdd�}|dkr$dn	t|�}|dj|�S(u6
    Pull the body from the request and return it
    uCONTENT_LENGTHu0uiu
wsgi.input(Rtinttread(tenvirontlength((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyt	read_body�scC@sv|jdd�}|dkr0tdd��nytjjjt|��SWn"tk
rq}td|��nXdS(u)
    Return the request body as JSON
    uCONTENT_TYPEuuapplication/jsoni�u
JSON requiredi�N(RR	tsalttutilstjsontloadsR t
ValueError(Rtcontent_typeR((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytget_json�scC@s?itt|��d6}|r/|j|�nt|j��S(u�
    Takes the response data as well as any additional headers and returns a
    tuple of tuples of headers suitable for passing to start_response()
    uContent-Length(tstrtlentupdatetlisttitems(tdatat
extra_headerstresponse_headers((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytget_headers�s
cc@s-|d}x|D]}|j|�VqWdS(u]
    Expects a list of lowstate dictionaries that are executed and returned in
    order
    uSALT_APIClientN(trun(Rtlowstatetclienttchunk((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyt	run_chunk�s

cC@sX|dj�}|dkr dS|dkrEt|�}t||�Stdd��dS(u|
    Do any path/method dispatching here and return a JSON-serializable data
    structure appropriate for the response
    uREQUEST_METHODuGETuFThey found me. I don't know how, but they found me. Run for it, Marty!uPOSTi�uMethod Not AllowedN(tupperR'R5R	(RtmethodR-((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytdispatch�s
cC@scdt�kr?ddl}|jjtjjdd��}n||d<|jj|�|d<dS(uO
    Make Salt's opts dict and the APIClient available in the WSGI environ
    u__opts__iNuSALT_MASTER_CONFIGu/etc/salt/masteru	SALT_OPTSuSALT_APIClient(	tlocalstsalt.configtconfigt
client_configRRRtnetapitNetapiClient(RR!R((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytsaltenviron�s	
cC@st|�ytt|��}d}Wnvtk
rP}|j}t|�}nOtjjk
rz}d}t|�}n%t	k
r�}d}t|�}nXy tj
jji|d6�}Wn%t
k
r�}d}t|�}nX|t|t|idd6��|fS(up
    Process the request and return a JSON response. Catch errors and return the
    appropriate HTTP code.
    i�i�i�ureturnuapplication/jsonuContent-Type(R?R+R8R	R
R(R!t
exceptionstEauthAuthenticationErrorRR"R#tdumpst	TypeErrortHR0(Rtstart_responsetrespR
Rtret((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytapplications*

	 cC@s+ddl}|jjtjjdd��S(u3
    Return the Salt master config as __opts__
    iNuSALT_MASTER_CONFIGu/etc/salt/master(R:R;R<RRR(R!((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytget_opts*s	cC@s�ddlm}dt�krPt�t�d<t�tkrPtd��qPntjt	i�}|d|dt
�}y|j�Wntk
r�td��nXdS(u
    Start simple_server()
    i(tmake_serveru__opts__iu	localhostuportN(
twsgiref.simple_serverRJtglobalsRIRRt
SystemExitRRRRHt
serve_forevertKeyboardInterrupt(RJRthttpd((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pytstart4s
u__main__(Rt
__future__RRRRtloggingRR!tsalt.netapitsalt.utils.jsonRDRt	getLoggertloggerRRR	RR R'tNoneR0R5R8R?RHRIRQR(((s9/usr/lib/python2.7/site-packages/salt/netapi/rest_wsgi.pyt<module>{s>
					
				
	$	
	

Zerion Mini Shell 1.0