a
    kÞ dY!  ã                   @   sª   d dl Z d dlmZmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ e e¡ZG d
d„ dejƒZddd„Zdd„ Zdd„ ZdS )é    N)ÚdistrosÚhelpers)Úlog)ÚsubpÚutil)Únet_util)ÚHostnameConf)ÚRenderer)ÚRendererNotFoundError)ÚPER_INSTANCEc                       s¶   e Zd ZdZdZdgZdddddœiZd	d
„ Zd)dd„Zdd„ Z	e
dœ‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd*dd „Zd!d"„ Zd#d$„ Zd+d%d&„Zd'd(„ Z‡  ZS ),ÚDistroz/etc/locale.genz/etc/netctlZ	systemctlZnetplanz/etc/netplan/50-cloud-init.yamlz# generated by cloud-init
T)Znetplan_pathZnetplan_headerZpostcmdsc                 C   s0   t j | |||¡ t |¡| _d| _d|d< d S )NZarchZsshdZssh_svcname)r   r   Ú__init__r   ZRunnersÚ_runnerZosfamily)ÚselfÚnameZcfgÚpaths© r   ú:/usr/lib/python3.9/site-packages/cloudinit/distros/arch.pyr   !   s    zDistro.__init__Nc                 C   sj   |d ur|dkrt  d|¡ t ¡ d| dg}t | jd |¡¡ tjdgdd tjd	d
|gdd d S )Nz/etc/locale.confzFInvalid locale_configfile %s, only supported value is /etc/locale.confz%s UTF-8Ú Ú
z
locale-genF©ZcaptureZ	localectlz
set-locale)ÚLOGÚwarningr   Zmake_headerÚ
write_fileÚlocale_gen_fnÚjoinr   )r   ÚlocaleZout_fnÚlinesr   r   r   Úapply_locale*   s    ýüzDistro.apply_localec                 C   s   |   ¡  | jd|d d S )Nr   )Úpkgs)Úupdate_package_sourcesÚpackage_command)r   Úpkglistr   r   r   Úinstall_packages=   s    zDistro.install_packages)Úreturnc              
      s<   zt ƒ  ¡ W S  ty6 } zt|‚W Y d }~n
d }~0 0 d S ©N)ÚsuperÚ_get_rendererr
   ÚNotImplementedError)r   Úe©Ú	__class__r   r   r'   A   s    zDistro._get_rendererc                 C   s.   t  |¡}t d||¡ t|| j| j| jdS )Nz3Translated ubuntu style network settings %s into %s)Úresolv_confÚconf_dirÚenable_func)r   Ztranslate_networkr   ÚdebugÚ_render_networkZresolve_conf_fnÚnetwork_conf_dirÚ_enable_interface)r   ÚsettingsÚentriesr   r   r   Ú_write_networkH   s    
ýüzDistro._write_networkc                 C   sZ   dd|g}z(t   |¡\}}t|ƒr0t d||¡ W n" t jyT   t td|¡ Y n0 d S )NÚnetctlZreenableú(Running %s resulted in stderr output: %sú#Running interface command %s failed)r   Úlenr   r   ÚProcessExecutionErrorr   Úlogexc©r   Zdevice_nameÚcmdZ_outÚerrr   r   r   r2   V   s    
ÿzDistro._enable_interfacec                 C   sl   dd|g}t  d||¡ z*t |¡\}}t|ƒr>t  d||¡ W dS  tjyf   t t d|¡ Y dS 0 d S )Nr6   Zrestartz8Attempting to run bring up interface %s using command %sr7   Tr8   F)r   r/   r   r9   r   r:   r   r;   r<   r   r   r   Ú_bring_up_interfacea   s     
ýÿzDistro._bring_up_interfacec                 C   sV   d }z|   |¡}W n ty$   Y n0 |s2tdƒ}| |¡ tj|t|ƒddd d S )Nr   Úwi¤  )ZomodeÚmode)Ú_read_hostname_confÚIOErrorr   Zset_hostnamer   r   Ústr)r   ÚhostnameÚfilenameÚconfr   r   r   Ú_write_hostnames   s    
zDistro._write_hostnamec                 C   s   |   | j¡}| j|fS r%   )Ú_read_hostnameZhostname_conf_fn)r   Zsys_hostnamer   r   r   Ú_read_system_hostname€   s    zDistro._read_system_hostnamec                 C   s   t t |¡ƒ}| ¡  |S r%   )r   r   Z	load_fileÚparse)r   rF   rG   r   r   r   rB   „   s    zDistro._read_hostname_confc                 C   s8   d }z|   |¡}|j}W n ty*   Y n0 |s4|S |S r%   )rB   rE   rC   )r   rF   ÚdefaultrE   rG   r   r   r   rI   ‰   s    

zDistro._read_hostnamec                 C   sJ   t  d|¡ zt ddd|g¡ W n" tjyD   t t d|¡ Y n0 d S )Nz2Non-persistently setting the system hostname to %sZhostnamectlz--transientzset-hostnamez;Failed to non-persistently adjust the system hostname to %s)r   r/   r   r:   r   r;   )r   rE   r   r   r   Ú_apply_hostname–   s    ÿýzDistro._apply_hostnamec                 C   s   t j||  |¡d d S )N)ÚtzZtz_file)r   Zset_etc_timezoneZ_find_tz_file)r   rN   r   r   r   Úset_timezone£   s    zDistro.set_timezonec                 C   sˆ   |d u rg }g d¢}|r.t |tƒr.| |¡ n|rFt |tƒrF| |¡ |dkrRd}|r`| |¡ t d|¡}| |¡ tj|dd d S )N)Zpacmanz-Syz--quietz--noconfirmZupgradez-uz%s-%sFr   )Ú
isinstancerD   ÚappendÚlistÚextendr   Zexpand_package_listr   )r   ZcommandÚargsr   r=   r"   r   r   r   r!   ¦   s    


zDistro.package_commandc                 C   s   | j jd| jdgtd d S )Nzupdate-sourcesz-y)Zfreq)r   Úrunr!   r   )r   r   r   r   r    ½   s    ÿzDistro.update_package_sources)N)N)NN)Ú__name__Ú
__module__Ú__qualname__r   r1   Zinit_cmdZrenderer_configsr   r   r#   r	   r'   r5   r2   r?   rH   rJ   rB   rI   rM   rO   r!   r    Ú__classcell__r   r   r*   r   r      s0   ýÿ	


r   ú/ú
etc/netctlúetc/resolv.confc              
   C   sâ   g }g }t  ||¡}t  ||¡}|  ¡ D ] \}}|dkr:q(| |¡ tj ||¡}	d|| d¡d| d¡| d¡f | d¡| dg ¡d	œ}
t 	|	t
|
ƒ¡ |r²| d
¡r²||ƒ d|v r(| |d ¡ q(|rÞt 	|t|ƒ¡ |S )znRender the translate_network format into netctl files in target.
    Paths will be rendered under target.
    ÚloZethernetZ	bootprotoz%s/%sZaddressZnetmaskZgatewayzdns-nameservers)Z
ConnectionZ	InterfaceZIPZAddressZGatewayZDNSÚauto)r   Ztarget_pathÚitemsrQ   ÚosÚpathr   Úgetr   r   Úconvert_netctlrS   Úconvert_resolv_conf)r4   Útargetr-   r,   r.   ZdevsZnameserversÚdevÚinfoZnet_fnZnet_cfgr   r   r   r0   Ã   s0    

úr0   c                 C   sn   g }t | ƒD ]V}| | }|du r&d}n*t|ttfƒrPdd dd„ |D ƒ¡ d }| d||f ¡ qd |¡S )	zìGiven a dictionary, returns a string in netctl profile format.

    netctl profile is described at:
    https://git.archlinux.org/netctl.git/tree/docs/netctl.profile.5.txt

    Note that the 'Special Quoting Rules' are not handled here.Nr   ú(ú c                 s   s   | ]}d | V  qdS )z'%s'Nr   )Ú.0Úvr   r   r   Ú	<genexpr>ù   ó    z!convert_netctl.<locals>.<genexpr>ú)z%s=%s
)ÚsortedrP   ÚtuplerR   r   rQ   )r3   ÚresultÚkeyÚvalr   r   r   rc   ì   s    rc   c                 C   s(   d}t | tƒr$| D ]}|d|  }q|S )z4Returns a settings string formatted for resolv.conf.r   znameserver %s
)rP   rR   )r3   rq   Únsr   r   r   rd   þ   s
    
rd   )rZ   r[   r\   N)r`   Z	cloudinitr   r   r   Zloggingr   r   Zcloudinit.distrosr   Z"cloudinit.distros.parsers.hostnamer   Zcloudinit.net.rendererr	   Zcloudinit.net.renderersr
   Zcloudinit.settingsr   Z	getLoggerrV   r   r   r0   rc   rd   r   r   r   r   Ú<module>   s$   
 1    û
)