o
    x[h“6  ã                
   @   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|
|¡ q§t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Ú	logstringéÿÿÿÿz#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rÝt d|d ¡ qÌ| |¡ qÌ|�r"g }|D ].}| d¡}|du�r| |¡ d|› d|d › �}nd|d › d|d › �}t t|¡ qêt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~   )ÚoptionszŠThe 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|ƒrŠt|ƒ d S | ¡ dhks—t|ƒ 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""