
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
\¼me\'  ã               @   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 e jeƒZd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)ÚHostnameConfa8  # 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/network/interfaces.d/50-cloud-initz/etc/default/localec                   sÀ   e Zd ZdZdddœZed edœed edd	œ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-initz/etc/netplan/50-cloud-init.yaml)ÚeniÚnetplanr
   )Zeni_pathZ
eni_headerr   T)Znetplan_pathZnetplan_headerZpostcmdsc                s@   t ƒ j|||ƒ d| _d| _d | _tj| 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.6/debian.pyr   3   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_locale>   s    zDistro.get_localeNÚLANGc       	      C   sÀ   |st }|stdƒ‚| jƒ }tjj|ƒ}| j }|rFtjdt | j	ƒ ntjd| jt ƒ |j
ƒ |j
ƒ kpn| pn|}| p||p||}|r’t|||d� ntjd|| j|ƒ |r¼t|||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)Ú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   ÚlocaleZout_fnr   Zdistro_localeZconf_fn_existsZsys_locale_unsetZ
need_regenZ	need_confr   r   r   Úapply_localeI   s<    
zDistro.apply_localec                s   t ƒ  tƒ j||ŽS )N)Ú_maybe_remove_legacy_eth0r   Ú_write_network_state)r   ÚargsÚkwargs)r   r   r   r(   z   s    zDistro._write_network_statec             C   sn   d }y| j |ƒ}W n. tk
r@   tj| jddƒ}|r8nd S Y nX |sNtdƒ}|j|ƒ tj|t|ƒdƒ d S )NÚcreate_hostname_fileTÚ i¤  )	Ú_read_hostname_confÚIOErrorr   Zget_cfg_option_boolZ_cfgr   Zset_hostnameÚ
write_fileÚstr)r   ÚhostnameÚfilenameÚconfr+   r   r   r   Ú_write_hostname~   s    

zDistro._write_hostnamec             C   s   | j | jƒ}| j|fS )N)Ú_read_hostnameÚhostname_conf_fn)r   Zsys_hostnamer   r   r   Ú_read_system_hostname‘   s    zDistro._read_system_hostnamec             C   s   t tj|ƒƒ}|jƒ  |S )N)r   r   Ú	load_fileÚparse)r   r2   r3   r   r   r   r-   •   s    zDistro._read_hostname_confc             C   s:   d }y| j |ƒ}|j}W n tk
r,   Y nX |s6|S |S )N)r-   r1   r.   )r   r2   Údefaultr1   r3   r   r   r   r5   š   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|| 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t d|› d�ƒ‚| j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 jƒ S )N)r   Zget_dpkg_architecture)r   r   r   r   Úget_primary_arch³   s    zDistro.get_primary_arch)ÚlayoutÚmodelÚvariantÚoptionsc          
   C   s^   dj ddd|› d�d|› d�d|› d�d|› d�dd	dg	ƒ}tjd
|ddd� | jddƒ d S )NÚ
z'# This file was generated by cloud-initr,   z
XKBMODEL="ú"zXKBLAYOUT="zXKBVARIANT="zXKBOPTIONS="zBACKSPACE="guess"z/etc/default/keyboardi¤  Úw)r2   ZcontentÚmodeZomodeZrestartzconsole-setup)Újoinr   r/   Zmanage_service)r   rA   rB   rC   rD   Úcontentsr   r   r   Ú
set_keymap¶   s     



zDistro.set_keymap)Nr   )N)NN)Ú__name__Ú
__module__Ú__qualname__r6   Znetwork_conf_fnÚNETWORK_FILE_HEADERZrenderer_configsr   r   r&   r(   r4   r7   r-   r5   r;   r=   r?   r@   r0   rK   Ú__classcell__r   r   )r   r   r	   !   s*   
1

r	   ú"/etc/network/interfaces.d/eth0.cfgc             C   s”   t jj| ƒsdS d}yPtj| ƒ}ddg}dd„ |jƒ D ƒ}||krVtj| ƒ d|  }n|d|   }W n  tk
r„   |d	|   }Y nX tj	|ƒ 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 | ]}|j d ƒs|jƒ ‘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   r8   Ú
splitlinesZdel_fileÚ	Exceptionr    Zwarning)r   ZbmsgrJ   Zknown_contentsÚlinesÚmsgr   r   r   r'   Ù   s    


r'   r   c             C   sD   d}| st d|  ƒ‚tjj| ƒr@tj| ƒ}tj|ƒ}|j|dƒ}|S )z.Read system default locale setting, if presentr,   zInvalid path: %s)r   r   r   r   r   r8   Zload_shell_contentÚget)Úsys_pathr   Zsys_valZlocale_contentZsys_defaultsr   r   r   r   ù   s    

r   c             C   s4   t j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   )r%   r]   r   r   r   r   r$     s    r$   c             C   s@   | j ƒ d
krtjd|| ƒ dS tj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`.

    Úcúc.utf-8Úposixz$%s=%s does not require rengenerationNzGenerating locales for %sz
locale-genF)r^   )r_   r`   ra   )r"   r    r!   r   )r%   r]   r   r   r   r   r#     s
    r#   )rQ   )r   )r   )Zloggingr   Ztypingr   Z	cloudinitr   r   r   Z(cloudinit.distros.package_management.aptr   Z4cloudinit.distros.package_management.package_managerr   Z"cloudinit.distros.parsers.hostnamer   Z	getLoggerrL   r    rO   ZNETWORK_CONF_FNr   r	   r'   r   r$   r#   r   r   r   r   Ú<module>
   s    
 9
 
