o
    w7e                      @   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
d4ddZdd Zdd Zdd Zdd Zd4ddZdd ZdedededefddZd d! Zd5d"d#Zd$d% Zed&d' Zd6d*d+Zed,d- Zedd.d/ed0ed1efd2d3ZdS )7Distrozpy3-pipz/usr/share/bkeymaps/z&/etc/profile.d/50-cloud-init-locale.shz/etc/network/interfaceseni)eni_path
eni_headerc                 C   s6   t j| ||| t|| _d| _d| _d|d< d S )NzC.UTF-8alpinesshdssh_svcname)r   r	   __init__r   Runners_runnerdefault_localeosfamily)selfname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   locale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   sR   d }z|  |}W n	 ty   Y nw |std}|| t|t|d d S )Nr   r    )_read_hostname_confIOErrorr   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_hostnamehostname_conf_fn)r   sys_hostnamer   r   r   _read_system_hostnameZ   s   
zDistro._read_system_hostnamec                 C   s   t t|}|  |S r6   )r   r   	load_fileparse)r   r3   r4   r   r   r   r.   ^   s   zDistro._read_hostname_confc                 C   s8   d }z
|  |}|j}W n	 ty   Y nw |s|S |S r6   )r.   r2   r/   )r   r3   defaultr2   r4   r   r   r   r7   c   s   

zDistro._read_hostnamec                 C      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|s(d| }t | t||s5d}t | t|tj|d| }tj|sQd| }t | t||rXt 	d |r_t 	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isfilewarningr   )r   r@   rA   rB   rC   msgkeymap_layout_pathkeymap_variant_pathr   r   r   
set_keymapq   s>   





zDistro.set_keymapc                 C   s   t j|| |d d S )N)tztz_file)r   set_etc_timezone_find_tz_file)r   rQ   r   r   r   set_timezone   s   zDistro.set_timezonec                 C   s   |d u rg }dg}| d |rt|tr| | n|r't|tr'|| |r.| | |dkr9|ddg td|}|| tj|dd d S )	Napkz--quietupgradez--update-cachez--availablez%s-%sF)capture)append
isinstancer1   listextendr   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)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 listchronyntp)_preferred_ntp_clientsr   r   r   r   preferred_ntp_clients   s   
zDistro.preferred_ntp_clientspoweroffnowc              
   C   sd   |dg}|dkr|dg7 }|S z| tt|d  W |S  ty1 } ztd|f |d }~ww )Nz-dri   0<   z?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.)rY   r1   int
ValueError	TypeError)r   modedelaymessager^   er   r   r   shutdown_command   s"   

zDistro.shutdown_commandc                   C   r>   )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)ry   rz   disableenabler|   reloadz
try-reloadr}   T)rX   ru   )r[   r   )	r   rv   rw   ru   rx   init_cmd
update_cmdcmdsr`   r   r   r   manage_service   s   
zDistro.manage_servicer6   )NN)rh   ri   N)__name__
__module____qualname__pip_package_namerI   r!   network_conf_fnNETWORK_FILE_HEADERrenderer_configsr   r   r'   r-   r5   r:   r.   r7   r?   r1   rP   rU   r+   r*   propertyrg   rs   staticmethodrt   classmethodr   r   r   r   r   r	      sD    



"



r	   )rG   	cloudinitr   r   r   loggingr   r   "cloudinit.distros.parsers.hostnamer   cloudinit.settingsr   	getLoggerr   rD   r   r	   r   r   r   r   <module>   s   

