
    }f$                        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e j$                        Z G d	 d
e      ZegZddZd ZddZd Zd Zd ZddZy)    N)util)get_devicelistread_sys_net_safez#/run/initramfs/open-iscsi.interfacedisabledc                   d    e Zd ZdZej
                  defd       Zej
                  defd       Z	y)InitramfsNetworkConfigSourcezBABC for net config sources that read config written by initramfsesreturnc                      y)zAIs this initramfs config source applicable to the current system?N selfs    7/usr/lib/python3/dist-packages/cloudinit/net/cmdline.pyis_applicablez*InitramfsNetworkConfigSource.is_applicable           c                      y)z;Render a v1 network config from the initramfs configurationNr   r   s    r   render_configz*InitramfsNetworkConfigSource.render_config"   r   r   N)
__name__
__module____qualname____doc__abcabstractmethodboolr   dictr   r   r   r   r   r      sK    LPt P P 	Jt J Jr   r   )	metaclassc                   0    e Zd ZdZddZdefdZdefdZy)KlibcNetworkConfigSourcezInitramfsNetworkConfigSource 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                 0   || _         || _        || _        | j                   t               | _         | j                  t	        j
                         | _        | j                  5i | _        t               D ]   }t        |d      }|s|| j                  |<   " y y )Naddress)_files
_mac_addrs_cmdline_get_klibc_net_cfg_filesr   get_cmdliner   r   )r   r!   r"   r#   kmac_addrs         r   __init__z!KlibcNetworkConfigSource.__init__/   s    $  ;;24DK==  ,,.DM??" DO#% 2,Q	:)1DOOA&2 #r   r	   c                     | j                   r[t        j                  | j                        D ]  }|j	                  d      s y t
        j                  j                  t              ryy)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
        )zip=zip6=TF)	r!   shlexsplitr#   
startswithospathexists_OPEN_ISCSI_INTERFACE_FILE)r   items     r   r   z&KlibcNetworkConfigSource.is_applicableA   sO     ;;DMM2  ???3  ww~~89r   c                 D    t        | j                  | j                        S )N)files	mac_addrs)config_from_klibc_net_cfgr!   r"   r   s    r   r   z&KlibcNetworkConfigSource.render_configT   s    (++oo
 	
r   )NNN)	r   r   r   r   r(   r   r   r   r   r   r   r   r   r   '   s$    2$t &
t 
r   r   c                    |i }t        j                  |       }	 d|v r|d   n|d   }|j	                  d|j	                  d            }|s|j	                  d      rd}nd	}|d
v rd	}|dvrt        d|z        d|g d}||v r||   |d<   dD ]  }|dz   |vr|j	                  |dz   |      }|d	k(  rd}|dd}	|dk(  r||dz      |	d<   dD ]#  }
||
z   |v s|||
z      |	|
j                         <   % g }dD ]K  }|j	                  ||z         }|st        |j                  d            s5|j                  |||z             M |rD||	d<   |j	                  d      }|r,d|v r|j                  d      |	d<   n|j                         |	d<   |d   j                  |	        ||fS # t        $ r}t        d      |d}~ww xY w)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) or 'static'
    or 'off' (LP: 2065787). Note that IPV6PROTO is also written 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
    NDEVICEDEVICE6z&no 'DEVICE' or 'DEVICE6' entry in dataPROTO	IPV6PROTOfilenamedhcpnone)staticoff)r=   r<   dhcp6zUnexpected value for PROTO: %sphysical)typenamesubnetsmac_address)IPV4IPV6ADDRr>   manual)rB   controlr    )NETMASK	BROADCASTGATEWAY)DNS0DNS1z:.0dns_nameserversDOMAINSEARCH,
dns_searchrD   )
r   load_shell_contentKeyError
ValueErrorgetlowerlenstripappendr+   )contentr4   datarC   eprotoifacepre	cur_protosubnetkeydnsnskeynssearchs                  r   _klibc_to_config_entryri   ^   s>   $ 	""7+DJ!)T!1tH~tI HHWdhh{34E88JEE!!--9EABB E y(m   '(<t# HHS7]E2	 I#9   $S6\ 2F9 7 	6CSyD &*39osyy{#	6 % 	.E#+&Bc"((5/*

4e,-		.
 (+F$%XXn-F&=+1<<+<F<(+1<<>F<(i'O'(R ;K  JABIJs   F& &	G /F;;G c                  X    t        j                   d      t        j                   d      z   S )Nz/run/net-*.confz/run/net6-*.conf)globr   r   r   r$   r$      s!    99&'$))4F*GGGr   c                    | 
t               } g }i }| D ]  }t        t        j                  |      |      \  }}||v r||   d   }|j	                  d      |j	                  d      k7  rQt        dj                  |dj                  ||   d         |j	                  d      |j	                  d                  |d   j                  |d          ||   d   j                  |       |g|d	||<   |j                  |        |d
dS )N)r4   entryrE   zedevice '{name}' was defined multiple times ({files}) but had differing mac addresses: {old} -> {new}. r3   )rC   r3   oldnewrD   )r3   rm      )configversion)
r$   ri   r   load_text_filerW   rV   formatjoinextendr[   )r3   r4   entriesnamescfg_filerC   rm   prevs           r   r5   r5      s%   }(*GE ",)Y
e 5=;w'Dxx&%))M*BB HHN!!hhuT{7';< HH]3!IIm4	 IO I  O""5#34$K ''1%-J?E$KNN5!)", !,,r   c                  l    t         D ]+  }  |        }|j                         s|j                         c S  y)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   )src_cls
cfg_sources     r   read_initramfs_configr      s=     - *Y
'')''))* r   c                 X   t        j                  |       5 }d }	 t        j                  d|      }|j	                         |r|j                          cd d d        S # t        $ r  | cY |r|j                          cd d d        S w xY w# |r|j                          w w xY w# 1 sw Y   y xY w)Nrb)modefileobj)ioBytesIOgzipGzipFilereadcloseIOError)blobiobufgzfps      r   _decomp_gzipr      s    	D	 	U	==dE:D99; 

	 	
  	K

	 	
	 

 	 	s@   B &A B B'B(B BBBB  B)c                     	 t        j                  |       }t        |      S # t        t        f$ r t        j                  d|        Y yw xY w)zDecode a string base64 encoding, if gzipped, uncompress as well

    :return: decompressed unencoded string of the data or empty string on
       unencoded data.
    zaExpected base64 encoded kernel command line parameter network-config. Ignoring network-config=%s. )base64	b64decode	TypeErrorrV   LOGerrorr   )r]   r   s     r   _b64dgzr     sS    %  z" 		;	

 s   " %A
	A
c                    | t        j                         } d| v rkd }| j                         D ])  }|j                  d      s|j                  dd      d   }+ |r+|t        k(  rddiS t        j
                  t        |            S y )Nznetwork-config==rq   rr   r   )r   r%   r+   r,   &KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED	load_yamlr   )cmdlinedata64toks      r   read_kernel_cmdline_configr     s    ""$G#==? 	.C~~/03*1-	. ?? *-->>'&/22r   )N)NN)r   r   rk   r   r   loggingr-   r*   	cloudinitr   cloudinit.netr   r   r0   r   	getLoggerr   r   ABCMetar   r   r}   ri   r$   r5   r   r   r   r   r   r   r   <module>r      s        	  	   ;B )3 &g!	JS[[ 	J1
; 1
h 66 ]@H->$&r   