o m[h}F@s\dZddlZddlZddlZddlZddlZddlmZddlm 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-dee.de/de.fddZ0d@ddZ1de#de$dee.e.ffddZ2d e,fd!d"Z3de4fd#d$Z5de/fd%d&Z6dee%e.ffd'd(Z7de/fd)d*Z8de/de/fd+d,Z9de/fd-d.Z:de#fd/d0Z;de.fd1d2Zdeeeffd7d8Z? 9dAd:eede/de,fd;d<Z@d=d>ZAeBd?kr,eAdSdS)BzGDefine '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 startedrunningdonedisabledN)__name__ __module__ __qualname____doc__ NOT_STARTEDRUNNINGDONEDISABLEDr r 6/usr/lib/python3/dist-packages/cloudinit/cmd/status.pyrs rc@seZdZdZdZdZdZdS)ConditionStatusz;Enum representing user-visible cloud-init condition status.errordegradedhealthyN)rrrrERRORDEGRADEDPEACHYr r r r!r"%s r"c@s0eZdZdZdZdZdZdZdZdZ dZ d Z d S) EnabledStatuszparserr r r! get_parseris4  rXr conditioncCs^|tjkrd|jd|jfS|tjkr)|tjtjfvr)|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)rrYr r r!translate_statuss   r\detailsc Cs`t|j|j\}}|j|jj|||j|j|j|j d|j }dt |i|d<d|d<|j dkrd}|d}t |d||jr|d rOd |d d }nd}|d r^d d |d nd}|drtd d dd|dDnd} t tj |d||d|d||| ddSdS|j dkrt tj|dddddS|j dkrt t|dSdS)zPrint status out to the CLI.)r;r6rHextended_statusdetailr8r9r:1schemas_schema_versionrKrHzstatus: r:z last_update:  r8 - z []r9cSs.g|]\}}|dddd|DqS)z: - recSsg|]}|ddqS)rdrZ)replace).0ir r r! sz+print_status...)join)rgkvr r r!ris z print_status..z {}r^r6r_)r^prefix boot_coder7r:r8r9rJT),z: )indent sort_keys separatorsrLN)r\r4r5r;r6r[r7r8r9r:r<rformatprintlonggetrjitemsTABULAR_LONG_TMPLrJdumpsr ) argsr]rHr^ details_dictrmstater: errors_outputrecoverable_errors_outputr r r! print_statussv        ! rcCst}t||j}|jr8|jtjtjfvr8|jdkr%tj dtj t||j}t d|jtjtjfvst |||jtjkrEdS|jtjkrMdSdS)z4Handle calls to 'cloud-init status' as a subcommand.rK.rCr)rget_status_detailsr@r4rrrrtsysrDwriteflushrrr5r"r&r')namer{pathsr]r r r!handle_status_argss,       rcCs8z tdg|d}Wd|vStjyd}Yd|vSw)z?Return whether cloud-init is disabled via environment variable.zshow-environmentr@rccloud-init=disabled)rGr rF)r@envr r r!_disabled_via_environmentsrcCst}tstj}d}||fSd|vrtj}d}||fStj|r0tj }d |}||fSd|vr=tj }d}||fSdtj ddvsNtrWt|d rWtj}d }||fStjtj|jd rltj}d }||fStjtj|jd 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..=)r)rgrr r r!riZs z"systemd_failed.. UnitFileStaterstaticT ActiveStateactiveSubStateexitedrMainPID0failed) rGr rFrustderrrdict splitlines startswith)r@servicerDestatesr r r!systemd_failed?sH      rcCstj|o tj| S)z%Return True if cloud-init is running.)rrr) status_file result_filer r r! is_runningysrcCs2|tvrtjSt||rtjS|dkrtjStjS)z(Return the running status of cloud-init.r)rrrrrrr)rrr6 latest_eventr r r!get_running_status~s rcCs4|dd}|r|d\}}}|dd}|S)zyGet the datasource from status.json. Return a lowercased non-prefixed version. So "DataSourceEc2" becomes "ec2" r;rcrZ)rw partitionlowerrf) status_v1r;ds_r r r!get_datasources rcCs.|d}|r |S|drd|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. r;stagezRunning in stage: )rw)rboot_descriptionr;r r r!get_descriptions  rcCs@d}|D]}t|trt||dpd|dpd}q|S)z,Return the latest event time from status_v1.rstartfinished)values isinstancermaxrw)rr stage_infor r r!get_latest_events    rcCsg}i}t|D]5\}}t|tr?||dg|di}|D]}||vr5t||||<q&||||q&q ||fS)z>Return a list of errors and recoverable_errors from status_v1.r8r9)sortedrxrrextendrwkeysr)rr8r9_keyrcurrent_recoverable_errorserr_typer r r! get_errorss$   rFrc Cstj}|pt}tj|jd}tj|jd}tt||\}}i}tj |r2t t | di}t |}t||} t|} | rHtdt| nd} t|\} } | rVtj}n| r[tj}t|||| }|tjkr|tr|t|dr|tj}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.jsonr<z%a, %d %b %Y %H:%M:%S %zrcrz"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.r;N)r"r(rrrrjrrCLOUDINIT_DISABLED_FILErrrrwrrrrrrr&r'rrrrrrappendpopr3)rr@r5rrr6rrr;r7rr:r8r9r4r r r!rs`      rcCst}ttd|dS)z$Tool to report status of cloud-init.rHN)rXrexitr parse_argsrVr r r!mainsr__main__r)NF)CrrSenumrJrrcopyrtimerrrtypingrrrr r r cloudinitr r cloudinit.cmd.develrcloudinit.distrosrcloudinit.helpersrcloudinit.utilrrrruniqueEnumrr"r) frozensetr+r,r-r.rr3ryr>boolrGrXr\rintrrrrrrrrrrrrrrr r r r!s          (  E *:    G