a ahiF@sHdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZmZmZmZmZddlmZmZddlmZddlmZdd lmZdd lmZmZmZd Z ej!Gd d d ej"Z#ej!Gdddej"Z$ej!Gdddej"Z%e&e%j'e%j(e%j)e%j*gZ+GdddeZ,dZ-ee.e/e.dddZ0d?ddZ1e#e$ee.e.fdddZ2e,dddZ3e4d d!d"Z5e/d d#d$Z6ee%e.fd d%d&Z7e/d d'd(Z8e/e/d)d*d+Z9e/d d,d-Z:e#d d.d/Z;e.d d0d1Zeeefd d6d7Z?d@eee/e,d9d:d;Z@dkrDeAdS)AzGDefine 'status' utility and handler as part of cloud-init command line.N)deepcopy)gmtimesleepstrftime)AnyDictList NamedTupleOptionalTuple)safeyamlsubp)read_cfg_paths) uses_systemd)Paths) get_cmdline load_jsonload_text_filez/etc/cloud/cloud-init.disabledc@s eZdZdZdZdZdZdZdS) RunningStatusz=Enum representing user-visible cloud-init application status.z not startedrunningZdonedisabledN)__name__ __module__ __qualname____doc__ NOT_STARTEDRUNNINGDONEDISABLEDrr8/usr/lib/python3.9/site-packages/cloudinit/cmd/status.pyrs rc@seZdZdZdZdZdZdS)ConditionStatusz;Enum representing user-visible cloud-init condition status.errorZdegradedZhealthyN)rrrrERRORDEGRADEDPEACHYrrrr r!%sr!c@s0eZdZdZdZdZdZdZdZdZ dZ d Z d S) EnabledStatuszcCs^|tjkr d|jd|jfS|tjkrR|tjtjfvrR|j|jd|jfS|j|jfS)zTranslate running and condition status to human readable strings. Returns (status, extended_status). Much of this is for backwards compatibility r"z -  )r!r#valuer$rrr)rrUrrr translate_statuss  rX)detailsc Cs@t|j|j\}}|j|jj|||j|j|j|j d|j }|j dkrd}|d}t |d||j r|drd|dd}nd}|d rd d |d nd }|d rddd d|d Dnd} t tj |d||d|d||| dn>|j dkr"t tj|ddddn|j dkrz+print_status...)join)r`kvrrr rbsz print_status..z {}rZr3r[)rZprefixZ boot_coder4r7r5r6rFT),z: )indentZ sort_keysZ separatorsrHN)rXr1r2r8r3rWr4r5r6r7r9formatprintZlonggetrditemsTABULAR_LONG_TMPLrFdumpsr ) argsrYrDrZZ details_dictrgstater7Z errors_outputZrecoverable_errors_outputrrr print_statussn        rs)r>cCst}t||j}|jr`|jtjtjfvr`|jdkrJtj dtj t||j}t dqt |||jtjkrzdS|jtjkrdSdS)z4Handle calls to 'cloud-init status' as a subcommand.rG.r?r)rget_status_detailsr=r1rrrrksysr@writeflushrrsr2r!r#r$)namerqpathsrYrrr handle_status_argss$         r|cCs4ztdg|d}Wntjy*d}Yn0d|vS)z?Return whether cloud-init is disabled via environment variable.zshow-environmentr=r\cloud-init=disabled)rCr rB)r=envrrr _disabled_via_environments  rcCst}tstj}d}nd|vr0tj}d}ntj|rNtj }d |}nd|vrbtj }d}ndtj ddvstrt|d rtj}d }nRtjtj|jd rtj}d }n.tjtj|jd rtj}d}n tj}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=enabledzc CsdD]}ztdd|g|d}Wn@tjy`}z&td|jtjdWYd}~dSd}~00td d |D}|d d s|d d ksdS|ddkr|ddkrqn|ddkr|ddkrdSn |ddks|ddkrdSdSdS)z2Return if systemd units report a cloud-init error.)zcloud-final.servicezcloud-config.servicezcloud-init.servicezcloud-init-local.serviceZshowz5--property=ActiveState,UnitFileState,SubState,MainPIDr}z^Failed to get status from systemd. Cloud-init status may be inaccurate. Error from systemctl: )fileNFcSs g|]}dd|dDqS)cSsg|] }|qSr)rA)r`xrrr rbWrcz-systemd_failed...=)r)r`rrrr rbWrcz"systemd_failed..Z UnitFileStaterZstaticTZ ActiveStateZactiveZSubStateZexitedrZMainPID0Zfailed) rCr rBrlstderrrwdict splitlines startswith)r=Zservicer@eZstatesrrr systemd_failed<sJ        rcCstj|otj| S)z%Return True if cloud-init is running.)rrr) status_file result_filerrr is_runningvsrcCs6|tvrtjSt||rtjS|dkr,tjStjSdS)z(Return the running status of cloud-init.rN)rrrrrrr)rrr3 latest_eventrrr get_running_status{s rcCs4|dd}|r0|d\}}}|dd}|S)zyGet the datasource from status.json. Return a lowercased non-prefixed version. So "DataSourceEc2" becomes "ec2" r8r\rV)rm partitionlowerr_) status_v1r8Zds_rrr get_datasources  rcCs2|d}|r|S|dr*d|dS|SdS)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. r8ZstagezRunning in stage: N)rm)rboot_descriptionr8rrr get_descriptions   rcCs@d}|D].}t|tr t||dp*d|dp6d}q |S)z,Return the latest event time from status_v1.rstartfinished)values isinstancermaxrm)rr stage_inforrr get_latest_events    rcCsg}i}t|D]j\}}t|tr||dg|di}|D]0}||vrjt||||<qL||||qLq||fS)z>Return a list of errors and recoverable_errors from status_v1.r5r6)sortedrnrrextendrmkeysr)rr5r6Z_keyrZcurrent_recoverable_errorsZerr_typerrr get_errorss"   rF)r{r=r>c Cstj}|pt}tj|jd}tj|jd}tt||\}}i}tj |rdt t | di}t |}t||} t|} | rtdt| nd} t|\} } | rtj}n | rtj}t|||| }|tjkrtrt|drtj}tj}d} | d|d 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.jsonr9z%a, %d %b %Y %H:%M:%S %zr\r}z"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.r8N)r!r%rrrrdrrCLOUDINIT_DISABLED_FILErrrrmrrrrrrr#r$rrrrrrappendpopr0)r{r=r2rrr3rrr8r4rr7r5r6r1rrr rvsb      rvcCst}ttd|dS)z$Tool to report status of cloud-init.rDN)rTrwexitr| parse_argsrRrrr mainsr__main__)N)NF)CrrOenumrFrrwcopyrtimerrrtypingrrrr r r Z cloudinitr r Zcloudinit.cmd.develrZcloudinit.distrosrZcloudinit.helpersrZcloudinit.utilrrrruniqueEnumrr!r& frozensetr(r)r*r+rr0ror;boolrCrTrXrsintr|rrrrrrrrrrrvrrrrrr st            ) B *:    G