
    x[h6              
          % S r SSKrSSKrSSKrSSKJrJr  SSKJr  SSK	J
r
JrJr  SSKJr  SSKJr  SSKJr  SS	KJr  \" S
S/5      rSS/\S/\" \5      -   S.r\\S'   \R4                  " \5      rSrSrSrS\ 4S jr!S\ 4S jr"S$S\4S jjr#S$S jr$S\4S jr%S\ S\&4S jr'S\ 4S jr(S\ 4S jr)S \*S!\S\S"\SS4
S# jr+g)%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                 h   SU ;  a  g U S   n[        U[        5      (       d8  S[        U5      R                   3n[        R                  U5        [        U5      eSU;  a  g US   n[        U[        5      (       d8  S[        U5      R                   3n[        R                  U5        [        U5      eg )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    s       @/usr/lib/python3/dist-packages/cloudinit/config/cc_ubuntu_pro.pyvalidate_schema_featuresr+   *   s    $ :&Hh%%X''(* 	 			#3 H,"#89)400/099:< 	 			#3 1    
pro_configc                    / n[        U R                  5       5       H^  u  p#U[        ;  a  [        R	                  SU5        M'  Uc  M,   [        U5      nUR                  S;  a  UR                  SU 35        M^  M`     U(       a)  [        SR                  SR                  U5      5      5      eg! [        [        4 a    UR                  SU 35         M  f = f)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_urls        r*   supplemental_schema_validationr@   E   s     FZ--/0
,,KKF ]	A!%J  (99CC5I : 1& 3::499V;LM
 	
  
+ 	AMM9#?@	As   	/B//$CCc                    U c  g [        U [        5      (       d"  [        S[        U 5      R                   S35      e[        U 5        / n[        U R                  5       5       H  u  p#S n0 nUc  [        R                  SU5        SSSU/nOkU S3n[        R                  SU5        [        R                  " S	U5      (       a  U S
[        R                  " U5       3nOU S
U 3nSSSU/nSUS S U/-   0n [        R                  " U40 UD6  M     U(       aQ  U H  u  p[        R'                  SX5        M     [        SR)                  SR+                  S U 5       5      5      5      eg ! [        R                   aB  n[        U5      n	Ub  U	R!                  U["        5      n	UR%                  X)45         S nAGMZ  S nAff = f)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              3   H   #    U  H  u  pS R                  U5      v   M     g7f)z"{}"N)r:   ).0param_s      r*   	<genexpr>!set_pro_config.<locals>.<genexpr>   s     M}85&--..}s    ")r!   r"   r'   r#   r$   r@   r2   r3   r%   debugresearchescaper   ProcessExecutionErrorstrreplacer   r7   r5   r:   r;   )r-   enable_errorsr=   r>   redacted_key_valuesubp_kwargs
config_cmd	key_valueeerr_msgrL   r&   s               r*   set_pro_configr^   l   s   j$''z"++, -33
 	

 #:.MZ--/0
!=II3S97C8J$'5	!2II.0BCyy&&"e1RYYu%5$67	"e1UG,	5)<J&
3B;M:N(NOK	1IIj0K0! 1, )LEKK>M *AHH		M}MM
 	
  )) 	1!fG!-!//%:  #00		1s   7E++G?6F<<Gc                 t   Uc  / nOn[        U[        5      (       a  [        R                  S5        U/nO@[        U[        5      (       d+  [        R                  S[        U5      R                  5        / nU(       a  SSSU /nOSSU /nUSS [        /-   n[        R                  SS	R                  U5      5         [        R                  " US
S1US9  U(       d  g/ SQU-   n [        R                  " USS
S1S9u  p [         R"                  " U5      n
/ nU
R'                  S/ 5       H8  nUS   S:X  a  [        R                  SUS   5        M'  UR)                  U5        M:     U(       a  / nU H]  nUR'                  S5      nUb  UR)                  U5        SU SUS    3nOSUS    SUS    3n[        R                  " [        U5        M_     [        SSR                  U5      -   5      eg! [        R                   aP  n[        U5      R                  U [        5      nSU 3n[        R                  " [        U5        [        U5      UeSnAff = f! [        R                   a#  n[        SSR                  U5      -   5      UeSnAff = f! [         R$                   a  n[        SU 35      UeSnAff = f) 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 servicesrB   attachz--no-auto-enablerH   zAttaching to Ubuntu Pro. %s r      )rcsrG   zFailure attaching Ubuntu Pro:
)rB   enablez--assume-yesz--formatjsonT   )capturerc   z!Error while enabling service(s): rI   zPro response was not json: r<   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!   rU   r%   r5   listr#   r$   r   rP   r;   r   rT   rV   r   logexcr'   re   loadsJSONDecodeErrorgetr7   )tokenrd   
attach_cmdredacted_cmdr\   err)   cmdenable_stdoutrM   enable_resprW   errerror_servicesri   s                  r*   configure_prory      s   ~	FC	 	 5	
 %%0L!!	

  X'95A
Xu-
cr?hZ/LII+SXXl-CD'		*1a&LA 
?&
HC99S$QFCjj/< !#Mx,~";;II5s9~FS!	 - $& Cggi(G"%%g.*7)3s9~6FG)#f+c#i.9IJKKS! ! 6ii'(
 	
 o %% 'V^^E8,/t4C3Q&	' %% /$))F2CC
	  )-9
	sI   ;G. #I >J .IAIIJ)JJJ7#J22J7cloudc                 T   [         R                  " S5      (       a  g U R                  R                  5          U R                  R                  S/5        g! [         a    [
        R                  " [        S5        e f = f! [         a    [
        R                  " [        S5        e f = f)z.Install ubuntu-advantage-tools if not present.rB   NzPackage update failedzubuntu-advantage-toolsz(Failed to install ubuntu-advantage-tools)	r   whichdistroupdate_package_sources	Exceptionr   rl   r%   install_packages)rz   s    r*   maybe_install_ua_toolsr      s    zz%++-%%'?&@A	  C01
  CCDs   A B  'A= 'B'returnc                    [        U R                  S0 5      R                  SS5      5      nU(       a  gSSKJn  SSKJn   [        R                  " SSS	9   U" 5       nS S S 5        UR
                  $ ! , (       d  f       WR
                  $ = f! U a9  n[        R                  S
U5        [        R                  [        5         S nAgS nAff = f)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(   ro   uaclient.api.exceptionsr   4uaclient.api.u.pro.attach.auto.should_auto_attach.v1r   r   Timedr%   rP   r5   ERROR_MSG_SHOULD_AUTO_ATTACH)r   r    r   r   resultexs         r*   _should_auto_attachr     s    
B'++,A5I  8	D
 ()F	
 $$$
 
 $$$	  		92>01s6   B A2B 2
B<B B C/CCc                 "   U R                  S5      nU(       d"  Sn[        R                  U5        [        U5      eU R                  S5      nU(       a%  [        R	                  SSR                  U5      5        [        XR                  S5      S9  g )Nrp   z8`ubuntu_pro.token` required in non-Pro Ubuntu instances.enable_betaz<Ignoring `ubuntu_pro.enable_beta` services in Pro attach: %srI   rd   )rp   rd   )ro   r%   r&   r'   rP   r;   ry   )r   rp   r)   r   s       r*   _attachr   (  sm    OOG$EH		#3//-0K		JIIk"	
 ooh&?@r,   c                    SSK JnJn  SSKJnJn  U R                  S5      nU R                  S5      nU" UUS9n [        R                  " SSS	9   U" US
9  S S S 5        g ! , (       d  f       g = f! U a     Uc  Ub  [        R                  S5         g  g U a5  nSUR                   3n	[        R                  U	5        [        U	5      UeS nAff = f)Nr   )AlreadyAttachedErrorr   )FullAutoAttachOptionsfull_auto_attachrd   r   )rd   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   ro   r   r   r%   r5   r)   r&   r'   )
r   r   r   r   r   rd   r   r   r   r)   s
             r*   _auto_attachr   7  s     N __X&F//-0K#G(88LW- MLL "f&8KK6 '9  (1"&&:		#3R'(s;   A1 A A1  
A.*A1 .A1 1!CC0CCnamecfgargsc                 z   S n[        [        R                  U5      5      nU(       aX  [        U5      S:  a  [	        SSR                  U5      -  5      e[        R                  SSR                  U5      5        XS      nSU;   a%  U(       a  [        R                  SUS   5        US   nUc  [        R                  SU 5        g [        U[        5      (       d8  S	[        U5      R                   3n[        R                  U5        [	        U5      eS
U;   a"  Sn[        R                  U5        [	        U5      e[        U5        [        UR!                  S5      5        [#        U5        [        R                  S5        [%        U5      (       a  ['        U5        g UR)                  5       S1::  d  [+        U5        g g )Nrf   zLUnable to configure Ubuntu Pro. Multiple deprecated config keys provided: %srI   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"rC   zETo discover more log info, please check /var/log/ubuntu-advantage.logr   )rk   DEPRECATED_KEYSintersectionlenr'   r;   r%   r5   rP   r!   r"   r#   r$   r&   r   r^   ro   r+   r   r   keysr   )r   r   rz   r   r   
deprecatedr)   s          r*   handler   Y  s   Ko22378Jz?Q%'+yy'<=  	@IIj!	

 Q-(sKKM1 ,'		K	
 	T**[!**+- 	 			#3[   	 			#35!;??8,- [)IIO ;''[! J</ 0r,   )N),__doc__re   loggingrQ   typingr   r   urllib.parser   	cloudinitr   r   r   cloudinit.cloudr	   cloudinit.configr
   cloudinit.config.schemar   cloudinit.settingsr   rF   r   rk   r   __annotations__	getLoggerr$   r%   r   r   r4   r"   r+   r@   r^   ry   r   r(   r   r   r   rU   r    r,   r*   <module>r      s$   8   	  ! - - ! # . +)+=>? z ,~_0EE	j  !&   $  6$
t $
N)
s )
Xa
H%  %T %d %8A A(d (DB B6 B% Bt B Br,   