f#F0dZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZmZmZmZmZddlmZmZddlmZddlmZdd lmZdd lmZmZmZd Z ejBGd d ejDZ#ejBGddejDZ$ejBGddejDZ%e&e%jNe%jPe%jRe%jTgZ+GddeZ,dZ-dee.de/de.fdZ0d.dZ1de#de$dee.e.ffdZ2de,fdZ3de4fdZ5de/fd Z6dee%e.ffd!Z7de/fd"Z8de/de/fd#Z9de/fd$Z:de#fd%Z;de.fd&Zdeeeffd)Z? d/d*eede/de,fd+Z@d,ZAeBd-k(reAyy)0zFDefine 'status' utility and handler as part of cloud-init commandline.N)deepcopy)gmtimesleepstrftime)AnyDictList NamedTupleOptionalTuple)safeyamlsubp)read_cfg_paths) uses_systemd)Paths) get_cmdline load_jsonload_text_filez/etc/cloud/cloud-init.disabledc eZdZdZdZdZdZdZy) RunningStatusz=Enum representing user-visible cloud-init application status.z not startedrunningdonedisabledN)__name__ __module__ __qualname____doc__ NOT_STARTEDRUNNINGDONEDISABLED6/usr/lib/python3/dist-packages/cloudinit/cmd/status.pyrrsGKG DHr#rceZdZdZdZdZdZy)ConditionStatusz;Enum representing user-visible cloud-init condition status.errordegradedhealthyN)rrrrERRORDEGRADEDPEACHYr"r#r$r&r&%sE EH Fr#r&c0eZdZdZdZdZdZdZdZdZ dZ d Z y ) EnabledStatusz>> o.. .7?4}} 17==/BBB =='-- ''r#detailsct|j|j\}}|j|jj |||j |j|j|jd|j}|jdk(r|jrdnd}|d}t|d||jr|jdr d|dd}nd}|d rd d j!|d znd }|d rmddj!|d j#D cgc]:\} } | d d j!| D cgc]} | j%ddc} z<c} } } znd} tt&j|d||d|d||| yy|jdk(r#tt)j*|dddy|jdk(rtt-j*|yycc} wcc} } } w)zPrint status out to the CLI.)rAr<rPextended_statusdetailr>r?r@rS rPzstatus: r@z last_update: r>z - z []r?z: - rcz {}rhr<ri)rhprefix boot_coder=r@r>r?rRT),z: )indent sort_keys separatorsrTN)rer:r;rAr<rdr=r>r?r@rBformatrFprintlonggetjoinitemsreplaceTABULAR_LONG_TMPLrRdumpsr ) argsrfrPrh details_dictrlstater@ errors_outputkvirecoverable_errors_outputs r$ print_statusrs9. 8 8FO((#44::*%%..%88** $ ** $L {{iX& () 99 . -l=.I-J"M   )8==h)?@@  45))%11E$F$L$L$N!Aq#W "--q(I!4)=(IJK & !(($01B$C!*+=> ,X 6 +('@) - B   JJQ$;    hnn\*+ 1)Js%HH HHcxt}t||j}|jr|jtj tj fvr|jdk(r=tjjdtjjt||j}td|jtj tj fvrt|||jtj k(ry|jtj"k(ryy)z4Handle calls to 'cloud-init status' as a subcommand.rS.rJrnr)rget_status_detailsrFr:rrrrssysrKwriteflushrrr;r&r*r+)namer|pathsrfs r$handle_status_argsrs  E  2G yy$$  % %  ! !)  {{i'   %   "( :G $K$$  % %  ! !)  w?#8#88  ! !_%=%= = r#cd tdg|}d|vS#tj$rd}Yd|vSwxYw)z?Return whether cloud-init is disabled via environment variable.zshow-environmentrFrkcloud-init=disabled)rNrrM)rFenvs r$_disabled_via_environmentrsK12> !C ''  % % C ''s //ctj}tstj}d}||fSd|vrtj }d}||fSt jj|r%tj}dj|}||fSd|vrtj}d}||fSdt jjddvstr"t| rtj}d }||fSt jjt jj!|j"d rtj$}d }||fSt jjt jj!|j"d rtj&}d}||fStj(}d}||fS)aXReport whether cloud-init current boot status @param disable_file: The path to the cloud-init disable file. @param paths: An initialized cloudinit.helpers.Paths object. @param wait: If user has indicated to wait for cloud-init to complete. @returns: A tuple containing (code, reason) about cloud-init's status and why. zCloud-init enabled on sysvinitzcloud-init=enabledz';;16 V $$5  .'AAO0 V $$/  %'??-44\B* V $$) - /'BBN$ V $$# "**..1A2"F F4$?'@@ 1  V $$  U]]J? @'==> V $$  U]]I> ?'<<E V $$(//: V $$r#cHttjtvSN)rrr<DISABLED_BOOT_CODESr"r#r$is_cloud_init_enabledr5s >+,==" #r#c 0dD]} tdd|g|}t |jDcgc]/}|jd Dcgc]}|jc}1c}}}|d jd s |d d k(sy |ddk(r|ddk(r|ddk(r |ddk(ryy|ddk(s|ddk(ry yy#tj$r3}td|jt jYd}~yd}~wwxYwcc}wcc}}w)z2Return if systemd units report a cloud-init error.)zcloud-final.servicezcloud-config.servicezcloud-init.servicezcloud-init-local.serviceshowz5--property=ActiveState,UnitFileState,SubState,MainPIDrz^Failed to get status from systemd. Cloud-init status may be inaccurate. Error from systemctl: )fileNF= UnitFileStaterstaticT ActiveStateactiveSubStateexitedrMainPID0failed) rNrrMrtstderrrdict splitlinesrrL startswith)rFservicerKerxstatess r$systemd_failedr<sK2  $K  F"8>8I8I8K L1 .Aaggi . L  ? # . .y 9o&(2 M "h .j!X- #y0VI5F#5M  = !X - 1Cx1Oe2j M))  ))* 4ZZ    / Ls.CD D D D (DD  D ctjj|xr tjj| S)z%Return True if cloud-init is running.)rrr) status_file result_files r$ is_runningrvs* 77>>+ & Jrww~~k/J+JJr#c|tvrtjSt||rtjS|dkDrtj Stj S)z(Return the running status of cloud-init.r)rrr!rrr r)rrr< latest_events r$get_running_statusr{sO..%%% K -$$$  !!!(((r#c|jdd}|r5|jd\}}}|jjdd}|S)zyGet the datasource from status.json. Return a lowercased non-prefixed version. So "DataSourceEc2" becomes "ec2" rArkrc)rv partitionlowerry) status_v1rAds_s r$get_datasourcersK |R0J'',AqXXZ'' b9 r#cb|jd}|r|S|jdrd|dS|S)zReturn a description of the current status. If we have a datasource, return that. If we're running in a particular stage, return that. Otherwise, return the boot_description. rAstagezRunning in stage: )rv)rboot_descriptionrAs r$get_descriptionrs@ |,J w #Ig$6#788r#cd}|jD]F}t|tst||j dxsd|j dxsd}H|S)z,Return the latest event time from status_v1.rstartfinished)values isinstancermaxrv)rr stage_infos r$get_latest_eventrs`L&&( j$ 'w',1z*/aL r#c`g}i}t|jD]\}}t|ts|j |j dg|j di}|j D]/}||vrt||||<||j ||1||fS)z>Return a list of errors and recoverable_errors from status_v1.r>r?)sortedrxrrextendrvkeysr)rr>r?_keyrcurrent_recoverable_errorserr_types r$ get_errorsrs F"9??#45j j$ ' MM*..26 7*4$b* &7;;= #553;28<4&x0'x07728<  " % %%r#rc tj}|xs t}tjj |j d}tjj |j d}tt||\}}i}tjj|r$tt|jdi}t|}t||} t|} | rt!dt#| nd} t%|\} } | rtj&}n| rtj(}t+|||| }|t,j.k(rIt1r?t3|r3t,j4}tj&}d} | j7d|j9d d t;|||| | | | || S) aReturn a dict with status, details and errors. @param paths: An initialized cloudinit.helpers.paths object. @param wait: If user has indicated to wait for cloud-init to complete. Values are obtained from parsing paths.run_dir/status.json. z status.jsonz result.jsonrBz%a, %d %b %Y %H:%M:%S %zrkrz"Failed due to systemd unit failurezFailed due to systemd unit failure. Ensure all cloud-init services are enabled, and check 'systemctl' or 'journalctl' for more information.rAN)r&r,rrrrwrrCLOUDINIT_DISABLED_FILErrrrvrrrrrrr*r+rrrrrr appendpopr9)rrFr;rrr<rrrAr=rr@r>r?r:s r$rrs'--  %^%E'',,u}}m>tRH  *J!)-=>K#I.L  +VL-AB  ",I!6F  *00 *33'["2LN -/// N  %&++*00:   $ MM,%   r#crt}tjtd|j y)z$Tool to report status of cloud-init.rPN)r`rexitr parse_argsr^s r$mainrs& \FHH &*;*;*= >?r#__main__r)NF)Crr[enumrRrrcopyrtimerrrtypingrrr r r r cloudinitr rcloudinit.cmd.develrcloudinit.distrosrcloudinit.helpersrcloudinit.utilrrrruniqueEnumrr&r. frozensetr0r1r2r3rr9rzrDboolrNr`rerintrrrrrrrrrrrrrrr"r#r$rs+M ((??$.*#AA:DII dii  DII    ++00--..  J + I    %P( ('6( 38_((?, ?,D c 6(t('%}c7I1J'%Tt77$7tKDK ) )     &U4:.&216D E?D)-DDN@  zFr#