HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/uaclient/__pycache__/actions.cpython-38.pyc
U

��Jhp:�	@sddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZm
Z
mZmZmZmZmZddl
mZddl
mZmZddl
mZddl
mZmZmZddlmZmZmZm Z dd	l!m"Z"m#Z#dd
l$m%Z%m&Z&m'Z'm(Z(m)Z)e�*�Z+e�,e�-e.��Z/dZ0dZ1e
j2ej3ejd
�dd�Z4d0e
j2eej5ej3eje6d�dd�Z7d1e
j2e8e6e6dd�dd�Z9d2e
j2ej:dd�dd�Z;ddddd�e
j2e8e6e8e6e	ee8d�dd�Z<ddd �e
j2e	e8e6d!�d"d#�Ze8dd$�d%d&�Z=d3e8e	ee>dd'�d(d)�Z?e
j2d*�d+d,�Z@e
j2e8d-�d.d/�ZAdS)4�N)�List�Optional)�api�clouds�config�contract�entitlements�event_logger�
exceptions�	livepatch)�log)�messages�secret_manager)�status)�system�timer�util)�APPARMOR_PROFILES�CLOUD_BUILD_INFO�DEFAULT_CONFIG_FILE�DEFAULT_LOG_PREFIX)�
machine_token�notices)�AttachmentData�attachment_data_file�machine_id_file�only_series_check_marker_file�timer_jobs_state_file)zapt-news.servicezesm-cache.servicezua-timer.servicezua-timer.timerzua-auto-attach.pathzua-auto-attach.servicezua-reboot-cmds.servicezubuntu-advantage.service�
)�cfg�contract_client�attached_atcCs<ddlm}t�t|d��tj|d�||�|��dS)Nr��update_motd_messages�r!�r)�uaclient.timer.update_messagingr#r�writer�	ua_statusrZupdate_activity_token)rr r!r#�r)�2/usr/lib/python3/dist-packages/uaclient/actions.py�_handle_partial_attach9s
r+F�r�services_to_be_enabledr r!�silentc

CsTd}g}g}zN|D]D}t||j|j|d�\}	}
||	M}|	sH|�|j�qtj|jd�qWn�tjk
r�}z t�|j�t	|||�|�W5d}~XYn`tj
k
r�|�|j�d}Yn<tk
r�}zd}|�|j�|�|�W5d}~XYnX|�sPt	|||�t�|�|�r:tj
dd�t||�D�d��ntjdd�|D�d��dS)	NT)r�name�variantr.)�serviceFcSs,g|]$\}}|tjjt|�t��d�f�qS))Z	error_msgZlog_path)r
ZUNEXPECTED_ERROR�format�str�pro_logZget_user_or_root_log_file_path)�.0r/Z	exceptionr)r)r*�
<listcomp>rs���z,_enable_default_services.<locals>.<listcomp>)�failed_servicescSsg|]}|tjf�qSr))r
Z!E_ATTACH_FAILURE_DEFAULT_SERVICES)r5r/r)r)r*r6�s�)�enable_entitlement_by_namer/r0�append�eventZservice_processedr
ZConnectivityErrorZservice_failedr+ZUbuntuProError�	ExceptionZservices_failedZAttachFailureUnknownError�zipZAttachFailureDefaultServices)
rr-r r!r.�retr7Zunexpected_errorsZenable_by_default_service�ent_ret�reason�exc�er)r)r*�_enable_default_servicesFsV�

�����rB)r�token�allow_enabler.�returnc
Cs�ddlm}ddlm}tj�|�t�|�}t	�
|�}tjjtj
jd�}|j||d�}	t��j}
|	�di��di�}tdd	�|�d
g�D��}|�di��di��d
d�}
|
�r$zt�|
�}Wntjk
r�Yn,Xt�|
�}|j|jk�rtj|j|jd��tjtjj|j|jd�t �!|
�|�!|	�z||�Wn2tj"k
�rl}z|�#�|�W5d}~XYnXtj$�%�|	�di��dt�$|��}t&�!|�|�r�t	�'||�(��}t)|||||d�t*�!t+|d��||�t,�-�dS)aC
    Common functionality to take a token and attach via contract backend
    :raise ConnectivityError: 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)�+check_entitlement_apt_directives_are_uniquer")Ztz)Zcontract_tokenZ
attachment_dtZmachineTokenInfo�contractInfocss*|]"}|�d�dkr|�d�|fVqdS)�type�supportN)�get)r5rAr)r)r*�	<genexpr>�s�z$attach_with_token.<locals>.<genexpr>ZresourceEntitlementsrIZaffordancesZ
onlySeriesN)�release�series_codenameZ	machineIdr,r$).Zuaclient.entitlementsrFr&r#rZsecretsZ
add_secretrZget_machine_token_filer�UAContractClient�datetimeZnow�timezoneZutcZadd_contract_machinerZget_release_infoZseriesrJ�dictZget_distro_infor
ZMissingSeriesInDistroInfoFileZeolZAttachFailureRestrictedReleaserLrMr�addZNoticeZLIMITED_TO_RELEASErr'Z%EntitlementsAPTDirectivesAreNotUnique�deleteZget_machine_id�cache_clearrZget_enabled_by_default_servicesrrBrrr�start)rrCrDr.rFr#Zmachine_token_filer r!Znew_machine_tokenZcurrent_seriesrGZsupport_resourceZonly_seriesZallowed_releaseZcurrent_releaserAZ
machine_idr-r)r)r*�attach_with_token�s�


�
�
����
��


�
��rVT)r�cloudrEcCs|�|�}t|||d�dS)a\
    :raise ConnectivityError: 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.
    )rCrDN)Zacquire_pro_tokenrV)rrWrDrCr)r)r*�auto_attach�s
rX�)�access_onlyr0r.�
extra_args)rr/rZr0r.r[c	Csftj|||||d�}|s.t�tjj|jd��|�t	�
��\}}|r^|s^t�tjj|jd��||fS)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.
    )rr/r0rZr[)�title)rZentitlement_factoryr:�infor
Z
ENABLING_TMPLr2r\�enablerZProgressWrapperZENABLED_TMPL)	rr/rZr0r.r[Zentitlementr>r?r)r)r*r8�s�r8)�simulate_with_token�show_all)rr_r`cCs4|rtj|||d�\}}ntj||d�}d}||fS)z6
    Construct the current Pro status dictionary.
    )rrCr`�rr`r)r(Zsimulate_statusr)rr_r`rr=r)r)r*rs	�r)�filenamerEc
Cs�ddddg}d}d}zt�|�\}}WnJtjk
rp}z*t�dt|��t�d�|�t|��W5d}~XYnBX|r�g}|�	d	�D]}t
�||�r�|�|�q�t�|d	�
|��dS)
z�
    Helper which gets ubuntu_pro apparmor logs from the kernel from the last
    day and writes them to the specified filename.
    Z
journalctlz-bz-kz--since=1 day agoz7apparmor=\".*(profile=\"ubuntu_pro_|name=\"ubuntu_pro_)Nz!Failed to collect kernel logs:
%s�{}-error�
)r�subpr
�ProcessExecutionError�LOG�warningr3�
write_filer2�split�re�searchr9�join)rb�cmdZapparmor_reZkernel_logs�_rAZ
apparmor_logsZkernel_liner)r)r*�_write_apparmor_logs_to_file0s(rp)rb�return_codesrEc
Csnztj|��|d�\}}Wn:tjk
rT}zt�d�|�t|��W5d}~XYnXt�|d�||��dS)zCHelper which runs a command and writes output or error to filename.)ZrcsrcNzstdout:
{}

stderr:
{})rrerjr
rfrir2r3)rnrbrq�out�errrAr)r)r*�_write_command_output_to_fileIs(
�rtr%cs,�jpt�jtjjtf�fdd�tjD��S)Nc3s&|]}t|tjj�r|��jVqdS)N)�
issubclassrZrepoZRepoEntitlementZ	repo_file)r5Zentitlement_clsr%r)r*rK^s�z#_get_state_files.<locals>.<genexpr>)	Zcfg_pathr�log_filerZua_file�pathrrZENTITLEMENT_CLASSESr%r)r%r*�_get_state_filesWs�
��rx)r�
output_dirc
Cs�tdd�|��td�tj�d�|��tdd�|��tdd�|��td	�d
�dd�tD���d
�|��tD]$}td�|�d�||�ddgd�qrt|dd�\}}t�d�|�t	j
|tjd��t�
�}t�d�|�t	�
|��t|�}t���rt��dt�nt��g}t|�D]r\}}	z0t�t�|	��}
t�tj�|d�|��|
�Wn6tk
�r�}zt�d|	t|��W5d}~XYnX�q|t�td�D]�}tj�|��r�zt�|�}
Wn@tk
�r}z t�d|t|��WY��q�W5d}~XYnXt�|
�}
t���r"t�||
�t�tj�|tj� |��|
��q�t!d�|��t"D]f}tj�|��rVzt#�$||�Wn@tk
�r�}z t�d|t|��WY��qVW5d}~XYnX�qVdS)zG
    Write all relevant Ubuntu Pro logs to the specified directory
    zcloud-idz{}/cloud-id.txtz	{} 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|krd�|��qS)z.servicez-u {})r2)r5�sr)r)r*r6�sz collect_logs.<locals>.<listcomp>z{}/pro-journal.txtzsystemctl status {}z	{}/{}.txtr�)rqFraz{}/pro-status.json)�clsz{}/environment_vars.jsonNz
user{}.logz&Failed to collect user log file: %s
%s�*zFailed to load file: %s
%sz{}/apparmor_logs.txtzFailed to copy file: %s
%s)%rtr2rZ
LIVEPATCH_CMDrm�UA_SERVICESrrri�json�dumpsrZDatetimeAwareJSONEncoderZget_pro_environmentrxZwe_are_currently_rootr4Zget_all_user_log_files�USER_LOG_COLLECTED_LIMITZget_user_log_file�	enumerateZredact_sensitive_logsZ	load_file�osrwr;rgrhr3�globr�isfile�basenamerpr�shutil�copy)
rryr1Z
pro_statusroZenv_varsZstate_filesZuser_log_filesZlog_file_idxrvZcontentrA�fr)r)r*�collect_logsfs��
���	���
�������

�r�)F)F)T)N)BrOr�r�Zloggingr�rkr��typingrrZuaclientrrrrrr	r
rrr4r
rrr(rrrZuaclient.defaultsrrrrZuaclient.filesrrZuaclient.files.state_filesrrrrrZget_event_loggerr:Z	getLoggerZreplace_top_level_logger_name�__name__rgrr�ZUAConfigrNr+ZEnableByDefaultService�boolrBr3rVZAutoAttachInstancerXr8rp�intrtrxr�r)r)r)r*�<module>s�(
���F��b���
�%���
�