
    gO                         d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ dd	lmZ d
Z G d dej"                        Zy)znetplan try command line    N   )ConfigManager   )utils   )NetplanApply)terminalx   c                   j     e Zd Z fdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Z xZS )
NetplanTryc                 *   t         |   ddd       d| _        d | _        d | _        d | _        d | _        d | _        t        j                  j                  dd      | _        t        j                  j                  | j                  dd	d
      | _        y )NtryzLTry to apply a new netplan config to running system, with automatic rollbackT)
command_iddescriptionleafFDBUS_TEST_NETPLAN_ROOT/runnetplanznetplan-try.ready)super__init__configuration_changednew_interfacesconfig_file_config_manager
t_settingstosenvironget_rootdirpathjoin_netplan_try_stamp)self	__class__s    :/usr/share/netplan/netplan_cli/cli/commands/try_command.pyr   zNetplanTry.__init__)   s    E&G" 	 	$ &+""#

'?E"$'',,t}}eYPc"d    c                 h    | j                   st        | j                        | _         | j                   S )N)prefix)r   r   r!   r%   s    r'   config_managerzNetplanTry.config_manager7   s(    ###0#FD ###r(   c                     t         j                  j                  | j                        r t        j                  | j                         yy)NTF)r   r"   isfiler$   remover+   s    r'   clear_ready_stampzNetplanTry.clear_ready_stamp=   s0    77>>$112IId--.r(   c                     t        j                  | j                  dz   dd       t        | j                  d      j                          y )Nz/run/netplani  T)modeexist_okw)r   makedirsr!   openr$   closer+   s    r'   touch_ready_stampzNetplanTry.touch_ready_stampC   s4    
DMMN2NT$$c*002r(   c                 (   | j                   j                  dd       | j                   j                  dt        t        d       | j                   j                  dd       | j                  | _        | j                          | j                          y )	Nz--config-filezGApply the config file in argument in addition to current configuration.)helpz	--timeoutz=Maximum number of seconds to wait for the user's confirmation)typedefaultr:   z--statez0Directory containing previous YAML configuration)parseradd_argumentintDEFAULT_INPUT_TIMEOUTcommand_tryfunc
parse_argsrun_commandr+   s    r'   r   zNetplanTry.runG   s      &o 	! 	q  &)3H&e 	! 	g 	  &X 	! 	Z $$	r(   c                 @   | j                         s#t        j                  t        j                         	 t        j
                  j                         }t        j                  |      | _	        | j                  j                  | j                         t        j                  t        j                  | j                         t        j                  t        j                  | j                         | j!                          | j#                          t%               j'                  ddd| j(                         | j+                          | j                  j-                  | j.                         | j                  r%| j                  j;                  | j                         | j=                          | j?                          y # t        j0                  $ r t3        d       | j5                          Y t        j6                  $ r t3        d       Y t8        $ r3}t3        d|z         t3        d       | j5                          Y d }~d }~ww xY w# | j                  r%| j                  j;                  | j                         | j=                          | j?                          w xY w)NTFrun_generatesyncexit_on_error	state_dir)timeoutz
Reverting.z
Configuration accepted.z
An error occurred: %s) is_revertablesysexitr   	EX_CONFIGstdinfilenor	   Terminalr   saver   signalSIGINT_signal_handlerSIGUSR1backupsetupr   command_applystater8   get_confirmation_inputrK   InputRejectedprintrevertInputAccepted	Exceptionresetcleanupr0   )r%   fdes      r'   rA   zNetplanTry.command_tryU   s   !!#HHR\\"	%!!#B&&r*DFFFKK( MM&--)=)=>MM&..$*>*>?KKMJJLN((dUZfjfpfp(q ""$FF))$,,)? vvT__-LLN""$ %% 	.!KKM%% 	/-. 	+a/0.!KKMM	
 vvT__-LLN""$sC   D6F= =.I+I
 -I
I
 I)I=I
 II
 
AJc                 \    d}| j                   rd}| j                  j                  |       y )NFT)backup_config_dir)r   r,   rX   )r%   rg   s     r'   rX   zNetplanTry.backupz   s/    ! $""5F"Gr(   c                    | j                   rt        j                  j                  ddd      }t        j                  j	                  | j                         j                  d      }t        j                         }t        j                  j                  |dj                  ||            }| j                  j                  | j                   |i       d| _
        y )Nr   etcr   z.yamlz
{}.{}.yamlT)r   r   r"   r#   basenamerstriptimeformatr,   addr   )r%   dest_dir	dest_namedest_suffix	dest_paths        r'   rY   zNetplanTry.setup   s    ww||C	:H(()9)9:AA'JI))+KX|/B/B9k/Z[I##T%5%5y$AB%)"r(   c                 `   t        j                         }t        j                  j	                  |dd      }t        j
                  |       t        j                  d|d       | j                  j                          t               j                  ddd|       t        j                  |       y )Nri   r   z/etc/netplanT)dirs_exist_okFrF   )tempfilemkdtempr   r"   r#   r5   shutilcopytreer,   r_   r   rZ   rmtree)r%   tempdirconfdirs      r'   r_   zNetplanTry.revert   s{    ""$'',,wy9
GtD""$$$%dRWcj$kgr(   c                 8    | j                   j                          y N)r,   rc   r+   s    r'   rc   zNetplanTry.cleanup   s    ##%r(   c                    g }| j                   r|j                  | j                          d}	 | j                  j                  |      }g }i }|j                  |j                         |j                  |j                         |j                         D ].  }|j                   rd}|j                  |j"                  |f       0 |r1|D ]   \  }}t%        dj'                  ||             " t%        d       yy# t        j
                  $ rB}t        j                  |       t        j                  t        j                         Y d}~d}~ww xY w)a  
        Check if the configuration is revertable, if it doesn't contain bits
        that we know are likely to render the system unstable if we apply it,
        or if we revert.

        Returns True if the parsed config is "revertable", meaning that we
        can actually rely on backends to re-apply /all/ of the relevant
        configuration to interfaces when their config changes.

        Returns False if the parsed config contains options that are known
        to not cleanly revert via the backend.
        N)extra_configzBreverting custom parameters for bridges and bonds is not supportedz{}: {}zL
Please carefully review the configuration and use 'netplan apply' directly.FT)r   appendr,   parser   config_errorsloggingerrorrM   rN   r   rO   updatebridgesbondsvalues_is_trivial_compound_itfidr^   rm   )	r%   r   np_statere   revert_unsupportedmulti_ifaceitfreasonifnames	            r'   rL   zNetplanTry.is_revertable   s,     0 01	#**00l0KH
   9;8++,8>>*%%' 	<C//]"))3666*:;	<
 "4 7hooff567ab/ "" 	#MM!HHR\\""	#s   C8 8E8EEc                     |t         j                  k(  rt        j                         | j                  rt        j
                         y r}   )rT   rW   r	   r`   r   r]   )r%   sigframes      r'   rV   zNetplanTry._signal_handler   s:    &.. ((**)),,.. *r(   )__name__
__module____qualname__r   propertyr,   r0   r8   r   rA   rX   rY   r_   rc   rL   rV   __classcell__)r&   s   @r'   r   r   '   sR    e $ $
3#%JH*
&+Z/r(   r   )__doc__r   r   r   rl   rw   rT   rM   ru   configmanagerr    r   applyr   r	   r@   NetplanCommandr    r(   r'   <module>r      sJ   $    	    
  *     c/%% c/r(   