
­­­­­­­­­­­­­­­­­­
<!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||}|rt|||d ntjd|| j| |rt|||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
 
