%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/utils/master.pyc

�
���^c@@sdZddlmZmZddlZddlZddlZddlmZm	Z	ddl
ZddlZddl
ZddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlZddlmZddlmZddlmZdd	l m!Z!ej"e#�Z$d
�Z%d�Z&d�Z'd
e(fd��YZ)defd��YZ*defd��YZ+defd��YZ,d�Z-e.d�Z/d�Z0e#dkr
ej1j2d�Z3e,e3�Z4e4j5�ndS(ue
    salt.utils.master
    -----------------

    Utilities that can only be used on a salt master.

i(tabsolute_importtunicode_literalsN(tThreadtEvent(t
SaltException(tCacheCli(tMultiprocessingProcess(tsix(tzmqcC@s�g}tjj|dd�}tjj|�s5|Sx�tj|�D]t}tjj||�}y/t||�}|dk	r�|j|�nWqEtt	fk
r�t
jd|�qEXqEW|S(u0
    Return the running jobs on this minion
    ucachediruprocu.%s removed during processing by master processN(tostpathtjointisdirtlistdirt_read_proc_filetNonetappendtIOErrortOSErrortlogttrace(toptstrettproc_dirtfn_R
tdata((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_running_jobs)sc	C@sptjj|�}tjjj|d��n}|j�}|j�|r[|j|�}n9yt	j
|�Wn!tk
r�tj
d|�nXdSWdQXt|t�s�dStjjj|d�s�yt	j
|�Wn!tk
r�tj
d|�nXdSt|�sl|jd�}|r3tjd|�nyt	j
|�Wn!tk
rgtj
d|�nXdS|S(u0
    Return a dict of JID metadata, or None
    urbuUnable to remove proc file %s.Nupidu7PID %s exists but does not appear to be a salt process.(tsalttpayloadtSerialtutilstfilestfopentreadtclosetloadsR	tremoveRRtdebugRt
isinstancetdicttprocesst
os_is_runningt_check_cmdlinetgettwarning(R
Rtserialtfp_tbufRtpid((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR@s<





cC@s�tjjj�stS|jd�}|s/tStjj	d�sEtStjj
dj|��}tjj|�svtSy5tjj
j|d��}d|j�kSWdQXWnttfk
r�tSXdS(u9
    In some cases where there are an insane number of processes being created
    on a system a PID can get recycled or assigned to a non-Salt process.
    On Linux this fn checks to make sure the PID we are checking on is actually
    a Salt process.

    For non-Linux systems we punt and just return True
    upidu/procu/proc/{0}/cmdlineurbRN(RRtplatformtis_linuxtTrueR+tFalseR	R
RRtformattisfileRR R!RR(RR0R
R.((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR*ks	tMasterPillarUtilc	B@s�eZdZdddeeeedd�Zd�Zd�Zd�Zddd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zeeedd�ZRS(u�
    Helper utility for easy access to targeted minion grain and
    pillar data, either from cached data on the master or retrieved
    on demand, or (by default) both.

    The minion pillar data returned in get_minion_pillar() is
    compiled directly from salt.pillar.Pillar on the master to
    avoid any possible 'pillar poisoning' from a compromised or
    untrusted minion.

    ** However, the minion grains are still possibly entirely
    supplied by the minion. **

    Example use case:
        For runner modules that need access minion pillar data,
        MasterPillarUtil.get_minion_pillar should be used instead
        of getting the pillar data by executing the "pillar" module
        on the minions:

        # my_runner.py
        tgt = 'web*'
        pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type='glob', opts=__opts__)
        pillar_data = pillar_util.get_minion_pillar()
    uuglobc			C@s�tjd|jj�|dkrVtjd|jj�tdj|jj���n	||_t	j
j|j�|_||_
||_||_||_||_||_||_t	jj|�|_tjd|||||||�dS(NuNew instance of %s created.u!%s: Missing master opts init arg.u"{0}: Missing master opts init arg.u�Init settings: tgt: '%s', tgt_type: '%s', saltenv: '%s', use_cached_grains: %s, use_cached_pillar: %s, grains_fallback: %s, pillar_fallback: %s(RR%t	__class__t__name__RterrorRR5RRRRR-ttgtttgt_typetsaltenvtuse_cached_grainstuse_cached_pillartgrains_fallbacktpillar_fallbacktcachetfactory(	tselfR;R<R=R>R?R@RAR((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt__init__�s*
	
	
									cG@s�tg|D]}|if^q
�}|jjdt�rb|jjdt�rbtjd�|S|s}|jjd�}nxi|D]a}tj	j
j|j|�s�q�n|jjdj
|�d�}t|t�r�|||<q�q�W|S(Numinion_data_cacheuenforce_mine_cacheuSSkipping cached mine data minion_data_cacheand enfore_mine_cache are both disabled.uminionsuminions/{0}umine(R'RR+R4RR%RBtlistRRtverifytvalid_idtfetchR5R&(RDt
minion_idst	minion_idt	mine_datatmdata((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_cached_mine_data�s%

cG@s=tg|D]}|if^q
�}|j�}|jjdt�s]tjd�||fS|sx|jjd�}nx�|D]�}t	j
jj|j|�s�qn|jj
dj|�d�}t|t�s�tjdt|�j|�qnd|kr|d||<nd|kr|d||<qqW||fS(	Numinion_data_cacheu>Skipping cached data because minion_data_cache is not enabled.uminionsuminions/{0}udatauGcache.fetch should always return a dict. ReturnedType: %s, MinionId: %sugrainsupillar(R'tcopyRR+R4RR%RBRFRRRGRHRIR5R&R,ttypeR9(RDRJRKtgrainstpillarsRM((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_cached_minion_data�s,%


cC@s[tjd|�tjj|jd�}|jdj|�dd|jddd�}|S(	Nu%Getting live grains for minions: "%s"u	conf_fileu,ugrains.itemsttimeoututimeoutR<ulist(RR%Rtclienttget_local_clientRtcmdR(RDRJRUR((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_live_minion_grains�s
	cC@s�|dkriS|s*tjd|�iStjd|�tjj|j|||j|jd�}tjd|�|j	�}|S(Nu2Cannot get pillar data for %s: no grains supplied.uGetting live pillar for %su
ext_pillaruCompiling pillar for %s(
RRR,R%RtpillartPillarRR=tcompile_pillar(RDRKt
minion_grainsRYR((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_live_minion_pillar�s"	c
O@s�i}|jdi�}i}i}|jrtgtj|�D]\}}|r@||f^q@�}g|D]}||krn|^qn}	tjd|	�|jr�|j|	�}ntt	tjtg|D]}|if^q����t	|j
��t	|j
���}n�|j|�}g|D]}||kr*|^q*}	tjd|	�|jr�tgtj|�D]\}}|rt||f^qt�}ntt	tjtg|D]}|if^q����t	|j
��t	|j
���}|S(Nu
cached_grainsu#Missed cached minion grains for: %su!Missed live minion grains for: %s(R+R>R'Rt	iteritemsRR%R@RXRFtitems(
RDRJtkwargsRt
cached_grainstcrettlretRKtmcachetmissed_minions((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_minion_grainss$	:%	]%	=Zc	O@sji}|jdi�}|jdi�}i}i}|jrTtgtj|�D]\}}	|	rR||	f^qR�}g|D]}||kr�|^q�}
tjd|
�|jr�tg|
D]*}||j||j|i��f^q��}ntt	tjtg|D]}|if^q
���t	|j
��t	|j
���}ntg|D]*}||j||j|i��f^q^�}g|D]}||kr�|^q�}
tjd|
�|jrtgtj|�D]\}}	|	r�||	f^q��}ntt	tjtg|D]}|if^q"���t	|j
��t	|j
���}|S(Nugrainsu
cached_pillaru$Missed cached minion pillars for: %su"Missed live minion pillars for: %s(R+R?R'RR^RR%RAR]RFR_(RDRJR`RRQt
cached_pillarRbRcRKRdRe((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_get_minion_pillar(s&	:%	@]=%	=ZcC@s�g}tjjj|j�}|j|j|j�}|d}t|�dkrot	j
d|j|j�iSt	j
d|j|j|�|S(Numinionsiu1No minions matched for tgt="%s" and tgt_type="%s"u3Matching minions for tgt="%s" and tgt_type="%s": %s(RRtminionst	CkMinionsRt
check_minionsR;R<tlenRR%(RDRJt	ckminionst_res((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt_tgt_to_listAs
cC@s�i}i}|j�}td�|j|j|j|jgD��rktjd�|j|�\}}ni}i}tjd|�|j	d||�}tjd|�|j
d|d||�}|S(u�
        Get pillar data for the targeted minions, either by fetching the
        cached minion data on the master, or by compiling the minion's
        pillar data on the master.

        For runner modules that need access minion pillar data, this
        function should be used instead of getting the pillar data by
        executing the pillar module on the minions.

        By default, this function tries hard to get the pillar data:
            - Try to get the cached minion grains and pillar if the
                master has minion_data_cache: True
            - If the pillar data for the minion is cached, use it.
            - If there is no cached grains/pillar data for a minion,
                then try to get the minion grains directly from the minion.
            - Use the minion grains to compile the pillar directly from the
                master using salt.pillar.Pillar
        cs@s|]}|VqdS(N((t.0targ((s5/usr/lib/python2.7/site-packages/salt/utils/master.pys	<genexpr>csuGetting cached minion datau!Getting minion grain data for: %sRau"Getting minion pillar data for: %sRQRg(RotanyR>R?R@RARR%RSRfRh(RDtminion_pillarsR\RJtcached_minion_grainstcached_minion_pillars((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_minion_pillarMs .
		cC@s�i}|j�}|siStd�|j|jgD��rctjd�|j|�\}}ni}tjd|�|jd||�}|S(u5
        Get grains data for the targeted minions, either by fetching the
        cached minion data on the master, or by fetching the grains
        directly on the minion.

        By default, this function tries hard to get the grains data:
            - Try to get the cached minion grains if the master
                has minion_data_cache: True
            - If the grains data for the minion is cached, use it.
            - If there is no cached grains data for a minion,
                then try to get the minion grains directly from the minion.
        cs@s|]}|VqdS(N((RpRq((s5/usr/lib/python2.7/site-packages/salt/utils/master.pys	<genexpr>�suGetting cached minion data.u!Getting minion grain data for: %sRa(RoRrR>R@RR%RSRf(RDR\RJRtRu((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_minion_grainsts
"
	cC@s5i}|j�}tjd|�|j|�}|S(u@
        Get cached mine data for the targeted minions.
        u Getting cached mine data for: %s(RoRR%RN(RDRLRJ((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_cached_mine_data�s
cC@s�g}|r|jd�n|r2|jd�n|rH|jd�n|dk	rm|jdj|��nt|�s�tjd�tS|j�}tjddj|�|�||kr�i}i}n|j	|�\}}y�|j
jd�}	x�|D]}}
tj
jj|j|
�s%q�n|
|	kr7q�nd	j|
�}|j|
t�}|j|
t�}
|rv|s�|r�|
s�|r�|r�|j
j|d
�nX|r�|
r�|j
j|d
i|
d6�n,|r�|r�|j
j|d
i|d6�n|r|j
j|d�q�|dk	r�|j
j|d�}t|t�r{|j|t�rx|j
j|d|�qxq{q�q�WWnttfk
r�tSXtS(uG
        Clear the cached data/files for the targeted minions.
        upillarugrainsumineumine_func: '{0}'u,No cached data types specified for clearing.uClearing cached %s data for: %su, uminionsuminions/{0}udataN(RRR5RlRR%R4RoRRSRBRFRRRGRHRtpoptflushtstoreRIR&R'RRR3(RDtclear_pillartclear_grainst
clear_minetclear_mine_funct
clear_whatRJRQRRt	c_minionsRKtbankt
minion_pillarR\RL((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytclear_cached_minion_data�s`
		


  'N(R9t
__module__t__doc__RR3RERNRSRXR]RfRhRoRvRwRxR4R�(((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR7�s.							'		t
CacheTimercB@s eZdZd�Zd�ZRS(uw
    A basic timer class the fires timer-events every second.
    This is used for cleanup by the ConnectedCache()
    cC@sltj|�||_||_t|_tjj|j	dd��|_
tjj
|jdd�|_dS(Nuserialuusock_diru
con_timer.ipc(RRERtstoppedR3tdaemonRRRR+R-R	R
Rt
timer_sock(RDRtevent((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyRE�s
			!cC@s�tj�}|jtj�}|jtjd�|jd|j�d}tj	d�xN|j
jd�s�|j|j
j|��|d7}|dkr[d}q[q[WdS(u=
        main loop that fires the event every second
        iduipc://iuConCache-Timer startedii<N(RtContexttsockettPUBt
setsockopttLINGERtbindR�RR%R�twaittsendR-tdumps(RDtcontextR�tcount((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytrun�s

(R9R�R�RER�(((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR��s	tCacheWorkercB@s2eZdZd�Zd�Zd�Zd�ZRS(u�
    Worker for ConnectedCache which runs in its
    own process to prevent blocking of ConnectedCache
    main-loop when refreshing minion-list
    cK@s#tt|�j|�||_dS(u<
        Sets up the zmq-connection to the ConCache
        N(tsuperR�RER(RDRR`((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyREscC@s2t|_|j|dd|dd|d�dS(Nuoptst	log_queueu	log_queuetlog_queue_levelulog_queue_level(R3t	_is_childRE(RDtstate((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt__setstate__s
	

cC@s"i|jd6|jd6|jd6S(Nuoptsu	log_queueulog_queue_level(RR�R�(RD((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt__getstate__s

cC@s^ttjjj|j�j��}t|j�}|j�|j	|g�t
jd�dS(uI
        Gather currently connected minions and update the cache
        u$ConCache CacheWorker update finishedN(RFRRRiRjRt
connected_idst	cache_clit
get_cachedt	put_cacheRR%(RDtnew_minstcc((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�!s
$
(R9R�R�RER�R�R�(((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�s
	
		tConnectedCachecB@sVeZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
RS(	u�
    Provides access to all minions ids that the master has
    successfully authenticated. The cache is cleaned up regularly by
    comparing it to the IPs that have open connections to
    the master publisher port.
    cK@s�tt|�j|�tjd�||_g|_tjj	|jdd�|_
tjj	|jdd�|_tjj	|jdd�|_|j
�t�|_t|j|j�|_|jj�t|_dS(u=
        starts the timer and inits the cache itself
        uConCache initializing...usock_diru
con_cache.ipcucon_upd.ipcu
con_timer.ipcN(R�R�RERR%RRiR	R
Rt
cache_socktupdate_sockt
upd_t_socktcleanupRt
timer_stopR�ttimertstartR3trunning(RDRR`((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyRE4s
		

cC@s2t|_|j|dd|dd|d�dS(NuoptsR�u	log_queueR�ulog_queue_level(R3R�RE(RDR�((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�Qs
	

cC@s"i|jd6|jd6|jd6S(Nuoptsu	log_queueulog_queue_level(RR�R�(RD((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�Ys

cC@s|j�dS(u-
        handle signals and shutdown
        N(tstop(RDtsigtframe((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytsignal_handler`scC@s�tjd�tjj|j�r5tj|j�ntjj|j�r]tj|j�ntjj|j�r�tj|j�ndS(u,
        remove sockets on shutdown
        uConCache cleaning upN(	RR%R	R
texistsR�R$R�R�(RD((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�fs
cC@s�tjd�tjj|j�r8tj|jd�ntjj|j�rctj|jd�ntjj|j�r�tj|jd�ndS(u9
        secure the sockets for root-only access
        uConCache securing socketsi�N(	RR%R	R
R�R�tchmodR�R�(RD((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytsecurers
cC@s=|j�|jr9t|_|jj�|jj�ndS(u(
        shutdown cache process
        N(R�R�R4R�tsetR�R(RD((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�~s

		
cC@sPtj�}|jtj�}|jtjd�|jd|j�|jtj�}|jtj	d�|jtjd�|jd|j
�|jtj�}|jtj	d�|jtjd�|jd|j�tj
�}|j|tj�|j|tj�|j|tj�tjj|jjdd��}tjtj|j�|j�tjd�x�|jryt|jd��}WnTtk
r�|j�n:tj k
r�}tj!d�tj"|�|j�nX|j|�tjkro|j#|j$��}	tj%d	|	�t&|	t'j(�ro|	d
krl|j)|j*�}
|j+|
�qlqon|j|�tjkr�|j#|j$��}t&|t,�s�tj!d�~qsny�t-|�dkr�tj%d
�wsn|d}t&|t'j(�r>||j*krftj%d|d�|j*j.|�qfn(t&|t,�rftj%d�||_*nWn!t/k
r�tj%d�~nXtjdt-|j*��n|j|�tjkrs|j#|j$��}
t0|
d�dkr	t1|j�}|j2�q	qsqsW|j�|j3�|j3�|j3�|j4�tj%d�dS(uw
        Main loop of the ConCache, starts updates in intervals and
        answers requests from the MWorkers
        iduipc://tuserialuuConCache startediuConCache ZeroMQ-Error occurreduConCache Received request: %suminionsu(ConCache Worker returned unusable resultiu%ConCache Got empty update from workeru"ConCache Adding minion %s to cacheu*ConCache Replacing minion list from workeru.ConCache Got malformed result dict from workeruConCache %s entries in cacheiuConCache Shutting downN(5RR�R�tREPR�R�R�R�tSUBt	SUBSCRIBER�tconnectR�tPollertregistertPOLLINRRRRR+tsignaltSIGINTR�R�RtinfoR�R'tpolltKeyboardInterruptR�tZMQErrorR:t	exceptionR#trecvR%R&Rtstring_typesR�RiR�RFRlRt
IndexErrortintR�R�R"tterm(RDR�tcreq_intcupd_inttimer_intpollerR-tsockstzmq_errtmsgtreplyt
new_c_dataRt	sec_eventtcw((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR��s�!








	







(R9R�R�RER�R�R�R�R�R�R�(((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyR�,s							cC@sitjj�}|drCttjjj|�j��}d}nd}d}|j|dd|�dS(Numinion_data_cacheulistu*uglobu	test.pingR<(	RRUtLocalClientRFRRiRjR�RW(RRUR;tform((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytping_all_connected_minionss
!	cC@s|dkr<|jdd�dkr<|jdd�}q<n|jd�r`|jdd�}ntjjj�r�|jdd�}ntjj	|ddj
|��}tjjj|d||�y/tjj
j|d��}|j�SWdQXWnttfk
rd	SXdS(
Nurootuuserusudo_u\u_ucachediru.{0}_keyuru(R+t
startswithRRR1t
is_windowstreplaceR	R
RR5RGtcheck_path_traversalRR R!RR(tkey_userRtskip_perm_errorstkeyfiletkey((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_master_keys"cC@sGg}x:|D]2}tjjj||�r
|j||�q
q
W|S(uL
    Check a whitelist and/or blacklist to see if the value matches it.
    (RRtstringutilst
expr_matchtextend(tpattern_dictt	user_nameRtexpr((s5/usr/lib/python2.7/site-packages/salt/utils/master.pytget_values_of_matching_keys's

u__main__u/etc/salt/master(6R�t
__future__RRR	tloggingR�t	threadingRRtsalt.logRt
salt.cachetsalt.clienttsalt.pillartsalt.utils.atomicfiletsalt.utils.filestsalt.utils.minionstsalt.utils.platformtsalt.utils.stringutilstsalt.utils.verifytsalt.payloadtsalt.exceptionsRtsalt.configtsalt.utils.cacheRR�tsalt.utils.processRtsalt.extRtsalt.utils.zeromqRt	getLoggerR9RRRR*tobjectR7R�R�R�R�R4R�R�tconfigt
master_configRtconcR�(((s5/usr/lib/python2.7/site-packages/salt/utils/master.pyt<module>sL		+	�\!+�		

Zerion Mini Shell 1.0