+
    i@                        ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
H
t
Ht ^ RIHt ^ RIHt ^ RIHtHtHtHt ^ RIHt ]P,                  ! ]4      t ! R R]4      t ! R	 R
] P6                  R7      t ! R R]4      t ! R R]4      t ! R R]4      t ! R R]4      t ]]]]]] 3,          t!]! 4       t"]"PG                  R]4       ]"PG                  R]4       ]"PG                  R]4       ]"PG                  R] 4       R# )    N)datetimetimezone)Event)Union)dmiperformance
url_helperutil)DictRegistryc                       ] tR t^tRtR# )ReportException N)__name__
__module____qualname____firstlineno____static_attributes__r       >/usr/lib/python3/dist-packages/cloudinit/reporting/handlers.pyr   r      s    r   r   c                   N   a  ] tR t^t o Rt]P                  R 4       tR tRt	V t
R# )ReportingHandlerzwBase class for report handlers.

Implement :meth:`~publish_event` for controlling what
the handler does with an event.
c                    R# )zPublish an event.Nr   selfevents   &&r   publish_eventReportingHandler.publish_event"       r   c                    R# )z0Ensure ReportingHandler has published all eventsNr   r   s   &r   flushReportingHandler.flush&   r   r   r   N)r   r   r   r   __doc__abcabstractmethodr   r!   r   __classdictcell____classdict__s   @r   r   r      s.      	   ? ?r   r   )	metaclassc                   @   a a ] tR t^*t oRtRV 3R lltR tRtVtV ;t	# )
LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.c                  < \         \        V `  4        \        V\        4      '       d   M!Tp \        \        VP                  4       4      pWn        R#   \         d*    \        P                  R T4       \        P                  p L;i ; i)zinvalid level '%s', using WARNN)superr+   __init__
isinstanceintgetattrloggingupper	ExceptionLOGwarningWARNlevel)r   r8   input_level	__class__s   && r   r.   LogHandler.__init__-   sl    j$(*eS!!K%7 
  %<kJ%s   A 1BBc                    \         P                  ! R P                  RRVP                  VP                  .4      4      pVP                  V P                  VP                  4       4       R# ).	cloudinit	reportingN)r2   	getLoggerjoin
event_typenamelogr8   	as_string)r   r   loggers   && r   r   LogHandler.publish_event:   sL    ""HHk;0@0@%**MN
 	

4::u01r   )r8   )DEBUG)
r   r   r   r   r#   r.   r   r   r&   __classcell__r:   r(   s   @@r   r+   r+   *   s     L2 2r   r+   c                   *   a  ] tR t^At o RtR tRtV tR# )PrintHandlerzPrint the event as a string.c                8    \        VP                  4       4       R # N)printrE   r   s   &&r   r   PrintHandler.publish_eventD   s    eoo r   r   N)r   r   r   r   r#   r   r   r&   r'   s   @r   rL   rL   A   s     &! !r   rL   c                   H   a a ] tR t^Ht oRV 3R lltR tR tR tRtVt	V ;t
# )WebHookHandlerc                   < \         \        V `  4        \        W#WE.4      '       d-   \        P
                  ! VVVVR 7      pVP                  V n        M\        P                  V n        Wn        W`n        Wpn	        \        P                  ! 4       V n        \        4       V n        \        P                   ! 4       V n        \"        P$                  ! V P&                  R7      V n        RV P(                  n        V P(                  P-                  4        R# ))consumer_key	token_keytoken_secretconsumer_secrettargetTN)r-   rR   r.   anyr	   OauthUrlHelperreadurlendpointtimeoutretriesr
   fetch_ssl_detailsssl_detailsr   flush_requestedqueueQueue	threadingThreadprocess_requestsevent_processordaemonstart)
r   r]   rT   rU   rV   rW   r^   r_   oauth_helperr:   s
   &&&&&&&& r   r.   WebHookHandler.__init__I   s     	nd,.GHH%44)#) /	L (//DL%--DL 113$w"'++-
(//t7L7LM&*#""$r   c           	        ^ p V P                   P                  4       '       du   V^8  dn   \        P                  R4       V P                  P                  4       '       g7   V P                  P                  4        V P                  P                  4        KV  ^ pV P                  P                  RR7      p V P                  V^ ,          V^,          V^,          V^,          V^,          RR7       ^ pV P                  P                  4        EK    \         d2   p\        P                  RT^,          T4       T^,          p Rp?LTRp?ii ; i  T P                  P                  4        i ; i)r   TzNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.blockF)datar^   r_   ra   log_req_respz0Failed posting event: %s. This was caused by: %sN)rb   is_setr5   r6   rc   empty
get_nowait	task_donegetr\   r4   )r   consecutive_failedargses   &   r   rg   WebHookHandler.process_requestsj   s+   ##**,,1Ca1G 4 ****,,JJ))+JJ((*%&"::>>>-D'Ga G G $Q!&   &'" 

$$&  (FG
 #a'"( 

$$&s*   5<D E
'E E E

E E)c                    VP                  4       p\        P                  R V P                  V4       V P                  P                  V P                  \        P                  ! V4      V P                  V P                  V P                  34       R# )zQueuing POST to %s, data: %sN)as_dictr5   debugr]   rc   putjsondumpsr^   r_   ra   )r   r   
event_datas   && r   r   WebHookHandler.publish_event   sg    ]]_
		*MM	

 	



:&  	
r   c                    V P                   P                  4        \        P                  R 4       V P                  P                  4        V P                   P                  4        R# )z(WebHookHandler flushing remaining eventsN)rb   setr5   r}   rc   rA   clearr    s   &r   r!   WebHookHandler.flush   sA      "		<=

""$r   )r]   rh   rb   rc   r\   r_   ra   r^   )NNNNNN)r   r   r   r   r.   rg   r   r!   r   r&   rI   rJ   s   @@r   rR   rR   H   s      %B$'L
"% %r   rR   c                   B  a a ] tR t^t oRtRtRtRt]],           tRt	]
! ]P                  ! ^ R7      4      tRtRtR	tRtR
tRt]R3V 3R llt]R 4       tR t]R 4       t]P4                  R 4       tR tR tR tR tR tR t V3R lR lt!R t"R t#R t$R t%Rt&Vt'V ;t(# ) HyperVKvpReportingHandlera  
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
i   i   i   
CLOUD_INIT)r0   msgresultmsg_iz/var/lib/hyperv/.kvp_pool_1FNc                .  < \         \        V `  4        Wn        \        P	                  V P                  4       W n        \        P                  ! 4       V n        V P                  4       V n
        R P                  V P                  V P                  4      V n        V P                  V n        \         P"                  ! 4       V n        \         P&                  ! V P(                  R7      V n        RV P*                  n        V P*                  P/                  4        R# )z{0}|{1}rX   TN)r-   r   r.   _kvp_file_path_truncate_guest_pool_file_event_typesrc   rd   q_get_incarnation_noincarnation_noformatEVENT_PREFIXevent_key_prefix	ZERO_GUID_vm_idre   Lock_vm_id_lockrf   _publish_event_routinepublish_threadri   rj   )r   kvp_file_pathevent_typesr:   s   &&&r   r.   "HyperVKvpReportingHandler.__init__   s    '79+!;;	
 (#kkm"668 ) 0 0t22!
 nn$>>+'....
 &*"!!#r   c                   V P                   '       d   R# \        P                  ! 4       \        \        P                  ! 4       4      ,
          p \
        P                  P                  V4      V8  d    \        VR4      ;_uu_ 4         RRR4       M RV n         R#   + '       g   i     L; i  \        \        3 d!   p\        P                  RT4        Rp?LHRp?ii ; i  RT n         i ; i)a  
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.
Nwz$failed to truncate kvp pool file, %sT)_already_truncated_pool_filetimefloatr
   uptimeospathgetmtimeopenOSErrorIOErrorr5   r6   )clskvp_file	boot_timery   s   &&  r   r   3HyperVKvpReportingHandler._truncate_guest_pool_file   s     +++IIK%"66		4ww)I5(C(( )( 6 04C, )(! 	CKK>BB	C 04C,sT   8B. BB. C" B+	&B. *C" +B. .C?CC" CC" "	C+c                    \         P                  ! 4       p \        \        P                  ! 4       \	        V4      ,
          4      #   \
         d    \        P                  RT4        ^ # i ; i)z
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.
z"uptime '%s' not in correct format.)r
   r   r0   r   r   
ValueErrorr5   r6   )r   
uptime_strs   & r   r   -HyperVKvpReportingHandler._get_incarnation_no   sP     [[]
	tyy{U:%6677 	KK<jI	s   -A !A*)A*c                z   V P                   ;_uu_ 4        V P                  V P                  8X  d5    \        P                  ! R4      pV'       d   VP                  4       V n        V P                  uuRRR4       #   \         d!   p\        P                  RT4        Rp?L<Rp?ii ; i  + '       g   i     R# ; i)zGet vm_id, with DMI system-uuid fallback.

If vm_id hasn't been set explicitly, falls back to reading
the system-uuid from DMI data.
zsystem-uuidz(Failed reading system-uuid for vm_id: %sN)	r   r   r   r   read_dmi_datalowerr4   r5   r}   )r   system_uuidry   s   &  r   vm_idHyperVKvpReportingHandler.vm_id  s     {{dnn,M"%"3"3M"BK"&1&7&7&9 ;;  ! MIIH!LLM s:   B)A;A;%B);B&B!B)!B&&B))B:	c                t    V P                   ;_uu_ 4        Wn        RRR4       R#   + '       g   i     R# ; i)zSet vm_id directly.N)r   r   )r   values   &&r   r   r     s$     K s   &7	c              #    "   \        V P                  R4      ;_uu_ 4       p\        P                  ! V\        P                  4       VP                  V4       VP                  V P                  4      p\        V4      V P                  8X  d3   V P                  V4      pVx  VP                  V P                  4      pKL  \        P                  ! V\        P                  4       RRR4       R#   + '       g   i     R# ; i5i)z-iterate the kvp file from the current offset.rbN)r   r   fcntlflockLOCK_EXseekreadHV_KVP_RECORD_SIZElen_decode_kvp_itemLOCK_UN)r   offsetfrecord_datakvp_items   &&   r   _iterate_kvps'HyperVKvpReportingHandler._iterate_kvps  s     $%%t,,KK5==)FF6N&&!8!89Kk"d&=&==00=ffT%<%<=KK5==) -,,,s   DCC.$
D.C?	9	Dc                    RP                  V P                  VP                  VP                  V P                  \
        P                  ! 4       4      # )zu
the event key format is:
CLOUD_INIT|<incarnation number>|<event_type>|<event_name>|<vm_id>
|<uuid>[|subevent_index]
z{0}|{1}|{2}|{3}|{4})r   r   rB   rC   r   uuiduuid4r   s   &&r   
_event_key$HyperVKvpReportingHandler._event_key'  s@     %++!!JJJJJJL
 	
r   c                    \         P                  ! R V P                  V P                  3,          VP	                  R4      VP	                  R4      4      pV# )z%ds%dsutf-8)structpackHV_KVP_EXCHANGE_MAX_KEY_SIZEHV_KVP_EXCHANGE_MAX_VALUE_SIZEencoder   keyr   rp   s   &&& r   _encode_kvp_item*HyperVKvpReportingHandler._encode_kvp_item5  sP    {{1133
 JJwLL!
 r   c                T   \        V4      pW P                  8w  d%   \        R P                  W P                  4      4      hV^ V P                   P                  R4      P                  R4      pVV P                  V P                   P                  R4      P                  R4      pRVRV/# )z$record_data len not correct {0} {1}.r    r   r   )r   r   r   r   r   decodestrip)r   r   record_data_lenkvs   &&   r   r   *HyperVKvpReportingHandler._decode_kvp_itemA  s    k*555!6==#%<%<  D==>VG_U6] 	
 11D4K4K VG_U6] 	
 q'1%%r   c           	        \         P                  ! R V P                   24      ;_uu_ 4        \        V P                  R4      ;_uu_ 4       p\        P
                  ! V\        P                  4       V F  pVP                  V4       K  	  VP                  4        \        P
                  ! V\        P                  4       RRR4       RRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i)z
Appending abN)
r   Timedr   r   r   r   r   writer!   r   )r   r   r   rp   s   &&  r   _append_kvp_item*HyperVKvpReportingHandler._append_kvp_itemX  s    D,?,?+@ABBDE
 E
KK5==)# $GGIKK5==)E
BB E
 E
BBBs$   C%A5C	 C%C"C%%C6	c           	     d   W P                    \        P                  ! V4      pV^\        V4      ^,
           p^ p. pRV P                   ,           R,           p WRV P                  &   RW P                   &   \        P                  ! W P
                  R7      pV P                  \        V4      ,
          ^,
          p	VP                  VRP                  V P                   VRV	 R7      4      p
RP                  W4      pVP                  V P                  W4      4       V^,          pWIR pV'       d   K   V# )	   "z":"" 
separatorsz"{key}":"{desc}"N)r   descz{}|{})MSG_KEYr   r   r   DESC_IDX_KEYJSON_SEPARATORSHV_KVP_AZURE_MAX_VALUE_SIZEreplacer   appendr   )r   r   	meta_datadescriptiondes_in_jsoniresult_arraymessage_place_holderdata_without_descroom_for_descr   subkeys   &&&&        r   _break_down%HyperVKvpReportingHandler._break_downb  s%   ll#jj-!!s;'7!';="T\\1F:+,d''(&(Ill# $

&:&:! 0037H3IIAM  &--$"));~+F * E ^^C+F 5 5f DEFA%n5K;r   c                *   < V ^8  d   QhRS[ RS[ RR/# )   r   r   returnN)str)r   r(   s   "r   __annotate__&HyperVKvpReportingHandler.__annotate__  s'     F FS F F Fr   c                
   \        V4      V P                  8  d   V^ V P                  ^,
           pV P                  W4      .p V P                  V4       R#   \        \
        3 d    \        P                  RY4        R# i ; i)z:Write KVP key-value.

Values will be truncated as needed.
zfailed posting kvp=%s value=%sN)r   r   r   r   r   r   r5   r6   r   s   &&& r   	write_key#HyperVKvpReportingHandler.write_key  sy    
 u:999!d>>BCE%%c12	F!!$'! 	FKK8#E	Fs   A 'BBc           	        V P                  V4      pRVP                  RVP                  R\        P                  ! VP
                  \        P                  4      P                  4       /p\        WP                  4      '       d   VP                  W0P                  &   \        VR4      '       d   VP                  VR&   VP                  W0P                  &   \        P                   ! W0P"                  R7      p\%        V4      V P&                  8  d   V P)                  W#VP                  4      # V P+                  W$4      pV.# )z
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.
rC   typetsdurationr   )r   rC   rB   r   fromtimestamp	timestampr   utc	isoformathasattr
RESULT_KEYr   r  r   r   r   r   r   r   r   r  r   )r   r   r   r   r   rp   s   &&    r   _encode_event'HyperVKvpReportingHandler._encode_event  s     ooe$EJJE$$&&OOX\\)+
	 5//**).Ioo&5*%%$)NNIj!"'"3"3	,,

91E1EF u:888##CE4E4EFF((4D6Mr   c                    ^ p V P                   P                  R R7      pV^,          p. pVe@   W0P                  V4      ,          p V P                   P                  RR7      pV^,          pKC   V P                  V4       \        V4       F  pV P                   P                  4        K  	  K    \        P                   d    Rp K  i ; i  \        \        3 d!   p\        P                  RT4        Rp?LxRp?ii ; i  \        T4       F  pT P                   P                  4        K  	  i ; i  \         d     R# i ; i)Trn   NFz failed posting events to kvp, %s)r   rv   r  rc   Emptyr   r   r   r5   r6   rangeru   EOFError)r   items_from_queuer   encoded_datary   _s   &     r   r   0HyperVKvpReportingHandler._publish_event_routine  s     


. A% !' $6$6u$==L% $


 7(A-(+)),7 ##34((* 5 !;; % $%  ) GKK BAFFG ##34((* 5  sl   AD2 %B/ -D2 0C ,D2 /C
D2 	C

D2 C>C94D 9C>>D .D//D2 2E Ec                    V P                   '       d   VP                  V P                   9   d   V P                  P                  V4       R # R # rN   )r   rB   r   r~   r   s   &&r   r   'HyperVKvpReportingHandler.publish_event  s7       E$4$48I8I$IFFJJu %Jr   c                d    \         P                  R 4       V P                  P                  4        R# )z0HyperVReportingHandler flushing remaining eventsN)r5   r}   r   rA   r    s   &r   r!   HyperVKvpReportingHandler.flush  s    		DEr   )r   r   r   r   r   r   r   r   ),:))r   r   r   r   r#   r   r   r   r   r   r  r   UUIDr   r   r  r   r   KVP_POOL_FILE_GUESTr   r.   classmethodr   r   propertyr   setterr   r   r   r   r   r  r
  r  r   r   r!   r   r&   rI   rJ   s   @@r   r   r      s      &*""&#& $'EE   LDII!$%IGJL O7#( %8d $. 4 4.    \\   

*

&.*<F F>< r   r   rD   rO   webhookhyperv)$r$   r   r   r2   r   rc   r   re   r   r   r   r   r   typingr   r>   r   r   r	   r
   cloudinit.registryr   r@   r   r5   r4   r   ABCMetar   r+   rL   rR   r   HandlerTypeavailable_handlersregister_itemr   r   r   <module>r3     s       	      '   8 8 +!	i 	? ?2! 2.!# !]%% ]%@j 0 j^	 	 "^      
 3     , 7     N ;     +D Er   