U
    Jh                     @   s   d dl mZmZmZmZmZmZ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mZ d dlmZ e Ze
je
je
 e
ded	d
dZej e!e!edddZ"edej#ej$eej%eedej&j'ddddedej(j'dddddgddgdgdZ)dS )    )configdaemonentitlementsevent_logger
exceptionsmessagestimerutil)cli_util)ProArgumentProArgumentGroup
ProCommand)perform_disable)HelpCategory)machine_tokenstate_files)update_motd_messagesz
pro detach)returnc                K   s:   t || j| jdkd}|dkr.t  t  t  |S )zXPerform the detach action for this machine.

    @return: 0 on success, 1 otherwise
    json)
assume_yesjson_outputr   )	_detachr   formatr   startr   stopeventZprocess_events)argscfgkwargsret r    5/usr/lib/python3/dist-packages/uaclient/cli/detach.pyaction_detach   s    	  r"   )r   r   r   r   c           	   	   C   s   g }t | D ]P}zt j| |d}W n tjk
r>   Y qY nX |jdd\}}|r|| q|rtt	j
t| |D ]}td|j q~tj|dsdS |D ]}t|| ||dd qt| }|  t  t|  tt	j d	S )
aj  Detach the machine from the active Ubuntu Pro subscription,

    :param cfg: a ``config.UAConfig`` instance
    :param assume_yes: Assume a yes answer to any prompts requested.
         In this case, it means automatically disable any service during
         detach.
    :param json_output: output should be json only

    @return: 0 on success, 1 otherwise
    )r   nameT)Zignore_dependent_servicesz    {})r      F)r   r   Zupdate_statusr   )r   Zentitlements_disable_orderZentitlement_factoryr   ZEntitlementNotFoundErrorZcan_disableappendr   infor   ZDETACH_WILL_DISABLEZ	pluralizelenr   r#   r	   Zprompt_for_confirmationr   r   Zget_machine_token_filedeleter   Zdelete_state_filesr   ZDETACH_SUCCESS)	r   r   r   Z
to_disableZent_nameZentr   _Zmachine_token_filer    r    r!   r   (   s@    


r   detachz--assume-yes)Zcommand
store_true)helpactionz--formatZcli)defaultZstorer   )r,   r-   choicesr.   )Z	arguments)r,   Zdescriptionr-   Zhelp_categoryZargument_groupsN)*Zuaclientr   r   r   r   r   r   r   r	   Zuaclient.clir
   Zuaclient.cli.commandsr   r   r   Zuaclient.cli.disabler   Zuaclient.cli.parserr   Zuaclient.filesr   r   Zuaclient.timer.update_messagingr   Zget_event_loggerr   Zverify_json_format_argsZassert_rootZassert_attachedZassert_lock_fileintr"   ZUAConfigboolr   ZCLI_ROOT_DETACHZCLI_DETACH_DESCZOTHERZCLI_ASSUME_YESr   ZCLI_FORMAT_DESCZdetach_commandr    r    r    r!   <module>   sH   (
3