HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/uaclient/__pycache__/actions.cpython-310.pyc
o

nHJe*#�@s�ddlZddlZddlZddlZddlmZmZddlmZm	Z	m
Z
mZmZm
Z
ddlmZddlmZddlmZmZmZddlmZddlmZdd	lmZmZmZdd
lmZmZmZe� e�!e"��Z#dZ$dZ%d
e	j&de'de(ddfdd�Z)	d0d
e	j&dejddfdd�Z*dddddd�d
e	j&de'de(de(de(de'deee'fd d!�Z+ddd"�d
e	j&d#ee'd$e(fd%d&�Z	d1d'e'd(eee,ddfd)d*�Z-d
e	j&fd+d,�Z.d
e	j&d-e'fd.d/�Z/dS)2�N)�List�Optional)�clouds�config�contract�entitlements�
exceptions�	livepatch)�log)�status)�system�timer�util)�AutoAttachCloudInstance)�identity)�CLOUD_BUILD_INFO�DEFAULT_CONFIG_FILE�DEFAULT_LOG_PREFIX)�AttachmentData�attachment_data_file�timer_jobs_state_file)zua-timer.servicezua-timer.timerzua-auto-attach.pathzua-auto-attach.servicezua-reboot-cmds.servicezubuntu-advantage.service�
�cfg�token�allow_enable�returnc
CsNddlm}t�|�}tjjtjjd�}z	|j||d�}Wnt	j
y5}zt�t
|��t	���d}~ww|j�|�tj��|�di��dt�|��}|�d|�zt�|i|jj|�Wn(t	j
t	jfy�}	zt�t|d	��tj|d
�||�|��|	�d}	~	wwt��}
|
r�|�d|
�t�t|d	��||�t �!�dS)a:
    Common functionality to take a token and attach via contract backend
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    r)�update_motd_messages)�tz)�contract_token�
attachment_dtN�machineTokenInfo�	machineIdz
machine-id)�attached_at�rzinstance-id)"�uaclient.timer.update_messagingrr�UAContractClient�datetime�now�timezone�utc�add_contract_machiner�UrlError�LOG�	exception�str�ConnectivityError�machine_token_file�writer�get_machine_id�cache_clear�get�write_cache�process_entitlements_deltar�UbuntuProErrorrr�	ua_statusr�update_activity_tokenr�get_instance_idr
�start)rrrr�contract_clientr"�new_machine_token�e�
machine_id�exc�current_iid�rB�2/usr/lib/python3/dist-packages/uaclient/actions.py�attach_with_token.sN


���

��	��rDT�cloudcCs0t�|�}|j|d�}|d}t|||d�dS)aS
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    :raise NonAutoAttachImageError: If this cloud type does not have
        auto-attach support.
    )�instance�
contractToken)rrN)rr%�%get_contract_token_for_cloud_instancerD)rrErr<�
tokenResponserrBrBrC�auto_attachhs

�rJF�)�
assume_yes�
allow_beta�access_only�variant�
extra_args�namerLrMrNrOrPc	Cs,tj|||d�}|||||||d�}|��S)z�
    Constructs an entitlement based on the name provided. Passes kwargs onto
    the entitlement constructor.
    :raise EntitlementNotFoundError: If no entitlement with the given name is
        found, then raises this error.
    )rrQrO)rLrM�called_namerNrP)r�entitlement_factory�enable)	rrQrLrMrNrOrP�ent_cls�entitlementrBrBrC�enable_entitlement_by_names��rW)�simulate_with_token�show_allrXrYcCs:|rtj|||d�\}}||fStj||d�}d}||fS)z6
    Construct the current Pro status dictionary.
    )rrrY)rrYr)r8�simulate_statusr)rrXrYr�retrBrBrCr�s	
�	�r�filename�return_codesc
Csjz
tj|��|d�\}}Wntjy,}zt�d�|�t|��WYd}~dSd}~wwt�||�dS)zCHelper which runs a command and writes output or error to filename.)�rcsz{}-errorN)r�subp�splitr�ProcessExecutionError�
write_file�formatr.)�cmdr\r]�out�_r>rBrBrC�_write_command_output_to_file�s$��rgcCs(|jpt|jtjjtgdd�tjD��S)Ncss.�|]}t|tjj�r|jj|jd�VqdS))rQN)�
issubclassr�repo�RepoEntitlement�repo_list_file_tmplrcrQ)�.0rVrBrBrC�	<genexpr>�s��
�z#_get_state_files.<locals>.<genexpr>)	�cfg_pathr�log_filer�ua_file�pathrr�ENTITLEMENT_CLASSESr#rBrBrC�_get_state_files�s���rs�
output_dirc
Cs�tdd�|��tdd�|��td�tj�d�|��tdd�|��td	d
�|��td�d�d
d�tD���d�|��tD]}td�|�d�||�ddgd�qAt|�}t��rdt	�
�dt�nt	��g}t
|�D]7\}}zt�t�|��}t�tj�|d�|��|�Wqmty�}zt�d|t|��WYd}~qmd}~ww|t�td�D]I}	tj�|	�r�zt�|	�}Wnty�}zt�d|	t|��WYd}~q�d}~wwt�|�}t��r�t�|	|�t�tj�|tj�|	��|�q�dS)zG
    Write all relevant Ubuntu Pro logs to the specified directory
    zcloud-idz{}/cloud-id.txtzpro status --format jsonz{}/ua-status.jsonz	{} statusz{}/livepatch-status.txtzsystemctl list-timers --allz{}/systemd-timers.txtzujournalctl --boot=0 -o short-precise -u cloud-init-local.service -u cloud-init-config.service -u cloud-config.servicez{}/cloud-init-journal.txtzjournalctl -o short-precise {}� cSsg|]}d|vrd�|��qS)z.servicez-u {})rc)rl�srBrBrC�
<listcomp>�sz collect_logs.<locals>.<listcomp>z{}/pro-journal.txtzsystemctl status {}z	{}/{}.txtr�)r]Nz
user{}.logz&Failed to collect user log file: %s
%s�*zFailed to load file: %s
%s)rgrcr	�
LIVEPATCH_CMD�join�UA_SERVICESrsr�we_are_currently_root�pro_log�get_all_user_log_files�USER_LOG_COLLECTED_LIMIT�get_user_log_file�	enumerate�redact_sensitive_logsr�	load_filerb�osrq�	Exceptionr,�warningr.�globr�isfile�basename)
rrt�service�state_files�user_log_files�log_file_idxro�contentr>�frBrBrC�collect_logs�s�
�
�
���	���
����
�����
���r�)T)N)0r&r��loggingr��typingrr�uaclientrrrrrr	r
r~rr8rr
r�uaclient.cloudsrr�uaclient.defaultsrrr�uaclient.files.state_filesrrr�	getLogger�replace_top_level_logger_name�__name__r,r|r��UAConfigr.�boolrDrJrW�intrgrsr�rBrBrBrC�<module>s� 	���
�=���
��������

�!���
���
�
�