x[h< ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z ddlm Z ddl mZddlmZmZmZddlmZej*eZGddeZGd d ej4 ZGd d eZGddeZGddeZGddeZeeeeeefZ eZ!e!jEdee!jEdee!jEdee!jEdey)N)datetimetimezone)Event)Union) performance url_helperutil) DictRegistryc eZdZy)ReportExceptionN)__name__ __module__ __qualname__>/usr/lib/python3/dist-packages/cloudinit/reporting/handlers.pyr r srr c:eZdZdZej dZdZy)ReportingHandlerzBase class for report handlers. Implement :meth:`~publish_event` for controlling what the handler does with an event. cy)zPublish an event.Nrselfevents r publish_eventzReportingHandler.publish_event"rcy)z0Ensure ReportingHandler has published all eventsNrrs rflushzReportingHandler.flush&rrN)r rr__doc__abcabstractmethodrrrrrrrs&    ?rr) metaclassc*eZdZdZdfd ZdZxZS) LogHandlerzBPublishes events to the cloud-init log at the ``DEBUG`` log level.c$tt| t|tr ||_ y|} t t |j}||_ y#t$r0tjd|t j}Y||_ ywxYw)Nzinvalid level '%s', using WARN) superr#__init__ isinstanceintgetattrloggingupper ExceptionLOGwarningWARNlevel)rr0 input_level __class__s rr&zLogHandler.__init__-s} j$(* eS !   K %7  % eZdZ dfd ZdZdZdZxZS)WebHookHandlerctt| t||||gr+t j ||||}|j |_ntj |_||_||_||_ tj|_ t|_tj |_t#j$|j&|_d|j(_|j(j-y)N) consumer_key token_key token_secretconsumer_secrettargetT)r%rGr&anyrOauthUrlHelperreadurlendpointtimeoutretriesr fetch_ssl_details ssl_detailsrflush_requestedqueueQueue threadingThreadprocess_requestsevent_processordaemonstart) rrRrIrJrKrLrSrT oauth_helperr2s rr&zWebHookHandler.__init__Is nd,.  iG H%44)#) / L (//DL%--DL    113$w"'++- (//t7L7LM&*# ""$rcd} |jjr|dkDrtjd|jj sO|jj |jj|jj sOd}|jjd} |j|d|d|d|d|dd d}|jj#t$r)}tjd |d||dz }Yd}~Hd}~wwxYw#|jjwxYw) NrTzNMultiple consecutive failures in WebHookHandler. Cancelling all queued events.blockF)datarSrTrV log_req_respz0Failed posting event: %s. This was caused by: %s) rWis_setr-r.rXempty get_nowait task_donegetrQr,)rconsecutive_failedargses rr\zWebHookHandler.process_requestsjs?##**,1Ca1G  4****,JJ))+JJ((*****,&'"::>>>-D ' Ga G G $Q!& &'" $$&E4 ( FG #a'"  ( $$&s*(D D5 D0+D80D55D88Ec|j}tjd|j||jj |jt j||j|j|jfy)NzQueuing POST to %s, data: %s) as_dictr-debugrRrXputjsondumpsrSrTrV)rr event_datas rrzWebHookHandler.publish_eventsg]]_  * MM    :&      rc|jjtjd|jj |jj y)Nz(WebHookHandler flushing remaining events)rWsetr-rtrXr8clearrs rrzWebHookHandler.flushsA   " <=  ""$r)NNNNNN)r rrr&r\rrr?r@s@rrGrGHs-%B$'L "%rrGceZdZdZdZdZdZeezZdZdZ dZ dZ d Z d Z d Ze d ffd ZedZdZdZdZdZdZdZdZdededd fdZdZdZdZdZxZS)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| ||_tj |j||_t j|_|j|_ dj|j|j|_ tj|j |_d|j"_|j"j'y)Nz{0}|{1}rMT)r%r}r&_kvp_file_path_truncate_guest_pool_file _event_typesrXrYq_get_incarnation_noincarnation_noformat EVENT_PREFIXevent_key_prefixrZr[_publish_event_routinepublish_threadr^r_)r kvp_file_path event_typesr2s rr&z"HyperVKvpReportingHandler.__init__s '79+!;;    (#kkm"668 ) 0 0   t22! (.... &*" !!#rc|jrytjttjz } t j j||krt|d5 dddd|_y#1swYxYw#ttf$r }tjd|Yd}~>d}~wwxYw#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_filetimefloatr uptimeospathgetmtimeopenOSErrorIOErrorr-r.)clskvp_file boot_timerqs rrz3HyperVKvpReportingHandler._truncate_guest_pool_files  + + IIK% "66  4ww)I5(C( 04C , ! C KK> B B C04C ,sB.B /B1B B B B<B72B?7B<<B?? Cctj} ttjt |z S#t $rt jd|YywxYw)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 rr(rr ValueErrorr-r.)r uptime_strs rrz-HyperVKvpReportingHandler._get_incarnation_nosP [[]  tyy{U:%667 7  KK||| [|subevent_index] z{0}|{1}|{2}|{3})rrr9r:uuiduuid4rs r _event_keyz$HyperVKvpReportingHandler._event_key s6 !''  ! !5#3#3UZZ  rctjd|j|jfz|j d|j d}|S)Nz%ds%dsutf-8)structpackHV_KVP_EXCHANGE_MAX_KEY_SIZEHV_KVP_EXCHANGE_MAX_VALUE_SIZEencoderkeyvaluerhs r_encode_kvp_itemz*HyperVKvpReportingHandler._encode_kvp_itemsP{{ 1133  JJw  LL !  rcRt|}||jk7r%tdj||j|d|jj dj d}||j|jj dj d}||dS)Nz$record_data len not correct {0} {1}.rr)rr)rrr rrdecodestrip)rrrecord_data_lenkvs rrz*HyperVKvpReportingHandler._decode_kvp_item!sk* d55 5!6==#T%<%<  D== > VG_ U6] 11D4K4K VG_ U6] 1%%rctjd|j5t|jd5}t j |tj |D]}|j||jt j |tjddddddy#1swYxYw#1swYyxYw)Nz Appending ab) rTimedrrrrrwriterr)rrrrhs r_append_kvp_itemz*HyperVKvpReportingHandler._append_kvp_item8s   D,?,?+@A B *D   E  *  KK5== )#    GGI KK5== ) * * * * * *s#CA1B<+C<C CCc 0||j=tj|}|dt|dz }d}g}d|jzdz} |||j<d||j<tj||j }|j t|z dz } |j|dj|j|d|  } d j||} |j|j| | |dz }|| d}|s |S) Nrer"z":"" separatorsz"{key}":"{desc}")rdescz{}|{}) MSG_KEYrvrwr 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_downBs6 dll #jj- !!s;'7!';=  "T\\1F:+,Id'' (&(Idll # $ d&:&:! 0037H3IIAM &--$")) ;~ +F*E ^^C+F    5 5fe D E FA%mn5K+rrrreturnct||jk\r|d|jdz }|j||g} |j|y#tt f$rt jd||YywxYw)zJWrite KVP key-value. Values will be truncated as needed. rrezfailed posting kvp=%s value=%sN)rrrrrrr-r.rs r write_keyz#HyperVKvpReportingHandler.write_key`s} u:99 9!d>>BCE%%c512 F  ! !$ '! F KK8#u E FsA&A:9A:c2|j|}|j|jtj|j t jjd}t||jr|j||j<|j||j<tj||j }t#||j$kDr|j'|||jS|j)||}|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. )r:typetsr)rr:r9r fromtimestamp timestamprutc isoformathasattr RESULT_KEYrrrrvrwrrrrr)rrrrrrhs r _encode_eventz'HyperVKvpReportingHandler._encode_eventos ooe$JJ$$&&OOX\\)+   5$// *).Idoo &"'"3"3 $,, 91E1EF u:88 8##CE4E4EF F((e4D6MrcX d} |jjd}|dz }g}|9||j|z } |jjd}|dz }|9 |j |t|D]}|jj #tj$rd}YXwxYw#t tf$r }tjd|Yd}~qd}~wwxYw#t|D]}|jjwxYw#t$rYywxYw)NTrrcreFz failed posting events to kvp, %s) rrnrrXEmptyrrrr-r.rangermEOFError)ritems_from_queuer encoded_datarq_s rrz0HyperVKvpReportingHandler._publish_event_routines9    . A% ! ' D$6$6u$==L% $  7(A-( '+)),7##34+((*+'!;;% $% )GKK BAFFG##34+((*+  sj9D!B! D$B<5*D!B96D8B99D<C+ C&!C.&C++C..,DD D)(D)c|jr|j|jvr|jj|yyrD)rr9rrurs rrz'HyperVKvpReportingHandler.publish_events4  E$4$48I8I$I FFJJu %Jrcbtjd|jjy)Nz0HyperVReportingHandler flushing remaining events)r-rtrr8rs rrzHyperVKvpReportingHandler.flushs DE  r) r rrrrrrrrrrrrKVP_POOL_FILE_GUESTrr& classmethodrrrrrrrrstrrrrrrr?r@s@rr}r}s &*""&#& $'EE LGJL O7#( %8d$&44.  *  &.*< FS F F F:<rr}r;rEwebhookhyperv)#rrrvr*rrXrrZrrrrrtypingrr5rrr cloudinit.registryr r7r r-r,r ABCMetarr#rBrGr} HandlerTypeavailable_handlers register_itemrrrrs    '33+g! i  ? ?2!2.!#!]%%]%@H 0HZ  "^  3 ,7 N; +DEr