�
oB�]c@s�ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m
Zddlm
Z
ddl mZmZddlmZddlmZejr�ddlmZnddlmZeje�Zd efd
��YZejej �de!fd��Y�Z"d
e"fd��YZ#de"fd��YZ$de"fd��YZ%de"fd��YZ&e
�Z'e'j(de#�e'j(de$�e'j(de%�e'j(de&�dS(i����N(tlog(tDictRegistry(t
url_helpertutil(tdatetime(tEmpty(t
JoinableQueue(tQueuetReportExceptioncBseZRS((t__name__t
__module__(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRstReportingHandlercBs)eZdZejd��Zd�ZRS(s�Base class for report handlers.
Implement :meth:`~publish_event` for controlling what
the handler does with an event.
cCsdS(sPublish an event.N((tselftevent((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt
publish_event'scCsdS(s0Ensure ReportingHandler has published all eventsN((R((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pytflush+s(R R
t__doc__tabctabstractmethodRR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRst
LogHandlercBs#eZdZdd�Zd�ZRS(sBPublishes events to the cloud-init log at the ``DEBUG`` log level.tDEBUGcCs~tt|�j�t|t�r%nL|}ytt|j��}Wn*tk
rpt j
d|�tj}nX||_dS(Nsinvalid level '%s', using WARN(
tsuperRt__init__t
isinstancetinttgetattrtloggingtuppert ExceptiontLOGtwarningtWARNtlevel(RR tinput_level((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR3s
cCsGtjdjdd|j|jg��}|j|j|j��dS(Nt.t cloudinitt reporting(Rt getLoggertjoint
event_typetnameRR t as_string(RR
tlogger((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR@s$(R R
RRR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR0s
tPrintHandlercBseZdZd�ZRS(sPrint the event as a string.cCs|j�GHdS(N(R)(RR
((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRIs(R R
RR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR+FstWebHookHandlercBs,eZddddddd�Zd�ZRS(c Cs�tt|�j�t||||g�rUtjd|d|d|d|�|_n d|_||_||_ ||_
tj�|_
dS(Ntconsumer_keyt token_keyttoken_secrettconsumer_secret(RR,RtanyRtOauthUrlHelpertoauth_helpertNonetendpointttimeouttretriesRtfetch_ssl_detailstssl_details(RR5R-R.R/R0R6R7((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRNs c
Cs�|jr|jj}n tj}yA||jdtj|j��d|jd|jd|j �SWn't
k
r�tjd|j
��nXdS(NtdataR6R7R9sfailed posting event: %s(R3treadurlRR5tjsontdumpstas_dictR6R7R9RRRR)(RR
R;((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR^s
N(R R
R4RR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR,Ms tHyperVKvpReportingHandlercBs�eZdZdZdZeeZdZdZdZdZ dZ
d ZeZ
edd
�Zed��Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZRS(s,
Reports events to a Hyper-V host using Key-Value-Pair exchange protocol
and can be used to obtain high level diagnostic information from the host.
To use this facility, the KVP user-space daemon (hv_kvp_daemon) has to be
running. It reads the kvp_file when the host requests the guest to
enumerate the KVP's.
This reporter collates all events for a module (origin|name) in a single
json string in the dictionary.
For more information, see
https://technet.microsoft.com/en-us/library/dn798287.aspx#Linux%20guests
iit
CLOUD_INITtmsgtresulttmsg_it,t:s/var/lib/hyperv/.kvp_pool_1cCs�tt|�j�||_tj|j�||_t�|_|j�|_ dj
|j|j �|_t
jd|j�|_t|j_|jj�dS(Nu{0}|{1}ttarget(RR?Rt_kvp_file_patht_truncate_guest_pool_filet_event_typestJQueuetqt_get_incarnation_notincarnation_notformattEVENT_PREFIXtevent_key_prefixt threadingtThreadt_publish_event_routinetpublish_threadtTruetdaemontstart(Rt
kvp_file_pathtevent_types((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR�s
cCs�|jr
dStj�ttj��}zey5tjj|�|kr`t|d��WdQXnWn)t t
fk
r�}tjd|�nXWdt
|_XdS(s
Truncate the pool file if it has not been truncated since boot.
This should be done exactly once for the file indicated by
KVP_POOL_FILE_GUEST constant above. This method takes a filename
so that we can use an arbitrary file during unit testing.
Since KVP is a best-effort telemetry channel we only attempt to
truncate the file once and only if the file has not been modified
since boot. Additional truncation can lead to loss of existing
KVPs.
Ntws$failed to truncate kvp pool file, %s(t_already_truncated_pool_filettimetfloatRtuptimetostpathtgetmtimetopentOSErrortIOErrorRRRU(tclstkvp_filet boot_timete((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRH�s
cCsStj�}yttj�t|��SWn"tk
rNtjd|�dSXdS(s�
use the time passed as the incarnation number.
the incarnation number is the number which are used to
distinguish the old data stored in kvp and the new data.
s"uptime '%s' not in correct format.iN(RR^RR\R]t
ValueErrorRR(Rt
uptime_str((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRL�s
ccs�t|jd���}tj|tj�|j|�|j|j�}x?t|�|jkr�|j |�}|V|j|j�}qJWtj|tj
�WdQXdS(s-iterate the kvp file from the current offset.trbN(RbRGtfcntltflocktLOCK_EXtseektreadtHV_KVP_RECORD_SIZEtlent_decode_kvp_itemtLOCK_UN(Rtoffsettftrecord_datatkvp_item((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt
_iterate_kvps�s
cCs%dj|j|j|jtj��S(ss
the event key format is:
CLOUD_INIT|<incarnation number>|<event_type>|<event_name>|<time>
u{0}|{1}|{2}|{3}(RNRPR'R(tuuidtuuid4(RR
((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt
_event_key�scCs;tjd|j|jf|jd�|jd��}|S(Ns%ds%dssutf-8(tstructtpacktHV_KVP_EXCHANGE_MAX_KEY_SIZEtHV_KVP_EXCHANGE_MAX_VALUE_SIZEtencode(RtkeytvalueR:((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt_encode_kvp_item�s
cCs�t|�}||jkr9tdj||j���n|d|j!jd�jd�}||j|j!jd�jd�}i|d6|d6S(Ns$record_data len not correct {0} {1}.isutf-8tR�R�(RrRqRRNRtdecodetstrip(RRwtrecord_data_lentktv((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRs�s cCsmt|jd��U}tj|tj�x|D]}|j|�q/W|j�tj|tj�WdQXdS(Ntab(RbRGRlRmRntwriteRRt(RRwRvR:((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt_append_kvp_item�s
cCs||j=tj|�}|dt|�d!}d}g}d|jd}x�tr|||j<d||j<tj|d|j�}|jt|�d} |j|dj d |jd
|| ��}
|j
|j||
��|d7}|| }t|�dkrPPqPqPW|S(Niis"s":""tt
separatorsis"{key}":"{desc}"R�tdesc(tMSG_KEYR<R=RrRUtDESC_IDX_KEYtJSON_SEPARATORSR�treplaceRNtappendR�(RR�t meta_datatdescriptiontdes_in_jsontitresult_arraytmessage_place_holdertdata_without_desct
room_for_descR�((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt_break_down�s,
cCs�|j|�}i|jd6|jd6tj|j�j�dd6}t||j�rk|j ||j<n|j
||j<tj
|d|j�}t|�|jkr�|j|||j
�S|j||�}|gSdS(s�
encode the event into kvp data bytes.
if the event content reaches the maximum length of kvp value.
then it would be cut to multiple slices.
R(ttypetZttsR�N(R|R(R'Rtutcfromtimestampt timestampt isoformatthasattrt
RESULT_KEYRBR�R�R<R=R�RrR�R�R�(RR
R�R�R�R:((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt
_encode_events
cCs!xtrd}y�|jjdt�}|d7}g}x`|dk r�||j|�7}y#|jjdt�}|d7}Wq:tk
r�d}q:Xq:WzAy|j|�Wn)tt fk
r�}t
jd|�nXWdx!t|�D]}|jj
�q�WXWqtk
rdSXqWdS(Nitblockis failed posting events to kvp, %s(RURKtgetR4R�tFalsetQueueEmptyErrorR�RcRdRRtranget task_donetEOFError(Rtitems_from_queueR
tencoded_dataRht_((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRS%s,
cCs3|js|j|jkr/|jj|�ndS(N(RIR'RKtput(RR
((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRCscCstjd�|jj�dS(Ns0HyperVReportingHandler flushing remaining events(RtdebugRKR&(R((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRGs
(RDREN(R R
RR�RRqROR�R�R�R�tKVP_POOL_FILE_GUESTR�R[R4RtclassmethodRHRLRyR|R�RsR�R�R�RSRR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR?ls4
Rtprinttwebhookthyperv()RRzRlR<tsixR_R}RQR\R#RRtcloudinit.registryRRRRtsix.moves.queueRR�tPY2tmultiprocessing.queuesRRJtqueueRR%R RRRt
add_metaclasstABCMetatobjectRRR+R,R?tavailable_handlerst
register_item(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt<module>s: � |