name : status.cpython-39.pyc
a

puhF�@sHdZddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
mZddlm
Z
mZmZmZmZmZddlmZmZddlmZddlmZdd	lmZdd
lmZmZmZdZ ej!Gdd
�d
ej"��Z#ej!Gdd�dej"��Z$ej!Gdd�dej"��Z%e&e%j'e%j(e%j)e%j*g�Z+Gdd�de�Z,dZ-ee.e/e.d�dd�Z0d?dd�Z1e#e$ee.e.fd�dd�Z2e,d�dd�Z3e4d �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 �d0d1�Z<d2d3�Z=d4d5�Z>eeefd �d6d7�Z?d@eee/e,d9�d:d;�Z@d<d=�ZAeBd>k�rDeA�dS)AzGDefine 'status' utility and handler as part of cloud-init command line.�N)�deepcopy)�gmtime�sleep�strftime)�Any�Dict�List�
NamedTuple�Optional�Tuple)�safeyaml�subp)�read_cfg_paths)�uses_systemd)�Paths)�get_cmdline�	load_json�load_text_filez/etc/cloud/cloud-init.disabledc@s eZdZdZdZdZdZdZdS)�
RunningStatusz=Enum representing user-visible cloud-init application status.znot started�runningZdone�disabledN)�__name__�
__module__�__qualname__�__doc__�NOT_STARTED�RUNNING�DONE�DISABLED�rr�8/usr/lib/python3.9/site-packages/cloudinit/cmd/status.pyrs
rc@seZdZdZdZdZdZdS)�ConditionStatusz;Enum representing user-visible cloud-init condition status.�errorZdegradedZhealthyN)rrrr�ERROR�DEGRADED�PEACHYrrrr r!%sr!c@s0eZdZdZdZdZdZdZdZdZ	dZ
d	Zd
S)�
EnabledStatusz<Enum representing user-visible cloud-init boot status codes.zdisabled-by-generatorzdisabled-by-kernel-command-linezdisabled-by-marker-filez disabled-by-environment-variablezenabled-by-generatorzenabled-by-kernel-command-linezenabled-by-sysvinit�unknownN)rrrr�DISABLED_BY_GENERATOR�DISABLED_BY_KERNEL_CMDLINE�DISABLED_BY_MARKER_FILE�DISABLED_BY_ENV_VARIABLE�ENABLED_BY_GENERATOR�ENABLED_BY_KERNEL_CMDLINE�ENABLED_BY_SYSVINIT�UNKNOWNrrrr r&.sr&c@sreZdZUeed<eed<eed<eed<eeed<e	eeefed<eed<e
eed<e	eefed	<d
S)�
StatusDetails�running_status�condition_status�boot_status_code�description�errors�recoverable_errors�last_update�
datasource�v1N)rrrr�__annotations__r!r&�strrrr
rrrrr r0Fs
r0z�extended_status: {extended_status}
boot_status_code: {boot_code}
{last_update}detail: {description}
errors:{errors}
recoverable_errors:{recoverable_errors})�systemctl_args�wait�returncCsBzt�dg|��j��WStjy:|s.�td�Yq0qdS)z-Query systemd with retries and return output.Z	systemctl��?N)r
�stdout�strip�ProcessExecutionErrorr)r<r=rrr �query_systemctlZsrCcCsV|stjddd�}|jdtgd�ddd�|jd	d
ddd
d�|jdddddd�|S)a%Build or extend an arg parser for status utility.

    @param parser: Optional existing ArgumentParser instance representing the
        status subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    �statuszReport run status of cloud init)�progr4z--format)�json�tabular�yamlrGz5Specify output format for cloud-id (default: tabular))�type�choices�default�helpz-lz--long�
store_trueFzJReport long format of statuses including run stage name and error messages)�actionrKrLz-wz--waitz'Block waiting on cloud-init to complete)�argparse�ArgumentParser�add_argumentr;��parserrrr �
get_parseris4	���
�rT)r�	conditionr>cCs^|tjkr d|j�d|j��fS|tjkrR|tjtjfvrR|j|j�d|j��fS|j|jfS)z�Translate 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_status�s	
�rX)�detailsc

Cs@t|j|j�\}}|j|jj|||j|j|j|j	d�|j
�}|jdkr�d}|d}t|�d|���|j
r�|�d�r�d|d�d�}nd}|d	r�d
d
�|d	�nd}|dr�dd�d
d�|d��D��nd}	ttj|d||d|d|||	d��n>|jdk�r"ttj|dddd��n|jdk�r<tt�|��dS)zPrint status out to the CLI.)r8r3rD�extended_status�detailr5r6r7rG�rDzstatus: r7z
last_update: �
r5�
	- z []r6cSs.g|]&\}}|�d�d�dd�|D���qS)z:
	- r^cSsg|]}|�dd��qS)r]rV)�replace)�.0�irrr �
<listcomp>��z+print_status.<locals>.<listcomp>.<listcomp>)�join)r`�k�vrrr rb�s��z print_status.<locals>.<listcomp>z {}rZr3r[)rZ�prefixZ	boot_coder4r7r5r6rF�T)�,z: )�indentZ	sort_keysZ
separatorsrHN)rXr1r2r8r3rWr4r5r6r7r9�format�printZlong�getrd�items�TABULAR_LONG_TMPLrF�dumpsr)
�argsrYrDrZZdetails_dictrg�stater7Z
errors_outputZrecoverable_errors_outputrrr �print_status�sn��	�

���
���	�����rs)r>cCs�t�}t||j�}|jr`|jtjtjfvr`|jdkrJtj	�
d�tj	��t||j�}td�qt
||�|jtjkrzdS|jtjkr�dSdS)z4Handle calls to 'cloud-init status' as a subcommand.rG�.r?�r)r�get_status_detailsr=r1rrrrk�sysr@�write�flushrrsr2r!r#r$)�namerq�pathsrYrrr �handle_status_args�s$�



r|cCs4ztdg|d�}Wntjy*d}Yn0d|vS)z?Return whether cloud-init is disabled via environment variable.zshow-environment�r=r\�cloud-init=disabled)rCr
rB)r=�envrrr �_disabled_via_environments

r�cCs�t���}t�stj}d}n�d|vr0tj}d}n�tj�|�rNtj	}d�
|�}n�d|vrbtj}d}n�dtj�
dd�vs�t�r�t|d	�r�tj}d
}nRtj�tj�|jd��r�tj}d}n.tj�tj�|jd
��r�tj}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=enabledz<Cloud-init enabled by kernel command line cloud-init=enabledzCloud-init disabled by {0}r~z;Cloud-init disabled by kernel parameter cloud-init=disabledZKERNEL_CMDLINEr\r}zNCloud-init disabled by environment variable KERNEL_CMDLINE=cloud-init=disabledrz+Cloud-init disabled by cloud-init-generator�enabledz2Cloud-init enabled by systemd cloud-init-generatorz'Systemd generator may not have run yet.)r�splitrr&r.r-�os�path�existsr*rkr)�environrmr�r+rd�run_dirr(r,r/)Zdisable_filer{r=Z
cmdline_partsZbootstatus_code�reasonrrr �get_bootstatuss<	
���r�cCstt��jtvS)N)rvrr3�DISABLED_BOOT_CODESrrrr �is_cloud_init_enabled5s
��r�)r=r>cCs�dD�]�}ztdd|g|d�}Wn@tjy`}z&td|j��tjd�WYd}~dSd}~00td	d
�|��D��}|d�d�s�|dd
ks�dS|ddkr�|ddkr�qn|ddkr�|ddkr�dSn |ddks�|ddk�r�dSdSdS)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�|�d�D��qS)cSsg|]}|���qSr)rA)r`�xrrr rbWrcz-systemd_failed.<locals>.<listcomp>.<listcomp>�=)r�)r`�rrrr rbWrcz"systemd_failed.<locals>.<listcomp>Z
UnitFileStater�ZstaticTZActiveStateZactiveZSubStateZexitedrZMainPID�0Zfailed)	rCr
rBrl�stderrrw�dict�
splitlines�
startswith)r=Zservicer@�eZstatesrrr �systemd_failed<sJ
��
����
�
�
�r�cCstj�|�otj�|�S)z%Return True if cloud-init is running.)r�r�r�)�status_file�result_filerrr �
is_runningvsr�cCs6|tvrtjSt||�rtjS|dkr,tjStjSdS)z(Return the running status of cloud-init.rN)r�rrr�rrr)r�r�r3�latest_eventrrr �get_running_status{s
r�cCs4|�dd�}|r0|�d�\}}}|���dd�}|S)zyGet the datasource from status.json.

    Return a lowercased non-prefixed version. So "DataSourceEc2" becomes "ec2"
    r8r\rV)rm�	partition�lowerr_)�	status_v1r8Zds�_rrr �get_datasource�s
r�cCs2|�d�}|r|S|�d�r*d|d��S|SdS)z�Return 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)r��boot_descriptionr8rrr �get_description�s

r�cCs@d}|��D].}t|t�rt||�d�p*d|�d�p6d�}q|S)z,Return the latest event time from status_v1.r�start�finished)�values�
isinstancer��maxrm)r�r��
stage_inforrr �get_latest_event�s
�r�cCs�g}i}t|���D]j\}}t|t�r|�|�dg��|�di�}|��D]0}||vrjt||�||<qL||�||�qLq||fS)z>Return a list of errors and recoverable_errors from status_v1.r5r6)�sortedrnr�r��extendrm�keysr)r�r5r6Z_keyr�Zcurrent_recoverable_errorsZerr_typerrr �
get_errors�s"
��
�r�F)r{r=r>c
Cstj}|pt�}tj�|jd�}tj�|jd�}tt||�\}}i}tj�	|�rdt
t|���di�}t
|�}t||�}	t|�}
|
r�tdt|
��nd}t|�\}}
|r�tj}n
|
r�tj}t||||
�}|tjkr�t�r�t|d�r�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.
    zstatus.jsonzresult.jsonr9z%a, %d %b %Y %H:%M:%S %zr\r}z"Failed due to systemd unit failurez�Failed due to systemd unit failure. Ensure all cloud-init services are enabled, and check 'systemctl' or 'journalctl' for more information.r8N)r!r%rr�r�rdr�r��CLOUDINIT_DISABLED_FILEr�rrrmr�r�r�rrr�r#r$r�rrrr�r�append�popr0)r{r=r2r�r�r3r�r�r8r4r�r7r5r6r1rrr rv�sb

�
��������rvcCst�}t�td|����dS)z$Tool to report status of cloud-init.rDN)rTrw�exitr|�
parse_argsrRrrr �mainsr��__main__)N)NF)CrrO�enumrFr�rw�copyr�timerrr�typingrrrr	r
rZ	cloudinitrr
Zcloudinit.cmd.develrZcloudinit.distrosrZcloudinit.helpersrZcloudinit.utilrrrr��unique�Enumrr!r&�	frozensetr(r)r*r+r�r0ror;�boolrCrTrXrs�intr|r�r�r�r�r�r�r�r�r�r�rvr�rrrrr �<module>st 	
��
	�
)
�B	*:�
��G


© 2025 Cubjrnet7