name : handlers.cpython-39.pyc
a

'�Dg
<�@sRddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
m
Z
mZddlmZddl
mZddlmZmZmZddlmZe�e�ZGdd�de�ZGd	d
�d
ejd�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�ZeeeeeefZ e�Z!e!�"de�e!�"de�e!�"de�e!�"de�dS)�N)�datetime�timezone)�Event)�Union)�performance�
url_helper�util)�DictRegistryc@seZdZdS)�ReportExceptionN)�__name__�
__module__�__qualname__�rr�@/usr/lib/python3.9/site-packages/cloudinit/reporting/handlers.pyr
sr
c@s&eZdZdZejdd��Zdd�ZdS)�ReportingHandlerz�Base class for report handlers.

    Implement :meth:`~publish_event` for controlling what
    the handler does with an event.
    cCsdS)zPublish an event.Nr��self�eventrrr�
publish_event"szReportingHandler.publish_eventcCsdS)z0Ensure ReportingHandler has published all eventsNr�rrrr�flush&szReportingHandler.flushN)rrr
�__doc__�abc�abstractmethodrrrrrrrs
r)�	metaclasscs*eZdZdZd�fdd�	Zdd�Z�ZS)�
LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.�DEBUGcs`tt|���t|t�rn<|}ztt|���}Wn$tyTt	�
d|�tj}Yn0||_dS)Nzinvalid level '%s', using WARN)
�superr�__init__�
isinstance�int�getattr�logging�upper�	Exception�LOG�warningZWARN�level)rr'Zinput_level��	__class__rrr-s
zLogHandler.__init__cCs2t�d�dd|j|jg��}|�|j|���dS)N�.�	cloudinitZ	reporting)r"�	getLogger�join�
event_type�name�logr'�	as_string)rr�loggerrrrr:s�zLogHandler.publish_event)r)rrr
rrr�
__classcell__rrr(rr*s
rc@seZdZdZdd�ZdS)�PrintHandlerzPrint the event as a string.cCst|���dS�N)�printr1rrrrrDszPrintHandler.publish_eventN)rrr
rrrrrrr4Asr4cs6eZdZd
�fdd�	Zdd�Zdd�Zdd	�Z�ZS)�WebHookHandlerNc	s�tt|���t||||g�r:tj||||d�}|j|_ntj|_||_||_||_	t
��|_t
�|_t��|_tj|jd�|_d|j_|j��dS)N)�consumer_key�	token_key�token_secret�consumer_secret��targetT)rr7r�anyrZOauthUrlHelper�readurl�endpoint�timeout�retriesrZfetch_ssl_details�ssl_detailsr�flush_requested�queue�Queue�	threading�Thread�process_requestsZevent_processor�daemon�start)	rr@r8r9r:r;rArBZoauth_helperr(rrrIs&
�


zWebHookHandler.__init__c
Cs�d}|j��rD|dkrDt�d�|j��s@|j��|j��q d}|jjdd�}z�z2|j	|d|d|d|d|dd	d
�d}Wn>t
y�}z&t�d|d|�|d7}WYd}~n
d}~00W|j��q|j��0qdS)Nr�zNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.T��block���F)�datarArBrCZlog_req_respz0Failed posting event: %s. This was caused by: %s)rD�is_setr%r&rE�emptyZ
get_nowait�	task_done�getr?r$)rZconsecutive_failed�args�errrrIjs8�

�� zWebHookHandler.process_requestscCs@|��}t�d|j|�|j�|jt�|�|j|j	|j
f�dS)NzQueuing POST to %s, data: %s)Zas_dictr%�debugr@rE�put�json�dumpsrArBrC)rrZ
event_datarrrr�s���zWebHookHandler.publish_eventcCs,|j��t�d�|j��|j��dS)Nz(WebHookHandler flushing remaining events)rD�setr%rYrEr-�clearrrrrr�s


zWebHookHandler.flush)NNNNNN)rrr
rrIrrr3rrr(rr7Hs�!&r7cs�eZdZdZdZdZdZeeZdZdZ	dZ
dZd	Zd
Z
dZe
df�fd
d�	Zedd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zeedd�d d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Z�ZS)*�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
    iiiZ
CLOUD_INIT�msg�resultZmsg_i)�,�:z/var/lib/hyperv/.kvp_pool_1FNcsrtt|���||_t�|j�||_t��|_|�	�|_
d�|j|j
�|_
tj|jd�|_d|j_|j��dS)Nz{0}|{1}r<T)rr_r�_kvp_file_path�_truncate_guest_pool_file�_event_typesrErF�q�_get_incarnation_noZincarnation_no�format�EVENT_PREFIX�event_key_prefixrGrH�_publish_event_routineZpublish_threadrJrK)rZ
kvp_file_pathZevent_typesr(rrr�s �

��z"HyperVKvpReportingHandler.__init__c
Cs�|jr
dSt��tt���}z~z>tj�|�|kr\t|d��Wd�n1sR0YWn4t	t
fy�}zt�d|�WYd}~n
d}~00Wd|_nd|_0dS)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&)�clsZkvp_fileZ	boot_timerXrrrre�s"$z3HyperVKvpReportingHandler._truncate_guest_pool_filecCsDt��}ztt��t|��WSty>t�d|�YdS0dS)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.rN)rrqr rorp�
ValueErrorr%r&)rZ
uptime_strrrrrh�sz-HyperVKvpReportingHandler._get_incarnation_noccs�t|jd��n}t�|tj�|�|�|�|j�}t|�|jkr^|�	|�}|V|�|j�}q2t�|tj
�Wd�n1s�0YdS)z-iterate the kvp file from the current offset.�rbN)rurd�fcntl�flock�LOCK_EX�seek�read�HV_KVP_RECORD_SIZE�len�_decode_kvp_item�LOCK_UN)r�offset�f�record_dataZkvp_itemrrr�
_iterate_kvps�s

z'HyperVKvpReportingHandler._iterate_kvpscCsd�|j|j|jt���S)z�
        the event key format is:
        CLOUD_INIT|<incarnation number>|<event_type>|<event_name>|<uuid>
        [|subevent_index]
        z{0}|{1}|{2}|{3})rirkr.r/�uuidZuuid4rrrr�
_event_keys�z$HyperVKvpReportingHandler._event_keycCs*t�d|j|jf|�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�valuerRrrr�_encode_kvp_items���	z*HyperVKvpReportingHandler._encode_kvp_itemcCsdt|�}||jkr$td�||j���|d|j��d��d�}||j|j��d��d�}||d�S)Nz$record_data len not correct {0} {1}.rr��)r�r�)r�r�r
rir��decode�strip)rr�Zrecord_data_len�k�vrrrr�!s,
�����
����z*HyperVKvpReportingHandler._decode_kvp_itemc	Cs�t�d|j����tt|jd��H}t�|tj�|D]}|�|�q4|��t�|tj	�Wd�n1sn0YWd�n1s�0YdS)Nz
Appending �ab)
rZTimedrdrur{r|r}�writerr�)rr�r�rRrrr�_append_kvp_item8s�z*HyperVKvpReportingHandler._append_kvp_itemcCs�||j=t�|�}|dt|�d�}d}g}d|jd}|||j<d||j<tj||jd�}|jt|�d}	|�|dj|j|d|	�d	��}
d
�||�}|�	|�
||
��|d7}||	d�}t|�dkr<q�q<|S)NrOr�"z":""��Z
separators�z"{key}":"{desc}")r�Zdescz{}|{})�MSG_KEYr[r\r��DESC_IDX_KEY�JSON_SEPARATORS�HV_KVP_AZURE_MAX_VALUE_SIZE�replaceri�appendr�)rr��	meta_data�descriptionZdes_in_json�iZresult_arrayZmessage_place_holderZdata_without_descZ
room_for_descr�Zsubkeyrrr�_break_downBs4


����z%HyperVKvpReportingHandler._break_down)r�r��returnc	Csft|�|jkr |d|jd�}|�||�g}z|�|�Wn$ttfy`t�d||�Yn0dS)zJWrite KVP key-value.

        Values will be truncated as needed.
        rrOzfailed posting kvp=%s value=%sN)r�r�r�r�rvrwr%r&r�rrr�	write_key`sz#HyperVKvpReportingHandler.write_keycCs�|�|�}|j|jt�|jtj���d�}t	||j
�rB|j||j
<|j||j
<tj||jd�}t|�|jkr||�|||j�S|�||�}|gSdS)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.
        )r/�type�tsr�N)r�r/r.rZ
fromtimestampZ	timestamprZutcZ	isoformat�hasattr�
RESULT_KEYrar�r�r[r\r�r�r�r�r�)rrr�r�r�rRrrr�
_encode_eventos
��	z'HyperVKvpReportingHandler._encode_eventc
Csd}z�|jjdd�}|d7}g}|durl||�|�7}z|jjdd�}|d7}Wq tjyhd}Yq 0q z`z|�|�Wn4ttfy�}zt�	d|�WYd}~n
d}~00Wt
|�D]}|j��q�nt
|�D]}|j��q�0Wqt�yYdS0qdS)NrTrMrOFz failed posting events to kvp, %s)
rgrVr�rEZEmptyr�rvrwr%r&�rangerU�EOFError)rZitems_from_queuerZencoded_datarX�_rrrrl�s,$�z0HyperVKvpReportingHandler._publish_event_routinecCs"|jr|j|jvr|j�|�dSr5)rfr.rgrZrrrrr�sz'HyperVKvpReportingHandler.publish_eventcCst�d�|j��dS)Nz0HyperVReportingHandler flushing remaining events)r%rYrgr-rrrrr�s
zHyperVKvpReportingHandler.flush) rrr
rr�r�r�r�rjr�r�r�r�ZKVP_POOL_FILE_GUESTrnr�classmethodrerhr�r�r�r�r�r��strr�r�rlrrr3rrr(rr_�s8�



r_r0r6ZwebhookZhyperv)#rr{r[r"rrrEr�rGror�rrr�typingrr+rrrZcloudinit.registryr	r,rr%r$r
�ABCMetarrr4r7r_ZHandlerTypeZavailable_handlersZ
register_itemrrrr�<module>sH
`��

© 2025 Cubjrnet7