
    x[h<                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
J
r
Jr  S SKJr  S SKJr  S SKJrJrJr  S SKJr  \R*                  " \5      r " S S\5      r " S	 S
\ R4                  S9r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\\\\\\4   r \" 5       r!\!RE                  S\5        \!RE                  S\5        \!RE                  S\5        \!RE                  S\5        g)    N)datetimetimezone)Event)Union)performance
url_helperutil)DictRegistryc                       \ rS rSrSrg)ReportException    N)__name__
__module____qualname____firstlineno____static_attributes__r       >/usr/lib/python3/dist-packages/cloudinit/reporting/handlers.pyr   r      s    r   r   c                   B    \ rS rSrSr\R                  S 5       rS rSr	g)ReportingHandler   zwBase class for report handlers.

Implement :meth:`~publish_event` for controlling what
the handler does with an event.
c                     g)zPublish an event.Nr   selfevents     r   publish_eventReportingHandler.publish_event"       r   c                     g)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   r   r   r   r   r      s&     	   ?r   r   )	metaclassc                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )
LogHandler*   zBPublishes events to the cloud-init log at the ``DEBUG`` log level.c                   > [         [        U ]  5         [        U[        5      (       a  O!Un [        [        UR                  5       5      nXl        g ! [         a)    [        R                  SU5        [        R                  n N9f = f)Nzinvalid level '%s', using WARN)superr)   __init__
isinstanceintgetattrloggingupper	ExceptionLOGwarningWARNlevel)r   r7   input_level	__class__s      r   r-   LogHandler.__init__-   sl    j$(*eS!!K%7 
  %<kJ%s   A 0BBc                     [         R                  " SR                  SSUR                  UR                  /5      5      nUR                  U R                  UR                  5       5        g )N.	cloudinit	reporting)r1   	getLoggerjoin
event_typenamelogr7   	as_string)r   r   loggers      r   r   LogHandler.publish_event:   sL    ""HHk;0@0@%**MN
 	

4::u01r   )r7   )DEBUG)	r   r   r   r   r$   r-   r   r   __classcell__r9   s   @r   r)   r)   *   s    L2 2r   r)   c                       \ rS rSrSrS rSrg)PrintHandlerA   zPrint the event as a string.c                 6    [        UR                  5       5        g N)printrD   r   s     r   r   PrintHandler.publish_eventD   s    eoo r   r   N)r   r   r   r   r$   r   r   r   r   r   rK   rK   A   s
    &!r   rK   c                   J   ^  \ rS rSr      SU 4S jjrS rS rS rSrU =r	$ )WebHookHandlerH   c                   > [         [        U ]  5         [        X#XE/5      (       a)  [        R
                  " UUUUS9nUR                  U l        O[        R                  U l        Xl        X`l        Xpl	        [        R                  " 5       U l        [        5       U l        [        R                   " 5       U l        ["        R$                  " U R&                  S9U l        SU R(                  l        U R(                  R-                  5         g )N)consumer_key	token_keytoken_secretconsumer_secrettargetT)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^   rU   rV   rW   rX   r_   r`   oauth_helperr9   s
            r   r-   WebHookHandler.__init__I   s     	nd,.GHH%44)#) /	L (//DL%--DL 113$w"'++-
(//t7L7LM&*#""$r   c           	         Sn U R                   R                  5       (       a  US:  a  [        R                  S5        U R                  R                  5       (       dU  U R                  R                  5         U R                  R                  5         U R                  R                  5       (       d  MU  SnU R                  R                  SS9n U R                  US   US   US   US   US   S	S
9  SnU R                  R                  5         GM  ! [         a)  n[        R                  SUS   U5        US-  n S nANKS nAff = f! U R                  R                  5         f = f)Nr   T   zNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.block         F)datar_   r`   rb   log_req_respz0Failed posting event: %s. This was caused by: %s)rc   is_setr4   r5   rd   empty
get_nowait	task_donegetr]   r3   )r   consecutive_failedargses       r   rh   WebHookHandler.process_requestsj   sH   ##**,,1Ca1G 4 ****,,JJ))+JJ((* ****,, &'"::>>>-D'Ga G G $Q!&   &'" 

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

$$&s*   %D 
ED>9E >EE E"c                    UR                  5       n[        R                  SU R                  U5        U R                  R                  U R                  [        R                  " U5      U R                  U R                  U R                  45        g )NzQueuing POST to %s, data: %s)as_dictr4   debugr^   rd   putjsondumpsr_   r`   rb   )r   r   
event_datas      r   r   WebHookHandler.publish_event   sg    ]]_
		*MM	

 	



:&  	
r   c                     U R                   R                  5         [        R                  S5        U R                  R                  5         U R                   R                  5         g )Nz(WebHookHandler flushing remaining events)rc   setr4   r   rd   r@   clearr!   s    r   r"   WebHookHandler.flush   sA      "		<=

""$r   )r^   ri   rc   rd   r]   r`   rb   r_   )NNNNNN)
r   r   r   r   r-   rh   r   r"   r   rH   rI   s   @r   rR   rR   H   s2     %B$'L
"% %r   rR   c                      ^  \ rS rSrSrSrSrSr\\-   rSr	Sr
SrS	rS
rSrSr\S4U 4S jjr\S 5       rS rS rS rS rS rS rS rS\S\SS4S jrS rS rS rS rSr U =r!$ ) HyperVKvpReportingHandler   a  
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msgresultmsg_i),:z/var/lib/hyperv/.kvp_pool_1FNc                   > [         [        U ]  5         Xl        [        R	                  U R                  5        X l        [        R                  " 5       U l        U R                  5       U l
        SR                  U R                  U R                  5      U l        [        R                  " U R                   S9U l        SU R"                  l        U R"                  R'                  5         g )Nz{0}|{1}rY   T)r,   r   r-   _kvp_file_path_truncate_guest_pool_file_event_typesrd   re   q_get_incarnation_noincarnation_noformatEVENT_PREFIXevent_key_prefixrf   rg   _publish_event_routinepublish_threadrj   rk   )r   kvp_file_pathevent_typesr9   s      r   r-   "HyperVKvpReportingHandler.__init__   s    '79+!;;	
 (#kkm"668 ) 0 0t22!
 (....
 &*"!!#r   c                    U R                   (       a  g[        R                  " 5       [        [        R                  " 5       5      -
  n [
        R                  R                  U5      U:  a  [        US5          SSS5        O SU l         g! , (       d  f       N= f! [        [        4 a   n[        R                  SU5         SnANDSnAff = f! SU l         f = f)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IOErrorr4   r5   )clskvp_file	boot_timer~   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   /B 7B9B C 
BB C B C,CC CC 	Cc                     [         R                  " 5       n [        [        R                  " 5       [	        U5      -
  5      $ ! [
         a    [        R                  SU5         gf = f)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	   r   r/   r   r   
ValueErrorr4   r5   )r   
uptime_strs     r   r   -HyperVKvpReportingHandler._get_incarnation_no   sP     [[]
	tyy{U:%6677 	KK<jI	s   )A  A$#A$c              #      #    [        U R                  S5       n[        R                  " U[        R                  5        UR                  U5        UR                  U R                  5      n[        U5      U R                  :X  aK  U R                  U5      nUv   UR                  U R                  5      n[        U5      U R                  :X  a  MK  [        R                  " U[        R                  5        SSS5        g! , (       d  f       g= f7f)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%<%<= k"d&=&== KK5==) -,,s#   DB4C=%C=4	D=
DDc                     SR                  U R                  UR                  UR                  [        R
                  " 5       5      $ )zm
the event key format is:
CLOUD_INIT|<incarnation number>|<event_type>|<event_name>|<uuid>
[|subevent_index]
z{0}|{1}|{2}|{3})r   r   rA   rB   uuiduuid4r   s     r   
_event_key$HyperVKvpReportingHandler._event_key  s6     !''!!5#3#3UZZ
 	
r   c                     [         R                  " SU R                  U R                  4-  UR	                  S5      UR	                  S5      5      nU$ )Nz%ds%dsutf-8)structpackHV_KVP_EXCHANGE_MAX_KEY_SIZEHV_KVP_EXCHANGE_MAX_VALUE_SIZEencoder   keyvalueru   s       r   _encode_kvp_item*HyperVKvpReportingHandler._encode_kvp_item  sP    {{1133
 JJwLL!
 r   c                 N   [        U5      nX R                  :w  a$  [        SR                  X R                  5      5      eUSU R                   R                  S5      R                  S5      nUU R                  U R                   R                  S5      R                  S5      nX4S.$ )Nz$record_data len not correct {0} {1}.r   r    )r   r   )r   r   r   r   r   decodestrip)r   r   record_data_lenkvs        r   r   *HyperVKvpReportingHandler._decode_kvp_item!  s    k*555!6==#%<%<  D==>VG_U6] 	
 11D4K4K VG_U6] 	
 %%r   c                    [         R                  " SU R                   35         [        U R                  S5       n[        R
                  " U[        R                  5        U H  nUR                  U5        M     UR                  5         [        R
                  " U[        R                  5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nz
Appending ab)
r   Timedr   r   r   r   r   writer"   r   )r   r   r   ru   s       r   _append_kvp_item*HyperVKvpReportingHandler._append_kvp_item8  s    D,?,?+@ABDE
KK5==)# $GGIKK5==)E
BB E
 E
BBs#   CA5C0C
C	C
C c           	      &   X R                   	 [        R                  " U5      nUS[        U5      S-
   nSn/ nSU R                   -   S-   n XRU R                  '   SX R                   '   [        R                  " X R
                  S9nU R                  [        U5      -
  S-
  n	UR                  USR                  U R                   US U	 S	95      n
S
R                  X5      nUR                  U R                  X5      5        US-  nXIS  nU(       d   U$ M  )Nrr   r   "z":"" 
separators   z"{key}":"{desc}")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   r   r   returnc                     [        U5      U R                  :  a  USU R                  S-
   nU R                  X5      /n U R                  U5        g! [        [
        4 a    [        R                  SX5         gf = f)z:Write KVP key-value.

Values will be truncated as needed.
r   rr   zfailed posting kvp=%s value=%sN)r   r   r   r   r   r   r4   r5   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 &A:9A:c                 0   U R                  U5      nUR                  UR                  [        R                  " UR
                  [        R                  5      R                  5       S.n[        XR                  5      (       a  UR                  X0R                  '   UR                  X0R                  '   [        R                  " X0R                   S9n[#        U5      U R$                  :  a  U R'                  X#UR                  5      $ U R)                  X$5      nU/$ )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.
)rB   typetsr   )r   rB   rA   r   fromtimestamp	timestampr   utc	isoformathasattr
RESULT_KEYr   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   ru   s         r   _encode_event'HyperVKvpReportingHandler._encode_evento  s     ooe$JJ$$&&OOX\\)+
	 5//**).Ioo&"'"3"3	,,

91E1EF u:888##CE4E4EFF((4D6Mr   c                 ^    Sn U R                   R                  SS9nUS-  n/ nUb7  X0R                  U5      -  n U R                   R                  SS9nUS-  nUb  M7   U R                  U5        [        U5       H  nU R                   R                  5         M     M  ! [        R                   a    S n N]f = f! [        [        4 a   n[        R                  SU5         S nANuS nAff = f! [        U5       H  nU R                   R                  5         M     f = f! [         a     g f = f)NTr   rp   rr   Fz failed posting events to kvp, %s)r   r{   r  rd   Emptyr   r   r   r4   r5   rangerz   EOFError)r   items_from_queuer   encoded_datar~   _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  sj   6D B D  B; 1,D B85D 7B88D ;C+C&!C. &C++C. ..DD 
D,+D,c                     U R                   (       a  UR                  U R                   ;   a  U R                  R                  U5        g g rN   )r   rA   r   r   r   s     r   r   'HyperVKvpReportingHandler.publish_event  s4      E$4$48I8I$IFFJJu %Jr   c                 b    [         R                  S5        U R                  R                  5         g )Nz0HyperVReportingHandler flushing remaining events)r4   r   r   r@   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   KVP_POOL_FILE_GUESTr   r-   classmethodr   r   r   r   r   r   r   r   strr  r  r   r   r"   r   rH   rI   s   @r   r   r      s     &*""&#& $'EE   LGJL O7#( %8d $& 4 4.
*

&.*<FS F F F:< r   r   rC   rO   webhookhyperv)#r%   r   r   r1   r   rd   r   rf   r   r   r   r   r   typingr   r=   r   r   r	   cloudinit.registryr
   r?   r   r4   r3   r   ABCMetar   r)   rK   rR   r   HandlerTypeavailable_handlersregister_itemr   r   r   <module>r&     s       	      '   3 3 +!	i 	? ?2! 2.!# !]%% ]%@H 0 HZ 	 "^      
 3     , 7     N ;     +D Er   