%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/thread-self/root/usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //proc/thread-self/root/usr/lib/python2.7/site-packages/salt/modules/postgres.pyo

�
���^c@@s�	dZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZyddlZe
ZWnek
r�eZnXddlZddlZddlZddlZddlZddlmZmZddlmZddlmZddlm Z ddlm!Z!ej"e#�Z$e
Z%d	Z&d
Z'dZ(dZ)e&e'e(e)fZ*i
d
d6dd6dd6dd6dd6dd6dd6dd6dd6dd 6d!d"6d#d$6d%d&6Z+e,d}�Z-id/d+6dd(6dd)6d0d*6d1d'6d2d-6d d.6Z.d3�Z/d4�Z0dddddd5�Z2d6ddd7ddded8�Z3ddddddd9�Z4ddddddd:�Z5ddddd;�Z6d<�Z7ddddddd=�Z8dddddded>�Z9ddddddd?�Z:ddddddd@�Z;dAdB�Z<dddddddddddddC�Z=dddddddeddD�	Z>dddddddE�Z?dddddddF�Z@dddddddG�ZAdddddddddH�ZBdddddddddddI�
ZCdddddddJ�ZDddddddedK�ZEddddddedL�ZFdddddddM�ZGddNdOedP�ZHe%dQ�ZIdOdRdddddddddddddS�ZJddddddddddddddddRdddT�ZKddddddddddddddddddU�ZLdddddddRddddddddddddV�ZMddddddddddddddddddW�ZNdddddddX�ZOdddddddY�ZPdddddddZ�ZQddddddd[�ZRddddddd\�ZSddddddd]�ZTd^�ZUddddddd_�ZVddddddddd`�ZWdddddddddda�	ZXdddddddddddb�
ZYdddddddc�ZZddddddddddddddddd�Z[dddddddddddddddde�Z\dddddddf�Z]ddddddg�Z^dddddddh�Z_ddddddi�Z`ddddddj�Zaddddddk�Zbddddddl�Zcddddddm�Zdddddddn�Zeddddddo�Zfddddddp�Zgdq�Zhdrddddddds�Zidt�Zjdu�Zkdv�Zldrdddddddw�Zmdddrdddddddx�	Zndddrdddddddy�	Zoddrdddddddz�Zpd6ddd7ddedd{�Zqd|�ZrdS(~u:
Module to provide Postgres compatibility to salt.

:configuration: In order to connect to Postgres, certain configuration is
    required in /etc/salt/minion on the relevant minions. Some sample configs
    might look like::

        postgres.host: 'localhost'
        postgres.port: '5432'
        postgres.user: 'postgres' -> db user
        postgres.pass: ''
        postgres.maintenance_db: 'postgres'

    The default for the maintenance_db is 'postgres' and in most cases it can
    be left at the default setting.
    This data can also be passed into pillar. Options passed into opts will
    overwrite options passed into pillar

:note: This module uses MD5 hashing which may not be compliant with certain
    security audits.

:note: When installing postgres from the official postgres repos, on certain
    linux distributions, either the psql or the initdb binary is *not*
    automatically placed on the path. Add a configuration to the location
    of the postgres bin's path to the relevant minion for this module::

        postgres.bins_dir: '/usr/pgsql-9.5/bin/'
i(tabsolute_importtunicode_literalstprint_functionN(tCommandExecutionErrortSaltInvocationError(tLooseVersion(tsix(tzip(tStringIOuEXTENSION NOT INSTALLEDuEXTENSION INSTALLEDuEXTENSION TO UPGRADEuEXTENSION TO MOVEuINSERTuauCREATEuCuTRUNCATEuDuCONNECTucuTRIGGERutuSELECTuruUSAGEuUu	TEMPORARYuTuUPDATEuwuEXECUTEuXu
REFERENCESuxuDELETEuduGRANTu*uschemau
tablespaceulanguageusequenceutableugroupudatabaseufunctionuarwdDxturwUuUCuCTccC@s_dg}tstSxE|D]=}tjjj|�st|�sWtdj|�fSqqWtS(u�
    Only load this module if the psql bin exist.
    initdb bin might also be used, but its presence will be detected on runtime.
    upsqlu{0} was not found(	tHAS_CSVtFalsetsalttutilstpathtwhicht_find_pg_binarytformattTrue(Rtutil((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt__virtual__ts	
cC@s^tdd�}tjjj|�}|sV|rZtjjjtjj||��Sn|SdS(uc
    ... versionadded::  2016.3.2

    Helper function to locate various psql related binaries
    u
config.optionupostgres.bins_dirN(t__salt__RRR
Rtostjoin(Rt
pg_bin_dirtutil_bin((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyR�s%c

C@sitd6td6}|dkr�|s9tdd�}n|sO|jd�r�dtdkrhd}q�d	tdkr�d
}q�d}q�n|dkr�|}n|r�||d<n|dkr�tdd
�}n|dk	r�tjjj	dt�}tjjj
|d���}|jtjjj
dj|sA|jd�rGdn||rV|nd|re|nd|���td||d�i|d6|d<WdQXntd|dt|�}	|	jdd�dkr�tjd�n|dk	rtd|�rtjd�n|	S(u�
    Helper function to call psql, because the password requirement
    makes this too much code to be repeated in each function below
    ureset_system_localeu	clean_envu
config.optionu
postgres.hostu/uFreeBSDu	os_familyupgsqluOpenBSDu_postgresqlupostgresurunasu
postgres.passttextuwu{0}:{1}:*:{2}:{3}u	localhostu*u
file.chownuu
PGPASSFILEuenvNucmd.run_alltpython_shelluretcodeiu%Error connecting to Postgresql serverufile.removeuRemove PGPASSFILE failed(R
RtNoneRt
startswitht
__grains__RRtfilestmkstemptfopentwritetstringutilstto_strRtgettlogterrortwarning(
tcmdtrunastpasswordthosttporttusertkwargst
pgpassfiletfp_tret((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt	_run_psql�sD
			

upassworduUTF8c	C@sf|dkrGdtdkr%d}qGdtdkr>d}qGd}n|dkr\|}ntd�}	|	s}td��n|	d	j|�d
j|�dj|�dj|�g}
|dk	r�|
jd
j|��n|dk	r|
jd�|
j|�n|r|
jd�n|dk	r�tjjj	dt
�}tjjj|d��@}|jtjj
jdj|���td||d�WdQX|
jdj|�g�ntd|dt
�}
djg|
D]}tj|�^q��}td|dt|
�}|jdd�dkr5tjd�n|dk	rbtd|�rbtjd �n|S(!u(
    Helper function to call initdb
    uFreeBSDu	os_familyupgsqluOpenBSDu_postgresqlupostgresuinitdbuinitdb executable not found.u--pgdata={0}u--username={0}u
--auth={0}u--encoding={0}u--locale={0}u-Xu--data-checksumsRuwu{0}u
file.chownuNu--pwfile={0}R)t	clean_envu ucmd.run_allRuretcodeiu-Error initilizing the postgres data directoryufile.removeuRemoval of PGPASSFILE failed(RRRRRtappendRRRRRR R!R"R#RtextendtdictRtpipestquoteR
R$R%R&R'(tnametauthR-R*tencodingtlocaleR)twaldirt	checksumst_INITDB_BINR(R/R0R.tctcmdstrR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_run_initdb�sL				
%+c
C@s�d}td|dd|d|d|d|d|�}t|d	|d|d|d|d|�}x%tjjj|d
d�D]}	|	SWdS(
u�
    Return the version of a Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.version
    uHSELECT setting FROM pg_catalog.pg_settings WHERE name = 'server_version'u-cu-tR+R-R,tmaintenance_dbR*R)ustdoutu
N(t	_psql_cmdR2RRt	itertoolstsplit(
R-R+R,RCR*R)tqueryR(R1tline((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytversions		'#cC@sOt|d|d|d|d|d|�}|r:t|�Stjd�dSdS(u�
    Returns the server version properly parsed and int casted for internal use.

    If the Postgres server does not respond, None will be returned.
    R+R,RCR*R)uMAttempt to parse version of Postgres server failed. Is the server responding?N(RIt
_LooseVersionR%R'R(R-R+R,RCR*R)tpsql_version((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_parsed_version#s	

cC@st|stdd�}n|s2tdd�}n|sKtdd�}n|sdtdd�}n||||fS(uz
    Returns a tuple of (user, host, port, db) with config, pillar, or default
    values assigned to missing values.
    u
config.optionu
postgres.useru
postgres.hostu
postgres.portupostgres.maintenance_db(R(R-R+R,RC((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_connection_defaults<scO@s�t|jd�|jd�|jd�|jd��\}}}}td�}|dddd	g}|r|d
|g7}n|r�|d|g7}n|r�|dtj|�g7}n|s�d
}n|jd|g�|j|�|S(u�
    Return string with fully composed psql command.

    Accepts optional keyword arguments: user, host, port and maintenance_db,
    as well as any number of positional arguments to be added to the end of
    the command.
    uuseruhostuportumaintenance_dbupsqlu
--no-alignu
--no-readlineu--no-psqlrcu
--no-passwordu
--usernameu--hostu--portupostgresu--dbname(RMR$RRt	text_typeR5(targsR.R-R+R,RCt	_PSQL_BINR(((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyRDMs,		
c	C@sRtd|d|d|d||�}t|d|d|d|d|d|�}|S(NR+R-R,RCR)R*(RDR2(	R(R+R,RCR*R)R-trcmdtcmdret((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_psql_prepare_and_runms	'cC@s:g}dj|j�jd��}	|r<dj|	�}	ntddd|	gd|d|d	|d
|d|d|�}
|
d
dkr�|St|
d�}i}x{tj|dtjj	j
d�dtjj	j
d��D]@}
|
s�q�n|s|
}q�n|jtt
||
���q�W|r6|dd!}n|S(u�
    Run an SQL-Query and return the results as a list. This command
    only supports SELECT statements.  This limitation can be worked around
    with a query like this:

    WITH updated AS (UPDATE pg_authid SET rolconnlimit = 2000 WHERE
    rolname = 'rolename' RETURNING rolconnlimit) SELECT * FROM updated;

    query
        The query string.

    user
        Database username, if different from config or default.

    host
        Database host, if different from config or default.

    port
        Database port, if different from the config or default.

    maintenance_db
        The database to run the query against.

    password
        User password, if different from the config or default.

    runas
        User to run the command as.

    write
        Mark query as READ WRITE transaction.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.psql_query 'select * from pg_stat_activity'
    u$COPY ({0}) TO STDOUT WITH CSV HEADERu;u6START TRANSACTION READ WRITE; {0}; COMMIT TRANSACTION;u-vudatestyle=ISO,MDYu-cR)R+R-R,RCR*uretcodeiustdoutt	delimiteru,t	quotecharu"i����(RtstriptrstripRSRtcsvtreaderRRR"R#R4R6R(RGR-R+R,RCR*R)R!R1t	csv_queryRRtcsv_filetheadertrow((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt
psql_query}s6(		 c
C@sti}d}t|d|d|d|d|d|d|�}x1|D])}	|	||	d<||	djd�qCW|S(	u�
    Return dictionary with information about databases of a Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.db_list
    u6SELECT datname as "Name", pga.rolname as "Owner", pg_encoding_to_char(encoding) as "Encoding", datcollate as "Collate", datctype as "Ctype", datacl as "Access privileges", spcname as "Tablespace" FROM pg_database pgd, pg_roles pga, pg_tablespace pgts WHERE pga.oid = pgd.datdba AND pgts.oid = pgd.dattablespaceR)R+R-R,RCR*uName(R^tpop(
R-R+R,RCR*R)R1RGtrowsR]((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytdb_list�s	
c
C@s7td|d|d|d|d|d|�}||kS(u�
    Checks if a database exists on the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.db_exists 'dbname'
    R-R+R,RCR*R)(Ra(R9R-R+R,RCR*R)t	databases((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt	db_exists�su'cC@sM|dkrdS||kr7tdj||���ndjd|d|�S(Nu'Unsupported character {0} in value: {1}u{quote}{value}{quote}R8tvalue(RRR(RdR8((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_quote_ddl_valuesc
C@s3dj|�}
tjjjdt|d�fdt|
d�fd|fdt|�fdt|�fdt|	�fg�}g}x<|j�D].\}}|dk	r�||d	|g7}q�q�W|r�|jd
d�|
dj	|�7}
nt
d
|
gd|d|d|d|d|d|�}|dd
kS(u�
    Adds a databases to the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.db_create 'dbname'

        salt '*' postgres.db_create 'dbname' template=template_postgis

    uCREATE DATABASE "{0}"u
TABLESPACEu"uOWNERuTEMPLATEuENCODINGu
LC_COLLATEuLC_CTYPEu=iu WITHu u-cR-R+R,RCR*R)uretcodeN(RRRtodicttOrderedDictRetitemsRtinsertRRS(R9R-R+R,RCR*t
tablespaceR;t
lc_collatetlc_ctypetownerttemplateR)RGt	with_argstwith_chunkstkeyRdR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt	db_create
s(	c

C@s�t||f�stS|rR|rRt||d|d|d|d|d|	�}
n�g}|rz|jdj||��n|r�|jdj||��nxD|D]<}td|gd|d|d|d	|d|d|	�}
q�W|
d
dkr�tStS(u�
    Change tablespace or/and owner of database.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.db_alter dbname owner=otheruser
    R-R+R,R*R)u#ALTER DATABASE "{0}" OWNER TO "{1}"u)ALTER DATABASE "{0}" SET TABLESPACE "{1}"u-cRCuretcodei(tanyRtowner_toR4RRSR
(
R9R-R+R,RCR*RjRmt
owner_recurseR)R1tqueriesRG((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytdb_alterFs0
c	C@sSdj|�}td|gd|d|d|d|d|d|�}|d	d
kS(u�
    Removes a databases from the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.db_remove 'dbname'
    uDROP DATABASE "{0}"u-cR-R+R,R)RCR*uretcodei(RRS(	R9R-R+R,RCR*R)RGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt	db_removers
	c
C@sxi}d}td|d|d|d|d|d|d|�}x1|D])}	|	||	d	<||	d	jd	�qGW|S(
u�
    Return dictionary with information about tablespaces of a Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.tablespace_list

    .. versionadded:: 2015.8.0
    u�SELECT spcname as "Name", pga.rolname as "Owner", spcacl as "ACL", spcoptions as "Opts", pg_tablespace_location(pgts.oid) as "Location" FROM pg_tablespace pgts, pg_roles pga WHERE pga.oid = pgts.spcownerupostgres.psql_queryR)R+R-R,RCR*uName(RR_(
R-R+R,RCR*R)R1RGR`R]((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyttablespace_list�s	
c
C@s7td|d|d|d|d|d|�}||kS(u�
    Checks if a tablespace exists on the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.tablespace_exists 'dbname'

    .. versionadded:: 2015.8.0
    R-R+R,RCR*R)(Ry(R9R-R+R,RCR*R)ttablespaces((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyttablespace_exists�sc
C@s�d}
d}|r$dj|�}
n|rygtj|�D]\}}
dj||
�^q:}djdj|��}ndj||
||�}td|gd|d	|d
|d|d|d
|	�}|ddkS(u�
    Adds a tablespace to the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.tablespace_create tablespacename '/path/datadir'

    .. versionadded:: 2015.8.0
    uuOWNER "{0}"u	{0} = {1}uWITH ( {0} )u, u.CREATE TABLESPACE "{0}" {1} LOCATION '{2}' {3}u-cR-R+R,RCR*R)uretcodei(RRt	iteritemsRRS(R9tlocationtoptionsRmR-R+R,RCR*R)towner_queryt
options_querytktvtoptionstextRGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyttablespace_create�s 4		cC@s t||||	g�stSg}|rD|jdj||��n|rf|jdj||��n|r�|jdj||j�d|j�d��n|	r�|jdj||	��nxX|D]P}td|gd|d|d	|d
|d|d|
�}
|
d
dkr�tSq�WtS(u�
    Change tablespace name, owner, or options.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.tablespace_alter tsname new_owner=otheruser
        salt '*' postgres.tablespace_alter index_space new_name=fast_raid
        salt '*' postgres.tablespace_alter test set_option="{'seq_page_cost': '1.1'}"
        salt '*' postgres.tablespace_alter tsname reset_option=seq_page_cost

    .. versionadded:: 2015.8.0
    u&ALTER TABLESPACE "{0}" RENAME TO "{1}"u%ALTER TABLESPACE "{0}" OWNER TO "{1}"u&ALTER TABLESPACE "{0}" SET ({1} = {2})iu"ALTER TABLESPACE "{0}" RESET ({1})u-cR-R+R,RCR*R)uretcode(RsRR4RtkeystvaluesRSR
(R9R-R+R,RCR*tnew_namet	new_ownert
set_optiontreset_optionR)RvRGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyttablespace_alter�s.'
c	C@sSdj|�}td|gd|d|d|d|d|d|�}|d	d
kS(u�
    Removes a tablespace from the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.tablespace_remove tsname

    .. versionadded:: 2015.8.0
    uDROP TABLESPACE "{0}"u-cR-R+R,R)RCR*uretcodei(RRS(	R9R-R+R,RCR*R)RGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyttablespace_removes
	c@si}td|d|d|d|d|d|�}|r~|td�krTd}	nd	}	|td
�krud	}
q�d}
ntjd�tS�fd
�}djd|d�d|d�|d�g�j|
|	�}t|d|d|d|d|d|d|�}
d�}x�|
D]�}i}x!d%D]}|||�||<q-WxMd&D]E}y!tjj	||d�||<WqQt
k
r�d'||<qQXqQW|d|d<�r�|d |d <ntt
j|d!jd"�g��d#|d!<|||d$<qW|S((u�
    Return a dict with information about users of a Postgres server.

    Set return_password to True to get password hash in the result.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.user_list
    R-R+R,RCR*R)u9.1upg_roles.rolreplicationuNULLu9.5upg_roles.rolcatupdateuBCould not retrieve Postgres version. Is Postgresql server running?c@s�r
|SdS(Nu((ts(treturn_password(s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt<lambda>Rsuu�SELECT pg_roles.rolname as "name",pg_roles.rolsuper as "superuser", pg_roles.rolinherit as "inherits privileges", pg_roles.rolcreaterole as "can create roles", pg_roles.rolcreatedb as "can create databases", {0} as "can update system catalogs", pg_roles.rolcanlogin as "can login", {1} as "replication", pg_roles.rolconnlimit as "connections", (SELECT array_agg(pg_roles2.rolname)    FROM pg_catalog.pg_auth_members    JOIN pg_catalog.pg_roles pg_roles2 ON (pg_auth_members.roleid = pg_roles2.oid)    WHERE pg_auth_members.member = pg_roles.oid) as "groups",pg_roles.rolvaliduntil::timestamp(0) as "expiry time", pg_roles.rolconfig  as "defaults variables" uB, COALESCE(pg_shadow.passwd, pg_authid.rolpassword) as "password" uFROM pg_roles u4LEFT JOIN pg_authid ON pg_roles.oid = pg_authid.oid u8LEFT JOIN pg_shadow ON pg_roles.oid = pg_shadow.usesysidcS@s0||dkrtS||dkr(tSdSdS(uW
        Returns the boolean value of the key, instead of 't' and 'f' strings.
        utufN(RR
R(trowdictRq((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytget_boolss
u	superuseruinherits privilegesucan create rolesucan create databasesucan update system catalogsu	can loginureplicationuconnectionsuexpiry timeu%Y-%m-%d %H:%M:%Sudefaults variablesupasswordugroupsu{}iuname(u	superuseruinherits privilegesucan create rolesucan create databasesucan update system catalogsu	can loginureplicationuconnections(uexpiry timeN(RLRJR%R&R
RRR^tdatetimetstrptimet
ValueErrorRtlistRXRYRV(R-R+R,RCR*R)R�R1tvertreplication_columntrolcatupdate_columnt_xRGR`R�R]tretrowRqtdate_key((R�s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt	user_list.sd				
					


	
-c	C@smtd|d|d|d|d|d|d|�}y|j|d	�SWntk
rhtjd�d	SXd	S(
u�
    Return a dict with information about users of a Postgres server.

    Set return_password to True to get password hash in the result.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.role_get postgres
    R-R+R,RCR*R)R�u6Could not retrieve Postgres role. Is Postgres running?N(R�R$RtAttributeErrorR%R&(	R9R-R+R,RCR*R)R�t	all_users((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytrole_get�s
	

cC@s:tt|d|d|d|d|d|d|dt��S(u�
    Checks if a user exists on the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.user_exists 'username'
    R-R+R,RCR*R)R�(tboolR�R
(R9R-R+R,RCR*R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytuser_exists�s
	uNOucC@s||sx|dkr|}n|dk	rZ|rBdj||�}qZdj|||�}n|rxdj||�}qxn|S(Nu{0} {1}u
{0} {2}{1}(RR(tstringttesttflagtcondtprefixtaddtxttskip((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_add_role_flag�s	cC@sz|dk	rtj|�}n|rv|rv|jd�rvdjtjtjj	j
dj||���j��}n|S(uQ
    pgsql passwords are md5 hashes of the string: 'md5{password}{rolename}'
    umd5umd5{0}u{0}{1}N(RRRNRRthashlibtmd5RRR"tto_bytest	hexdigest(troleR*t	encrypted((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_maybe_encrypt_password�s6urolecC@sc|dkr$|dkr$t}q$n|dkr]|dkrEt}n|dkr]t}q]n|dkrrt}nt}d}d}|dk	r�t|tj�r�t|�p�t|t�s�t}nt|tj�rt|�rdjt	||j
dd�d|��}nt|
tj�rMt|
�rMdj|
j
dd��}nt}t|�r�t|	�t|d�kr�t}nid	d
6|d6idd
6|d6id
d
6|d6idd
6|	d6|d6idd
6|d6idd
6|d6idd
6t|�d6tj|�d6|dkd6idd
6|dk	oDt|�d6|pZt|t�d6|d6dd6idd
6t|�d6|d6|d6idd
6t|
�d6|
dkd6|d6f
}x|D]}t||�}q�W|j
d�r|j
dd�}n|
r_t|
t�r-dj|
�}
nx/|
jd�D]}dj|||�}q=Wn|S(Nuuserugroupuu'{0}'u'u''R�u	superuseruINHERITuflagutestuCREATEDBu
CREATEROLEu	SUPERUSERuskipuREPLICATIONuLOGINuCONNECTION LIMITuaddtxtu	ENCRYPTEDuconduUNuprefixuPASSWORDuVALID UNTILuWITHu WITHu,u{0}; GRANT "{1}" TO "{2}"(uuserugroup(RRR
t_DEFAULT_PASSWORDS_ENCRYPTIONt
isinstanceRtstring_typesR�RR�treplaceRNR�tendswithR�RRF(R9tsub_cmdttyp_R�tlogint	connlimittinherittcreatedbtcreaterolest	superusertgroupstreplicationtrolepasswordtvalid_untiltdb_roletskip_passwdtescaped_passwordtescaped_valid_untiltskip_superusertflagstdatatgroup((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_role_cmd_args�sz			(	








cC@s�t|||||d|d|�rAtjd|j�|�tSdj|�}dj|t|d|d|d|
d	|d
|d|d|d
|	d|d|
d|d|��}td|gd|d|d|d|d|d|�}|ddkS(u|
    Creates a Postgres role. Users and Groups are both roles in postgres.
    However, users can login, groups cannot.
    R*R)u%s '%s' already existsuCREATE ROLE "{0}" WITHu{0} {1}R�R�R�R�R�R�R�R�R�R�R�R�u-cR+R-R,RCuretcodei(R�R%tinfot
capitalizeR
RR�RS(R9R-R+R,RCR*R�R�R�R�R�R�R�R�R�R�R�R�R)R�R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_role_createCs0	c&C@svt|ddd|d|d|d|d|d|d	|d
|d|	d|
d
|d|d|
d|d|d|d|�S(u
    Creates a Postgres user.

    CLI Examples:

    .. code-block:: bash

        salt '*' postgres.user_create 'username' user='user' \
                host='hostname' port='port' password='password' \
                rolepassword='rolepassword' valid_until='valid_until'
    R�uuserR-R+R,RCR*R�R�R�R�R�R�R�R�R�R�R�R)(R�(tusernameR-R+R,RCR*R�R�R�R�R�R�R�R�R�R�R�R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytuser_createys&	cC@st|d|d|d|d|d|d|dt�}t|�s_tjd|j�|�tSd	j|�}d
j|t|d|d|
d
|d|	d|d|d|
d|d|d|d|d|��}td|gd|d|d|d|d|d|�}|ddkS(u"
    Updates a postgres role.
    R-R+R,RCR*R)R�u%s '%s' could not be founduALTER ROLE "{0}" WITHu{0} {1}R�R�R�R�R�R�R�R�R�R�R�R�u-curetcodei(	R�R
R�R%R�R�RR�RS(R9R-R+R,RCR*R�R�R�R�R�R�R�R�R�R�R�R�R)R�R�R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_role_update�s@			c&C@svt|d|d|d|d|d|ddd|
d	|d
|d|d|d
|d|	d|
d|d|d|d|�S(u
    Updates a Postgres user.

    CLI Examples:

    .. code-block:: bash

        salt '*' postgres.user_update 'username' user='user' \
                host='hostname' port='port' password='password' \
                rolepassword='rolepassword' valid_until='valid_until'
    R-R+R,RCR*R�uuserR�R�R�R�R�R�R�R�R�R�R�R)(R�(R�R-R+R,RCR*R�R�R�R�R�R�R�R�R�R�R�R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytuser_update�s&	cC@s�t|||||d|d|�s8tjd|�tSdj|�}td|gd|d|d|d|d	|d|�t|||||d|d|�s�tStjd
|�tSdS(u1
    Removes a role from the Postgres Server
    R*R)uUser '%s' does not existuDROP ROLE "{0}"u-cR+R-R,RCuFailed to delete user '%s'.N(R�R%R�R
RRSR(R9R-R+R,RCR*R)R�((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_role_removes
c
C@s�g}d}t|d|d|d|d|d|d|�}i}x7|D]/}	d|	krId	|	krI|	||	d	<qIqIW|S(
u�
    List available postgresql extensions

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.available_extensions

    u(select * from pg_available_extensions();R-R+R,RCR*R)udefault_versionuname(R^(
R-R+R,RCR*R)textsRGR1R]((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytavailable_extensions6s
c
C@s�g}d}t|d|d|d|d|d|d|�}i}x7|D]/}	d|	krId	|	krI|	||	d	<qIqIW|S(
u�
    List installed postgresql extensions

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.installed_extensions

    ugselect a.*, b.nspname as schema_name from pg_extension a,  pg_namespace b where a.extnamespace = b.oid;R-R+R,RCR*R)u
extversionuextname(R^(
R-R+R,RCR*R)R�RGR1R]((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytinstalled_extensionsUs
c
C@s7td|d|d|d|d|d|�j|d�S(u�
    Get info about an available postgresql extension

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.get_available_extension plpgsql

    R-R+R,RCR*R)N(R�R$R(R9R-R+R,RCR*R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytget_available_extensiontsc
C@s7td|d|d|d|d|d|�j|d�S(u�
    Get info about an installed postgresql extension

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.get_installed_extension plpgsql

    R-R+R,RCR*R)N(R�R$R(R9R-R+R,RCR*R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytget_installed_extension�sc	
C@s`td|d|d|d|d|d|�}|j�g|D]}|j�^q=kr\tStS(u�
    Test if a specific extension is available

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.is_available_extension

    R-R+R,RCR*R)(R�tlowerRR
(	R9R-R+R,RCR*R)R�ta((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytis_available_extension�s		cC@st|�t|�kS(u�
    Compare versions of extensions using salt.utils.versions.LooseVersion.

    Returns ``True`` if version a is lesser than b.
    (RJ(R�tb((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_pg_is_older_ext_ver�scC@s:t|d|d|d|d|d|d|�}t|�S(u�
    Test if a specific extension is installed

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.is_installed_extension

    R-R+R,RCR*R)(R�R�(R9R-R+R,RCR*R)t
installed_ext((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytis_installed_extension�s	c	C@s�t|d|d|d|d|d|d|�}	tg}
|	r�tg}
|dk	rt|	jd|�|�r|
jt�n|dk	r�|	jdd	�d
kr�|	jd|�|kr�|
jt�q�n|
S(
u�
    Get lifecycle information about an extension

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.create_metadata adminpack

    R-R+R,RCR*R)u
extversionuextrelocatableufutuschema_nameN(	R�t_EXTENSION_NOT_INSTALLEDt_EXTENSION_INSTALLEDRR�R$R4t_EXTENSION_TO_UPGRADEt_EXTENSION_TO_MOVE(R9text_versiontschemaR-R+R,RCR*R)R�R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytcreate_metadata�s*				c

C@sh|dkrt}n|dkr*t}n|dkr?t}ndg}
|r^|
jd�n|
j|�|r�|
jd�n|r�|
jd�n|
jd�dj|
�}t|d|d|d	|d
|d|d|	�rtd
|gd|	d|d|d	|d
|d|�nt|d|d|d	|d
|d|d|	�}|sdtjd|�n|S(u�
    Drop an installed postgresql extension

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.drop_extension 'adminpack'

    uDROP EXTENSIONu	IF EXISTSuCASCADEuRESTRICTu;u R-R+R,RCR*R)u-cuFailed to drop ext: %sN(	RRR
R4RR�RSR%R�(
R9t	if_existstrestricttcascadeR-R+R,RCR*R)ROR(R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytdrop_extensionsJ				

			
cC@s�|dkrt}nt|d|d|d|d|d|d|d|	d|
�}t|k}t|d|d|d|d|d|	d|
�}
|
r0|syd	g}|r�|jd
�n|jdj|��g}|r�|jdj|��n|r|jd
j|��n|r1|jdj|��n|rT|jd�|j|�n|jd�dj|�j	�}nwg}|r�t
|kr�|jdj||��n|r�t|kr�|jdj||��ndj|�j	�}|r0td|gd|
d|d|d|d|d|	�q0nt|d|d|d|d|d|d|d|	d|
�}t}x/t
D]'}||kry|tkryt}qyqyW|s�tjd|�n|S(u�
    Install a postgresql extension

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.create_extension 'adminpack'

    R�R�R-R+R,RCR*R)uCREATE EXTENSIONu
IF NOT EXISTSu"{0}"uSCHEMA "{0}"uVERSION {0}uFROM {0}uWITHu;u u'ALTER EXTENSION "{0}" SET SCHEMA "{1}";u$ALTER EXTENSION "{0}" UPDATE TO {1};u-cuFailed to create ext: %sN(RRR�R�R�R4RR5RRVR�R�RSt_EXTENSION_FLAGSR�R
R%R�(R9t
if_not_existsR�R�tfrom_versionR-R+R,RCR*R)tmtdatat	installedtinstallableROtsargsR(R1ti((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytcreate_extensionTs�						

		

cC@s.t|d|d|d|d|d|d|�S(u�
    Removes a user from the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.user_remove 'username'
    R-R+R,RCR*R)(R�(R�R-R+R,RCR*R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytuser_remove�s	c"C@sjt|d|ddd|d|d|d|d|d	|d
|d|	d|
d
|d|d|
d|d|�S(u?
    Creates a Postgres group. A group is postgres is similar to a user, but
    cannot login.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.group_create 'groupname' user='user' \
                host='hostname' port='port' password='password' \
                rolepassword='rolepassword'
    R-R�ugroupR+R,RCR*R�R�R�R�R�R�R�R�R�R)(R�(t	groupnameR-R+R,RCR*R�R�R�R�R�R�R�R�R�R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytgroup_create�s"	c"C@sjt|d|d|d|d|d|d|ddd	|d
|d|
d|	d
|d|d|
d|d|�S(u�
    Updates a postgres group

    CLI Examples:

    .. code-block:: bash

        salt '*' postgres.group_update 'username' user='user' \
                host='hostname' port='port' password='password' \
                rolepassword='rolepassword'
    R-R+R,RCR*R�R�ugroupR�R�R�R�R�R�R�R�R)(R�(R�R-R+R,RCR*R�R�R�R�R�R�R�R�R�R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytgroup_update�s"	cC@s.t|d|d|d|d|d|d|�S(u�
    Removes a group from the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.group_remove 'groupname'
    R-R+R,RCR*R)(R�(R�R-R+R,RCR*R)((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytgroup_remove&s	cC@s-tj�}|jd�|jdj||��dddddf}x||D]t\}	}
t|
d
|d|d|d|d|d|�}x5|D]-}|j|	jd|d|d�d�q�WqNW|jd�|j�tj|jd�t	d|jgd
|d|d|d|d|d|�}
|
S(u�
    Set the owner of all schemas, functions, tables, views and sequences to
    the given username.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.owner_to 'dbname' 'username'
    ubegin;
u%alter database "{0}" owner to "{1}";
u"alter schema {n} owner to {owner};uFselect quote_ident(schema_name) as n from information_schema.schemata;u!alter table {n} owner to {owner};u�select quote_ident(table_schema)||'.'||quote_ident(table_name) as n from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema');u$alter function {n} owner to {owner};u�select p.oid::regprocedure::text as n from pg_catalog.pg_proc p join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where ns.nspname not in ('pg_catalog', 'information_schema')  and not p.proisagg;u%alter aggregate {n} owner to {owner};u�select p.oid::regprocedure::text as n from pg_catalog.pg_proc p join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where ns.nspname not in ('pg_catalog', 'information_schema') and p.proisagg;u$alter sequence {n} owner to {owner};ulselect quote_ident(sequence_schema)||'.'||quote_ident(sequence_name) as n from information_schema.sequences;R-R+R,RCR*R)Rmtnunu
ucommit;
i�u-f(u"alter schema {n} owner to {owner};uFselect quote_ident(schema_name) as n from information_schema.schemata;(u!alter table {n} owner to {owner};u�select quote_ident(table_schema)||'.'||quote_ident(table_name) as n from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema');(u$alter function {n} owner to {owner};u�select p.oid::regprocedure::text as n from pg_catalog.pg_proc p join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where ns.nspname not in ('pg_catalog', 'information_schema')  and not p.proisagg;(u%alter aggregate {n} owner to {owner};u�select p.oid::regprocedure::text as n from pg_catalog.pg_proc p join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where ns.nspname not in ('pg_catalog', 'information_schema') and p.proisagg;(u$alter sequence {n} owner to {owner};ulselect quote_ident(sequence_schema)||'.'||quote_ident(sequence_name) as n from information_schema.sequences;(
ttempfiletNamedTemporaryFileR!RR^tflushRtchmodR9RS(tdbnamet	ownernameR-R+R,R*R)tsqlfileRvtfmtRGR1R]RR((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyRt?s>

	
/

	c
C@s�t||d|d|d|d|d|�rDtjd||�tSdj|�}|dk	rtdj||�}ntd	|gd|d
|d|d|d
|d|�}	|	ddkS(u
    Creates a Postgres schema.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.schema_create dbname name owner='owner' \
                user='user' \
                db_user='user' db_password='password'
                db_host='hostname' db_port='port'
    R-tdb_usertdb_passwordtdb_hosttdb_portu'%s' already exists in '%s'uCREATE SCHEMA "{0}"u{0} AUTHORIZATION "{1}"u-cR*R,R+RCR)uretcodeiN(t
schema_existsR%R�R
RRRS(
R�R9RmR-RRRRR�R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt
schema_create�scC@s�t||ddd|d|d|d|�sDtjd||�tSdj|�}td|gd	|d
|d|d|d|d
|�t|||d|d|d|d|�s�tStjd|�tSdS(us
    Removes a schema from the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.schema_remove dbname schemaname

    dbname
        Database name we work on

    schemaname
        The schema's name we'll remove

    user
        System user all operations should be performed on behalf of

    db_user
        database username if different from config or default

    db_password
        user password if any password for a specified user

    db_host
        Database host if different from config or default

    db_port
        Database port if different from config or default

    R-RRRRu"Schema '%s' does not exist in '%s'uDROP SCHEMA "{0}"u-cR)RCR+R,R*uFailed to delete schema '%s'.N(RRR%R�R
RRSR(R�R9R-RRRRR�((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt
schema_remove�s"%cC@s1tt||d|d|d|d|d|��S(up
    Checks if a schema exists on the Postgres server.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.schema_exists dbname schemaname

    dbname
        Database name we query on

    name
       Schema name we look for

    user
        The system user the operation should be performed on behalf of

    db_user
        database username if different from config or default

    db_password
        user password if any password for a specified user

    db_host
        Database host if different from config or default

    db_port
        Database port if different from config or default

    R-RRRR(R�t
schema_get(R�R9R-RRRR((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyR�s"cC@sdt|d|d|d|d|d|�}y|j|d�SWntk
r_tjd�tSXdS(up
    Return a dict with information about schemas in a database.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.schema_get dbname name

    dbname
        Database name we query on

    name
       Schema name we look for

    user
        The system user the operation should be performed on behalf of

    db_user
        database username if different from config or default

    db_password
        user password if any password for a specified user

    db_host
        Database host if different from config or default

    db_port
        Database port if different from config or default
    R-RRRRu8Could not retrieve Postgres schema. Is Postgres running?N(tschema_listR$RR�R%R&R
(R�R9R-RRRRtall_schemas((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyRs!	

cC@s�i}djdg�}t|d|d|d|d|d|d|�}xA|D]9}	i}
xdD]}|	||
|<qbW|
||	d<qOW|S(
uL
    Return a dict with information about schemas in a Postgres database.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.schema_list dbname

    dbname
        Database name we query on

    user
        The system user the operation should be performed on behalf of

    db_user
        database username if different from config or default

    db_password
        user password if any password for a specified user

    db_host
        Database host if different from config or default

    db_port
        Database port if different from config or default
    uu�SELECT pg_namespace.nspname as "name",pg_namespace.nspacl as "acl", pg_roles.rolname as "owner" FROM pg_namespace LEFT JOIN pg_roles ON pg_roles.oid = pg_namespace.nspowner R)R+R-R,RCR*uowneruacluname(uowneruacl(RR^(R�R-RRRRR1RGR`R]R�Rq((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyR	Cs	

c
C@sci}d}t|d|d|d|d|d|d|�}x |D]}	|	d||	d<qCW|S(	uI
    .. versionadded:: 2016.3.0

    Return a list of languages in a database.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.language_list dbname

    maintenance_db
        The database to check

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    u)SELECT lanname AS "Name" FROM pg_languageR)R+R-R,RCR*uName(R^(
RCR-R+R,R*R)R1RGR`R]((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt
language_list}s%	
cC@s4t|d|d|d|d|d|�}||kS(u}
    .. versionadded:: 2016.3.0

    Checks if language exists in a database.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.language_exists plpgsql dbname

    name
       Language to check for

    maintenance_db
        The database to check in

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of

    R-R+R,R*R)(R(R9RCR-R+R,R*R)t	languages((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytlanguage_exists�s
*	c	C@syt||�r&tjd||�tSdj|�}td|gd|d|d|d|d|d	|�}|d
dkS(u�
    .. versionadded:: 2016.3.0

    Installs a language into a database

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.language_create plpgsql dbname

    name
       Language to install

    maintenance_db
        The database to install the language in

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    u Language %s already exists in %suCREATE LANGUAGE {0}u-cR-R+R,RCR*R)uretcodei(R
R%R�R
RRS(	R9RCR-R+R,R*R)RGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytlanguage_create�s(	c	C@syt||�s&tjd||�tSdj|�}td|gd|d|d|d|d|d	|�}|d
dkS(u�
    .. versionadded:: 2016.3.0

    Removes a language from a database

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.language_remove plpgsql dbname

    name
       Language to remove

    maintenance_db
        The database to install the language in

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    u Language %s does not exist in %suDROP LANGUAGE {0}u-cR-R+R,R)RCR*uretcodei(R
R%R�R
RRS(	R9RCR-R+R,R*R)RGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytlanguage_remove	s(	c
C@s�|dkrBdjddddddd	d
g�j||�}n�|dkr�djdddddddd
g�j||�}nM|d
kr�djddddg�j|�}n|dkr�djdddddddg�j||�}n�|dkr)djddddg�j|�}n�|dkr\djdddd g�j|�}nu|d!kr�djd"d#d$d%g�j|�}nB|d&kr�djd'd(d)d*d+d,d-d.d/g	�j|�}n|S(0u<
    Generate the SQL required for specific object type
    utableu uSELECT relacl AS nameuFROM pg_catalog.pg_class cuJOIN pg_catalog.pg_namespace nuON n.oid = c.relnamespaceuWHERE nspname = '{0}'uAND relname = '{1}'uAND relkind = 'r'uORDER BY relnameusequenceuAND relkind = 'S'uschemauSELECT nspacl AS nameuFROM pg_catalog.pg_namespaceuORDER BY nspnameufunctionuSELECT proacl AS nameuFROM pg_catalog.pg_proc puON n.oid = p.pronamespaceu%AND p.oid::regprocedure::text = '{1}'uORDER BY proname, proargtypesu
tablespaceuSELECT spcacl AS nameuFROM pg_catalog.pg_tablespaceuWHERE spcname = '{0}'uORDER BY spcnameulanguageuSELECT lanacl AS nameuFROM pg_catalog.pg_languageuWHERE lanname = '{0}'uORDER BY lannameudatabaseuSELECT datacl AS nameuFROM pg_catalog.pg_databaseuWHERE datname = '{0}'uORDER BY datnameugroupuSELECT rolname, admin_optionu!FROM pg_catalog.pg_auth_members muJOIN pg_catalog.pg_roles ruON m.member=r.oiduWHERE m.roleid INu(SELECT oiduFROM pg_catalog.pg_rolesuWHERE rolname='{0}')uORDER BY rolname(RR(R9tobject_typetprependRG((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_make_privileges_list_queryX	s�upublicc	C@s�|dkr6djddddg�j||�}	n\|dkr{djdd	d
ddd
dddg	�j||�}	n|dkr�djddd
ddg�j|�}	n�|dkr�djdddddddg�j||�}	n�|dkr&djddd
ddg�j|�}	nl|dkr\djddd
d d!g�j|�}	n6|d"kr�djdd#d
d$d%g�j|�}	nt|	d&|d'|d(|d)|d*|d+|�}
y|
d,d-}Wntk
r�d.}nX|S(/u/
    Return the owner of a postgres object
    utableu uSELECT tableowner AS nameuFROM pg_tablesuWHERE schemaname = '{0}'uAND tablename = '{1}'usequenceuSELECT rolname AS nameuFROM pg_catalog.pg_class cuJOIN pg_roles ruON c.relowner = r.oiduJOIN pg_catalog.pg_namespace nuON n.oid = c.relnamespaceuWHERE relkind='S'uAND nspname='{0}'uAND relname = '{1}'uschemauFROM pg_namespace nuON n.nspowner = r.oiduWHERE nspname = '{0}'ufunctionuFROM pg_catalog.pg_proc puON n.oid = p.pronamespaceu%AND p.oid::regprocedure::text = '{1}'uORDER BY proname, proargtypesu
tablespaceuFROM pg_tablespace tuON t.spcowner = r.oiduWHERE spcname = '{0}'ulanguageuFROM pg_language luON l.lanowner = r.oiduWHERE lanname = '{0}'udatabaseuFROM pg_database duON d.datdba = r.oiduWHERE datname = '{0}'R)R+R-R,RCR*iunameN(RRR^t
IndexErrorR(R9RRRCR-R+R,R*R)RGR`R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_get_object_owner�	s�	

cC@s�|dkr�gt|D]}t|^q}|jd�|tkr^tdj|���nt|�jt|��s�tdj||���q�n|r�td��ndS(u*
    Validate the supplied privileges
    ugroupuALLu!Invalid object_type: {0} providedu1Invalid privilege(s): {0} provided for object {1}u=The privileges option should not be set for object_type groupN(t_PRIVILEGE_TYPE_MAPt_PRIVILEGES_MAPR4t_PRIVILEGES_OBJECTSRRtsettissubset(Rtprivst
privilegestpermt_perms((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_validate_privileges
s
cC@sI|j�}|dkrdn|}tjd|j��}|||fS(u
    Format options
    uu\s?,\s?N(R�RtreRFtupper(RRt_privs((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_mod_priv_opts
scC@syi}d}xf|D]^}|dkr@t|t|<t|}q|dkrYt||<qt|t|<t|}qW|S(u
    Process part
    u*N(RR
RR(tpermst_tmptpreviousR((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt_process_priv_part%
s


c	C@sW|j�}t|||�}	|tkrBtdj|���nt|	d|d|d|d|d|d|�}
i}x�|
D]�}|dkr"|d	}
|
jd
�}
|
jd�}x�|D][}|jd�\}}|jd
�\}}|dkrd}nt|�}|||<q�Wq|ddkr;t	}nt
}|||d<qW|S(uH
    .. versionadded:: 2016.3.0

    Return a list of privileges for the specified object.

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.privileges_list table_name table maintenance_db=db_name

    name
       Name of the object for which the permissions should be returned

    object_type
       The object type, which can be one of the following:

       - table
       - sequence
       - schema
       - tablespace
       - language
       - database
       - group
       - function

    prepend
        Table and Sequence object types live under a schema so this should be
        provided if the object is not under the default `public` schema

    maintenance_db
        The database to connect to

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    u!Invalid object_type: {0} providedR)R+R-R,RCR*ugroupunameu{}u,u/u=uupublicuadmin_optionuturolname(R�RRRRR^RVRFR&RR
(R9RRRCR-R+R,R*R)RGR`R1R]tresulttpartstpartt
perms_partt_trolenameR#R$tadmin_option((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytprivileges_list8
s>:	


		cC@s�t||�\}}}t|||�|dkr�t||d|d|d|d|d|	d|
d|�}
|
dk	r�||
kr�tSnt||d|d|d|d|d|	d|
d|�}||kr�|dkr�|r�||}nt}|St|}|r3td	�|D��}|||k}nsg|D]}t|^q:}d
|kr�|j	�||j
�j	�k}n%t|�jt||j
���}|Snt
S(uQ
    .. versionadded:: 2016.3.0

    Check if a role has the specified privileges on an object

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.has_privileges user_name table_name table \
        SELECT,INSERT maintenance_db=db_name

    name
       Name of the role whose privileges should be checked on object_type

    object_name
       Name of the object on which the check is to be performed

    object_type
       The object type, which can be one of the following:

       - table
       - sequence
       - schema
       - tablespace
       - language
       - database
       - group
       - function

    privileges
       Comma separated list of privileges to check, from the list below:

       - INSERT
       - CREATE
       - TRUNCATE
       - CONNECT
       - TRIGGER
       - SELECT
       - USAGE
       - TEMPORARY
       - UPDATE
       - EXECUTE
       - REFERENCES
       - DELETE
       - ALL

    grant_option
        If grant_option is set to True, the grant option check is performed

    prepend
        Table and Sequence object types live under a schema so this should be
        provided if the object is not under the default `public` schema

    maintenance_db
        The database to connect to

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    ugroupRRCR-R+R,R*R)cs@s|]}t|tfVqdS(N(RR(t.0R((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pys	<genexpr>suALLN(R"RRRRR.RR6RtsortR�RRR
(R9tobject_nameRRtgrant_optionRRCR-R+R,R*R)R!Rmt_privilegestretvalRR#R((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pythas_privileges�
s8T

%cC@s't||�\}}}t|||�t||||d|d|d|d|d|	d|
d|�r�tjd|||�tSd	j|�}
|dkr�dj||�}n-|d
kr�dj|�}ndj|�}|re|dkrdj||�}q�|dkrD|j�dkrDdj|
|j�||�}q�dj|
|j�||�}n~|dkr�dj||�}n]|dkr�|j�dkr�dj|
|j�||�}ndj|
|j�||�}t	d|gd|d|d|	d|d|
d|�}|ddkS(uU
    .. versionadded:: 2016.3.0

    Grant privileges on a postgres object

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.privileges_grant user_name table_name table \
        SELECT,UPDATE maintenance_db=db_name

    name
       Name of the role to which privileges should be granted

    object_name
       Name of the object on which the grant is to be performed

    object_type
       The object type, which can be one of the following:

       - table
       - sequence
       - schema
       - tablespace
       - language
       - database
       - group
       - function

    privileges
       Comma separated list of privileges to grant, from the list below:

       - INSERT
       - CREATE
       - TRUNCATE
       - CONNECT
       - TRIGGER
       - SELECT
       - USAGE
       - TEMPORARY
       - UPDATE
       - EXECUTE
       - REFERENCES
       - DELETE
       - ALL

    grant_option
        If grant_option is set to True, the recipient of the privilege can
        in turn grant it to others

    prepend
        Table and Sequence object types live under a schema so this should be
        provided if the object is not under the default `public` schema

    maintenance_db
        The database to connect to

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    RRCR-R+R,R*R)u9The object: %s of type: %s already has privileges: %s setu,utableusequenceu	{0}."{1}"ufunctionu{0}u"{0}"ugroupu$GRANT {0} TO "{1}" WITH ADMIN OPTIONuALLu>GRANT {0} ON ALL {1}S IN SCHEMA {2} TO "{3}" WITH GRANT OPTIONu/GRANT {0} ON {1} {2} TO "{3}" WITH GRANT OPTIONuGRANT {0} TO "{1}"u,GRANT {0} ON ALL {1}S IN SCHEMA {2} TO "{3}"uGRANT {0} ON {1} {2} TO "{3}"u-curetcodei(utableusequence(utableusequence(utableusequence(
R"RR5R%R�R
RRR RS(R9R1RRR2RRCR-R+R,R*R)R!t_grantston_partRGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytprivileges_grantsPU	
	cC@s:t||�\}}}t|||�t||||d|d|d|d|d|d|	d|
�s�tjd|||�tSd	j|�}|dkr�dj||�}
n|}
|d
kr�dj||�}ndj||j�|
|�}t	d|gd|d|d|d|d|	d|
�}|ddkS(u3
    .. versionadded:: 2016.3.0

    Revoke privileges on a postgres object

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.privileges_revoke user_name table_name table \
        SELECT,UPDATE maintenance_db=db_name

    name
       Name of the role whose privileges should be revoked

    object_name
       Name of the object on which the revoke is to be performed

    object_type
       The object type, which can be one of the following:

       - table
       - sequence
       - schema
       - tablespace
       - language
       - database
       - group
       - function

    privileges
       Comma separated list of privileges to revoke, from the list below:

       - INSERT
       - CREATE
       - TRUNCATE
       - CONNECT
       - TRIGGER
       - SELECT
       - USAGE
       - TEMPORARY
       - UPDATE
       - EXECUTE
       - REFERENCES
       - DELETE
       - ALL

    maintenance_db
        The database to connect to

    user
        database username if different from config or default

    password
        user password if any password for a specified user

    host
        Database host if different from config or default

    port
        Database port if different from config or default

    runas
        System user all operations should be performed on behalf of
    RRCR-R+R,R*R)u;The object: %s of type: %s does not have privileges: %s setu,utableusequenceu{0}.{1}ugroupuREVOKE {0} FROM {1}uREVOKE {0} ON {1} {2} FROM {3}u-curetcodei(utableusequence(
R"RR5R%R�R
RRR RS(R9R1RRRRCR-R+R,R*R)R!R6R7RGR1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytprivileges_revoke�s0L	
	c	
C@s^t|�r tjd|�tSt|d|d|d|d|d|d|�}	|	dd	kS(
u�
    .. versionadded:: 2016.3.0

    Initializes a postgres data directory

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.datadir_init '/var/lib/pgsql/data'

    name
        The name of the directory to initialize

    auth
        The default authentication method for local connections

    password
        The password to set for the postgres user

    user
        The database superuser name

    encoding
        The default encoding for new databases

    locale
        The default locale for new databases

    waldir
        The transaction log (WAL) directory (default is to keep WAL
        inside the data directory)

        .. versionadded:: 2019.2.0

    checksums
        If True, the cluster will be created with data page checksums.

        .. note::  Data page checksums are supported since PostgreSQL 9.3.

        .. versionadded:: 2019.2.0

    runas
        The system user the operation should be performed on behalf of

    u%s already existsR:R-R*R;R<R)uretcodei(tdatadir_existsR%R�R
RB(
R9R:R-R*R;R<R=R>R)R1((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pytdatadir_inits7	cC@sLtjj|d�}tjj|d�}tjj|�oKtjj|�S(u�
    .. versionadded:: 2016.3.0

    Checks if postgres data directory has been initialized

    CLI Example:

    .. code-block:: bash

        salt '*' postgres.datadir_exists '/var/lib/pgsql/data'

    name
        Name of the directory to check
    u
PG_VERSIONupostgresql.conf(RR
Rtisfile(R9t
_version_filet_config_file((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyR:Vs(uschemau
tablespaceulanguageusequenceutableugroupudatabaseufunction(st__doc__t
__future__RRRR�tloggingR�RRR7R�RXRR	tImportErrorR
tsalt.utils.filesRtsalt.utils.itertoolstsalt.utils.odicttsalt.utils.pathtsalt.utils.stringutilstsalt.exceptionsRRtsalt.utils.versionsRRJtsalt.extRtsalt.ext.six.movesRRt	getLoggert__name__R%R�R�R�R�R�R�Rt	frozensetRRRRRR2RBRIRLRMRDRSR^RaRcReRrRwRxRyR{R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�RtRRRRR	RR
RRRRRR"R&R.R5R8R9R;R:(((s9/usr/lib/python2.7/site-packages/salt/modules/postgres.pyt<module>sJ


	
	
		3;	!
Q!
-*!	"	,	e
M%"-!	
(3Q! G"9(+:4-53	RR			]s�f>

Zerion Mini Shell 1.0