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: //proc/1233/root/usr/lib/python3/dist-packages/uaclient/cli/__pycache__/__init__.cpython-310.pyc
o

nHJec��
@sndZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZmZddlmZmZmZmZmZmZmZmZmZmZmZmZddlmZddlmZm Z ddlm!Z"dd	lm#Z#m$Z$m%Z%dd
l&m'Z'ddl(m)Z)m*Z*ddl+m,Z,dd
l-m.Z.m/Z/ddl0m1Z1m2Z2ddl3m4Z4ddl5m6Z6ddl7m8Z8m9Z9ddl:m;Z;m<Z<ddl=m>Z>ddl?m@Z@mAZAddlBmCZCddlDmEZEmFZFmGZGddlHmIZImJZJmKZKmLZLddlMmNZNmOZOddlPmQZQddlRmSZSddlTmUZUmVZVddlWmXZXmYZYdZZgd�Z[dZ\e�]�Z^e�_e$�`ea��ZbGd d!�d!ejc�Zdd�d"d#�Zed$d%�Zfd&d'�Zgd�d(d)�Zhd*d+�Zid,d-�Zjd.d/�Zkd0d1�Zld2emfd3d4�Znd2emfd5d6�Zod2emfd7d8�Zpd9d:�Zqd;d<�Zrd=d>�Zsd?d@�ZtdAdB�ZudCdD�ZvdEejwfdFdG�ZxdEejwfdHdI�ZydEejwfdJdK�ZzdLdM�Z{dNdO�Z|dPdQ�Z}dEejwdRemdSemfdTdU�Z~dVdW�dXdY�ZdZd[�Z�d\d]�Z�efd^d_��Z�efd`da��Z�dbdc�Z�egefehe��eedd�dedf�����Z�egefehe��eedg�dhdi�����Z�egefeh�eedj�dke�fdldm�����Z�dEejwdne�dke�fdodp�Z�dEejwdkdfdqdr�Z�dsdt�Z�efdEejwdke�fdudv��Z�dwdx�Z�eiefeedy�dzd{����Z�dEejwfd|d}�Z�dEejwfd~d�Z�dEejwfd�d��Z�d�d��Z�dEejwfd�d��Z�d�d�d��Z�dEejwfd�d��Z�eh�dEejwfd�d���Z�dEejwfd�d��Z�efeed��dEejwfd�d����Z�dEejwd�e8d�emd�eemdkdf
d�d��Z�d�d��Z�d�d�d�d��Z�d�d�d��Z�d�d�d��Z�d�d��Z�d�d��Z�e�d�d�d���Z�ead�k�r�e��e���dSdS)�z2Client to manage Ubuntu Pro services on a machine.�N��wraps)�List�Optional�Tuple)�actions�apt�apt_news�config�contract�daemon�defaults�entitlements�event_logger�
exceptions�http�lock)�log)�messages�security_status)�status)�timer�util�version)�call_api)�FullAutoAttachOptions�_full_auto_attach)�	_initiate)�MagicAttachRevokeOptions�_revoke)�MagicAttachWaitOptions�_wait)�_reboot_required)�_is_attached)�
AptProxyScope�setup_apt_proxy)�NAME�
USAGE_TMPL)�set_fix_parser)�AttachActionsConfigFile�IncorrectTypeError)�PRINT_WRAP_WIDTH)�*create_enable_entitlements_not_found_error�entitlements_disable_order�get_valid_entitlement_names)�ApplicationStatus�CanDisableFailure�CanEnableFailure�CanEnableFailureReason)�notices�state_files)�Notice)�JsonArrayFormatter)�refresh_motd�update_motd_messages)�	safe_dump�	safe_loadz$https://auth.contracts.canonical.com)�tabular�json�yamlzua_logs.tar.gzcsleZdZdddejdfdeef�fdd�
Zdd�Zd
�fdd	�	Z	e
d
eeeeeffdd��Z
�ZS)�UAArgumentParserN�	base_desccst�j||||d�||_dS)N)�prog�usage�epilog�formatter_class)�super�__init__r?)�selfr@rArBrCr?��	__class__��7/usr/lib/python3/dist-packages/uaclient/cli/__init__.pyrETs�
zUAArgumentParser.__init__cCs.|�tj�|dkr
tj}|�d|d�dS)Nz&the following arguments are required: ��
)�print_usage�sys�stderrr�CLI_TRY_HELP�exit)rF�messagerIrIrJ�erroreszUAArgumentParser.errorFcsd|jr)t��\}}t|�}|rt||�}d�|jg|�|_|jdtj7_t�j	|d�dS)NrLz

��file)
r?r>�_get_service_descriptions�sorted�join�descriptionr�PRO_HELP_SERVICE_INFOrD�
print_help)rFrU�show_all�non_beta_services_desc�beta_services_desc�service_descriptionsrGrIrJr[us��
�zUAArgumentParser.print_help�returnc
	Cs�t��}d}g}g}t�t���}|D]K}ztj||dd�}Wn
tjy*Yqw|�d|d�}|j	r=d�
|j	�}nd}tj|j
||j
|d�tdd	d	d
�}	|jrY|�|	�q|�|	�q||fS)Nz - {name}: {description}{url}�name��cfgra�presentedAsz ({})�)rarY�urlz   F)�width�subsequent_indent�break_long_words�break_on_hyphens)r
�UAConfigr�get_available_resourcesr�entitlement_factoryr�EntitlementNotFoundError�get�help_doc_url�format�textwrap�fillrYr+�is_beta�append)
rc�service_info_tmplr]r^�	resources�resource�ent_cls�presentation_namerf�service_inforIrIrJrV�s@
����z*UAArgumentParser._get_service_descriptions)NF)�__name__�
__module__�__qualname__�argparse�
HelpFormatterr�strrErSr[�staticmethodrrrV�
__classcell__rIrIrGrJr>Ss��(r>c��fdd�}|S)z1Decorator asserting exclusive access to lock filec�t����fdd��}|S)NcsFtj|�d���|d|i|��}Wd�|S1swY|S)N)rc�lock_holderrc)r�SingleAttemptLock)rc�args�kwargs�retval)�fr�rIrJ�new_f�s
��z0assert_lock_file.<locals>.wrapper.<locals>.new_fr�r�r��r��r�rJ�wrapper�sz!assert_lock_file.<locals>.wrapperrI)r�r�rIr�rJ�assert_lock_file�s	r�c�t���fdd��}|S)zDecorator asserting root usercst��st����|i|��S�N)r�we_are_currently_rootr�NonRootUserError)r�r�r�rIrJr��szassert_root.<locals>.new_frr�rIr�rJ�assert_root�sr�cr�)z>Decorator to verify if correct params are used for json formatcsH|s
�|g|�Ri|��S|jdkr|jst����|g|�Ri|��S)Nr<)rq�
assume_yesr�CLIJSONFormatRequireAssumeYes)�cmd_argsr�r�r�rIrJr��s
z&verify_json_format_args.<locals>.new_frr�rIr�rJ�verify_json_format_args�s	r�cr�)z�Decorator asserting attached config.
    :param msg_function: Optional function to generate a custom message
    if raising an UnattachedError
    cr�)NcsRt|�js�rt|dd�}t|dd�}�|||d�nt����|fd|i|��S)N�commandre�service)r��
service_namesrcrc)r#�is_attached�getattrr�UnattachedError)r�rcr�r�r�)r��raise_custom_error_functionrIrJr��s
�z/assert_attached.<locals>.wrapper.<locals>.new_frr��r�r�rJr��sz assert_attached.<locals>.wrapperrI)r�r�rIr�rJ�assert_attached�sr�cr�)z&Decorator asserting unattached config.cs6t|�jrtj|jj�dd�d���|fd|i|��S)Nrare)�account_namerc)r#r�r�AlreadyAttachedError�machine_token_file�accountro�r�rcr�r�rIrJr��s

�z"assert_not_attached.<locals>.new_frr�rIr�rJ�assert_not_attached�sr�cCsNd|_tj|_|jddtjd�|jddgdtjd�|jd	d
dtjd�|S)
z5Build or extend an arg parser for the api subcommand.�api�
endpoint_path�endpoint)�metavar�helpz--args�options�*)�dest�default�nargsr�z--data�datare)r�r�r�)r@r�CLI_API_DESCrY�add_argument�CLI_API_ENDPOINT�CLI_API_ARGS�CLI_API_DATA��parserrIrIrJ�
api_parsers ��
�r�cCs.d|_tj|_tjt|jd�|_tj|j	_
|S)z9Build or extend an arg parser for auto-attach subcommand.�auto-attach�rar�)r@r�CLI_AUTO_ATTACH_DESCrYr'rqr&rA�	CLI_FLAGS�
_optionals�titler�rIrIrJ�auto_attach_parsers

r�cCs6d|_tj|_tjt|jd�|_|jddtj	d�|S)z<Build or extend an arg parser for 'collect-logs' subcommand.�collect-logsr�z-oz--output�r�)
r@r�CLI_COLLECT_LOGS_DESCrYr'rqr&rAr��CLI_COLLECT_LOGS_OUTPUTr�rIrIrJ�collect_logs_parser!s�r��parent_commandcCs:tjtd�|�d�|_d|_tj|_|jddtj	d�|S)z;Build or extend an arg parser for 'config show' subcommand.z
{} show [key]r��show�key�?�r�r�)
r'rqr&rAr@r�CLI_CONFIG_SHOW_DESCrYr��CLI_CONFIG_SHOW_KEY�r�r�rIrIrJ�config_show_parser.s
��r�cCsRtjtd�|�d�|_d|_tj|_tj|j	_
|jdtjjd�
tj�d�d�|S)z:Build or extend an arg parser for 'config set' subcommand.z{} set <key>=<value>r��aset�key_value_pair�, �r�r�)r'rqr&rAr@r�CLI_CONFIG_SET_DESCrYr�r�r�r��CLI_CONFIG_SET_KEY_VALUErXr
�UA_CONFIGURABLE_KEYSr�rIrIrJ�config_set_parser=s
�

��r�cCsTtjtd�|�d�|_d|_tj|_|jdtj	jd�
tj�d�dd�tj
|j_|S)z<Build or extend an arg parser for 'config unset' subcommand.z{} unset <key>r��unsetr�r�r�)r�r�)r'rqr&rAr@r�CLI_CONFIG_UNSET_DESCrYr��CLI_CONFIG_UNSET_KEYrXr
r�r�r�r�r�rIrIrJ�config_unset_parserPs
�
��
	r�cCs�d}tjtd�|�d�|_||_tj|_tj|j	_
|jtjddd�}|j
dtjd�}|jtd	�t||d
�|j
dtjd�}|jtd	�t||d
�|j
dtjd�}|jtd	�t||d
�|S)
z4Build or extend an arg parser for config subcommand.r
z{} <command>r�r�re�r�r�r�r�r���action)r��setr�)r'rqr&rAr@r�CLI_CONFIG_DESCrYr�r�r��add_subparsers�CLI_AVAILABLE_COMMANDS�
add_parserr��set_defaults�action_config_showr�r��action_config_setr�r��action_config_unsetr�)r�r��
subparsers�parser_show�
parser_set�parser_unsetrIrIrJ�
config_parserds4
�
����r�cCs�tjtdd�|_tj|_d|_tj	|_
tj|j_
|jddtjd�|jddd	tjd
�|jdt�d�tjd
�|jddddgdtjjdd�d�|S)z4Build or extend an arg parser for attach subcommand.zattach <token>r��attach�tokenr�r�z--no-auto-enable�store_false�auto_enable)r�r�r�z--attach-config�r)�typer��--format�store�clir<�r��r��choicesr�r�)r'rqr&rAr�RawDescriptionHelpFormatterrCr@r�CLI_ATTACH_DESCrYr�r�r�r��CLI_ATTACH_TOKEN�CLI_ATTACH_NO_AUTO_ENABLE�FileType�CLI_ATTACH_ATTACH_CONFIG�CLI_FORMAT_DESCr�rIrIrJ�
attach_parser�s2
���rcCs�d|_tj|_tj|_|jdtjj	dd�ddd�|�
�}|jdtjdd	�|jd
tjdd	�|jdtj
dd	�|jdtjdd	�|S)
z=Build or extend an arg parser for security-status subcommand.�security-statusr��textr�)r<r=r)r�r�r�z--thirdparty�
store_true)r�r�z
--unavailablez--esm-infraz
--esm-apps)r@rr�rCr�CLI_SS_DESCrYr�rrq�add_mutually_exclusive_group�CLI_SS_THIRDPARTY�CLI_SS_UNAVAILABLE�CLI_SS_ESM_INFRA�CLI_SS_ESM_APPS)r��grouprIrIrJ�security_status_parser�s>�����r
cCsNd|_tjtdd�|_tj|j_t	j
|_tj|_
|jdgd�ddtjd�|S)	z5Build or extend an arg parser for refresh subcommand.�refreshz"refresh [contract|config|messages]r��target)rr
rr�N)r�r�r�r�)r@r'rqr&rArr�r�r�rr�rC�CLI_REFRESH_DESCrYr��CLI_REFRESH_TARGETr�rIrIrJ�refresh_parser�s�
�rcKs�|jdkr2|jrt��dS|jrt��dS|jr!t�|�dS|jr+t�	|�dSt�|�dS|jdkrGt
tjt�
|�dtjd��dSt
tt�
|�dd��dS)Nrr<T)�	sort_keys�clsF)�default_flow_styler)rq�
thirdpartyr�list_third_party_packages�unavailable�list_unavailable_packages�	esm_infra�list_esm_infra_packages�esm_apps�list_esm_apps_packages�printr<�dumps�security_status_dictr�DatetimeAwareJSONEncoderr9r�rIrIrJ�action_security_status�s>
��
�

�
������r"cCsjtjtdd�}||_d|_tj|_d|j_	|j
ddtjjdd�d�|j
dd	d
dgd
tjjd
d�d
�|S)z4Build or extend an arg parser for detach subcommand.�detachr��Flags�--assume-yesr�r��r�r�r�r�r�r<r�r�)
r'rqr&rAr@r�CLI_DETACH_DESCrYr�r�r��CLI_ASSUME_YESr�r�rArIrIrJ�
detach_parser�s$��r+rcc
Cs�tjtdd�}||_d|_tj|_tj|j	_
|jdddtjjd�
tj|d��d	�d
�|jddttdtjjtdd
�d�|jddtjd�|S)z2Build or extend an arg parser for help subcommand.zhelp [service]r�r�r�r�r�r��rcr��r�r�r�r�rr�r��--allrr')r'rqr&rAr@r�
CLI_HELP_DESCrY�CLI_ARGS�_positionalsr�r��CLI_HELP_SERVICErXr�valid_services�STATUS_FORMATSr�CLI_HELP_ALL�r�rcrArIrIrJ�help_parsers0
��	��r7c
Cs�tjtdd�}tj|_||_d|_tj|j	_
tj|j_
|j
dddtjjd�tj|d��d	�d
�|j
ddtjjdd
�d�|j
ddtjd�|j
ddtjd�|j
ddddgdtjjdd�d�|j
ddtjd�|S)z4Build or extend an arg parser for enable subcommand.zenable <service> [<service>]r��enabler�r��+r�r,r�r-r%rr&r'�
--access-onlyz--betar�r�r<r�r��	--variant)r'rqr&r�CLI_ENABLE_DESCrYrAr@r0r1r�r�r�r��CLI_ENABLE_SERVICErXrr3r)�CLI_ENABLE_ACCESS_ONLY�CLI_ENABLE_BETAr�CLI_ENABLE_VARIANTr6rIrIrJ�
enable_parser.sP�

��
�����rAc
Cs�tjtdd�}tj|_||_d|_tj|j	_
tj|j_
|j
dddtjjd�tj|d��d	�d
�|j
ddtjjdd
�d�|j
ddddgdtjjdd�d�|j
ddtjd�|S)z5Build or extend an arg parser for disable subcommand.zdisable <service> [<service>]r��disabler�r�r9r�r,r�r-r%rr&r'r�r�r<r�r��--purge)r'rqr&r�CLI_DISABLE_DESCrYrAr@r0r1r�r�r�r��CLI_DISABLE_SERVICErXrr3r)r�	CLI_PURGEr6rIrIrJ�disable_parser\sD�

��
���rGcCsbtjtdd�|_tj|_d|_tj|j	_
|jtjddd�}|j
dtjd�}|jtd	�t|�|S)
z4Build or extend an arg parser for system subcommand.zsystem <command>r��systemr�rer��reboot-requiredr�r�)r'rqr&rAr�CLI_SYSTEM_DESCrYr@r�r�r�r�r�r��CLI_SYSTEM_REBOOT_REQUIREDr��action_system_reboot_required�reboot_required_parser)r�r��parser_reboot_requiredrIrIrJ�
system_parser�s
��rOcCs*tjtdd�|_d|_tj|_tj	|_
|S)Nzsystem reboot-requiredr�rI)r'rqr&rA�prorr�rCr�CLI_SYSTEM_REBOOT_REQUIRED_DESCrYr�rIrIrJrM�s�rMcCs�tjtdd�}||_d|_tj|_tj	|_
|jdddtjd�|jddt
t
d	tjjt
d	d
�d�|jdd
dtjd�|jddtjd�d|j_|S)z4Build or extend an arg parser for status subcommand.rr�z--waitrF)r�r�r�r�r�rr�r�z--simulate-with-token�TOKEN)r�r�r�r.r'r$)r'rqr&rAr@rr�rCr�CLI_STATUS_DESCrYr��CLI_STATUS_WAITr4r�CLI_STATUS_SIMULATE_WITH_TOKEN�CLI_STATUS_ALLr�r�r*rIrIrJ�
status_parser�s:����rW�cmd_name�subcmd_namecCsdt|d�}|��dj|}|��dj��}||vr0|��dj|��tjdd�|�d��dS)Nr,r�	<command>r���argr�)�
get_parser�_get_positional_actionsr��keysr[r�InvalidArgChoicerX)rcrXrYr��	subparser�
valid_choicesrIrIrJ�_print_help_for_subcommand�s

��rcT)�
update_statuscCs�|j}|dur	|}|��\}}|s:t�|j�|dur9t|t�r9|jdur9t�|jj	�tj
|jj	|jj|jd�nt�|j�|rHtj
|d�|S)a=Perform the disable action on a named entitlement.

    :param entitlement_name: the name of the entitlement to enable
    :param cfg: the UAConfig to pass to the entitlement
    :param assume_yes:
        Assume a yes response for any prompts during service enable

    @return: True on success, False otherwise
    N��	error_msg�
error_coder�r,)�enabled_variantrB�event�service_failedra�
isinstancer0rR�info�msgrS�service_processed�	ua_statusr)�entitlementrcr�rd�variant�ret�reasonrIrIrJ�_perform_disable�s&
��rtcK�t|d|jd�dS)zGPerform the config action.

    :return: 0 on success, 1 otherwise
    r
�rXrYr�rcr�r�rIrIrJ�
action_config���rxcKs�|jr)|jtjvrtjd�|j�d�tj�d��tdj|jt||jd�d��dSt	t
dd	�tjD��d
�}d|d}tjD]}t|j|t||d�d��q@|jsV|jrc|j
s\|jrettj�dSdSdS)
z�Perform the 'config show' action optionally limit output to a single key

    :return: 0 on success
    :raise UbuntuProError: on invalid keys
    z'{}'r�r[z
{key} {value}N�r��valuercSsg|]}t|��qSrI)�len)�.0�xrIrIrJ�
<listcomp>sz&action_config_show.<locals>.<listcomp>�z{key: <z	} {value})r�r
r�rr`rqrXrr�r��max�global_apt_http_proxy�global_apt_https_proxy�ua_apt_http_proxy�ua_apt_https_proxyr�CLI_CONFIG_GLOBAL_XOR_UA_PROXY)r�rcr��	col_width�row_tmplr�rIrIrJr�s.

���
���r�cKsddlm}ddlm}t|d�}|��djd}|��djd}z
|j�d�\}}	Wnt	y@|�
�tjd|jd	��w|t
jvrU|�
�tjd
d�t
j�d��|d
vr�|�d�d}
|
dkrhtj}ntj}t�|
|	|�||	i}|d i|��tj�|�}|��\}
}|
tjkr�|d i|��n�||jvr�|�d�d}
|
dkr�tj}ntj}t�|
|	|�t|jp�|j �}|r�t!t"j#j$ddd��t%|t&j'||	�d|_d|_ n�||j(|j)v�r9d|vr�dnd}
|
dkr�tj}ntj}||j(v�r
t!t"j*j$d�$|
�d�$|
�d��d|}t�|
|	|�t|j+�p|j,�}|�r*t!t"j#j$ddd��t%|t&j-||	�d|_+d|_,nH|dv�rhzt.|	�}	|	dk�rOt	d�$|���Wn0t	�yg|�
�t!d�tj/||	d��w|dk�r�|	�0�dk}	|	�r|t1�2|�nt3j4�5�t6|||	�dS)!zMPerform the 'config set' action.

    @return: 0 on success, 1 otherwise
    r)�configure_livepatch_proxy)�configure_snap_proxyr,r
r��=z
<key>=<value>)�expected�actual�<key>r�r[��
http_proxy�https_proxy�_rrKzpro scoped aptz
global apt)�
current_proxy�previous_proxyN�https�apt_{}_proxy�global_apt_{}_proxy��old�new�global_)�update_messaging_timer�metering_timerzInvalid interval for {}rerzr	�truerI)7�uaclient.livepatchr��
uaclient.snapr�r]r^r�r��split�
ValueErrorr[r�GenericInvalidFormatr
r�r`rXr�PROXY_VALIDATION_SNAP_HTTP_URL�PROXY_VALIDATION_SNAP_HTTPS_URL�validate_proxyr�	livepatch�LivepatchEntitlement�application_statusr/�ENABLED�ua_scoped_proxy_options�PROXY_VALIDATION_APT_HTTP_URL�PROXY_VALIDATION_APT_HTTPS_URL�boolr�r�rr�WARNING_APT_PROXY_OVERWRITErq�configure_apt_proxyr$�UACLIENT�&deprecated_global_scoped_proxy_options�global_scoped_proxy_options�WARNING_CONFIG_FIELD_RENAMEr�r��GLOBAL�int�InvalidPosIntConfigValue�lowerr	�update_apt_newsr4�apt_news_contents_file�delete�setattr)r�rcr�r�r�r�r�ra�set_key�	set_value�
protocol_type�validate_urlrp�livepatch_statusr��
unset_currentrIrIrJr�!s�
��
�
�

���������

���

r�c
Kspddlm}ddlm}ddlm}|jtjvr>t	|d�}|�
�djd}|�
�djd}|��t
jdd	�tj�d
��|jdvrg|j�d�d}	||	d
�tj�|�}
|
��\}}|tjkrf||	d
�nH|j|jvrwt||j|jd�n8|j|j|jvr�|j|jvr�d|jvr�dnd}	t�tjj d� |	�d� |	�d��d|j|_t||j!|jd�t"||jd�dS)zOPerform the 'config unset' action.

    @return: 0 on success, 1 otherwise
    r)r$)�unconfigure_livepatch_proxy)�unconfigure_snap_proxyr,r
r�r�r�r[r�r�)r�Nr�rr�r�r�r�)#�uaclient.aptr$r�r�r�r�r�r
r�r]r^r�r[rr`rXr�rr�r�r�r/r�r�r�r�r�r�rirlrr�rqr�r�)
r�rcr�r$r�r�r�r�rar�rpr�r�rIrIrJr��sL
�



�����r�cCsbt||d�\}}|r|rtjd�|�|d�|�dd��|r&tjd�|�d��tj|d�|�dd��)z�Raises a custom error for enable/disable commands when unattached.

    Takes into consideration if the services exist or not, and notify the user
    accordingly.)�namesrcr�re)�
valid_service�	operation�invalid_service�service_msg)r��r�r�r�)r.r�UnattachedMixedServicesErrorrX�UnattachedValidServicesError�UnattachedInvalidServicesError)r�r�rc�entitlements_found�entitlements_not_foundrIrIrJ�&_raise_enable_disable_unattached_error�s&
����r�zpro disablec
Ks�|jr
|jr
tjddd��t|dg�}t||�\}}d}|D]}tj||d�}|||j|jd�}	|t|	||jd�M}q|rbd	d
�	tj
|dd��d}
d
�	tj|
dddd��}tj
dd
�	|�|d��t�|�}|��t��|rsdSdS)zbPerform the disable action on a list of entitlements.

    @return: 0 on success, 1 otherwise
    rCr%��option1�option2r�Trb)r��purge�r�zTry r��rc�
allow_beta�.rL�PF)rgrirjrBr�rr�)r�r�r�InvalidOptionCombinationr�r.rrmrtrXr3rr�wrap�InvalidServiceOpErrorr�UAContractClient�update_activity_tokenri�process_events)
r�rcr�r�r�r�rr�ent_namery�ent�valid_namesr��contract_clientrIrIrJ�action_disable�sJ	��������
r�z
pro enablecKs�t|dd�}|j}|r|rtjddd��t�tj�zt�	|�Wntj
tjfy<tj
ddd�tj
tjd	�Ynwt|d
g�}t||�\}}d}|D]�}	zctj||	|j|j|||�d�d�\}
}tj|d
�|
s�|dur�t|t�r�|jdur�t�|jj�tj|jj|jj|	d�|jtjkr�|� |	�n|
r�tj!|	d�n|
s�|dur�tj"|	d�||
M}WqNtjy�}zt�|j�tj|j|j#|	d�d}WYd}~qNd}~ww|r�t�$|�t%|||jd��t�&|�}
|
�'�t�(�|r�dSdS)z^Perform the enable action on a named entitlement.

    @return: 0 on success, 1 otherwise
    rqrer:r;r�zFailed to refresh contractT��exc_info)�warning_msgr��
extra_args)r�r��access_onlyrqr�r,Nre)r�Fr�rr�))r�r�rr�rirlr�REFRESH_CONTRACT_ENABLErr�UrlError�UbuntuProError�LOG�warning�E_REFRESH_CONTRACT_FAILUREr.r�enable_entitlement_by_namer��betarororrkr1rRrmrSrarsr2�IS_BETArurnrj�msg_code�services_failedr,r�r�r�)r�rcr�rqr�r�r�r�rrr��ent_retrs�er�rIrIrJ�
action_enables�	���
�	��
�
�
���
�
r�z
pro detachr`cKs2t||jd�}|dkrt��t��t��|S)zXPerform the detach action for this machine.

    @return: 0 on success, 1 otherwise
    r�r)�_detachr�r�startr�stoprir�)r�rcr�rrrIrIrJ�
action_detachms	r�r�c	Cs�g}t|�D]+}z	tj||d�}Wn
tjyYqw|||d�}|jdd�\}}|r1|�|�q|rMt�t	j
�t|���|D]}t�d�
|j��qAtj|d�sUdS|D]
}t|||dd	�qW|��|j��t|�t�t	j�d
S)a7Detach the machine from the active Ubuntu Pro subscription,

    :param cfg: a ``config.UAConfig`` instance
    :param assume_yes: Assume a yes answer to any prompts requested.
         In this case, it means automatically disable any service during
         detach.

    @return: 0 on success, 1 otherwise
    rb)rcr�T)�ignore_dependent_servicesz    {}r�r�F)r�rdr)r-rrmrrn�can_disablerurirlr�DETACH_WILL_DISABLE�	pluralizer|rqrar�prompt_for_confirmationrt�delete_cacher�r�r8�DETACH_SUCCESS)rcr��
to_disabler�ryr�rrr�rIrIrJr�~s2
�
�
r�cCs�d}|jr|j�di��di��d�}|r t�tjj|d��nt�tj�t�	�t�
|�t�|�\}}t
�|�}t�t�|��t��dS)N�machineTokenInfo�contractInfora)�
contract_name)�
machine_tokenrorirlr�ATTACH_SUCCESS_TMPLrq�ATTACH_SUCCESS_NO_CONTRACT_NAMErr��cleanuprrro�format_tabularr�handle_unicode_charactersr�)rcrr�_ret�outputrIrIrJ�_post_cli_attach�s"��

rcKsF|jr
|jr
t���t|j|j|j|�}t|���|jdkr!dSdS)N�successrr�)	r�r�r�CLIAPIOptionsXORDatarr�r�to_json�result�r�rcr�rrIrIrJ�
action_api�s
rcKsJztt�|tjjd�Wntjyt�t	j
j�YdSwt|�dS)N)rc�moder�r)
rrr�EventLoggerMode�CLIrr�rirlr�E_ATTACH_FAILURErmrr�rIrIrJ�action_auto_attach�s
��rc
Ks�|jdkr
tjd|jd��t�tj�t|d�}t�dtjj|j	d��t
|jd�}zt||d�}Wn!tj
yT}zt�tj�t|jd�}t||d�|�d}~wwt�dtj�|jS)	Nr<r�)�paramr{r,rL)�	user_code)�magic_token)r�rc)rqr�MagicAttachInvalidParamrirlr�CLI_MAGIC_ATTACH_INITr�CLI_MAGIC_ATTACH_SIGN_INrr r�r!�MagicAttachTokenError�CLI_MAGIC_ATTACH_FAILEDrr�CLI_MAGIC_ATTACH_PROCESSING�contract_token)r�rcr��
initiate_resp�wait_options�	wait_respr��revoke_optionsrIrIrJ�
_magic_attach�s8
�
������	r*z
pro attachc
Ks�|jr
|jr
t���|js|jst||d�}d}n0|jr"|j}d}n'z
t�t|j��}WntyB}z
tj	|jj
|jd��d}~ww|j}|j}|j
oO|du}z
tj|||d�Wntjyft���wd}|dur�|j
r�t||�\}	}
|	D]8}tj||ddd�\}}
|s�d}|
dur�t|
t�r�|
jdur�t�|
jj�tj|
jj|
jj
|d�qyt�|�qy|
r�t|
|dd	�}tj|jtjd
�tj|j|jd�d}t� |�}|�!�t"|�|S)Nr,)�config_namerS)r��allow_enablerT)r�r�r�rer���	file_type�rfrg)#r��
attach_configr�CLIAttachTokenArgXORConfigr*r)�	from_dictr:r*�AttachInvalidConfigFileErrorrarm�enable_servicesr�r�attach_with_tokenr��AttachErrorr.r�rkr1rRrirlrSrnr,rNrOr�rr�r�r)r�rcr�r��enable_services_overrider0r�r,rr�found�	not_foundrar�rsrSr�rIrIrJ�
action_attach�sx�
�����
��
���
r:cKs�|jpt}t���M}t�||�zt�|d��}|j|dd�Wd�n1s)wYWnt	yM}zt
�|�WYd}~Wd�dSd}~wwWd�dS1sYwYdS)Nzw:gzzlogs/)�arcnamer�r)r�UA_COLLECT_LOGS_FILE�tempfile�TemporaryDirectoryr�collect_logs�tarfile�open�add�PermissionErrorr�rS)r�rcr��output_file�
output_dir�resultsr�rIrIrJ�action_collect_logsCs&

��

���
��rGcCs�t}tttjtjtdd�tjjtdd�|d�}|j	ddtj
d�|j	ddt��tj
jtd	�d
�tj|j_|jtjddd
�}d|_|jdtjd�}t|�|jtd�|jdtjd�}t|�|jtd�|jdtjd�}t|�|jtd�|jdtjd�}t |�|jt!d�|jdtj"d�}t#|�|jt$d�|jdtj%d�}	t&|	�|	jt'd�|jdtj(d�}
t)|
|d�|
jt*d�|jdtj+d�}t,||d�|jt-d�t.|�|jdtj/d�}t0|�|jt1d�|jdtj2d�}
t3|
|d�|
jt4d�|jdtj5d�}|jt6d�t7|�|jdtj8d�}|jt9d�t:|�|jdtj
jtd	�d�}|jt;d�|jdtj<d�}|jt=d�t>|�|S)NrZr�)r@rCrArBr?z--debugrr'z	--versionr)ra)r�rr�r�rer�Tr�r�r�r�r�r�r
r#rBr,r8rr�rrrH)?�__doc__r>r&rr�r'rqr�CLI_HELP_EPILOGr��CLI_ROOT_DEBUGr�get_version�CLI_ROOT_VERSIONr�r�r�r�r��requiredr��CLI_ROOT_ATTACHrr�r:�CLI_ROOT_APIr�r�CLI_ROOT_AUTO_ATTACHr�r�CLI_ROOT_COLLECT_LOGSr�rG�CLI_ROOT_CONFIGr�rx�CLI_ROOT_DETACHr+r��CLI_ROOT_DISABLErGr��CLI_ROOT_ENABLErAr�r(�CLI_ROOT_SECURITY_STATUSr
r"�
CLI_ROOT_HELPr7�action_help�CLI_ROOT_REFRESH�action_refreshr�CLI_ROOT_STATUS�
action_statusrW�
print_version�CLI_ROOT_SYSTEM�
action_systemrO)rcr?r�r��
parser_attach�
parser_api�parser_auto_attach�parser_collect_logs�
parser_config�
parser_detach�parser_disable�
parser_enable�parser_security_status�parser_help�parser_refresh�
parser_status�parser_version�
parser_systemrIrIrJr]Ps����
�������������r]c
Ks�|st��}|r|jnd}|r|jnd}tjjj}tj	|||d�\}}t
|d|k�}|rY|jrY|rY|d|krTtj
ddd�t�d�tj	|||d�\}}|d|ks8t�
d�t�|�tj||d�}	t�
t�|	��t��|S)	NF)�simulate_with_tokenr\�execution_statusr�re)�endr��r\)r
rk�allrnro�UserFacingConfigStatus�ACTIVEr{rrr��waitrirl�time�sleep�set_output_contentrrr
r�)
r�rcr�r\r��active_valuerrr�
config_activerrIrIrJr\�s2

�

��

r\cKru)zGPerform the system action.

    :return: 0 on success, 1 otherwise
    rHrvrrwr�rIrIrJr_�ryr_cKst|�}t�|j�dS)Nr)r"rirl�reboot_requiredrrIrIrJrL�srLcKstt���dSr�)rrrK)�_argsrcr�rIrIrJr]�sr]c
CsFz|��Wnty}z	t�|�t���d}~wwttj�dSr�)	�process_config�RuntimeErrorr��	exceptionr�RefreshConfigFailurerr�REFRESH_CONFIG_SUCCESS)r�rc�excrIrIrJ�_action_refresh_config�s
��r�c
CsJzt�|�Wntjy}z	t�|�t���d}~wwttj	�dSr�)
rrrr�r�r�RefreshContractFailurerr�REFRESH_CONTRACT_SUCCESS�r|rcr�rIrIrJ�_action_refresh_contract�s
��r�c
Cs\zt|�t�|jrt�|�Wnty&}z	t�|�t���d}~wwt	t
j�dSr�)r8r7r	r��	Exceptionr�rr�RefreshMessagesFailurerr�REFRESH_MESSAGES_SUCCESSr�rIrIrJ�_action_refresh_messagess
�
��r�zpro refreshcKsj|jdus
|jdkrt||�|jdus|jdkr$t||�t�tj�|jdus.|jdkr3t||�dS)Nr
rrr)rr�r�r3�remover5�CONTRACT_REFRESH_WARNINGr�r�rIrIrJrZs


rZ�scoper�r�cCsR|tjkr|j}|j}n|tjkr|j}|j}d|vr|}n|}t|||d�dS)zS
    Handles setting part the apt proxies - global and uaclient scoped proxies
    r�)r�r��proxy_scopeN)r$r�r�r�r�r�r�r%)rcr�r�r�r�r�rIrIrJr� s
	

�r�cKs�|j}|j}|st|d�j|d�dS|st��}t�||�}|jdkr-t	t
�|��dS|��D]\}}t	d�|�
�|��q1dS)Nr,rqrr<z{}:
{}
)r�rrr]r[r
rkror�rqrr<r�itemsr�)r�rcr�r�r\�
help_responser�r{rIrIrJrX8s
�rXcCs\|r|jdkst|dd�dvrdSt��}|r,t�d|�tjtj	j
|d�tjd�dSdS)Nr�rqre�r<r=zNew version available: %s)rr-)
r�r�r�check_for_new_versionr�r�rirlr�WARN_NEW_VERSION_AVAILABLE_CLIrqrNrO)r��new_versionrIrIrJ�_warn_about_new_versionNs�
�
��r�cCs^|jdvr+tj��s-t|d�r|jdvrdSt�d�tj	t
jj|jd�tjd�dSdSdS)z4Warn users that the user readable output may change.)rrrqr�Nz.Not in a tty and human-readable command calledr&r-)
r�rN�stdout�isatty�hasattrrqr�r�rirlr�WARNING_HUMAN_READABLE_OUTPUTrO�r�rIrIrJ�_warn_about_output_redirectionds
�
�
��r�cCs�|durt��}|j}t��st��}t|t�r|�	�}|s#t
�d�}|�|�|�
t���g|_t�|�}|��sI|jjddd�|jdd�t
�|�}|�t��|�|�|�d�|�|�dS)z�Setup console logging and debug logging to log_file

    It configures the pro client logger.
    If run as non_root and cfg.log_file is provided, it is replaced
    with another non-root log file.
    N�	ubuntuproT)�parents�exist_oki�)rz	upro-file)r
rk�log_filerr��pro_log�get_user_log_filerkr��upper�logging�	getLogger�setLevel�	addFilter�RedactionFilter�handlers�pathlib�Path�exists�parent�mkdir�touch�FileHandler�setFormatterr6�set_name�
addHandler)�	log_levelr��loggerrc�
log_file_path�file_handlerrIrIrJ�
setup_loggingus*






r�cCs`|jdvr*t�|j�t|d�r,|jdkrt�tjj�|jdkr.t�tjj	�dSdSdSdS)z3Set the right event mode based on the args provided)r�r#r8rBrrqr<r=N)
r�ri�set_commandr�rq�set_event_moderr�JSON�YAMLr�rIrIrJr��s



�r�cr�)Nc
s�z�|i|��WSty(t�d�ttjtjd�t�	�t�
d�YdStjy�}z`dt
|�vr[tj}t�d�rAtj}|j|jd�}tj|j|jd�tj|jtjd�ntjd	|j|d
�tj}tj|j|jd�tj|jtjd�t�	�t��t�t�
d�WYd}~dSd}~wtjy�}z;tj}t�d�r�tj}|j|jd�}tj|j|jd�tj|jtjd�t�	�t��t�t�
d�WYd}~dSd}~wtj�y$}z<t�|j�tj|j|j|jd�tjd�|j�tjd�t |tj!��st�	�t��t�t�
|j"�WYd}~dSd}~wt#�yc}z3t�d
�t�	�tjtj$jtjd�tjt%|dt
|��dd�t��t�t�
d�WYd}~dSd}~ww)N�KeyboardInterruptrTr��CERTIFICATE_VERIFY_FAILEDzca-certificates)rfr/)�info_msgr.zFailed to access URL: %sr�)rfrg�additional_infoz{}z&Unhandled exception, please file a bugrmr)rf�
error_type)&r�r�rSrr�CLI_INTERRUPT_RECEIVEDrNrOr�clear_lock_file_if_presentrQrr�r��&SSL_VERIFICATION_ERROR_CA_CERTIFICATESr�is_installed�%SSL_VERIFICATION_ERROR_OPENSSL_CONFIGrqrfrirmrarlr�E_CONNECTIVITY_ERRORr�r��PycurlCACertificatesErrorr�r�r�rk�
LockHeldError�	exit_coder��UNEXPECTED_ERRORr�)r�r�r��tmplrmr���funcrIrJr��s�

��
���

����z#main_error_handler.<locals>.wrapperrI)r�r�rIr�rJ�main_error_handler�sLr�c
Csfttjdtjd�t��}t|j|j�|stj}t	|d�}|dd�}|s6|�
�ttj
�t�d�d|vrN|�d�}|d|�}||dd�}n|}g}|j|d�}|jrot�tj�}|�tj�t�d��|�t|�|j}	|j}
tj|	|
d�t�d	|�|��d
d�t t!�"��#��D�}|r�t�d|�t$|�|j%|||d
�}t&|�|S)Nr�r�r,r�z--)r�r�r�zExecuted with sys.argv: %rcSsg|]
\}}d�||��qS)z{}={})rq)r}�k�vrIrIrJr!s
��zmain.<locals>.<listcomp>z'Executed with environment variables: %r)rcr�)'r�r
�CONFIG_DEFAULTSr
rkr�r�rN�argvr]rMrrrPrQ�index�
parse_args�debugr��
StreamHandlerrOr��DEBUGr�r�r�r�r�r�configure_web_proxyr��warn_about_invalid_keysrWr�get_pro_environmentr�r�r�r�)
�sys_argvrcr��
cli_arguments�double_dash_index�pro_cli_argsr�r��console_handlerr�r��pro_environment�return_valuerIrIrJ�main�sP�



�r��__main__r�)NN)r`N)�rHrr<r�r�rNr@r=rrrv�	functoolsr�typingrrr�uaclientrrr	r
rrr
rrrrrrr�rrrrorrr�uaclient.api.apir�2uaclient.api.u.pro.attach.auto.full_auto_attach.v1rr�+uaclient.api.u.pro.attach.magic.initiate.v1r�)uaclient.api.u.pro.attach.magic.revoke.v1rr�'uaclient.api.u.pro.attach.magic.wait.v1r r!�5uaclient.api.u.pro.security.status.reboot_required.v1r"�(uaclient.api.u.pro.status.is_attached.v1r#r�r$r%�uaclient.cli.constantsr&r'�uaclient.cli.fixr(�uaclient.data_typesr)r*�uaclient.defaultsr+�uaclient.entitlementsr,r-r.�(uaclient.entitlements.entitlement_statusr/r0r1r2�uaclient.filesr3r4�uaclient.files.noticesr5�uaclient.logr6�uaclient.timer.update_messagingr7r8�
uaclient.yamlr9r:�UA_AUTH_TOKEN_URLr4r<�get_event_loggerrir��replace_top_level_logger_namer|r��ArgumentParserr>r�r�r�r�r�r�r�r�r�r�r�r�r�rr
rr"r+rkr7rArGrOrMrWrcrtrxr�r�r�r�r�r�r�r�r�r�rrrr*r:rGr]r\r_rLr]r�r�r�rZr�rXr�r�r�r�r�r�rQrIrIrIrJ�<module>s8
^
	
 &.(#��
�
&
w
.1Q
)	!E
k
		����
�

%P
;�