U
    Ld                      @   sp   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 eeZdZG dd	 d	ejZdS )
    N)distroshelpers)log)subputil)HostnameConf)PER_INSTANCEa7  # This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

c                   @   s   e Zd ZdZdZdZdZdeediZdd Z	d	d
 Z
d/ddZdd Zdd Zdd Zdd Zd0ddZdd ZeeeedddZdd Zd1dd Zd!d" Zed#d$ Zd2d'd(Zed)d* Zedd+eeed,d-d.ZdS )3Distrozpy3-pipz/usr/share/bkeymaps/z&/etc/profile.d/50-cloud-init-locale.shz/etc/network/interfacesZeni)Zeni_pathZ
eni_headerc                 C   s6   t j| ||| t|| _d| _d| _d|d< d S )NzC.UTF-8ZalpineZsshdZssh_svcname)r   r	   __init__r   ZRunners_runnerdefault_localeZosfamily)selfnameZcfgpaths r   :/usr/lib/python3/dist-packages/cloudinit/distros/alpine.pyr
   &   s
    zDistro.__init__c                 C   s   | j S )zgThe default locale for Alpine Linux is different than
        cloud-init's DataSource default.
        )r   r   r   r   r   
get_locale0   s    zDistro.get_localeNc                 C   sB   |s
| j }|s| j}ddddd| ddg}t|d|d d S )	N#z?# This file is created by cloud-init once per new instance bootzexport CHARSET=UTF-8zexport LANG=%szexport LC_COLLATE=C 
  )r   locale_conf_fnr   
write_filejoin)r   ZlocaleZout_fnlinesr   r   r   apply_locale6   s    	zDistro.apply_localec                 C   s   |    | jd|d d S )Nadd)pkgs)update_package_sourcespackage_command)r   pkglistr   r   r   install_packagesI   s    zDistro.install_packagesc                 C   sT   d }z|  |}W n tk
r&   Y nX |s4td}|| t|t|d d S )Nr   r   )_read_hostname_confIOErrorr   Zset_hostnamer   r   str)r   hostnamefilenameconfr   r   r   _write_hostnameM   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_hostnameZ   s    zDistro._read_system_hostnamec                 C   s   t t|}|  |S r*   )r   r   Z	load_fileparse)r   r'   r(   r   r   r   r#   ^   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+   c   s    

zDistro._read_hostnamec                 C   s   dS )Nz	127.0.1.1r   r   r   r   r   _get_localhost_ipn   s    zDistro._get_localhost_ip)layoutmodelvariantoptionsc                 C   s   |sd}t | t|tj| j|}tj|sPd| }t | t||sjd}t | t|tj|d| }tj|sd| }t | t||rt 	d |rt 	d t

d||g d S )	NzKeyboard layout not specified.z,Keyboard layout directory %s does not exist.zKeyboard variant not specified.z
%s.bmap.gzz(Keyboard variant file %s does not exist.z+Keyboard model is ignored for Alpine Linux.z.Keyboard options are ignored for Alpine Linux.zsetup-keymap)LOGerrorRuntimeErrorospathr   keymap_pathisdirisfileZwarningr   )r   r0   r1   r2   r3   msgZkeymap_layout_pathZkeymap_variant_pathr   r   r   
set_keymapq   s@    


 


zDistro.set_keymapc                 C   s   t j|| |d d S )N)tzZtz_file)r   Zset_etc_timezoneZ_find_tz_file)r   r>   r   r   r   set_timezone   s    zDistro.set_timezonec                 C   s   |d krg }dg}| d |r6t|tr6| | n|rNt|trN|| |r\| | |dkrr|ddg td|}|| tj|dd d S )	NZapkz--quietZupgradez--update-cachez--availablez%s-%sF)capture)append
isinstancer%   listextendr   Zexpand_package_listr   )r   commandargsr   cmdr!   r   r   r   r       s    



zDistro.package_commandc                 C   s   | j jd| jdgtd d S )Nzupdate-sourcesupdate)Zfreq)r   runr    r   r   r   r   r   r      s    zDistro.update_package_sourcesc                 C   s   | j sddg| _ | j S )z7Allow distro to determine the preferred ntp client listZchronyZntp)Z_preferred_ntp_clientsr   r   r   r   preferred_ntp_clients   s    
zDistro.preferred_ntp_clientspoweroffnowc              
   C   sn   |dg}|dkr|dg7 }nNz| tt|d  W n2 tk
rh } ztd|f |W 5 d }~X Y nX |S )Nz-drL   0<   z?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.)rA   r%   int
ValueError	TypeError)r   modeZdelaymessagerE   er   r   r   shutdown_command   s    zDistro.shutdown_commandc                   C   s   dS )z1
        Alpine uses OpenRC, not systemd
        Fr   r   r   r   r   uses_systemd   s    zDistro.uses_systemd)rcs)actionservice
extra_argsc          	   
   G   s   ddg}ddg}t ||dg t ||dg t |d|g t |d|g t ||dg t ||dg t ||dg t ||d	g d
}t || }tj|d|dS )a<  
        Perform the requested action on a service. This handles OpenRC
        specific implementation details.

        OpenRC has two distinct commands relating to services,
        'rc-service' and 'rc-update' and the order of their argument
        lists differ.
        May raise ProcessExecutionError
        z
rc-servicez	--nocolorz	rc-updatestopstartdelr   restartstatus)r[   r\   disableenabler^   reloadz
try-reloadr_   T)r@   rW   )rC   r   )	r   rX   rY   rW   rZ   Zinit_cmdZ
update_cmdZcmdsrG   r   r   r   manage_service   s    
zDistro.manage_service)N)N)NN)rK   rL   N)__name__
__module____qualname__Zpip_package_namer9   r   Znetwork_conf_fnNETWORK_FILE_HEADERZrenderer_configsr
   r   r   r"   r)   r,   r#   r+   r/   r%   r=   r?   r    r   propertyrJ   rU   staticmethodrV   classmethodrc   r   r   r   r   r	      s@    


"



  r	   )r7   Z	cloudinitr   r   r   Zloggingr   r   Z"cloudinit.distros.parsers.hostnamer   Zcloudinit.settingsr   Z	getLoggerrd   r4   rg   r	   r   r   r   r   <module>	   s   

