%PDF- %PDF-
Mini Shell

Mini Shell

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

�
���^c@@sdZddlmZmZmZddlmZddlZddlZddl	Z
ddlZ
ddlZ
ddl
Z
ddlmZy8ddlZddlZddlZddlmZWnyek
r=yNddlZej�ddlZddlZddlZddlmZWq>ek
r9dZq>XnXeje�ZdZd�Zdd	�Zede d
��Z!d�Z"d�Z#dd
�Z$dd�Z%d�Z&d�Z'd�Z(d�Z)e*d�Z+d�Z,e dd�Z-d�Z.d�Z/d�Z0dS(u�
Return data to a mysql server

:maintainer:    Dave Boucha <dave@saltstack.com>, Seth House <shouse@saltstack.com>
:maturity:      mature
:depends:       python-mysqldb
:platform:      all

To enable this returner, the minion will need the python client for mysql
installed and the following values configured in the minion or master
config. These are the defaults:

.. code-block:: yaml

    mysql.host: 'salt'
    mysql.user: 'salt'
    mysql.pass: 'salt'
    mysql.db: 'salt'
    mysql.port: 3306

SSL is optional. The defaults are set to None. If you do not want to use SSL,
either exclude these options or set them to None.

.. code-block:: yaml

    mysql.ssl_ca: None
    mysql.ssl_cert: None
    mysql.ssl_key: None

Alternative configuration values can be used by prefacing the configuration
with `alternative.`. Any values not found in the alternative configuration will
be pulled from the default location. As stated above, SSL configuration is
optional. The following ssl options are simply for illustration purposes:

.. code-block:: yaml

    alternative.mysql.host: 'salt'
    alternative.mysql.user: 'salt'
    alternative.mysql.pass: 'salt'
    alternative.mysql.db: 'salt'
    alternative.mysql.port: 3306
    alternative.mysql.ssl_ca: '/etc/pki/mysql/certs/localhost.pem'
    alternative.mysql.ssl_cert: '/etc/pki/mysql/certs/localhost.crt'
    alternative.mysql.ssl_key: '/etc/pki/mysql/certs/localhost.key'

Should you wish the returner data to be cleaned out every so often, set
`keep_jobs` to the number of hours for the jobs to live in the tables.
Setting it to `0` or leaving it unset will cause the data to stay in the tables.

Should you wish to archive jobs in a different table for later processing,
set `archive_jobs` to True.  Salt will create 3 archive tables

- `jids_archive`
- `salt_returns_archive`
- `salt_events_archive`

and move the contents of `jids`, `salt_returns`, and `salt_events` that are
more than `keep_jobs` hours old to these tables.

Use the following mysql database schema:

.. code-block:: sql

    CREATE DATABASE  `salt`
      DEFAULT CHARACTER SET utf8
      DEFAULT COLLATE utf8_general_ci;

    USE `salt`;

    --
    -- Table structure for table `jids`
    --

    DROP TABLE IF EXISTS `jids`;
    CREATE TABLE `jids` (
      `jid` varchar(255) NOT NULL,
      `load` mediumtext NOT NULL,
      UNIQUE KEY `jid` (`jid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    CREATE INDEX jid ON jids(jid) USING BTREE;

    --
    -- Table structure for table `salt_returns`
    --

    DROP TABLE IF EXISTS `salt_returns`;
    CREATE TABLE `salt_returns` (
      `fun` varchar(50) NOT NULL,
      `jid` varchar(255) NOT NULL,
      `return` mediumtext NOT NULL,
      `id` varchar(255) NOT NULL,
      `success` varchar(10) NOT NULL,
      `full_ret` mediumtext NOT NULL,
      `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      KEY `id` (`id`),
      KEY `jid` (`jid`),
      KEY `fun` (`fun`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    --
    -- Table structure for table `salt_events`
    --

    DROP TABLE IF EXISTS `salt_events`;
    CREATE TABLE `salt_events` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `tag` varchar(255) NOT NULL,
    `data` mediumtext NOT NULL,
    `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `master_id` varchar(255) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `tag` (`tag`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Required python modules: MySQLdb

To use the mysql returner, append '--return mysql' to the salt command.

.. code-block:: bash

    salt '*' test.ping --return mysql

To use the alternative configuration, append '--return_config alternative' to the salt command.

.. versionadded:: 2015.5.0

.. code-block:: bash

    salt '*' test.ping --return mysql --return_config alternative

To override individual configuration items, append --return_kwargs '{"key:": "value"}' to the salt command.

.. versionadded:: 2016.3.0

.. code-block:: bash

    salt '*' test.ping --return mysql --return_kwargs '{"db": "another-salt"}'

i(tabsolute_importtprint_functiontunicode_literals(tcontextmanagerN(tsix(tOperationalErrorumysqlcC@s"tt�tdkrdndfS(u:
    Confirm that a python mysql client is installed.
    u!No python mysql client installed.uN(tbooltMySQLdbtNone(((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt__virtual__�sc
C@sidd6dd6dd6dd6dd6dd6dd	6dd
6}idd6dd6dd6dd6dd6dd6d	d	6d
d
6}tjjt||dtdtd
|�}xmtj|�D]\\}}t	|tj
�r�|j�dkr�d||<n|dkr�t|�||<q�q�W|S(u8
    Returns options used for the MySQL connection.
    usaltuhostuuserupassudbi�uportussl_caussl_certussl_keyt__salt__t__opts__tdefaultsunoneN(
Rtsaltt	returnerstget_returner_optionst__virtualname__R
RRt	iteritemst
isinstancetstring_typestlowertint(tretRtattrst_optionstktv((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt_get_options�s8



	$
c
c@sDt|�}t}trxdtkrxy+tjd�td}|j�t}Wqxtk
rt}tjd|�qxXn|r�tjd�y�i}|jd�r�|jd�|d<n|jd�r�|jd�|d<n|jd	�r|jd	�|d
<nt	j
d|jd�d
|jd�d|jd�d|jd�d|jd�d|�}y|td<Wntk
r�nXWq�tk
r�}tj
jdjd|���q�Xn|j�}y	|VWnKt	jk
r}|j}	tjjtj|	��|jd�|�n$X|r3|jd�n
|jd�dS(u
    Return a mysql cursor
    umysql_returner_connu%Trying to reuse MySQL connection pooluOperationalError on ping: %su$Generating new MySQL connection poolussl_caucaussl_certucertussl_keyukeythostuhosttuseruusertpasswdupasstdbudbtportuporttsslu3MySQL returner could not connect to database: {exc}texcuROLLBACKuCOMMITN(RtTruet__context__tlogtdebugtpingtFalseRtgetRtconnectt	TypeErrorR
t
exceptionstSaltMasterErrortformattcursort
DatabaseErrortargstsyststderrtwriteRt	text_typetexecute(
RtcommitRR*tconnR"tssl_optionsR/terrterror((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt	_get_serv�sV




	
%		

c
C@s�|ddkrCtd|jdt��|d<t|d|�ny�t|dt��i}d}|j||d|dtjj	j
|d�|d	|jd
t�tjj	j
|�f�WdQXWn3tjjk
r�}t
j|�t
jd�nXdS(
u'
    Return data to a mysql server
    ujidureqtnocacheunocacheR7u�INSERT INTO `salt_returns`
                     (`fun`, `jid`, `return`, `id`, `success`, `full_ret`)
                     VALUES (%s, %s, %s, %s, %s, %s)ufunureturnuidusuccessNuECould not store return with MySQL returner. MySQL server unavailable.(tprep_jidR)R(t	save_loadR<R#R6R
tutilstjsontdumpsR,R-R%tcritical(RtcurtsqlR"((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytreturner!s#
cC@s�t|dt��n}xd|D]\}|jdd�}|jdd�}d}|j||tjjj|�tdf�qWWdQXdS(u�
    Return event to mysql server

    Requires that configuration be enabled via 'event_return'
    option in master config.
    R7utaguudatau_INSERT INTO `salt_events` (`tag`, `data`, `master_id`)
                     VALUES (%s, %s, %s)uidN(	R<R#R)R6R
R@RARBR(teventsRDteventttagtdataRE((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytevent_return:s
c	C@sbtdt��M}d}y)|j||tjjj|�f�Wntjk
rWnXWdQXdS(u/
    Save the load to the specified jid id
    R7u2INSERT INTO `jids` (`jid`, `load`) VALUES (%s, %s)N(	R<R#R6R
R@RARBRtIntegrityError(tjidtloadtminionsRDRE((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyR?Js)cC@sdS(u&
    Included for API consistency
    N((RMROt	syndic_id((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytsave_minions[scC@shtdddt��M}d}|j||f�|j�}|rZtjjj|d�SiSWdQXdS(u9
    Return the load data that marks a specified jid
    RR7u+SELECT `load` FROM `jids` WHERE `jid` = %s;iN(	R<RR#R6tfetchoneR
R@RAtloads(RMRDRERJ((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_loadbscC@s�tdddt��o}d}|j||f�|j�}i}|r|x0|D]%\}}tjjj|�||<qPWn|SWdQXdS(uP
    Return the information returned when the specified job id was executed
    RR7uHSELECT id, full_ret FROM `salt_returns`
                WHERE `jid` = %sN(	R<RR#R6tfetchallR
R@RARS(RMRDRERJRtminiontfull_ret((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_jidps cC@s�tdddt��r}d}|j||f�|j�}i}|rx3|D](\}}}tjjj|�||<qPWn|SWdQXdS(uC
    Return a dict of the last function called for all minions
    RR7uSELECT s.id,s.jid, s.full_ret
                FROM `salt_returns` s
                JOIN ( SELECT MAX(`jid`) as jid
                    from `salt_returns` GROUP BY fun, id) max
                ON s.jid = max.jid
                WHERE s.fun = %s
                N(	R<RR#R6RUR
R@RARS(tfunRDRERJRRVt_RW((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_fun�s c
C@s�tdddt��x}d}|j|�|j�}i}xE|D]=}tjjj|dtjj	j
|d��||d<qDW|SWdQXdS(u&
    Return a list of all job ids
    RR7u9SELECT DISTINCT `jid`, `load`
                FROM `jids`iiN(R<RR#R6RUR
R@RMtformat_jid_instanceRARS(RDRERJRRM((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_jids�s

(cC@s�tdddt���}d}d}|j|j|r<|nd|��|j�}g}xD|D]<}|jtjj	j
|dtjjj|d���qbW|SWdQXdS(	u�
    Return a list of all job ids
    :param int count: show not more than the count of most recent jobs
    :param bool filter_find_jobs: filter out 'saltutil.find_job' jobs
    RR7u�SELECT * FROM (
                     SELECT DISTINCT `jid` ,`load` FROM `jids`
                     {0}
                     ORDER BY `jid` DESC limit {1}
                     ) `tmp`
                 ORDER BY `jid`;u5WHERE `load` NOT LIKE '%"fun": "saltutil.find_job"%' uiiN(
R<RR#R6R.RUtappendR
R@RMtformat_jid_instance_extRARS(tcounttfilter_find_jobRDREtwhereRJRRM((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_jids_filter�s%
!cC@smtdddt��R}d}|j|�|j�}g}x|D]}|j|d�qDW|SWdQXdS(u"
    Return a list of minions
    RR7u6SELECT DISTINCT id
                FROM `salt_returns`iN(R<RR#R6RUR^(RDRERJRRV((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytget_minions�s

cC@s#|dk	r|Stjjjt�S(uO
    Do any work necessary to prepare a JID, including sending a custom id
    N(RR
R@RMtgen_jidR(R=t
passed_jid((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyR>�sc	C@s�t���}y*d}|j||f�|jd�WnTtjk
r�}tjd�tjtj|��tj	j
tj|���nXy*d}|j||f�|jd�WnTtjk
r
}tjd�tjtj|��tj	j
tj|���nXy*d}|j||f�|jd�WnTtjk
r�}tjd�tjtj|��tj	j
tj|���nXWdQXtS(	uy
    Purge records from the returner tables.
    :param job_age_in_seconds:  Purge jobs older than this
    :return:
    u]delete from `jids` where jid in (select distinct jid from salt_returns where alter_time < %s)uCOMMITuEmysql returner archiver was unable to delete contents of table 'jids'u0delete from `salt_returns` where alter_time < %suMmysql returner archiver was unable to delete contents of table 'salt_returns'u/delete from `salt_events` where alter_time < %suLmysql returner archiver was unable to delete contents of table 'salt_events'N(R<R6RtErrorR%R;RR5R
R,tSaltRunnerErrorR#(t	timestampRDREte((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt_purge_jobs�s4


%c
C@s�dddg}t���}i}x�|D]�}yD|d}dj||�}|j|�|jd�|||<Wq(tjk
r�}tjd�tjtj|��t	j
jtj|���q(Xq(Wy:dj|dd�}|j||f�|jd�Wnvtjk
r]}tjd	�tjtj|��t	j
jtj|���n#tk
r}tj|��nXy:d
j|dd�}|j||f�|jd�WnTtjk
r}tjd�tjtj|��t	j
jtj|���nXy:d
j|dd�}|j||f�|jd�WnTtjk
r�}tjd�tjtj|��t	j
jtj|���nXWd
QXt
|�S(u�
    Copy rows to a set of backup tables, then purge rows.
    :param timestamp: Archive rows older than this timestamp
    :return:
    ujidsusalt_returnsusalt_eventsu_archiveu'create table if not exists {0} like {1}uCOMMITu@mysql returner archiver was unable to create the archive tables.upinsert into `{0}` select * from `{1}` where jid in (select distinct jid from salt_returns where alter_time < %s)uCmysql returner archiver was unable to copy contents of table 'jids'u;insert into `{0}` select * from `{1}` where alter_time < %suKmysql returner archiver was unable to copy contents of table 'salt_returns'uJmysql returner archiver was unable to copy contents of table 'salt_events'N(R<R.R6RRgR%R;RR5R
R,Rht	ExceptionRk(Rit
source_tablesRDt
target_tablest
table_namettmp_table_nameRERj((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt
_archive_jobssX	




#



%cC@stjdt�rttjdd��dkryyt��A}djtd�}|j|�|j�}|dd}WdQXtjdt�r�t|�n
t	|�Wqt
jk
r�}tj
d�tj
tj|��tjjtj|���qXndS(u�
    Called in the master's event loop every loop_interval.  Archives and/or
    deletes the events and job details from the database.
    :return:
    u	keep_jobsiu3select date_sub(now(), interval {0} hour) as stamp;Nuarchive_jobsuDMysql returner was unable to get timestamp for purge/archive of jobs(RR)R(RR<R.R6RURqRkRRgR%R;RR5R
R,Rh(RDREtrowststampRj((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pytclean_old_jobs=s0


(1t__doc__t
__future__RRRt
contextlibRR2tloggingtsalt.returnersR
tsalt.utils.jidtsalt.utils.jsontsalt.exceptionstsalt.extRRtMySQLdb.cursorstMySQLdb.converterstMySQLdb.connectionsRtImportErrortpymysqltinstall_as_MySQLdbtMySQLdb.errRt	getLoggert__name__R%RR	RR(R<RFRKR?RQRTRXR[R]R#RcRdR>RkRqRt(((s8/usr/lib/python2.7/site-packages/salt/returners/mysql.pyt<module>�sX


	(:								%	9

Zerion Mini Shell 1.0