관리-도구
편집 파일: handlers.cpython-39.opt-1.pyc
a ����'�Dg <����������������������@���sR��d�dl�Z�d�dlZd�dlZd�dlZd�dlZd�dlZd�dlZd�dlZd�dlZd�dl Z d�dl m Z mZ�d�dlmZ�d�dl mZ�d�dlmZmZmZ�d�dlmZ�e�e�ZG�dd��de�ZG�d d ��d e�jd�ZG�dd ��d e�ZG�dd��de�ZG�dd��de�ZG�dd��de�Zeeeeeef�Z e��Z!e!�"de��e!�"de��e!�"de��e!�"de��dS�)�����N)�datetime�timezone)�Event)�Union)�performance� url_helper�util)�DictRegistryc�������������������@���s���e�Zd�ZdS�)�ReportExceptionN)�__name__� __module__�__qualname__��r���r����@/usr/lib/python3.9/site-packages/cloudinit/reporting/handlers.pyr ������s���r ���c�������������������@���s&���e�Zd�ZdZejdd���Zdd��ZdS�)�ReportingHandlerz�Base class for report handlers. Implement :meth:`~publish_event` for controlling what the handler does with an event. c�����������������C���s���dS�)zPublish an event.Nr�����self�eventr���r���r���� publish_event"���s����zReportingHandler.publish_eventc�����������������C���s���dS�)z0Ensure ReportingHandler has published all eventsNr����r���r���r���r����flush&���s����zReportingHandler.flushN)r���r���r ����__doc__�abc�abstractmethodr���r���r���r���r���r���r������s��� r���)� metaclassc�����������������������s*���e�Zd�ZdZd��fdd� Zdd��Z���ZS�)� LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.�DEBUGc��������������������s`���t�t|������t|t�rn<|}ztt|����}W�n$�tyT���t � d|��tj}Y�n0�||�_d�S�)Nzinvalid level '%s', using WARN) �superr����__init__� isinstance�int�getattr�logging�upper� Exception�LOG�warningZWARN�level)r���r'���Zinput_level�� __class__r���r���r���-���s���� zLogHandler.__init__c�����������������C���s2���t��d�dd|j|jg��}|�|�j|�����d�S�)N�.� cloudinitZ reporting)r"���� getLogger�join� event_type�name�logr'���� as_string)r���r����loggerr���r���r���r���:���s�����zLogHandler.publish_event)r���)r���r���r ���r���r���r���� __classcell__r���r���r(���r���r���*���s��� r���c�������������������@���s���e�Zd�ZdZdd��ZdS�)�PrintHandlerzPrint the event as a string.c�����������������C���s���t�|�����d�S��N)�printr1���r���r���r���r���r���D���s����zPrintHandler.publish_eventN)r���r���r ���r���r���r���r���r���r���r4���A���s���r4���c�����������������������s6���e�Zd�Zd ��fdd� Zdd��Zdd��Zdd ��Z���ZS�)�WebHookHandlerNc����������� ���������s����t�t|������t||||g�r:tj||||d�}|j|�_ntj|�_||�_||�_||�_ t ���|�_t ��|�_t���|�_tj|�jd�|�_d|�j_|�j����d�S�)N)�consumer_key� token_key�token_secret�consumer_secret��targetT)r���r7���r����anyr���ZOauthUrlHelper�readurl�endpoint�timeout�retriesr���Zfetch_ssl_details�ssl_detailsr����flush_requested�queue�Queue� threading�Thread�process_requestsZevent_processor�daemon�start) r���r@���r8���r9���r:���r;���rA���rB���Zoauth_helperr(���r���r���r���I���s&���� � zWebHookHandler.__init__c�������������� ���C���s����d}|�j����rD|dkrDt�d��|�j���s@|�j����|�j����q d}|�jjdd�}z�z2|�j |d�|d�|d�|d�|d�d d ��d}W�n>�t y��}�z&t�d|d�|��|d7�}W�Y�d�}~n d�}~0�0�W�|�j����q|�j����0�qd�S�)Nr�������zNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.T��block������������F)�datarA���rB���rC���Zlog_req_respz0Failed posting event: %s. This was caused by: %s)rD����is_setr%���r&���rE����emptyZ get_nowait� task_done�getr?���r$���)r���Zconsecutive_failed�args�er���r���r���rI���j���s8����� �� zWebHookHandler.process_requestsc�����������������C���s@���|����}t�d|�j|��|�j�|�jt�|�|�j|�j |�j f��d�S�)NzQueuing POST to %s, data: %s)Zas_dictr%����debugr@���rE����put�json�dumpsrA���rB���rC���)r���r���Z event_datar���r���r���r�������s�������zWebHookHandler.publish_eventc�����������������C���s,���|�j�����t�d��|�j����|�j�����d�S�)Nz(WebHookHandler flushing remaining events)rD����setr%���rY���rE���r-����clearr���r���r���r���r�������s���� zWebHookHandler.flush)NNNNNN)r���r���r ���r���rI���r���r���r3���r���r���r(���r���r7���H���s����������!&r7���c�����������������������s����e�Zd�ZdZdZdZdZee�ZdZdZ dZ dZd Zd Z dZe df��fd d� Zedd���Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zeedd�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 i���i���i���Z CLOUD_INIT�msg�resultZmsg_i)�,�:z/var/lib/hyperv/.kvp_pool_1FNc��������������������sr���t�t|������||�_t�|�j��||�_t���|�_|�� ��|�_ d�|�j|�j �|�_ tj|�jd�|�_d|�j_|�j����d�S�)Nz{0}|{1}r<���T)r���r_���r����_kvp_file_path�_truncate_guest_pool_file�_event_typesrE���rF����q�_get_incarnation_noZincarnation_no�format�EVENT_PREFIX�event_key_prefixrG���rH����_publish_event_routineZpublish_threadrJ���rK���)r���Z kvp_file_pathZevent_typesr(���r���r���r�������s ����� ��z"HyperVKvpReportingHandler.__init__c�������������� ���C���s����|�j�r dS�t���tt�����}z~z>tj�|�|k�r\t|d���W�d����n1�sR0����Y��W�n4�t t fy��}�zt�d|��W�Y�d}~n d}~0�0�W�d|�_�nd|�_�0�dS�)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_timerX���r���r���r���re�������s����"$z3HyperVKvpReportingHandler._truncate_guest_pool_filec�����������������C���sD���t����}ztt���t|���W�S��ty>���t�d|��Y�dS�0�dS�)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���N)r���rq���r ���ro���rp���� ValueErrorr%���r&���)r���Z uptime_strr���r���r���rh�������s����z-HyperVKvpReportingHandler._get_incarnation_noc�����������������c���s����t�|�jd��n}t�|tj��|�|��|�|�j�}t|�|�jkr^|�� |�}|V��|�|�j�}q2t�|tj ��W�d����n1�s�0����Y��dS�)z-iterate the kvp file from the current offset.�rbN)ru���rd����fcntl�flock�LOCK_EX�seek�read�HV_KVP_RECORD_SIZE�len�_decode_kvp_item�LOCK_UN)r����offset�f�record_dataZkvp_itemr���r���r���� _iterate_kvps����s���� z'HyperVKvpReportingHandler._iterate_kvpsc�����������������C���s���d��|�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})ri���rk���r.���r/����uuidZuuid4r���r���r���r���� _event_key��s�����z$HyperVKvpReportingHandler._event_keyc�����������������C���s*���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�valuerR���r���r���r����_encode_kvp_item��s������� z*HyperVKvpReportingHandler._encode_kvp_itemc�����������������C���sd���t�|�}||�jkr$td�||�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 ���ri���r�����decode�strip)r���r����Zrecord_data_len�k�vr���r���r���r����!��s,���� ����� ����z*HyperVKvpReportingHandler._decode_kvp_itemc�������������� ���C���s����t��d|�j�����t�t|�jd��H}t�|tj��|D�]}|�|��q4|����t�|tj ��W�d�����n1�sn0����Y��W�d�����n1�s�0����Y��d�S�)Nz Appending �ab) r���ZTimedrd���ru���r{���r|���r}����writer���r����)r���r����r����rR���r���r���r����_append_kvp_item8��s�����z*HyperVKvpReportingHandler._append_kvp_itemc�����������������C���s����||�j�=�t�|�}|dt|�d���}d}g�}d|�j��d�}|||�j<�d||�j�<�tj||�jd�}|�jt|��d�} |�|dj|�j�|d�| ��d ��} d �||�}|� |�� || ���|d7�}|| d���}t|�dkr<q�q<|S�)NrO���r����"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����)r���r����� meta_data�descriptionZdes_in_json�iZresult_arrayZmessage_place_holderZdata_without_descZ room_for_descr����Zsubkeyr���r���r����_break_downB��s4���� ����z%HyperVKvpReportingHandler._break_down)r����r�����returnc�������������� ���C���sf���t�|�|�jkr |d|�jd���}|��||�g}z|��|��W�n$�ttfy`���t�d||��Y�n0�dS�)zJWrite KVP key-value. Values will be truncated as needed. r���rO���zfailed posting kvp=%s value=%sN)r����r����r����r����rv���rw���r%���r&���r����r���r���r���� write_key`��s����z#HyperVKvpReportingHandler.write_keyc�����������������C���s����|���|�}|j|jt�|jtj����d�}t ||�j �rB|j||�j <�|j||�j <�tj||�jd�}t|�|�jkr||��|||j�S�|��||�}|gS�dS�)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.���r���Z fromtimestampZ timestampr���ZutcZ isoformat�hasattr� RESULT_KEYra���r����r����r[���r\���r����r����r����r����r����)r���r���r����r����r����rR���r���r���r���� _encode_evento��s���� �� z'HyperVKvpReportingHandler._encode_eventc�������������� ���C���s��d}z�|�j�jdd�}|d7�}g�}|d�url||��|�7�}z|�j�jdd�}|d7�}W�q �tjyh���d�}Y�q 0�q z`z|��|��W�n4�ttfy��}�zt� d|��W�Y�d�}~n d�}~0�0�W�t |�D�]}|�j�����q�nt |�D�]}|�j�����q�0�W�q��t�y����Y�d�S�0�q�d�S�)Nr���TrM���rO���Fz failed posting events to kvp, %s) rg���rV���r����rE���ZEmptyr����rv���rw���r%���r&����rangerU����EOFError)r���Zitems_from_queuer���Zencoded_datarX����_r���r���r���rl������s,����$�z0HyperVKvpReportingHandler._publish_event_routinec�����������������C���s"���|�j�r|j|�j�v�r|�j�|��d�S�r5���)rf���r.���rg���rZ���r���r���r���r���r������s����z'HyperVKvpReportingHandler.publish_eventc�����������������C���s���t��d��|�j����d�S�)Nz0HyperVReportingHandler flushing remaining events)r%���rY���rg���r-���r���r���r���r���r������s���� zHyperVKvpReportingHandler.flush) r���r���r ���r���r����r����r����r����rj���r����r����r����r����ZKVP_POOL_FILE_GUESTrn���r����classmethodre���rh���r����r����r����r����r����r�����strr����r����rl���r���r���r3���r���r���r(���r���r_�������s8���� r_���r0���r6���ZwebhookZhyperv)#r���r{���r[���r"���rr���rE���r����rG���ro���r����r���r���r����typingr���r+���r���r���r���Zcloudinit.registryr ���r,���r���r%���r$���r ����ABCMetar���r���r4���r7���r_���ZHandlerTypeZavailable_handlersZ register_itemr���r���r���r����<module>���sH��� `����