fa;ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddlm Z ddl m Z ddlmZmZddlmZej&eZGddeZGd d ej0 ZGd d eZGddeZGddeZGddeZe eeeeefZeZejAdeejAdeejAdeejAdey)N)datetime)Event)Union) 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 r r __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)rr. 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#rEr$anyrOauthUrlHelperreadurlendpointtimeoutretriesrfetch_ssl_details ssl_detailsrflush_requestedqueueQueue threadingThreadprocess_requestsevent_processordaemonstart) rrPrGrHrIrJrQrR oauth_helperr0s 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)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?##**,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+debugrPrVputjsondumpsrQrRrT)rr event_datas rrzWebHookHandler.publish_eventsg]]_  * MM    :&      rc|jjtjd|jj |jj y)Nz(WebHookHandler flushing remaining events)rUsetr+rrrVr6clearrs rrzWebHookHandler.flushsA   " <=  ""$r)NNNNNN)r r r r$rZrrr=r>s@rrErEHs-%B$'L "%rrEceZdZdZdZdZdZeezZdZdZ 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}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_typesr0s rr$z"HyperVKvpReportingHandler.__init__s '79+!;;    ("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_filetimefloatruptimeospathgetmtimeopenOSErrorIOErrorr+r,)clskvp_file boot_timeros 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)rrr&rr ValueErrorr+r,)r uptime_strs rrz-HyperVKvpReportingHandler._get_incarnation_nosP [[]  tyy{U:%667 7  KK||| [|subevent_index] z{0}|{1}|{2}|{3})rrr7r8uuiduuid4rs 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_SIZEencoderkeyvaluerfs 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%%rc:t|jd5}tj|tj|D]}|j ||j tj|tjdddy#1swYyxYw)Nab)rrrrrwriterr)rrrrfs r_append_kvp_itemz*HyperVKvpReportingHandler._append_kvp_item8sp $%%t , * KK5== )#    GGI KK5== )  * * *s A1BBc H||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}t|dk(r |S) Nrcr"z":"" 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@s@ dll #jj- !!s;'7!';=  "T\\1F:+,Id'' (&(Idll # $ d&:&:! 0037H3IIAM &--$")) ;~ +F*E ^^C+F    5 5fe D E FA%mn5K;1$+rrrreturnct||jk\r|d|jdz }|j||g} |j|y#tt f$rt jd||YywxYw)zJWrite KVP key-value. Values will be truncated as needed. rrczfailed 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:c|j|}|j|jtj|j j dzd}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. Z)r8typetsr)rr8r7rutcfromtimestamp timestamp isoformathasattr RESULT_KEYr~rrrtrurrrrr)rrrrrrfs r _encode_eventz'HyperVKvpReportingHandler._encode_eventms ooe$JJ$$))%//:DDFL   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)NTrrarcFz failed posting events to kvp, %s) rrlrrVEmptyrrrr+r,rangerkEOFError)ritems_from_queuer encoded_dataro_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|yyrB)rr7rrsrs rrz'HyperVKvpReportingHandler.publish_events4  E$4$48I8I$I FFJJu %Jrcbtjd|jjy)Nz0HyperVReportingHandler flushing remaining events)r+rrrr6rs rrzHyperVKvpReportingHandler.flushs DE  r) r r r rrrrrrrrrrKVP_POOL_FILE_GUESTrr$ classmethodrrrrrrrrstrrrrrrr=r>s@rr{r{s &*""&#& $'EE LGJL O7#( %8d$&44.  *  &.*< FS F F F6<rr{r9rCwebhookhyperv)!rrrtr(rrVrrXrrrrtypingrr3rrcloudinit.registryrr5r r+r*r ABCMetarr!r@rEr{ HandlerTypeavailable_handlers register_itemrrrrs   &+g! i  ? ?2!2.!#!]%%]%@D 0DR  "^  3 ,7 N; +DEr