o
    i6                  
   @   sV  U d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ ed
dgZddgedgee dZeed< eeZdZdZdZde fddZ!de fddZ"d-defddZ#d-ddZ$defdd Z%de d!e&fd"d#Z'de fd$d%Z(de fd&d'Z)d(e*d)eded*ed!df
d+d,Z+dS ).z1ubuntu_pro: Configure Ubuntu Pro support services    N)AnyList)urlparse)performancesubputil)Cloud)Config)
MetaSchema)PER_INSTANCEzubuntu-advantageubuntu_advantagecc_ubuntu_proubuntu
ubuntu_pro)iddistros	frequencyactivate_by_schema_keysmetaREDACTEDzUUnable to determine if this is an Ubuntu Pro instance. Fallback to normal Pro attach.)
http_proxyhttps_proxyglobal_apt_http_proxyglobal_apt_https_proxyua_apt_http_proxyua_apt_https_proxypro_sectionc                 C   s   d| vrd S | d }t |ts dt|j }t| t|d|vr&d S |d }t |ts@dt|j }t| t|d S )Nfeaturesz.'ubuntu_pro.features' should be a dict, not a disable_auto_attachzB'ubuntu_pro.features.disable_auto_attach' should be a bool, not a )
isinstancedicttype__name__LOGerrorRuntimeErrorbool)r   r   msgr    r(   @/usr/lib/python3/dist-packages/cloudinit/config/cc_ubuntu_pro.pyvalidate_schema_features*   s*   



r*   
pro_configc              
   C   s   g }t |  D ]:\}}|tvrtd| q|du rqzt|}|jdvr.|d|  W q tt	fyB   |d|  Y qw |rOt	d
d|dS )a  Validate user-provided ua:config option values.

    This function supplements flexible jsonschema validation with specific
    value checks to aid in triage of invalid user-provided configuration.

    Note: It does not log/raise config values as they could be urls containing
    sensitive auth info.

    @param pro_config: Dictionary of config value under 'ubuntu_pro'.

    @raises: ValueError describing invalid values provided.
    z4Not validating unknown ubuntu_pro.config.%s propertyN)httphttpsz-Expected URL scheme http/https for ua:config:zExpected a URL for ua:config:z$Invalid ubuntu_pro configuration:
{}
)sorteditemsKNOWN_PRO_CONFIG_PROPSr#   warningr   schemeappendAttributeError
ValueErrorformatjoin)r+   errorskeyvalue
parsed_urlr(   r(   r)   supplemental_schema_validationE   s2   
r=   c                 C   s  | d u rd S t | tstdt| j dt|  g }t|  D ]\}}d }i }|d u r;t	d| ddd|g}n4| d}t	d| t
d	|rW| d
t
| }n| d
| }ddd|g}d|d d |g i}ztj|fi | W q" tjy } zt|}	|d ur|	|t}	|||	f W Y d }~q"d }~ww |r|D ]\}
}td|
| qtdddd |D d S )Nz+ubuntu_pro: config should be a dict, not a z%; skipping enabling config parameterszDisabling Pro config for %sproconfigunsetz	=REDACTEDzEnabling Pro config %sz\s=set	logstringz#Failure enabling/disabling "%s":
%sz3Failure enabling/disabling Ubuntu Pro config(s): {}, c                 s   s    | ]
\}}d  |V  qdS )z"{}"N)r7   ).0param_r(   r(   r)   	<genexpr>   s    z!set_pro_config.<locals>.<genexpr>)r   r    r%   r!   r"   r=   r/   r0   r#   debugresearchescaper   ProcessExecutionErrorstrreplacer   r4   r2   r7   r8   )r+   enable_errorsr:   r;   redacted_key_valuesubp_kwargs
config_cmd	key_valueeerr_msgrG   r$   r(   r(   r)   set_pro_configl   sR   

rX   c              
   C   sH  |du rg }nt |trtd |g}nt |ts%tdt|j g }|r.ddd| g}ndd| g}|dd tg }tdd		| zt
j
|d
dh|d W n% t
jyv } zt|| t}d| }tt| t||d}~ww |s{dS g d| }zt
j
|dd
dhd\}}	W n t
jy } z
tdd	| |d}~ww zt|}
W n tjy } ztd| |d}~ww g }|
dg D ]}|d dkrtd|d  q|| q|r"g }|D ].}|d}|dur|| d| d|d  }nd|d  d|d  }tt| qtdd	| dS ) z=Call ua command line client to attach and/or enable services.NzNubuntu_pro: enable should be a list, not a string; treating as a single enablezIubuntu_pro: enable should be a list, not a %s; skipping enabling servicesr>   attachz--no-auto-enablerD   zAttaching to Ubuntu Pro. %s r      )rcsrC   zFailure attaching Ubuntu Pro:
)r>   enablez--assume-yesz--formatjsonT   )capturer\   z!Error while enabling service(s): rE   zPro response was not json: r9   message_codezservice-already-enabledzService `%s` already enabled.servicezFailure enabling `z`: messagezFailure of type `r!   z(Failure enabling Ubuntu Pro service(s): )r   rO   r#   r2   listr!   r"   r   rJ   r8   r   rN   rP   r   logexcr%   r^   loadsJSONDecodeErrorgetr4   )tokenr]   
attach_cmdredacted_cmdrV   err'   cmdenable_stdoutrH   enable_resprQ   errerror_servicesrb   r(   r(   r)   configure_pro   s   







rr   cloudc                 C   sl   t drdS z| j  W n ty   ttd  w z
| jdg W dS  ty5   ttd  w )z.Install ubuntu-advantage-tools if not present.r>   NzPackage update failedzubuntu-advantage-toolsz(Failed to install ubuntu-advantage-tools)	r   whichdistroupdate_package_sources	Exceptionr   re   r#   install_packages)rs   r(   r(   r)   maybe_install_ua_tools   s   
ry   returnc              
   C   s   t | di dd}|rdS ddlm} ddlm} z!tjddd	 | }W d    W |jS 1 s5w   Y  W |jS  |y[ } zt	d
| t
t W Y d }~dS d }~ww )Nr   r   Fr   )UserFacingError)should_auto_attachz6Checking if the instance can be attached to Ubuntu Proalwayslog_modez%Error during `should_auto_attach`: %s)r&   rh   uaclient.api.exceptionsr{   4uaclient.api.u.pro.attach.auto.should_auto_attach.v1r|   r   Timedr#   rJ   r2   ERROR_MSG_SHOULD_AUTO_ATTACH)r   r   r{   r|   resultexr(   r(   r)   _should_auto_attach  s.   		
r   c                 C   sZ   |  d}|sd}t| t||  d}|r"tdd| t||  dd d S )Nri   z8`ubuntu_pro.token` required in non-Pro Ubuntu instances.enable_betaz<Ignoring `ubuntu_pro.enable_beta` services in Pro attach: %srE   r]   )ri   r]   )rh   r#   r$   r%   rJ   r8   rr   )r   ri   r'   r   r(   r(   r)   _attach(  s   


r   c           
   
   C   s   ddl m}m} ddlm}m} | d}| d}|||d}z!tjddd	 ||d
 W d    W d S 1 s:w   Y  W d S  |y[   |d usP|d urXt	
d Y d S Y d S  |yv } zd|j }	t	|	 t|	|d }~ww )Nr   )AlreadyAttachedErrorr{   )FullAutoAttachOptionsfull_auto_attachr]   r   )r]   r   zAttaching to Ubuntu Pror}   r~   )optionszThe instance is already attached to Pro. Leaving enabled services untouched. Ignoring config directives ubuntu_pro: enable and enable_betaz!Error during `full_auto_attach`: )r   r   r{   2uaclient.api.u.pro.attach.auto.full_auto_attach.v1r   r   rh   r   r   r#   r2   r'   r$   r%   )
r   r   r{   r   r   r]   r   r   r   r'   r(   r(   r)   _auto_attach7  s0   

&


r   namecfgargsc                 C   s2  d }t t|}|r)t|dkrtdd| tdd| ||d  }d|v r;|r7td|d  |d }|d u rGtd|  d S t	|t
s]d	t|j }t| t|d
|v rld}t| t|t| t|d t| td t|rt| d S | dhkst| d S d S )Nr_   zLUnable to configure Ubuntu Pro. Multiple deprecated config keys provided: %srE   z^Deprecated configuration key(s) provided: %s. Expected "ubuntu_pro"; will attempt to continue.r   r   z;Ignoring deprecated key %s and preferring ubuntu_pro configz=Skipping module named %s, no 'ubuntu_pro' configuration foundz%'ubuntu_pro' should be a dict, not a commandszPDeprecated configuration "ubuntu-advantage: commands" provided. Expected "token"r?   zETo discover more log info, please check /var/log/ubuntu-advantage.logr   )rd   DEPRECATED_KEYSintersectionlenr%   r8   r#   r2   rJ   r   r    r!   r"   r$   ry   rX   rh   r*   r   r   keysr   )r   r   rs   r   r   
deprecatedr'   r(   r(   r)   handleY  sd   


r   )N),__doc__r^   loggingrK   typingr   r   urllib.parser   	cloudinitr   r   r   cloudinit.cloudr   cloudinit.configr	   cloudinit.config.schemar
   cloudinit.settingsr   rB   r   rd   r   __annotations__	getLoggerr"   r#   r   r   r1   r    r*   r=   rX   rr   ry   r&   r   r   r   rO   r   r(   r(   r(   r)   <module>   s>   

'
,d""