Your IP : 216.73.216.14


Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/actions.cpython-38.pyc

U

8�-d� �@srddlZddlZddlZddlmZmZddlmZmZm	Z	m
Z
mZmZm
Z
ddlmZddlmZmZddlmZddlmZddlmZmZmZdd	lmZe�d
�ZdZejee dd�d
d�Z!d(ejejdd�dd�Z"dddd�ejee e e d�dd�Z#ddd�ejeee d�dd�Zd)eeee$dd�dd�Z%ejd�d d!�Z&ejed"�d#d$�Z'ejed%�d&d'�Z(dS)*�N)�List�Optional)�clouds�config�contract�entitlements�
exceptions�	livepatch�messages)�status)�system�util)�AutoAttachCloudInstance)�identity)�CLOUD_BUILD_INFO�DEFAULT_CONFIG_FILE�DEFAULT_LOG_PREFIX)�timer_jobs_state_filezpro.actions)zua-timer.servicezua-timer.timerzua-auto-attach.pathzua-auto-attach.servicezua-reboot-cmds.servicezubuntu-advantage.service)�cfg�token�allow_enable�returnc
Cs�ddlm}ztj|||d�Wnvtjk
r\}ztj|d�||�|�W5d}~XYn<tjk
r�}ztj|d�||�|�W5d}~XYnXt	�
�}|r�|�d|�||�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)r�rNzinstance-id)Zuaclient.jobs.update_messagingrrZrequest_updated_contractrZUrlError�	ua_statusr�UserFacingErrorrZget_instance_idZwrite_cache)rrrr�excZcurrent_iid�r�2/usr/lib/python3/dist-packages/uaclient/actions.py�attach_with_token's&
�
rT)r�cloudrcCs0t�|�}|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.
    )�instanceZ
contractToken)rrN)rZUAContractClientZ"request_auto_attach_contract_tokenr)rr rZcontract_clientZ
tokenResponserrrr�auto_attachIs
�r"F)�
assume_yes�
allow_beta�access_only)r�namer#r$r%cCs(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.
    )rr&)r#r$Zcalled_namer%)rZentitlement_factory�enable)rr&r#r$r%Zent_cls�entitlementrrr�enable_entitlement_by_name`s�r))�simulate_with_token�show_all)rr*r+cCs4|rtj|||d�\}}ntj||d�}d}||fS)z6
    Construct the current Pro status dictionary.
    )rrr+)rr+r)rZsimulate_statusr)rr*r+rZretrrrrys	�r)�filename�return_codesrc
Csfztj|��|d�\}}Wn:tjk
rT}zt�d�|�t|��W5d}~XYnXt�||�dS)zCHelper which runs a command and writes output or error to filename.)Zrcsz{}-errorN)rZsubp�splitrZProcessExecutionError�
write_file�format�str)�cmdr,r-�out�_�errr�_write_command_output_to_file�s
(r6rcCs0|jpt|j|j|jtjjtfdd�t	j
D��S)Ncss,|]$}t|tjj�r|jj|jd�VqdS))r&N)�
issubclassrZrepoZRepoEntitlementZrepo_list_file_tmplr0r&)�.0r(rrr�	<genexpr>�s�z#_get_state_files.<locals>.<genexpr>)Zcfg_pathrZlog_fileZtimer_log_fileZdaemon_log_filerZua_file�pathrrZENTITLEMENT_CLASSESrrrr�_get_state_files�s���r;)r�
output_dircCsTtdd�|��tdd�|��td�tj�d�|��tdd�|��td	�d
�dd�tD���d
�|��tD]$}td�|�d�||�ddgd�qrt|�}|t�td�D]�}t	j
�|�r�zt�
|�}Wn>tk
�r}zt�d|t|��WY�q�W5d}~XYnXt�|�}t���r0t�||�t�t	j
�|t	j
�|��|�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.txtzxjournalctl --boot=0 -o short-precise {} -u cloud-init-local.service -u cloud-init-config.service -u cloud-config.service� cSsg|]}d|krd�|��qS)z.servicez-u {})r0)r8�srrr�
<listcomp>�sz collect_logs.<locals>.<listcomp>z{}/journalctl.txtzsystemctl status {}z	{}/{}.txtr�)r-�*zFailed to load file: %s
%sN)r6r0r	Z
LIVEPATCH_CMD�join�UA_SERVICESr;�globr�osr:�isfilerZ	load_file�	Exception�loggingZwarningr1r
Zredact_sensitive_logsZwe_are_currently_rootr/�basename)rr<�serviceZstate_files�fZcontentr5rrr�collect_logs�s\��
�����
�
�

�rL)rrc
Cs�d}zt��}Wn�tjk
r�}zjt|tj�r@tjtjdd��t|tj	�rXt�tj
��t|tj�r~tjtj
j|jd�dd��t�tj��W5d}~XYnX|s�t�tj��|S)Nzauto-attach-cloud-type-error)Zmsg_code)�
cloud_typez(auto-attach-unsupported-cloud-type-error)rZcloud_instance_factoryrZCloudFactoryError�
isinstanceZCloudFactoryNoCloudErrorrr
ZUNABLE_TO_DETERMINE_CLOUD_TYPEZCloudFactoryNonViableCloudErrorZUNSUPPORTED_AUTO_ATTACHZ!CloudFactoryUnsupportedCloudErrorZNonAutoAttachImageErrorZ"UNSUPPORTED_AUTO_ATTACH_CLOUD_TYPEr0rM)rr!r5rrr�get_cloud_instance�s4�����rO)T)N))rDrHrE�typingrrZuaclientrrrrrr	r
rrrr
Zuaclient.cloudsrrZuaclient.defaultsrrrZuaclient.files.state_filesrZ	getLoggerZLOGrCZUAConfigr1�boolrr"r)�intr6r;rLrOrrrr�<module>sf$	
�%�������
�>�