o
    K&b,                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZmZmZ d dlmZmZmZ e jG dd de jZe jG dd de jZe jG d	d
 d
e jZe jG dd de jZe jG dd de jZe jG dd de jZe jG dd de jZG dd dZe jG dd de jZG dd dZdZdZdZ ej!j"ej#ej!j" ej$ ej%j"ej&ej%j" ej$ ej'j"ej(ej'j" ej$ ej)j"ej(ej)j" ej$ ej*j"ej#ej*j" ej$ ej+j"ej(ej+j" ej$ eej#e ej$ eej#e ej$ e ej#e  ej$ i	Z,dZ-dZ.dZ/de- Z0d e- Z1d!e- Z2d"e- Z3d#4eZ5d$Z6d%4eZ7d&Z8d'Z9d(Z:d)Z;d*e<d+e<fd,d-Z=d.eee<  d+e<fd/d0Z>	d:d1ee	e<e<f  d2ee< d+ee< fd3d4Z?d5ee<ef d+e<fd6d7Z@d5ee<ef d+ee<ef fd8d9ZAdS );    N)AnyDictListOptionalTuple)BASE_UA_URLCONFIG_FIELD_ENVVAR_ALLOWLISTPRINT_WRAP_WIDTH)
UNATTACHEDNamedMessageTxtColorc                   @      e Zd ZdZe Ze ZdS )ApplicationStatuszO
    An enum to represent the current application status of an entitlement
    N)__name__
__module____qualname____doc__objectZENABLEDZDISABLED r   r   1/usr/lib/python3/dist-packages/uaclient/status.pyr          
r   c                   @      e Zd ZdZdZdZdS )ContractStatusz
    An enum to represent whether a user is entitled to an entitlement

    (The value of each member is the string that will be used in status
    output.)
    yesnoN)r   r   r   r   ENTITLED
UNENTITLEDr   r   r   r   r      s    r   c                   @   r   )ApplicabilityStatuszQ
    An enum to represent whether an entitlement could apply to this machine
    N)r   r   r   r   r   Z
APPLICABLEINAPPLICABLEr   r   r   r   r   &   r   r   c                   @   r   )UserFacingAvailabilitya  
    An enum representing whether a service could be available for a machine.

    'Availability' means whether a service is available to machines with this
    architecture, series and kernel. Whether a contract is entitled to use
    the specific service is determined by the contract level.

    This enum should only be used in display code, it should not be used in
    business logic.
    r   r   N)r   r   r   r   	AVAILABLEUNAVAILABLEr   r   r   r   r   0   s    r   c                   @   s   e Zd ZdZdZdZdZdS )UserFacingConfigStatusz
    An enum representing the user-visible config status of UA system.

    This enum will be used in display code and will be written to status.json
    ZinactiveZactivezreboot-requiredN)r   r   r   r   INACTIVEACTIVEZREBOOTREQUIREDr   r   r   r   r"   A   s
    r"   c                   @   s    e Zd ZdZdZdZdZdZdS )UserFacingStatusz
    An enum representing the states we will display in status output.

    This enum should only be used in display code, it should not be used in
    business logic.
    enabledZdisabledzn/au   —N)r   r   r   r   r$   r#   r   r!   r   r   r   r   r%   N   s    r%   c                   @   s4   e Zd ZdZe Ze Ze Ze Ze Z	e Z
dS )CanEnableFailureReasonzK
    An enum representing the reasons an entitlement can't be enabled.
    N)r   r   r   r   r   ZNOT_ENTITLEDZALREADY_ENABLEDr   ZIS_BETAZINCOMPATIBLE_SERVICEZINACTIVE_REQUIRED_SERVICESr   r   r   r   r'   ]   s    
r'   c                   @   *   e Zd Z	ddedee ddfddZdS )CanEnableFailureNreasonmessagereturnc                 C      || _ || _d S Nr*   r+   selfr*   r+   r   r   r   __init__l      
zCanEnableFailure.__init__r.   )r   r   r   r'   r   r   r2   r   r   r   r   r)   k       r)   c                   @   s"   e Zd ZdZe Ze Ze ZdS )CanDisableFailureReasonzL
    An enum representing the reasons an entitlement can't be disabled.
    N)r   r   r   r   r   ZALREADY_DISABLEDZACTIVE_DEPENDENT_SERVICESZNOT_FOUND_DEPENDENT_SERVICEr   r   r   r   r5   u   s
    
r5   c                   @   r(   )CanDisableFailureNr*   r+   r,   c                 C   r-   r.   r/   r0   r   r   r   r2      r3   zCanDisableFailure.__init__r.   )r   r   r   r5   r   r   r2   r   r   r   r   r6      r4   r6   Z	essentialZstandardZadvancedzAre you sure? (y/N) zsFIPS kernel is running in a disabled state.
  To manually remove fips kernel: https://discourse.ubuntu.com/t/20738
zWarning: FIPS kernel is not optimized for your specific cloud.
To fix it, run the following commands:

    1. sudo ua disable fips
    2. sudo apt-get remove ubuntu-fips
    3. sudo ua enable fips --assume-yes
    4. sudo reboot
zThis will install the FIPS packages. The Livepatch service will be unavailable.
Warning: This action can take some time and cannot be undone.
z~This will install the FIPS packages including security updates.
Warning: This action can take some time and cannot be undone.
a  Warning: Enabling {title} in a container.
         This will install the FIPS packages but not the kernel.
         This container must run on a host with {title} enabled to be
         compliant.
Warning: This action can take some time and cannot be undone.
zTThis will disable the FIPS entitlement but the FIPS packages will remain installed.
z1Enter your token (from {}) to attach this system:z=Enter your new token to renew UA subscription on this system:zOpen a browser to: {}/subscribez({name: <14}{available: <11}{description}zJ{name: <14}{available: <11}{entitled: <11}{auto_enabled: <14}{description}z-SERVICE       ENTITLED  STATUS    DESCRIPTIONz4{name: <14}{entitled: <19}{status: <19}{description}stringr,   c                 C   s   t j rt| | S | S )z=Return colorized string if using a tty, else original string.)sysstdoutisattySTATUS_COLORget)r7   r   r   r   colorize   s   r=   commandsc                 C   st   d}| D ]}|r|d7 }|d |7 }qd tj|td dd}d|v r*d	}d
}nd}d}djtj|||tjdS )N z &&  z \
   z  )widthsubsequent_indent
z{
  z
}z{ z }z%{color}{prefix}{content}{suffix}{end})Zcolorprefixcontentsuffixend)jointextwrapwrapr	   formatr   DISABLEGREYENDC)r>   rF   cmdZwrapped_contentrE   rG   r   r   r   colorize_commands   s,   
rP   column_dataheaderc                    sl   dg}|r
| | tdd | D }|dkr*d| | fdd| D  |S |dd | D  |S )zReturn a list of content lines to print to console for a section

    Content lines will be center-aligned based on max value length of first
    column.
    r?   c                 S   s   g | ]}t |d  qS )r   )len.0Zpairr   r   r   
<listcomp>  s    z.get_section_column_content.<locals>.<listcomp>r   z{{:>{}}}: {{}}c                    s   g | ]} j | qS r   )rL   rT   templater   r   rV      s    c                 S   s   g | ]}|d  qS )   r   rT   r   r   r   rV   #  s    )appendmaxrL   extend)rQ   rR   rF   Ztemplate_lengthr   rW   r   get_section_column_content  s   

r]   statusc                 C   s  |  dsT|  dr,tjddddddg}| d	 D ]}|tjd#i | qd
|S tjddddg}| d	 D ]}|tjd#i | q9|dtjg d
|S t	g}| d	 D ]-}|d }| d}|rj|n|d }|d t
|t
|d |d}|tjd#i | q[| d d }|  dr|t|  dpg dd |d g }	| d d }
|
r|	d|
f | d d }|r|	d|f | d dkr|	dt| d f |	d t
|f |	r|t|	d! d
|S )$z&Format status dict for tabular output.Zattached	simulatedZSERVICEr    r   ZAUTO_ENABLEDZDESCRIPTION)name	availableentitledZauto_enableddescriptionservicesrD   )r`   ra   rc   r?   rb   Zdescription_overriderc   r`   r^   )r`   rb   r^   rc   Zcontracttech_support_levelZnoticesZNOTICES)rR   z*
Enable services with: ua enable <service>ZaccountZAccountZSubscriptionoriginfreezValid untilZexpireszTechnical support level)rQ   Nr   )r<   STATUS_SIMULATED_TMPLrL   rZ   rI   STATUS_UNATTACHED_TMPLr\   r
   msgSTATUS_HEADERr=   STATUS_TMPLr]   str)r^   rF   serviceZservice_statusrb   Zdescr_overriderc   Zfmt_argsre   ZpairsZaccount_nameZcontract_namer   r   r   format_tabular'  sp   

	






ro   c                 C   sT   dd t tj D | d< | ds"dd | dg D }|| d< | dd | S )	Nc                 S   s8   g | ]\}}|  tv s|d s|dkr||dqS )ZUA_FEATURESZUA_CONFIG_FILE)r`   value)lowerr   
startswith)rU   r`   rp   r   r   r   rV   p  s    
z2format_machine_readable_output.<locals>.<listcomp>Zenvironment_varsr_   c                 S   s    g | ]}| d ddkr|qS )ra   r   )r<   )rU   rn   r   r   r   rV   y  s    rd   rf   r?   )sortedosenvironitemsr<   pop)r^   Zavailable_servicesr   r   r   format_machine_readable_outputo  s   


rx   r.   )Benumrt   r8   rJ   typingr   r   r   r   r   Zuaclient.defaultsr   r   r	   Zuaclient.messagesr
   r   r   uniqueEnumr   r   r   r   r"   r%   r'   r)   r5   r6   Z	ESSENTIALZSTANDARDZADVANCEDr$   rp   ZOKGREENrN   r#   ZFAILr   rM   r!   r   r   r;   ZPROMPT_YES_NOZNOTICE_FIPS_MANUAL_DISABLE_URLZ&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDZPROMPT_FIPS_PRE_ENABLEZPROMPT_FIPS_UPDATES_PRE_ENABLEZ PROMPT_FIPS_CONTAINER_PRE_ENABLEZPROMPT_FIPS_PRE_DISABLErL   ZPROMPT_ENTER_TOKENZPROMPT_EXPIRED_ENTER_TOKENZPROMPT_UA_SUBSCRIPTION_URLri   rh   rk   rl   rm   r=   rP   r]   ro   rx   r   r   r   r   <module>   s    		




&H