o
    f(                     @   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d%ed&ed'ed(efd)d*Z  ZS ).Distroz/etc/hostnamez'/etc/network/interfaces.d/50-cloud-init)eninetplanr   )eni_path
eni_headerr   T)netplan_pathnetplan_headerpostcmdsz/var/lib/dhcpzdhclient\.\w+\.leasesc                    s@   t  ||| d| _d| _d | _t| j|| _| jg| _	d S )NdebianzC.UTF-8)
super__init__osfamilydefault_localesystem_localer   from_config_runneraptpackage_managers)selfname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 }|r#tdt | j	 ntd| jt  |
 |
 kp7| p7|}| p>|p>|}|rIt|||d n	td|| j| |r`t|||d d| _dS 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)keynamez9System has '%s=%s' requested '%s', skipping regeneration.N)LOCALE_CONF_FN
ValueErrorr&   ospathexistsr   LOGdebugr   lowerregenerate_localeupdate_locale_conf)	r   localeout_fnr(   distro_localeconf_fn_existssys_locale_unset
need_regen	need_confr"   r"   r#   apply_localeM   sH   
zDistro.apply_localec                    s   t   t j|i |S N)_maybe_remove_legacy_eth0r   _write_network_state)r   argskwargsr    r"   r#   r=   ~   s   zDistro._write_network_statec                 C   sx   d }z|  |}W n ty%   t| jdd}|rntd Y d S Y nw |s,td}|| t	|t
|d d S )Ncreate_hostname_fileTz8create_hostname_file is False; hostname file not created   )_read_hostname_confIOErrorr   get_cfg_option_bool_cfgr.   infor   set_hostname
write_filestr)r   hostnamefilenameconfr@   r"   r"   r#   _write_hostname   s&   
zDistro._write_hostnamec                 C   s   |  | j}| j|fS r;   )_read_hostnamehostname_conf_fn)r   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   rL   rM   r"   r"   r#   rC      s   zDistro._read_hostname_confc                 C   s8   d }z
|  |}|j}W n	 ty   Y nw |s|S |S r;   )rC   rK   rD   )r   rL   defaultrK   rM   r"   r"   r#   rO      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)tztz_file)r   set_etc_timezone_find_tz_file)r   rW   r"   r"   r#   set_timezone   s   zDistro.set_timezonec                 C   s(   |dkrt d| d| jd d S )NupgradezUnable to handle z command)RuntimeErrorr   run_package_command)r   commandr>   pkgsr"   r"   r#   package_command   s   zDistro.package_commandc                 C   s   t  S r;   )r   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-initrA   z
XKBMODEL=""zXKBLAYOUT="zXKBVARIANT="zXKBOPTIONS="zBACKSPACE="guess"z/etc/default/keyboardrB   w)rL   contentmodeomoderestartzconsole-setup)joinr   rI   manage_service)r   rd   re   rf   rg   contentsr"   r"   r#   
set_keymap   s&   



zDistro.set_keymap)Nr'   r;   )NN)__name__
__module____qualname__rP   r	   network_conf_fnNETWORK_FILE_HEADERrenderer_configsdhclient_lease_directorydhclient_lease_file_regexr   r&   r:   r=   rN   rR   rC   rO   rV   r[   ra   rc   rJ   rr   __classcell__r"   r"   r    r#   r
   !   s8    
1

"r
   "/etc/network/interfaces.d/eth0.cfgc                 C   s   t j| sdS d}z(t| }ddg}dd | D }||kr+t|  d|  }n|d|   }W n tyA   |d	|   }Y nw 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   rS   
splitlinesdel_file	Exceptionr.   warning)r,   bmsgrq   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 presentrA   zInvalid path: %s)r*   r+   r,   r-   r   rS   load_shell_contentget)sys_pathr(   sys_vallocale_contentsys_defaultsr"   r"   r#   r$      s   

r$   c                 C   s4   t d|||  tjdd| d|| f gdd dS )zUpdate system locale configz%Updating %s with locale setting %s=%szupdate-localez--locale-file=z%s=%sFcaptureN)r.   r/   r   r3   r   r(   r"   r"   r#   r2     s   

r2   c                 C   s@   |   dv rtd||  dS 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Generating locales for %sz
locale-genFr   )r0   r.   r/   r   r   r"   r"   r#   r1     s
   r1   )r|   )r'   )loggingr+   typingr   	cloudinitr   r   r   (cloudinit.distros.package_management.aptr   4cloudinit.distros.package_management.package_managerr   "cloudinit.distros.parsers.hostnamer   cloudinit.net.netplanr	   	getLoggerrs   r.   rw   r)   r
   r<   r$   r2   r1   r"   r"   r"   r#   <module>   s"   	
 
@ 
