oB]c@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZmZddlmZddlmZejrddlmZnddlmZe jeZd efd YZejej d e!fd YZ"d e"fdYZ#de"fdYZ$de"fdYZ%de"fdYZ&e Z'e'j(de#e'j(de$e'j(de%e'j(de&dS(iN(tlog(t DictRegistry(t url_helpertutil(tdatetime(tEmpty(t JoinableQueue(tQueuetReportExceptioncBseZRS((t__name__t __module__(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRstReportingHandlercBs)eZdZejdZdZRS(sBase class for report handlers. Implement :meth:`~publish_event` for controlling what the handler does with an event. cCsdS(sPublish an event.N((tselftevent((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt publish_event'scCsdS(s0Ensure ReportingHandler has published all eventsN((R ((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pytflush+s(R R t__doc__tabctabstractmethodRR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR st LogHandlercBs#eZdZddZdZRS(sBPublishes events to the cloud-init log at the ``DEBUG`` log level.tDEBUGcCs~tt|jt|tr%nL|}ytt|j}Wn*tk rpt j d|tj }nX||_ dS(Nsinvalid level '%s', using WARN( tsuperRt__init__t isinstancetinttgetattrtloggingtuppert ExceptiontLOGtwarningtWARNtlevel(R R t input_level((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR3s  cCsGtjdjdd|j|jg}|j|j|jdS(Nt.t cloudinitt reporting(Rt getLoggertjoint event_typetnameRR t as_string(R R tlogger((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR@s$(R R RRR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR0s t PrintHandlercBseZdZdZRS(sPrint the event as a string.cCs|jGHdS(N(R)(R R ((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRIs(R R RR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR+FstWebHookHandlercBs,eZdddddddZdZRS(c Cstt|jt||||grUtjd|d|d|d||_n d|_||_||_ ||_ t j |_ dS(Nt consumer_keyt token_keyt token_secrettconsumer_secret(RR,RtanyRtOauthUrlHelpert oauth_helpertNonetendpointttimeouttretriesRtfetch_ssl_detailst ssl_details(R R5R-R.R/R0R6R7((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRNs      c Cs|jr|jj}n tj}yA||jdtj|jd|jd|jd|j SWn't k rt j d|j nXdS(NtdataR6R7R9sfailed posting event: %s(R3treadurlRR5tjsontdumpstas_dictR6R7R9RRRR)(R R R;((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR^s    N(R R R4RR(((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyR,Ms  tHyperVKvpReportingHandlercBseZdZdZdZeeZdZdZdZdZ dZ d Z e Z e dd Zed Zd Zd ZdZdZdZdZdZdZdZdZdZRS(s, 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 iit CLOUD_INITtmsgtresulttmsg_it,t:s/var/lib/hyperv/.kvp_pool_1cCstt|j||_tj|j||_t|_|j|_ dj |j |j |_ t jd|j|_t|j_|jjdS(Nu{0}|{1}ttarget(RR?Rt_kvp_file_patht_truncate_guest_pool_filet _event_typestJQueuetqt_get_incarnation_notincarnation_notformatt EVENT_PREFIXtevent_key_prefixt threadingtThreadt_publish_event_routinetpublish_threadtTruetdaemontstart(R t kvp_file_patht event_types((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRs       cCs|jr dStjttj}zey5tjj||kr`t|dWdQXnWn)t t fk r}t j d|nXWdt |_XdS(s 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. Ntws$failed to truncate kvp pool file, %s(t_already_truncated_pool_filettimetfloatRtuptimetostpathtgetmtimetopentOSErrortIOErrorRRRU(tclstkvp_filet boot_timete((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRHs  cCsStj}yttjt|SWn"tk rNtjd|dSXdS(s 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. s"uptime '%s' not in correct format.iN(RR^RR\R]t ValueErrorRR(R t uptime_str((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyRLs   ccst|jd}tj|tj|j||j|j}x?t||jkr|j |}|V|j|j}qJWtj|tj WdQXdS(s-iterate the kvp file from the current offset.trbN( RbRGtfcntltflocktLOCK_EXtseektreadtHV_KVP_RECORD_SIZEtlent_decode_kvp_itemtLOCK_UN(R toffsettft record_datatkvp_item((s@/usr/lib/python2.7/site-packages/cloudinit/reporting/handlers.pyt _iterate_kvpss cCs%dj|j|j|jtjS(ss the event key format is: CLOUD_INIT||||