U
    Ӈg                     @   st   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ e eZG dd dejZdS )	    N)distroshelperssubputil)PackageList)HostnameConf)CLOUDINIT_NETPLAN_FILE)
PER_ALWAYSPER_INSTANCEc                   @   s   e Zd ZdZdgZdedddiZdd Zd!d
dZe	d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dd Zd	S )$Distroz/etc/locale.genZ	systemctlZnetplanz# 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   8/usr/lib/python3/dist-packages/cloudinit/distros/arch.pyr      s    zDistro.__init__Nc                 C   sj   |d k	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)LOGZwarningr   Zmake_header
write_filelocale_gen_fnjoinr   )r   ZlocaleZout_fnlinesr   r   r   apply_locale&   s    zDistro.apply_locale)pkglistc                 C   s   |    | jd|d d S )Nr   )pkgs)update_package_sourcespackage_command)r   r   r   r   r   install_packages9   s    zDistro.install_packagesc                 C   s~   d }z|  |}W n: tk
rL   t| jdd}|r8ntd Y d S Y nX |sZtd}|| tj	|t
|ddd d S )Ncreate_hostname_fileTz8create_hostname_file is False; hostname file not createdr   wi  )Zomodemode)_read_hostname_confIOErrorr   Zget_cfg_option_boolZ_cfgr   infor   Zset_hostnamer   str)r   hostnamefilenameconfr!   r   r   r   _write_hostname=   s&      
zDistro._write_hostnamec                 C   s   |  | j}| j|fS N)_read_hostnameZhostname_conf_fn)r   Zsys_hostnamer   r   r   _read_system_hostnameS   s    zDistro._read_system_hostnamec                 C   s   t t|}|  |S r,   )r   r   Zload_text_fileparse)r   r)   r*   r   r   r   r$   W   s    zDistro._read_hostname_confc                 C   s:   d }z|  |}|j}W n tk
r,   Y nX |s6|S |S r,   )r$   r(   r%   )r   r)   defaultr(   r*   r   r   r   r-   \   s    

zDistro._read_hostnamec                 C   sL   t d| ztddd|g W n$ tjk
rF   tt d| Y nX 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   debugr   ZProcessExecutionErrorr   Zlogexc)r   r(   r   r   r   _apply_hostnamei   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   r3   r   r   r   set_timezonev   s    zDistro.set_timezonec                 C   s   |d krg }ddddg}|r2t |tr2|| n|rJt |trJ|| |dkrVd}|rd|| td|}|| tj|dd	 d S )
NZpacmanz-Syz--quietz--noconfirmZupgradez-uz%s-%sFr   )
isinstancer'   appendlistextendr   Zexpand_package_listr   )r   Zcommandargsr   cmdr   r   r   r   r   y   s    


zDistro.package_commandF)forcec                C   s$   | j jd| jdg|rtntd d S )Nzupdate-sourcesz-y)Zfreq)r   runr   r	   r
   )r   r;   r   r   r   r      s    
zDistro.update_package_sources)N)N)NN)__name__
__module____qualname__r   Zinit_cmdr   Zrenderer_configsr   r   r   r    r+   r.   r$   r-   r2   r4   r   r   r   r   r   r   r      s&   	


r   )ZloggingZ	cloudinitr   r   r   r   Zcloudinit.distrosr   Z"cloudinit.distros.parsers.hostnamer   Zcloudinit.net.netplanr   Zcloudinit.settingsr	   r
   Z	getLoggerr=   r   r   r   r   r   r   <module>   s   
