U
    Ad                     @   sJ   d dl mZmZmZmZ d dlmZ d dlmZ dZ	G dd dej
Z
dS )    )distroshelpers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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d+ddZdd Zdd Zd,ddZdd Zedd  Zd-d#d$Zd%d& Zeed'd(d)Zd
S ).Distrozpy3-pipz/etc/profile.d/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_locale)   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_locale/   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_packagesB   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_hostnameF   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_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   s   dS )Nz	127.0.1.1r   r   r   r   r   _get_localhost_ipg   s    zDistro._get_localhost_ipc                 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_timezonej   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Zcapture)append
isinstancer$   listextendr   Zexpand_package_listr   )r   commandargsr   cmdr    r   r   r   r   m   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-dr=   0<   z?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.)r2   r$   int
ValueError	TypeError)r   modeZdelaymessager6   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)actionservicec              
   C   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)rJ   rK   disableenablerM   reloadz
try-reloadrN   Tr1   )r4   r   )r   rH   rI   Zinit_cmdZ
update_cmdZcmdsr8   r   r   r   manage_service   s    

zDistro.manage_service)N)N)NN)r<   r=   N)__name__
__module____qualname__Zpip_package_namer   Znetwork_conf_fnNETWORK_FILE_HEADERZrenderer_configsr	   r   r   r!   r(   r+   r"   r*   r.   r0   r   r   propertyr;   rF   rG   r$   rR   r   r   r   r   r      s.    





r   N)Z	cloudinitr   r   r   r   Z"cloudinit.distros.parsers.hostnamer   Zcloudinit.settingsr   rV   r   r   r   r   r   <module>	   s   
