
    UٵgD                     ,   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mZmZmZmZmZmZmZ d dlmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d dlm Z  d d	l!m"Z"m#Z#m$Z$ d d
l%m&Z&  ejN                   ejP                  e)            Z* edde+fde+fdeee,ef      fg      Z-	 d=de
j\                  de+dee	j^                     fdZ0de
j\                  de,dee   dee   de,de,de,fdZ1de+dee,ef   dee,   dee,   deee,ef      deee,ef      de+fd Z2de
j\                  d!e,de,de+d"e+de+d#eee,      dee   dee   d$e-fd%Z3e jh                  e jj                   e jl                  e jn                        d$e8fd&                     Z9 e$d'ejt                  ejv                  e9e&jx                  d( e# e"dejz                  j}                  d)j                   ej                   e
j\                         *            +      d,d-.       e"d/ej                  j}                  d'0      d12       e"d3ej                  d12       e"d4ej                  d12       e"d5ej                  j}                  d67      d,d6d8gd69       e"d:ej                  d,2      g;      g<      ZFy)>    N)AnyDictList
NamedTupleOptional)
apiconfigcontractentitlementsevent_logger
exceptionslockmessagesstatusutil)ServiceWithDependencies_dependencies)EnableOptionsEnableResult_enable)EnabledService_enabled_services)_is_attached)cli_util)ProArgumentProArgumentGroup
ProCommand)HelpCategory_EnableOneServiceResultsuccessneeds_rebooterrorcfgaccess_onlyprogress_objectc                    t        j                  |      }t        j                  | d||      }d}d}	 t	        j
                  d      5  |j                  |      \  }}ddd       |sB||j                  |j                  }	nt        j                  }	t        j                  d|	      t        dgg dg 	      S # 1 sw Y   ]xY w# t        $ r}t	        j                          |d}~ww xY w)
a  
    Landscape gets special treatment because it currently not supported by our
    enable API. This function is a temporary workaround until we have a proper
    API for enabling landscape, which will happen after Landscape is fully
    integrated with the contracts backend.
    	landscape)called_namer$   
extra_argsFNzcli.enable._enable_landscape)lock_holder)progress)servicereason)enableddisabledreboot_requiredr   )r   ProgressWrapperr   LandscapeEntitlementr   	RetryLockenable	Exceptionclear_lock_file_if_presentmessager   GENERIC_UNKNOWN_ISSUEr   EntitlementNotEnabledErrorr   )
r#   r$   r)   r%   r+   r'   r    fail_reasoner-   s
             5/usr/lib/python3/dist-packages/uaclient/cli/enable.py_enable_landscaper=   /   s    ""?3H11	I GK^^6
 	G $-#3#3X#3#F G[	G "{':':'F ((F33F33
 	
 EB !	G 	G  '')s/   C 
B< C <CC 	C,C''C,r,   all_dependenciesenabled_servicesr(   variantservice_titlec                    g }g }|D 	cg c]  }	|	j                    }
}	t        fd|D        d       }|b|j                  D 	cg c]  }	|	j                   |
v r|	j                    }}	|j                  D 	cg c]  }	|	j                   |
vr|	j                    }}	|D ]  }t	        j
                  | j                  d      }t        j                  | |      }t        j                  j                  ||      }|st	        j                  |      rtt        j                  ||       |D ]f  }t        j                  | |      }t        j                  j                  ||      }t	        j                  |      rQt        j                   ||       t        fd|D        d       }|t        j                  |       }t        j                  | |j"                        }t	        j
                  | j                  d      }t        j                  j                  ||      }|st	        j                  |      st        j                  ||      y y y c c}	w c c}	w c c}	w )Nc              3   B   K   | ]  }|j                   k(  s|  y wN)name).0sr,   s     r<   	<genexpr>z1prompt_for_dependency_handling.<locals>.<genexpr>i   s     :q'(9:s   z features.block_disable_on_enable)r	   path_to_value)service_being_enabledincompatible_service)msg)rJ   required_servicec              3   v   K   | ]0  }|j                   k(  r|j                  r|j                  k7  r| 2 y wrD   rE   variant_enabledvariant_name)rF   rG   r,   r@   s     r<   rH   z1prompt_for_dependency_handling.<locals>.<genexpr>   s:      	
vv !!')	 	
s   69)rE   nextincompatible_with
depends_onr   is_config_value_truer#   r   	get_titler   INCOMPATIBLE_SERVICEformatprompt_for_confirmationr   IncompatibleServiceStopsEnableREQUIRED_SERVICERequiredServiceStopsEnablerQ   )r#   r,   r>   r?   r(   r@   rA   incompatible_servicesrequired_servicesrG   enabled_service_namesdependenciesrK   cfg_block_disable_on_enableincompatible_service_titleuser_msgrM   required_service_titlerP   to_be_enabled_titleenabled_variant_titles    `   `               r<   prompt_for_dependency_handlingrg   [   s    -=>QVV>>:$:DL  "33!
vv.. FF!
 !
 ",,
vv22 FF
 
 !6 &*&?&?77<'
# &2%;%;%&
" 0077"/!; 8 
 'd.J.J/
 ;;&3%? ( . 
!-!7!7=M!N,,33"/3 4 
 ++977&3!7 
 	
%	
 		O "w':*44S'7K , 6 6/66!
 '+&?&?77<'
# 0077"5!6 8 
 'd.J.J/
 ;;&9%: /
 (;"w ?!


s   I!I6!I"json_outputjson_responseprocessed_servicesfailed_serviceserrorswarningsc                     | rm|j                          |j                          |rdnd|d<   ||d<   ||d<   ||d<   ||d<   t        t        j                  |t        j
                  d	             y y )
Nr    failureresultrj   rk   rl   rm   T)cls	sort_keys)sortprintjsondumpsr   DatetimeAwareJSONEncoder)rh   ri   rj   rk   rl   rm   r    s          r<   _print_json_outputrx      s     !/6)Ih.@*++:'("(h$,j!JJ11	
     ent_name
assume_yesr)   returnc	                    t        j                  |      }	t        j                  | |||      }
|
j                  |
j
                  }t        fd|D        d       }|t        j                  j                  |      } |	|j                          |	t        j                  j                  |             t        ddd||j                  |j                  d      S |s	 t        | ||||       	 |rd }nt        j$                  |	      }d
k(  rt'        | |||      }nt)        t+        ||      | |      }t-        j,                  |        |r' |	t        j.                  j                  |             n& |	t        j0                  j                  |             |j2                  }|r& |	t        j4                  j                  d             |j                  D ]
  } |	|        t        d|d       S # t        j                  $ re}t         j#                  |        |	|j                          |	t        j                  j                  |             t        ddd       cY d }~S d }~ww xY w# t        j6                  $ rj}|j8                  d   }|d   }|d   }|d   } |	|        |	t        j                  j                  |             t        ddd||||d      cY d }~S d }~wt        j                  $ rt} |	|j                          |	t        j                  j                  |             t        ddd||j                  |j:                  |j8                  d      cY d }~S d }~ww xY w)N)r@   r$   r)   c              3   z   K   | ]2  }|j                   k(  r!r|j                  r|j                  k(  r| 4 y wrD   rO   )rF   rG   	real_namer@   s     r<   rH   z&_enable_one_service.<locals>.<genexpr>   s<      	
vv"%%!..G*C 	
s   8;)titleFr,   )typer,   r7   message_code)r    r!   r"   )r(   r@   rA   )r{   r'   )r)   r%   )r,   r@   r$   )r%   r#   install)	operationTr-   coder   additional_infor   r,   r7   r   r   )r   &create_interactive_only_print_functionr   entitlement_factoryrE   r   rR   r   ALREADY_ENABLEDrX   rL   ENABLE_FAILEDr   rg   r   UbuntuProErrorLOG	exceptionCLIEnableDisableProgressr=   r   r   r   ACCESS_ENABLED_TMPLENABLED_TMPLr0   ENABLE_REBOOT_REQUIRED_TMPLr9   r   msg_code)r#   rz   r@   r$   r{   rh   r)   r?   r>   interactive_only_printent	ent_titlealready_enabledrL   r;   r+   enable_resultr!   r7   r-   err_codeerr_msgerr_infor   s     `                    @r<   _enable_one_servicer      s    &LL 
*
*C I		I	
%	
 	O "&&--I->sww'x55<<9<MN&!#77 #		
 		
 	*  $'*S
H88JOH#-% (	M $$# +
  (M 	#",,33)3D #%%,,9,= %44"44;;' <  %-- 	,G"7+	, '%
 	
u (( 
	MM!"155)"&&--I-> +" 
	@ 00 
""8,&>/+,w'x55<<9<MN&!#" (#+

 
	
 $$ 
quu%x55<<9<MN&!#55 !

#$#4#4

 
	

sR   $H 7D	I< I9AI4.I94I9<M>AK4.M>4M>
A)M93M>9M>c                   g }g }g }g }t         j                  dd}| j                  dk(  }t        j                  |      }	t        | dd      }
| j                  }| j                  }|
r|rt        j                  dd       |	t        j                         	 t        j                  |       t-        |      j.                  sbt        j0                         } |	|j(                         |j%                  d|j(                  |j2                  d       t5        ||||||d       yt        | dg       }t7        j8                  ||      \  }}t;        |      j<                  }t?        |      j@                  }d
}t7        jB                  ||      D ]  }tE        |||
||||jG                  d      ||	      }|jH                  r$|j%                  |       |jJ                  sOd
|d<   Ud}|j%                  |       |jL                  u|j%                  |jL                          |red}||z  }t7        jN                  ||      } |	|j(                         |j%                  dd|j(                  |j2                  |jP                  d       t        jR                  |      }|jU                          t5        |||||||       |rdS dS # t        j                  t        j                  f$ r` t         j#                  d	d
       |j%                  dt        j&                  j(                  t        j&                  j*                  d       Y w xY w)z^Perform the enable action on a named entitlement.

    @return: 0 on success, 1 otherwise
    F)_schema_versionr!   ru   r@    --access-only	--variant)option1option2zFailed to refresh contractT)exc_infosystem)r   r7   r   )r       r,   r)   r!   Nr   r   r   )+r   JSON_SCHEMA_VERSIONrX   r   r   getattrr$   r{   r   InvalidOptionCombinationr   REFRESH_CONTRACT_ENABLEr
   refreshConnectivityErrorr   r   warningappendE_REFRESH_CONTRACT_FAILURErL   rE   r   is_attached_and_contract_validContractExpiredErrorr   rx   r   get_valid_entitlement_namesr   r?   r   servicesorder_entitlements_for_enablingr   getr    r!   r"   *create_enable_entitlements_not_found_errorr   UAContractClientupdate_activity_token)argsr#   kwargsrj   rk   rl   rm   ri   rh   r   r@   r$   r{   expired_errnamesentitlements_foundentitlements_not_foundr?   r>   retrz   rp   errcontract_clients                           r<   action_enabler   u  s9    OFH (;;M
 ++'K
 &LL dIr*G""KJ;11#[
 	
 8;;<
 ;; 557{/ &?? + 4 4	
 		
 D)R(E 	00<(->>$S)22
C @@ , %JJ|$

 >>%%h/""04n-C""8,||'fll+-,0 11EE"
 	sww' 77 ##&#6#6	
 //4O))+ 1 ((**C*CD 	
04@ #>>BB ( C C H H	
	
s   K A?MMr4      z, r   )optionsstore+)helpactionnargsz--assume-yes)command
store_true)r   r   r   z--betaz--formatcli)defaultru   )r   r   choicesr   r   )	arguments)r   descriptionr   help_categoryhelp_positionargument_groupsrD   )Gru   loggingtypingr   r   r   r   r   uaclientr   r	   r
   r   r   r   r   r   r   r   +uaclient.api.u.pro.services.dependencies.v1r   r   %uaclient.api.u.pro.services.enable.v1r   r   r   -uaclient.api.u.pro.status.enabled_services.v1r   r   (uaclient.api.u.pro.status.is_attached.v1r   uaclient.clir   uaclient.cli.commandsr   r   r   uaclient.cli.parserr   	getLoggerreplace_top_level_logger_name__name__r   boolstrr   UAConfigAbstractProgressr=   rg   rx   r   verify_json_format_argsassert_rootassert_attached&_raise_enable_disable_unattached_errorintr   CLI_ROOT_ENABLECLI_ENABLE_DESC
QUICKSTARTCLI_ENABLE_SERVICErX   joinvalid_servicesCLI_ASSUME_YESCLI_ENABLE_ACCESS_ONLYCLI_ENABLE_BETACLI_FORMAT_DESCCLI_ENABLE_VARIANTenable_command ry   r<   <module>r      s     8 8   
 B ! K K ,g:::8DE$	D		(4S>*+  7;	)	)) c223	)XY	YY 23Y >*	Y
 Y Y Yx

S>
 S	
 #Y	

 c3h 
 4S>"
 
8_
	_
_
 _
 	_

 _
 _
 c#_
 >*_
 23_
 _
D 
!!	(IIJCS C K  "CL 		!	!(())!44;; $		7L77OFOO<MN! < 
 #	 "!00777I'
 #!88'
 !11'
 !11888G""FO! !44"C&(	
*2ry   