o
    fa;                     @   sJ  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ eeZG dd deZG d	d
 d
e jdZG dd deZG dd deZG dd deZG dd deZeeeeeef Ze Ze de e de e de e de dS )    N)datetime)Event)Union)
url_helperutil)DictRegistryc                   @   s   e Zd ZdS )ReportExceptionN)__name__
__module____qualname__ r   r   >/usr/lib/python3/dist-packages/cloudinit/reporting/handlers.pyr      s    r   c                   @   s&   e Zd ZdZejdd Zdd ZdS )ReportingHandlerzBase class for report handlers.

    Implement :meth:`~publish_event` for controlling what
    the handler does with an event.
    c                 C      dS )zPublish an event.Nr   selfeventr   r   r   publish_event"       zReportingHandler.publish_eventc                 C   r   )z0Ensure ReportingHandler has published all eventsNr   r   r   r   r   flush&   r   zReportingHandler.flushN)r	   r
   r   __doc__abcabstractmethodr   r   r   r   r   r   r      s
    
r   )	metaclassc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )
LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.DEBUGc                    s`   t t|   t|trn|}z	tt| }W n ty*   t	
d| tj}Y nw || _d S )Nzinvalid level '%s', using WARN)superr   __init__
isinstanceintgetattrloggingupper	ExceptionLOGwarningWARNlevel)r   r(   input_level	__class__r   r   r   -   s   


zLogHandler.__init__c                 C   s2   t ddd|j|jg}|| j|  d S )N.	cloudinit	reporting)r"   	getLoggerjoin
event_typenamelogr(   	as_string)r   r   loggerr   r   r   r   :   s   zLogHandler.publish_event)r   )r	   r
   r   r   r   r   __classcell__r   r   r*   r   r   *   s    r   c                   @   s   e Zd ZdZdd ZdS )PrintHandlerzPrint the event as a string.c                 C   s   t |  d S N)printr4   r   r   r   r   r   D   s   zPrintHandler.publish_eventN)r	   r
   r   r   r   r   r   r   r   r7   A   s    r7   c                       sB   e Zd Z						d
 fdd	Zdd Zdd Zdd	 Z  ZS )WebHookHandlerNc           	         s   t t|   t||||grtj||||d}|j| _ntj| _|| _|| _|| _	t
 | _t | _t | _tj| jd| _d| j_| j  d S )N)consumer_key	token_keytoken_secretconsumer_secrettargetT)r   r:   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   rD   r;   r<   r=   r>   rE   rF   oauth_helperr*   r   r   r   I   s&   



zWebHookHandler.__init__c              
   C   s   d}	 | j  r'|dkr'td | j s%| j  | j  | j rd}| jjdd}z@z| j	|d |d |d |d |d d	d
 d}W n t
yg } ztd|d | |d7 }W Y d }~nd }~ww W | j  n| j  w q)Nr   T   zNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.block         F)datarE   rF   rH   log_req_respz0Failed posting event: %s. This was caused by: %s)rI   is_setr%   r&   rJ   empty
get_nowait	task_donegetrC   r$   )r   consecutive_failedargser   r   r   rN   j   sD   



zWebHookHandler.process_requestsc                 C   s@   |  }td| j| | j| jt|| j| j	| j
f d S )NzQueuing POST to %s, data: %s)as_dictr%   debugrD   rJ   putjsondumpsrE   rF   rH   )r   r   
event_datar   r   r   r      s   zWebHookHandler.publish_eventc                 C   s,   | j   td | j  | j   d S )Nz(WebHookHandler flushing remaining events)rI   setr%   rd   rJ   r0   clearr   r   r   r   r      s   


zWebHookHandler.flush)NNNNNN)r	   r
   r   r   rN   r   r   r6   r   r   r*   r   r:   H   s    !&r:   c                       s   e Zd ZdZdZdZdZee ZdZdZ	dZ
dZd	Zd
ZdZedf fdd	Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zded ed!dfd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Z  ZS ),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msgresultmsg_i),:z/var/lib/hyperv/.kvp_pool_1FNc                    sr   t t|   || _t| j || _t | _| 	 | _
d| j| j
| _tj| jd| _d| j_| j  d S )Nz{0}|{1}r?   T)r   rk   r   _kvp_file_path_truncate_guest_pool_file_event_typesrJ   rK   q_get_incarnation_noincarnation_noformatEVENT_PREFIXevent_key_prefixrL   rM   _publish_event_routinepublish_threadrP   rQ   )r   kvp_file_pathevent_typesr*   r   r   r      s    

z"HyperVKvpReportingHandler.__init__c              
   C   s   | j rdS t tt  }zFztj||k r.t|d W d   n1 s)w   Y  W n t	t
fyI } ztd| W Y d}~nd}~ww W d| _ dS W d| _ dS d| _ w )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IOErrorr%   r&   )clskvp_file	boot_timerb   r   r   r   rs      s"   
z3HyperVKvpReportingHandler._truncate_guest_pool_filec                 C   s@   t  }ztt t| W S  ty   td| Y dS w )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   
ValueErrorr%   r&   )r   
uptime_strr   r   r   rv      s   z-HyperVKvpReportingHandler._get_incarnation_noc                 c   s    t | jd>}t|tj || || j}t|| jkr6| 	|}|V  || j}t|| jks!t|tj
 W d   dS 1 sHw   Y  dS )z-iterate the kvp file from the current offset.rbN)r   rr   fcntlflockLOCK_EXseekreadHV_KVP_RECORD_SIZElen_decode_kvp_itemLOCK_UN)r   offsetfrecord_datakvp_itemr   r   r   _iterate_kvps   s   

"z'HyperVKvpReportingHandler._iterate_kvpsc                 C   s   d | j|j|jt S )z
        the event key format is:
        CLOUD_INIT|<incarnation number>|<event_type>|<event_name>|<uuid>
        [|subevent_index]
        z{0}|{1}|{2}|{3})rx   rz   r1   r2   uuiduuid4r   r   r   r   
_event_key  s   z$HyperVKvpReportingHandler._event_keyc                 C   s*   t d| j| jf |d|d}|S )Nz%ds%dsutf-8)structpackHV_KVP_EXCHANGE_MAX_KEY_SIZEHV_KVP_EXCHANGE_MAX_VALUE_SIZEencoder   keyvaluerY   r   r   r   _encode_kvp_item  s   	z*HyperVKvpReportingHandler._encode_kvp_itemc                 C   sd   t |}|| jkrtd|| j|d| j dd}|| j| j dd}||dS )Nz$record_data len not correct {0} {1}.r   r    )r   r   )r   r   r   rx   r   decodestrip)r   r   record_data_lenkvr   r   r   r   !  s$   


z*HyperVKvpReportingHandler._decode_kvp_itemc                 C   sj   t | jd%}t|tj |D ]}|| q|  t|tj W d    d S 1 s.w   Y  d S )Nab)r   rr   r   r   r   writer   r   )r   r   r   rY   r   r   r   _append_kvp_item8  s   "z*HyperVKvpReportingHandler._append_kvp_itemc                 C   s   || j = t|}|dt|d  }d}g }d| j  d }	 ||| j< d|| j < tj|| jd}| jt| d }	||d	j| j |d |	 d
}
d||}|	| 
||
 |d7 }||	d  }t|dkrl	 |S q)NrV   r   "z":""T 
separators   z"{key}":"{desc}")r   descz{}|{})MSG_KEYrf   rg   r   DESC_IDX_KEYJSON_SEPARATORSHV_KVP_AZURE_MAX_VALUE_SIZEreplacerx   appendr   )r   r   	meta_datadescriptiondes_in_jsoniresult_arraymessage_place_holderdata_without_descroom_for_descr   subkeyr   r   r   _break_down@  s8   


z%HyperVKvpReportingHandler._break_downr   r   returnc              	   C   sf   t || jkr|d| jd  }| ||g}z| | W dS  ttfy2   td|| Y dS w )zJWrite KVP key-value.

        Values will be truncated as needed.
        r   rV   zfailed posting kvp=%s value=%sN)r   r   r   r   r   r   r%   r&   r   r   r   r   	write_key^  s   z#HyperVKvpReportingHandler.write_keyc                 C   s   |  |}|j|jt|j d d}t|| jr!|j	|| j< |j
|| j< tj|| jd}t|| jkr>| |||j
S | ||}|gS )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.
        Z)r2   typetsr   )r   r2   r1   r   utcfromtimestamp	timestamp	isoformathasattr
RESULT_KEYrn   r   r   rf   rg   r   r   r   r   r   )r   r   r   r   r   rY   r   r   r   _encode_eventm  s   
z'HyperVKvpReportingHandler._encode_eventc              
   C   s
  	 d}zv| j jdd}|d7 }g }|d ur:|| |7 }z| j jdd}|d7 }W n tjy5   d }Y nw |d usz0z| | W n ttfy\ } zt	d| W Y d }~nd }~ww W t
|D ]}| j   qbnt
|D ]}| j   qow W n
 ty   Y d S w q)NTr   rT   rV   Fz failed posting events to kvp, %s)ru   r_   r   rJ   Emptyr   r   r   r%   r&   ranger^   EOFError)r   items_from_queuer   encoded_datarb   _r   r   r   r{     s>   z0HyperVKvpReportingHandler._publish_event_routinec                 C   s&   | j r	|j| j v r| j| d S d S r8   )rt   r1   ru   re   r   r   r   r   r     s   z'HyperVKvpReportingHandler.publish_eventc                 C   s   t d | j  d S )Nz0HyperVReportingHandler flushing remaining events)r%   rd   ru   r0   r   r   r   r   r     s   
zHyperVKvpReportingHandler.flush) r	   r
   r   r   r   r   r   r   ry   r   r   r   r   KVP_POOL_FILE_GUESTr   r   classmethodrs   rv   r   r   r   r   r   r   strr   r   r{   r   r   r6   r   r   r*   r   rk      s:    

rk   r3   r9   webhookhyperv)!r   r   rf   r"   r   rJ   r   rL   r   r   r   r   typingr   r-   r   r   cloudinit.registryr   r/   r	   r%   r$   r   ABCMetar   r   r7   r:   rk   HandlerTypeavailable_handlersregister_itemr   r   r   r   <module>   sJ   
`  