U
    Ӈgi*                     @   s   d dl Z d dlZd dlmZ d dl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 e eZdZd	ZG d
d dejZdddZedfddZdddZdddZdS )    N)List)distrossubputil)Apt)PackageManager)HostnameConf)CLOUDINIT_NETPLAN_FILEa8  # 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}
z/etc/default/localec                       s   e Zd ZdZdedZed eded edddZd	Zd
Z	 fddZ
dd Zd(ddZ f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eeed%d&d'Z  ZS )+Distroz/etc/hostnamez'/etc/network/interfaces.d/50-cloud-init)eninetplanr   )Zeni_pathZ
eni_headerr   T)Znetplan_pathZnetplan_headerZpostcmdsz/var/lib/dhcpzdhclient\.\w+\.leasesc                    s@   t  ||| d| _d| _d | _t| j|| _| jg| _	d S )NZdebianzC.UTF-8)
super__init__Zosfamilydefault_localesystem_localer   Zfrom_configZ_runneraptZpackage_managers)selfnameZcfgpaths	__class__ :/usr/lib/python3/dist-packages/cloudinit/distros/debian.pyr   7   s    zDistro.__init__c                 C   s    | j st | _ | j r| j S | jS )z9Return the default locale if set, else use default locale)r   read_system_localer   r   r   r   r   
get_localeB   s    zDistro.get_localeNLANGc           	      C   s   |st }|std|  }tj|}| j }|rFtdt | j	 ntd| jt  |
 |
 kpn| pn|}| p||p||}|rt|||| jd ntd|| j| |rt|||| jd d| _dS )zeApply specified locale to system, regenerate if specified locale
        differs from system default.zFailed to provide locale value.zVSystem locale not found in %s. Assuming system locale is %s based on hardcoded defaultzSystem locale set to %s via %s)keynameinstall_functionz9System has '%s=%s' requested '%s', skipping regeneration.N)LOCALE_CONF_FN
ValueErrorr   ospathexistsr   LOGdebugr   lowerregenerate_localeZinstall_packagesupdate_locale_conf)	r   localeZout_fnr   Zdistro_localeZconf_fn_existsZsys_locale_unsetZ
need_regenZ	need_confr   r   r   apply_localeM   sZ    zDistro.apply_localec                    s   t   t j||S N)_maybe_remove_legacy_eth0r   _write_network_state)r   argskwargsr   r   r   r-      s    zDistro._write_network_statec                 C   sz   d }z|  |}W n: tk
rL   t| jdd}|r8ntd Y d S Y nX |sZtd}|| t	|t
|d d S )Ncreate_hostname_fileTz8create_hostname_file is False; hostname file not created   )_read_hostname_confIOErrorr   Zget_cfg_option_boolZ_cfgr$   infor   Zset_hostname
write_filestr)r   hostnamefilenameconfr0   r   r   r   _write_hostname   s&      
zDistro._write_hostnamec                 C   s   |  | j}| j|fS r+   )_read_hostname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   load_text_fileparse)r   r9   r:   r   r   r   r3      s    zDistro._read_hostname_confc                 C   s:   d }z|  |}|j}W n tk
r,   Y nX |s6|S |S r+   )r3   r8   r4   )r   r9   defaultr8   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_ip   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   rC   r   r   r   set_timezone   s    zDistro.set_timezonec                 C   s(   |dkrt d| d| jd d S )NZupgradezUnable to handle z command)RuntimeErrorr   Zrun_package_command)r   Zcommandr.   Zpkgsr   r   r   package_command   s    zDistro.package_commandc                 C   s   t  S r+   )r   Zget_dpkg_architecturer   r   r   r   get_primary_arch   s    zDistro.get_primary_arch)layoutmodelvariantoptionsc                 C   s^   d ddd| dd| dd| dd| ddd	dg	}tjd
|ddd | dd d S )N
z'# This file was generated by cloud-initr1   z
XKBMODEL=""zXKBLAYOUT="zXKBVARIANT="zXKBOPTIONS="zBACKSPACE="guess"z/etc/default/keyboardr2   w)r9   ZcontentmodeZomodeZrestartzconsole-setup)joinr   r6   Zmanage_service)r   rH   rI   rJ   rK   contentsr   r   r   
set_keymap   s&    



zDistro.set_keymap)Nr   )N)NN)__name__
__module____qualname__r=   r	   Znetwork_conf_fnNETWORK_FILE_HEADERZrenderer_configsZdhclient_lease_directoryZdhclient_lease_file_regexr   r   r*   r-   r;   r>   r3   r<   rB   rD   rF   rG   r7   rR   __classcell__r   r   r   r   r
   !   s6   
;

r
   "/etc/network/interfaces.d/eth0.cfgc                 C   s   t j| sdS d}zPt| }ddg}dd | D }||krVt|  d|  }n|d|   }W n  tk
r   |d	|   }Y nX t	| dS )
aO  Ubuntu cloud images previously included a 'eth0.cfg' that had
    hard coded content.  That file would interfere with the rendered
    configuration if it was present.

    if the file does not exist do nothing.
    If the file exists:
      - with known content, remove it and warn
      - with unknown content, leave it and warn
    Nz(Dynamic networking config may not apply.z	auto eth0ziface eth0 inet dhcpc                 S   s   g | ]}| d s| qS )#)
startswithstrip).0fr   r   r   
<listcomp>   s    
 z-_maybe_remove_legacy_eth0.<locals>.<listcomp>zremoved %s with known contentsz* '%s' exists with user configured content.z" %s exists, but could not be read.)
r!   r"   r#   r   r?   
splitlinesZdel_file	Exceptionr$   Zwarning)r"   ZbmsgrQ   Zknown_contentslinesmsgr   r   r   r,      s     


r,   r   c                 C   sD   d}| st d|  tj| r@t| }t|}||d}|S )z.Read system default locale setting, if presentr1   zInvalid path: %s)r    r!   r"   r#   r   r?   Zload_shell_contentget)sys_pathr   Zsys_valZlocale_contentZsys_defaultsr   r   r   r   
  s    

r   c                 C   sH   t d|||  tds$|dg tjdd| d|| f gdd dS )	zUpdate system locale configz%Updating %s with locale setting %s=%szupdate-localelocalesz--locale-file=z%s=%sFZcaptureN)r$   r%   r   whichr)   rd   r   r   r   r   r   r(     s       


r(   c                 C   sT   |   dkrtd||  dS tds2|dg td|  tjd| gdd dS )	z
    Run locale-gen for the provided locale and set the default
    system variable `keyname` appropriately in the provided `sys_path`.

    )czc.utf-8posixz$%s=%s does not require rengenerationNz
locale-genre   zGenerating locales for %sFrf   )r&   r$   r%   r   rg   rh   r   r   r   r'   +  s    

r'   )rX   )r   N)r   N)Zloggingr!   typingr   Z	cloudinitr   r   r   Z(cloudinit.distros.package_management.aptr   Z4cloudinit.distros.package_management.package_managerr   Z"cloudinit.distros.parsers.hostnamer   Zcloudinit.net.netplanr	   Z	getLoggerrS   r$   rV   r   r
   r,   r   r(   r'   r   r   r   r   <module>
   s$   
 J
    
