܍~c9LddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddlm Z ddl m Z ddl mZddl mZmZddlmZejeZGdd eZGd d ej ZGd deZGddeZGddeZGddeZe eeeeefZeZ e !dee !dee !dee !dedS)N)datetime)Event)Union)log) url_helperutil) DictRegistryceZdZdS)ReportExceptionN)__name__ __module__ __qualname__>/usr/lib/python3/dist-packages/cloudinit/reporting/handlers.pyr r sDrr c8eZdZdZejdZdZdS)ReportingHandlerzBase class for report handlers. Implement :meth:`~publish_event` for controlling what the handler does with an event. cdS)zPublish an event.Nrselfevents r publish_eventzReportingHandler.publish_event"rcdS)z0Ensure ReportingHandler has published all eventsNrrs rflushzReportingHandler.flush&rrN)r r r__doc__abcabstractmethodrrrrrrrsL     ?????rr) metaclassc*eZdZdZdfd ZdZxZS) LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.DEBUGcTtt|t|trnb|} t t |}n7#t$r*t d|t j }YnwxYw||_ dS)Nzinvalid level '%s', using WARN) superr"__init__ isinstanceintgetattrloggingupper ExceptionLOGwarningWARNlevel)rr0 input_level __class__s rr&zLogHandler.__init__-s j$((*** eS ! ! % K %77 % % % eZdZ dfd ZdZdZdZxZS)WebHookHandlerNc tt|t||||gr%t j||||}|j|_ntj|_||_||_||_ tj |_ t|_tj|_t#j|j|_d|j_|jdS)N) consumer_key token_key token_secretconsumer_secrettargetT)r%rEr&anyrOauthUrlHelperreadurlendpointtimeoutretriesrfetch_ssl_details ssl_detailsrflush_requestedqueueQueue threadingThreadprocess_requestsevent_processordaemonstart) rrPrGrHrIrJrQrR oauth_helperr2s rr&zWebHookHandler.__init__Is nd##,,...  iG H H .%4)#) / L (/DLL%-DL    133$ww[]] (/t7LMMM&*# ""$$$$$rcd} |jr|dkrtd|jsK|j|j|jKd}|jd} | |d|d|d|d|dd d}n>#t$r1}td |d||dz }Yd}~nd}~wwxYw|jn#|jwxYwo) NrTzNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.blockF)datarQrRrT log_req_respz0Failed posting event: %s. This was caused by: %s) rUis_setr-r.rVempty get_nowait task_donegetrOr,)rconsecutive_failedargses rrZzWebHookHandler.process_requestsjs" '#**,, '1Ca1G1G  4***,,+J))+++J((******,,+&'":>>>--D ' Ga G G $Q!& &'"" ( ( ( FG #a'""""""  ( $$&&&& $$&&&&E" 's0?;C;:E; D6'D1,E1D66EE.c|}td|j||j|jt j||j|j |j fdS)NzQueuing POST to %s, data: %s) as_dictr-debugrPrVputjsondumpsrQrRrT)rr event_datas rrzWebHookHandler.publish_events{]]__  * M      :&&         rc|jtd|j|jdS)Nz(WebHookHandler flushing remaining events)rUsetr-rrrVr8clearrs rrzWebHookHandler.flushsW   """ <===  ""$$$$$r)NNNNNN)r r rr&rZrrr=r>s@rrErEHs%%%%%%B$'$'$'L   "%%%%%%%rrEceZdZdZdZdZdZeezZdZdZ dZ dZ d Z d Z d Ze d ffd ZedZdZdZdZdZdZdZdZdZdZdZdZxZS)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 iii CLOUD_INITmsgresultmsg_i),:z/var/lib/hyperv/.kvp_pool_1FNctt|||_t|j||_t j|_| |_ d |j |j |_ tj|j|_d|j_|jdS)Nz{0}|{1}rKT)r%r{r&_kvp_file_path_truncate_guest_pool_file _event_typesrVrWq_get_incarnation_noincarnation_noformat EVENT_PREFIXevent_key_prefixrXrY_publish_event_routinepublish_threadr\r])r kvp_file_path event_typesr2s rr&z"HyperVKvpReportingHandler.__init__s '..77999+!;;     ("6688 ) 0 0  t2! ! (..   &*" !!#####rc|jrdStjttjz } t j||kr)t|d5 dddn #1swxYwYn9#ttf$r%}t d|Yd}~nd}~wwxYwd|_dS#d|_wxYw)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. Nwz$failed to truncate kvp pool file, %sT) _already_truncated_pool_filetimefloatruptimeospathgetmtimeopenOSErrorIOErrorr-r.)clskvp_file boot_timeros rrz3HyperVKvpReportingHandler._truncate_guest_pool_files+  +  FIKK% "6"66  4w))I55(C((! C C C KK> B B B B B B B B C04C , , ,tC , 3 3 3 3sS3B 2B4 B BB BB C CB=8C=CC Cctj} ttjt |z S#t $rt d|YdSwxYw)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)rrr(rr ValueErrorr-r.)r uptime_strs rrz-HyperVKvpReportingHandler._get_incarnation_nosl []]  ty{{U:%6%6677 7    KK||| [|subevent_index] z{0}|{1}|{2}|{3})rrr9r:uuiduuid4rs r _event_keyz$HyperVKvpReportingHandler._event_key s3 !''  !5#3UZ   rctjd|j|jfz|d|d}|S)Nz%ds%dsutf-8)structpackHV_KVP_EXCHANGE_MAX_KEY_SIZEHV_KVP_EXCHANGE_MAX_VALUE_SIZEencode)rkeyvaluerfs r_encode_kvp_itemz*HyperVKvpReportingHandler._encode_kvp_itemsS{ 13  JJw   LL ! !   rcnt|}||jkr(td||j|d|jdd}||j|jdd}||dS)Nz$record_data len not correct {0} {1}.rr)rr)rrr rrdecodestrip)rrrecord_data_lenkvs rrz*HyperVKvpReportingHandler._decode_kvp_item!sk** d5 5 5!6==#T%<  D== > VG__ U6]] 1D4KK VG__ U6]] 1%%%rc:t|jd5}tj|tj|D]}|||tj|tjddddS#1swxYwYdS)Nab)rrrrrwriterr)rrrrfs r_append_kvp_itemz*HyperVKvpReportingHandler._append_kvp_item8s $%t , , * K5= ) ) )#   GGIII K5= ) ) )  * * * * * * * * * * * * * * * * * *sA-BBBc ^||j=tj|}|dt|dz }d}g}d|jzdz} |||j<d||j<tj||j}|jt|z dz } ||d |j|d|  } d ||} | | | | |dz }|| d}t|dkrn|S) Nrcr"z":""T separatorsz"{key}":"{desc}")rdescz{}|{}) MSG_KEYrtrur DESC_IDX_KEYJSON_SEPARATORSHV_KVP_AZURE_MAX_VALUE_SIZEreplacerappendr) rr meta_data description des_in_jsoni result_arraymessage_place_holderdata_without_desc room_for_descrsubkeys r _break_downz%HyperVKvpReportingHandler._break_down@se dl #j-- !!s;'7'7!';"<=  "T\1F: +,Id' (&(Idl # $ d&:!!! 037H3I3IIAM &--$")) ;~ ~+F*E ^^C++F    5 5fe D D E E E FA%mnn5K;1$$) *rc||}|j|jtj|jdzd}t||jr|j ||j<|j ||j <tj ||j}t||jkr||||j S|||}|gS)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)r:typetsr)rr:r9rutcfromtimestamp timestamp isoformathasattr RESULT_KEYr~rrrtrurrrrr)rrrrrrfs r _encode_eventz'HyperVKvpReportingHandler._encode_event^s ooe$$J$)%/::DDFFL    5$/ * * 6).Ido &"'"3 $, 91EFFF u::8 8 8##CE4EFF F((e44D6Mrcx d} |jd}|dz }g}|S|||z } |jd}|dz }n#tj$rd}YnwxYw|S ||n9#t tf$r%}t d|Yd}~nd}~wwxYwt|D]}|j n0#t|D]}|j wxYwn#t$rYdSwxYw:)NTrrarcFz failed posting events to kvp, %s) rrlrrVEmptyrrrr-r.rangerkEOFError)ritems_from_queuer encoded_dataro_s rrz0HyperVKvpReportingHandler._publish_event_routineys     .. A% ! ' D$6$6u$=$==L% $  7 7(A-(( ;%%% $% '+)),7777)GGGKK BAFFFFFFFFG##344++((****+U#344++((****++    / sqs@rr{r{sU  &*""&#& $'EE LGJL O7#( %8d$$$$$$&44[4.    * * *      &&&.***<6<rr{rrCwebhookhyperv)"rrrtrrVrrXrrrrtypingrr5rr*rrcloudinit.registryr r7r r-r,r ABCMetarr"r@rEr{ HandlerTypeavailable_handlers register_itemrrrrs  $$$$$$&&&&&&&&++++++g!!     i    ? ? ? ? ? ? ? ? ?22222!222.!!!!!#!!!]%]%]%]%]%%]%]%]%@uuuuu 0uuut  "\^^  333 ,777 N;;; +DEEEEEr