a '¡Dg <ã@sRddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z ddlm 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)r r 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'Z input_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)r r r rrrÚ __classcell__rrr(rr*s rc@seZdZdZdd„ZdS)Ú PrintHandlerzPrint the event as a string.cCst| ¡ƒdS©N)Úprintr1rrrrrDszPrintHandler.publish_eventN)r r 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)r r r rrIrrr3rrr(rr7Hsø!&r7csÌeZdZdZdZdZdZeeZdZdZ dZ dZ d Z d Z d Ze 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_pathZ event_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|||| [|subevent_index] z{0}|{1}|{2}|{3})rirkr.r/ÚuuidZuuid4rrrrÚ _event_key sÿ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|ƒdkrsH    `üÿ