509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
\¼me-$  ã               @   sÚ   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZ dZdZejeƒZG dd„ de jd�ZG d	d
„ d
eƒZegZddd„Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zddd„ZdS )é    N)Úutil)Úget_devicelistÚread_sys_net_safez#/run/initramfs/open-iscsi.interfaceÚdisabledc               @   s8   e Zd ZdZejedœdd„ƒZejedœdd„ƒZ	dS )ÚInitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfses)Úreturnc             C   s   dS )zAIs this initramfs config source applicable to the current system?N© )Úselfr   r   ú/usr/lib/python3.6/cmdline.pyÚis_applicable   s    z*InitramfsNetworkConfigSource.is_applicablec             C   s   dS )z;Render a v1 network config from the initramfs configurationNr   )r	   r   r   r
   Úrender_config"   s    z*InitramfsNetworkConfigSource.render_configN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚabcÚabstractmethodÚboolr   Údictr   r   r   r   r
   r      s
   r   )Ú	metaclassc               @   s6   e Zd ZdZd
dd„Zedœdd„Zedœdd	„ZdS )ÚKlibcNetworkConfigSourcezñInitramfsNetworkConfigSource for klibc initramfs (i.e. Debian/Ubuntu)

    Has three parameters, but they are intended to make testing simpler, _not_
    for use in production code.  (This is indicated by the prepended
    underscores.)
    Nc             C   st   || _ || _|| _| j d kr$tƒ | _ | jd kr8tjƒ | _| jd krpi | _x&tƒ D ]}t|dƒ}|rP|| j|< qPW d S )NÚaddress)Ú_filesÚ
_mac_addrsÚ_cmdlineÚ_get_klibc_net_cfg_filesr   Úget_cmdliner   r   )r	   r   r   r   ÚkZmac_addrr   r   r
   Ú__init__/   s    




z!KlibcNetworkConfigSource.__init__)r   c             C   s>   | j r:x"tj| jƒD ]}|jdƒrdS qW tjjtƒr:dS dS )aD  
        Return whether this system has klibc initramfs network config or not

        Will return True if:
            (a) klibc files exist in /run, AND
            (b) either:
                (i) ip= or ip6= are on the kernel cmdline, OR
                (ii) an open-iscsi interface file is present in the system
        úip=úip6=TF)r   r    )	r   ÚshlexÚsplitr   Ú
startswithÚosÚpathÚexistsÚ_OPEN_ISCSI_INTERFACE_FILE)r	   Úitemr   r   r
   r   A   s    

z&KlibcNetworkConfigSource.is_applicablec             C   s   t | j| jd�S )N)ÚfilesÚ	mac_addrs)Úconfig_from_klibc_net_cfgr   r   )r	   r   r   r
   r   T   s    z&KlibcNetworkConfigSource.render_config)NNN)	r   r   r   r   r   r   r   r   r   r   r   r   r
   r   '   s   
r   c             C   sö  |dkri }t j| ƒ}yd|kr(|d n|d }W n, tk
r^ } ztdƒ|‚W Y dd}~X nX |jd|jdƒƒ}|sŠ|jdƒr†d}nd	}|d!kržtd| ƒ‚d|g dœ}||kr¾|| |d< �x,d"D �]"}|d |krÚqÆ|j|d |ƒ}|d	k� rød}|ddœ}	|dk�r||d  |	d< x0d#D ](}
||
 |k�r"|||
  |	|
jƒ < �q"W g }xDd$D ]<}|j|| ƒ}|�rXt|jdƒƒ�rX|j|||  ƒ �qXW |�rÜ||	d< |jdƒ}|�rÜd|k�rÐ|j	dƒ|	d< n|j	ƒ |	d< |d  j|	ƒ qÆW ||fS )%að  Convert a klibc written shell content file to a 'config' entry
    When ip= is seen on the kernel command line in debian initramfs
    and networking is brought up, ipconfig will populate
    /run/net-<name>.cfg.

    The files are shell style syntax, and examples are in the tests
    provided here.  There is no good documentation on this unfortunately.

    DEVICE=<name> is expected/required and PROTO should indicate if
    this is 'none' (static) or 'dhcp' or 'dhcp6' (LP: #1621507).
    note that IPV6PROTO is also written by newer code to address the
    possibility of both ipv4 and ipv6 getting addresses.

    Full syntax is documented at:
    https://git.kernel.org/pub/scm/libs/klibc/klibc.git/plain/usr/kinit/ipconfig/README.ipconfig
    NZDEVICEZDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataZPROTOZ	IPV6PROTOÚfilenameÚdhcpÚnoneÚdhcp6zUnexpected value for PROTO: %sZphysical)ÚtypeÚnameÚsubnetsÚmac_addressÚIPV4ÚIPV6ZADDRZstaticZmanual)r0   Zcontrolr   ÚNETMASKÚ	BROADCASTÚGATEWAYÚDNS0ÚDNS1z:.0Zdns_nameserversZDOMAINSEARCHú,Z
dns_searchr2   )r.   r-   r/   )r4   r5   )r6   r7   r8   )r9   r:   )
r   Zload_shell_contentÚKeyErrorÚ
ValueErrorÚgetÚlowerÚlenÚstripÚappendr"   )Zcontentr*   Údatar1   ÚeÚprotoZifaceZpreZ	cur_protoZsubnetÚkeyZdnsZnskeyÚnsÚsearchr   r   r
   Ú_klibc_to_config_entry^   sZ    








rI   c               C   s   t j dƒt j dƒ S )Nz/run/net-*.confz/run/net6-*.conf)Úglobr   r   r   r
   r   »   s    r   c             C   sÜ   | d krt ƒ } g }i }xº| D ]²}ttj|ƒ|d�\}}||kr´|| d }|jdƒ|jdƒkrŽtdj|dj|| d ƒ|jdƒ|jdƒd�ƒ‚|d j|d ƒ || d j	|ƒ q|g|d	œ||< |j	|ƒ qW |d
dœS )N)r*   Úentryr3   zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}.ú r)   )r1   r)   ÚoldÚnewr2   )r)   rK   é   )ÚconfigÚversion)
r   rI   r   Z	load_filer>   r=   ÚformatÚjoinÚextendrB   )r)   r*   ZentriesÚnamesZcfg_filer1   rK   Úprevr   r   r
   r+   ¿   s*    
r+   c              C   s(   x"t D ]} | ƒ }|jƒ sq|jƒ S W dS )a  
    Return v1 network config for initramfs-configured networking (or None)

    This will consider each _INITRAMFS_CONFIG_SOURCES entry in turn, and return
    v1 network configuration for the first one that is applicable.  If none are
    applicable, return None.
    N)Ú_INITRAMFS_CONFIG_SOURCESr   r   )Zsrc_clsZ
cfg_sourcer   r   r
   Úread_initramfs_configÞ   s    

rX   c             C   s^   t j| ƒ�J}d }z0ytjd|d�}|jƒ S  tk
r<   | S X W d |rN|jƒ  X W d Q R X d S )NÚrb)ÚmodeZfileobj)ÚioÚBytesIOÚgzipZGzipFileÚreadÚIOErrorÚclose)ÚblobZiobufZgzfpr   r   r
   Ú_decomp_gzipð   s    
rb   c             C   s<   yt j| ƒ}W n$ ttfk
r2   tjd| ƒ dS X t|ƒS )z¦Decode a string base64 encoding, if gzipped, uncompress as well

    :return: decompressed unencoded string of the data or empty string on
       unencoded data.
    z`Expected base64 encoded kernel commandline parameter network-config. Ignoring network-config=%s.Ú )Úbase64Z	b64decodeÚ	TypeErrorr=   ÚLOGÚerrorrb   )rC   ra   r   r   r
   Ú_b64dgzþ   s    rh   c             C   sn   | d krt jƒ } d| krjd }x*| jƒ D ]}|jdƒr&|jddƒd }q&W |rj|tkr\ddiS t jt|ƒƒS d S )Nznetwork-config=ú=rO   rP   r   )r   r   r"   r#   Ú&KERNEL_CMDLINE_NETWORK_CONFIG_DISABLEDZ	load_yamlrh   )ZcmdlineZdata64Útokr   r   r
   Úread_kernel_cmdline_config  s    
rl   )N)NN)N)r   rd   rJ   r]   r[   Zloggingr$   r!   Z	cloudinitr   Zcloudinit.netr   r   r'   rj   Z	getLoggerr   rf   ÚABCMetar   r   rW   rI   r   r+   rX   rb   rh   rl   r   r   r   r
   Ú<module>   s,   
4
]
