
    f                         d dl Z d dlZd dlZd dlmZ d dlZd dlmZmZ d dl	m
Z
 d dlmZ  e j                  e      Z G d dej                   j"                  j$                        Zy)    N)StringIO)subputil)FreeBSDNetworking)PER_INSTANCEc                   
   e Zd ZdZeZdZdZdZdZ	g dZ
ddgZdd	gZdd
gZddgZdZdZdZdZed(d       Zedddededefd       Zd Zd Zd Zd)dZd Zd(dZd Zd Zeded ed!ed"ed#ed$e fd%       Z!ed&ed$dfd'       Z"y)*DistrozY
    Distro subclass for FreeBSD.

    (N.B. DragonFlyBSD inherits from this class.)
    z/usr/local/libz/etc/login.confz/etc/login.conf.origz,/usr/local/etc/sudoers.d/90-cloud-init-users)pwgroupaddpkginstallremoveupdateupgradeTz	/usr/homez/var/dbzdhclient.leases.\w+Nc                     t         j                  j                  d      }|t        j	                  d       yt        j
                  dd|gd|      S )z
        Tell rc to reload its configuration
        Note that this only works while we're still in the process of booting.
        May raise ProcessExecutionError
        RC_PIDNz0Unable to reload rc(8): no RC_PID in Environmentkillz-SIGALRMTcapturercs)osenvirongetLOGwarningr   )clsr   rc_pids      ;/usr/lib/python3/dist-packages/cloudinit/distros/freebsd.pyreload_initzDistro.reload_init,   sE     )>KKJKyy&*f5tMM    )r   actionservice
extra_argsc                    | j                   }|dg|dg|dg|dg|dg|dg|dg|dg|dg|dg|d	g|d
gd}|||   z   t        |      z   }t        j                  |d|      S )a  
        Perform the requested action on a service. This handles FreeBSD's
        'service' case. The FreeBSD 'service' is closer in features to
        'systemctl' than SysV init's 'service', so we override it.
        May raise ProcessExecutionError
        stopstartenableenableddisableonestartonestoprestartstatus	onestatus)r&   r'   r(   r)   r*   r+   r,   r-   reloadz
try-reloadr.   r/   Tr   )init_cmdlistr   )r   r"   r#   r   r$   r1   cmdscmds           r   manage_servicezDistro.manage_service:   s     <<f%w')++ *-++	*"I.)!;/
 f%Z(88yyd44r!   c                     ddd|d|gS )Nr
   usermod-n-G )selfmember_name
group_names      r   _get_add_member_to_group_cmdz#Distro._get_add_member_to_group_cmdV   s    i{D*EEr!   c                    t        j                  |      rt        j                  d|       yddd|g}ddd|g}dddd	d
ddd}dddd}|j	                         D ]q  \  }}||v r8|r6t        |t        t        f      r |j                  ||   t        |      g       B||v sG|sJ|j                  ||          |j                  ||          s d|v sd|v r#|j                  d       |j                  d       nz|j                  dj                  | j                  |             |j                  d       |j                  dj                  | j                  |             |j                  d       t        j                  d|       	 t        j                  ||       |j                  dd       }	|	| j!                  ||	d       y y # t        $ r t        j                  t        d|        w xY w)Nz!User %s already exists, skipping.Fr
   useraddr8   z-d-cz-gr9   z-sz-Ez-u)homedirgecosprimary_groupgroupsshellinactiveuidz--no-user-groupz--systemz--no-log-init)no_user_groupsystemno_log_initno_create_homerJ   z-d/nonexistentz-d{home_dir}/{name})home_dirnamez-mzAdding user %s)	logstringzFailed to create user %spasswdT)hashed)r   is_userr   infoitems
isinstancestrintextendappendformatrM   r   	Exceptionlogexcr   
set_passwd)
r;   rN   kwargspw_useradd_cmdlog_pw_useradd_cmdpw_useradd_optspw_useradd_flagskeyval
passwd_vals
             r   add_userzDistro.add_userY   s   <<HH8$?	46"ItT: !
 / *
  	AHCo%#*S3*2M%%s';SX&FG((S%%&6s&;<"))*:3*?@	A v%V);!!"23%%&67!!%,,dmm$,O !!$'%%%,,dmm$,O %%d+ 	!4(	IIn0BC ZZ$/
!OOD*TO: "  	KK7>	s   	G
 
&G0c                     	 t        j                   dd|ddg       y # t        $ r t        j                  t        d|        w xY w)Nr
   r7   -pz01-Jan-1970z"Failed to set pw expiration for %sr   r[   r   r\   r   )r;   users     r   expire_passwdzDistro.expire_passwd   sA    	IItYdMBC 	KKA4H		    &Ac                     |rd}nd}	 t        j                   dd||dg|d|z         y # t        $ r t        j                  t        d|        w xY w)	Nz-Hz-hr
   r7   0zchpasswd for %s)datarO   zFailed to set password for %sri   )r;   rj   rP   rQ   hash_opts        r   r]   zDistro.set_passwd   s^    HH	IIy$#6+d2
  	KK<dC	s	    * &Ac                     	 t        j                   dd|ddg       y # t        $ r t        j                  t        d|        w xY w)Nr
   r7   z-wnoz)Failed to lock password login for user %sri   )r;   rN   s     r   lock_passwdzDistro.lock_passwd   sA    	IItYdD9: 	KKH$O	rl   c                 8   t               }t        j                  | j                        j	                         D ]<  }|j                  t        j                  dd|z  |             |j                  d       > t        j                  | j                  | j                         t        j                  | j                  |j                                	 t        j                  d|       t        j                  d| j                  g       y # t        j                  $ r~ t        j                   t        d|       	 t        j                  | j                  | j                         Y y # t"        $ r) t        j                   t        d| j                         Y Y y w xY ww xY w)Nz	^default:zdefault:lang=%s:
zRunning cap_mkdb for %scap_mkdbzFailed to apply locale %szFailed to restore %s backup)r   r   load_text_filelogin_conf_fn
splitlineswriteresubcopylogin_conf_fn_bak
write_filegetvaluer   debugr   ProcessExecutionErrorr\   IOError)r;   localeout_fnnewconflines        r   apply_localezDistro.apply_locale   s6   *''(:(:;FFH 	 DMM|%86%A4H MM$		  			$$$d&<&<= 	**G,<,<,>?	II/8IIz4#5#567)) 	KK8&A		$00$2D2DE 68J8J	s*   7D .F7*E##.FFFFc                 
    ddiS )zJReturn environment vars used in FreeBSD package_command
        operationsASSUME_ALWAYS_YESYESr:   r;   s    r   _get_pkg_cmd_environzDistro._get_pkg_cmd_environ   s     $U++r!   c                 `    | j                   j                  d| j                  dgt               y )Nzupdate-sourcesr   )freq)_runnerrunpackage_commandr   r   s    r   update_package_sourceszDistro.update_package_sources   s-      J	 	 	
r!   path
lease_filepid_file	interfaceconfig_filereturnc                 *    | d|d|g|rd||gz   S |gz   S )Nz-lrh   rA   r:   )r   r   r   r   r   s        r   build_dhclient_cmdzDistro.build_dhclient_cmd   s7     dJh7.9T;	*
 	
@I{
 	
r!   devicec                 4    t        j                   dd| g       y )N
camcontroleject)r   )r   s    r   eject_mediazDistro.eject_media   s    		<&12r!   )N)F)#__name__
__module____qualname____doc__r   networking_clsusr_lib_execrx   r~   ci_sudoers_fngroup_add_cmd_prefixpkg_cmd_install_prefixpkg_cmd_remove_prefixpkg_cmd_update_prefixpkg_cmd_upgrade_prefixprefer_fqdnrM   dhclient_lease_directorydhclient_lease_file_regexclassmethodr    rV   r5   r>   rf   rk   r]   rs   r   r   r   staticmethodr2   r   r   r:   r!   r   r	   r	      sH    'N#L%M.BM1#Y/"H-"H-#Y/KH  ) 6N N >B55#&5585 56F8;t 8,

 	
	
	
 	
 		

 	
 
	
 	
 3C 3D 3 3r!   r	   )loggingr   r{   ior   cloudinit.distros.bsd	cloudinitr   r   cloudinit.distros.networkingr   cloudinit.settingsr   	getLoggerr   r   distrosbsdBSDr	   r:   r!   r   <module>r      sQ     	 	     : +g!T3Y""&& T3r!   