
"·T`U  ã               @   s¦   d  d l  Z  d  d l Z d  d l m 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 e j e ƒ Z Gd d „  d e j j j ƒ Z d S)	é    N)ÚStringIO)Úlog)Úsubp)Úutil)ÚPER_INSTANCEc               @   så   e  Z d  Z d Z d Z d Z d Z d d d g Z d d	 g Z d d
 g Z	 d d g Z
 d d g Z d d „  Z d d „  Z d d „  Z d d „  Z d d d „ Z d d „  Z d d d „ Z d d „  Z d d  „  Z d! d" „  Z d S)#ÚDistroz/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ÚupdateZupgradec             C   s   | r
 | S| S)N© )ÚselfZhostnameZfqdnr   r   ú;/usr/lib/python3/dist-packages/cloudinit/distros/freebsd.pyÚ_select_hostname   s    zDistro._select_hostnamec             C   s   d d d | d | g S)Nr   Úusermodz-nz-Gr   )r   Zmember_nameZ
group_namer   r   r   Ú_get_add_member_to_group_cmd%   s    z#Distro._get_add_member_to_group_cmdc       
      K   s  t  j | ƒ r# t j d | ƒ d Sd d d | g } d d d | g } d d d d	 d
 d d d d d d d i } d d d d d d i } x‰ | j ƒ  D]{ \ } } | | k rÝ | rÝ t | t ƒ rÝ | j | | | g ƒ q– | | k r– | r– | j | | ƒ | j | | ƒ q– Wd | k s-d | k rJ| j d ƒ | j d ƒ n< | j d | ƒ | j d ƒ | j d | ƒ | j d ƒ t j d | ƒ y t	 j	 | d | ƒWn( t
 k
 r×t  j t d | ƒ ‚  Yn X| j d d  ƒ }	 |	 d  k	 r|  j | |	 d  d! ƒd  S)"Nz!User %s already exists, skipping.Fr   Zuseraddz-nZhomedirz-dZgecosz-cZprimary_groupz-gÚgroupsz-GÚshellz-sZinactivez-EZno_user_groupz--no-user-groupÚsystemz--systemZno_log_initz--no-log-initZno_create_homez-d/nonexistentz-d/usr/home/%sz-mzAdding user %sÚ	logstringzFailed to create user %sÚpasswdÚhashedT)r   Zis_userÚLOGÚinfoÚitemsÚ
isinstanceÚstrÚextendÚappendr   Ú	ExceptionÚlogexcÚgetÚ
set_passwd)
r   ÚnameÚkwargsZpw_useradd_cmdZlog_pw_useradd_cmdZpw_useradd_optsZpw_useradd_flagsÚkeyÚvalZ
passwd_valr   r   r   Úadd_user(   sH    !zDistro.add_userc             C   sO   y  t  j  d d | d d g ƒ Wn( t k
 rJ t j t d | ƒ ‚  Yn Xd  S)Nr   r   z-pz01-Jan-1970z"Failed to set pw expiration for %s)r   r"   r   r#   r   )r   Úuserr   r   r   Úexpire_passwd\   s
     zDistro.expire_passwdFc             C   st   | r d } n d } y0 t  j  d d | | d g d | d d | ƒWn( t k
 ro t j t d	 | ƒ ‚  Yn Xd  S)
Nz-Hz-hr   r   Ú0Údatar   zchpasswd for %szFailed to set password for %s)r   r"   r   r#   r   )r   r+   r   r   Zhash_optr   r   r   r%   c   s    	zDistro.set_passwdc             C   sO   y  t  j  d d | d d g ƒ Wn( t k
 rJ t j t d | ƒ ‚  Yn Xd  S)Nr   r   z-hú-zFailed to lock user %s)r   r"   r   r#   r   )r   r&   r   r   r   Úlock_passwdp   s
     zDistro.lock_passwdNc             C   s&  t  ƒ  } xM t j |  j ƒ j ƒ  D]3 } | j t j d d | | ƒ ƒ | j d ƒ q" Wt j |  j |  j	 ƒ t j
 |  j | j ƒ  ƒ y* t j d | ƒ t j d |  j g ƒ Wnm t j k
 r!t j t d | ƒ y t j |  j	 |  j ƒ Wn( t k
 rt j t d |  j ƒ Yn XYn Xd  S)Nz	^default:zdefault:lang=%s:Ú
zRunning cap_mkdb for %sZcap_mkdbzFailed to apply locale %szFailed to restore %s backup)r   r   Z	load_fileÚlogin_conf_fnÚ
splitlinesÚwriteÚreÚsubÚcopyÚlogin_conf_fn_bakZ
write_fileÚgetvaluer   Údebugr   ZProcessExecutionErrorr#   ÚIOError)r   ZlocaleZout_fnZnewconfÚliner   r   r   Úapply_localew   s"    	zDistro.apply_localec             C   s   d  S)Nr   )r   Z	netconfigr   r   r   Úapply_network_config_names‘   s    z!Distro.apply_network_config_namesc             C   s   t  j j ƒ  } d | d <| S)z?Return environment vars used in *BSD package_command operationsZYESZASSUME_ALWAYS_YES)ÚosÚenvironr7   )r   Úer   r   r   Ú_get_pkg_cmd_environ˜   s    
zDistro._get_pkg_cmd_environc             C   s&   |  j  j d |  j d g d t ƒd  S)Nzupdate-sourcesr   Zfreq)Z_runnerZrunZpackage_commandr   )r   r   r   r   Úupdate_package_sourcesž   s    		zDistro.update_package_sources)Ú__name__Ú
__module__Ú__qualname__Zusr_lib_execr2   r8   Zci_sudoers_fnZgroup_add_cmd_prefixZpkg_cmd_install_prefixZpkg_cmd_remove_prefixZpkg_cmd_update_prefixZpkg_cmd_upgrade_prefixr   r   r*   r,   r%   r0   r=   r>   rB   rC   r   r   r   r   r      s&   4r   )r?   r5   Úior   Zcloudinit.distros.bsdZ	cloudinitr   Zloggingr   r   Zcloudinit.settingsr   Z	getLoggerrD   r   ZdistrosZbsdZBSDr   r   r   r   r   Ú<module>   s   