HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/nilrt_ip.cpython-310.pyc
o

�N�g��@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Zzddl
Z
Wney3dZ
YnwzddlZWneyEdZYnwzddl
Z
ddlmZmZWney_dZ
YnwzddlmZWneysdZYnwe�e�ZdZdZdZdZd	Zgd
�ZdZdd
�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�Z&d d!�Z'd"d#�Z(d$efd%d&�Z)d'd(�Z*d)d*�Z+d+d,�Z,d-d.�Z-d/d0�Z.d1d2�Z/d3d4�Z0d5d6�Z1d7d8�Z2d9d:�Z3d;d<�Z4d=d>�Z5d?efd@dA�Z6dBdC�Z7dfdDdE�Z8dFdG�Z9dfdHdI�Z:dJdK�Z;dLdM�Z<dNdO�Z=dPdQ�Z>dRdS�Z?dTdU�Z@dVdW�ZAdXdY�ZBdfdZd[�ZCd\d]�ZDd^d_�ZEd`da�ZFdbdc�ZGddde�ZHdS)gz6
The networking module for NI Linux Real-Time distro

�N)�IFF_LOOPBACK�IFF_RUNNING)�CaseInsensitiveDict�ipz/net/connman/service/z"/var/lib/connman/interfaces.configz/usr/local/natinst/bin/nirtcfgz/etc/natinst/share/ni-rt.ini)�yes�on�true�1TZEtherCATcCs|st|��dS)z6
    Raise an exception if the condition is false
    N)�RuntimeError)Z	condition�err�r�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/nilrt_ip.py�_assume_condition2s�rc
Cs�z3d}ttddk|�ttd�ttd�tddkr1ttd�ttd	�tt�d
kd�Wt	SWt	StyK}zdt|�fWYd
}~Sd
}~ww)zA
    Confine this module to NI Linux Real-Time based distros
    z>The nilrt_ip module could not be loaded: unsupported OS familyZ	os_familyZ	NILinuxRTz+The python package request is not installedz+The python pyiface package is not installed�lsb_distrib_id�nilrtz-The python package pyconnman is not installedz(The python DBus package is not installed�offlinezConnman is not runningFN)
r�
__grains__r�pyiface�	pyconnman�dbus�
_get_stater
�str�__virtualname__)�msg�excrrr
�__virtual__:s(�
�
����rc
CsPzt���d�WStyYdStjy'}z	tj�d|����d}~ww)z&
    Returns the state of connman
    �StaterzConnman daemon error: N)	r�ConnManager�get_property�KeyErrorrZ
DBusException�salt�
exceptions�CommandExecutionError)rrrr
rPs��rc
CsPd}t����}|D]\}}|d�||d|d|ddk|ddk�7}q
|S)z-
    Returns the technologies of connman
    �z7{}
	Name = {}
	Type = {}
	Powered = {}
	Connected = {}
�Name�TypeZPowered�Z	Connected)rrZget_technologies�format)ZtechZtechnologies�path�paramsrrr
�_get_technologies\s

��	r*cCs<g}t����}|D]\}}|�t|tt�d���q
|S)z2
    Returns a list with all connman services
    N)rrZget_services�appendr�len�SERVICE_PATH)ZservZservicesr(�_rrr
�
_get_servicesos
r/cCs*t�tj�t|���d�}|dkp|dkS)z2
    Verify if a connman service is connected
    r�online�ready)r�ConnService�osr(�joinr-r)�service�staterrr
�
_connectedzs�r7cCsJt|t�r|�d�}|ot|�}n	t|d�o|gk}|rdSd|�d�fS)zK
    validate that a value contains one or more space-delimited values
    � �__iter__)Tzspace-delimited stringFz is not a valid list.
)�
isinstancer�split�all�hasattr)�value�itemsZvalidrrr
�_space_delimited_list�s

r@cCs�t|�dkrJtjjj�|d���sdd|d�d�fStjjj�|d���s2dd|d�d�fStjjj�|d���sHdd	|d�d�fSdSdd
|�d�fS)z
    validate ipv4 values
    �rFzInvalid ip address: z for ipv4 optionr&zInvalid netmask: �zInvalid gateway: zInvalid value: )Tr#)r,r �utils�validate�netZ	ipv4_addr�strip�netmask�r>rrr
�_validate_ipv4�s�rIcCs>t�D]}t�tj�t|��}|�d�d|kr|SqdS)z_
    returns the corresponding service to given interface if exists, otherwise return None
    �Ethernet�	InterfaceN)r/rr2r3r(r4r-r)�ifaceZ_service�service_inforrr
�_interface_to_service�s
�rNcCs�t�tj�t|��}||�d�dkt|�d�d�t|�d�d�d�}|�d�}|dks1|d	kr�d
|d<dd
i|d<d}|�d�ddkrJ|d7}|�|�}dD]0}|�|�}|durdt	�
d||�qQ|dkrwd}|dkrqd}n|dvrwd}t|�|d|��<qQ|�d�}dD]}|�|�}|dur�t	�
d||�q�t|�g|d|��<q�g}	|�d�D]	}
|	�t|
��q�|	|dd <n
d!|d<dd"i|d<gd#�|dd$<|S)%z4
    return details about given connman service
    r%ZwifirJrK�Address)�label�wireless�connectionid�hwaddrrr1r0T�up�gateway�0.0.0.0�ipv4�IPv4�Method�manualz.Configuration)rYrO�Netmask�GatewayNz%Unable to get IPv4 %s for service %s
�requestmode�dhcp�dhcp_linklocal)rZ�fixed�staticZIPv6)rOZPrefixr\z%Unable to get IPv6 %s for service %s
Zipv6�Nameservers�dnsF�disabled�r_rdra�supportedrequestmodes)
rr2r3r(r4r-rr�get�log�warning�lowerr+)r5rM�datar6rWZ	ipv4_info�infor>Z	ipv6_info�nameserversZnameserver_proprrr
�_get_service_info�sR�




rncCs�g}z;tjj�dd��(}|��}|D]}d|vr)|��d��}||vr)|�|�qWd�W|S1s6wYW|StyLt	�
d�Y|Sw)z
    return dns list
    z/etc/resolv.confzr+Z
nameserverr&NzCould not get domain
)r rC�files�fopen�	readlinesr;rFr+�OSErrorrhri)Zdns_listZdns_info�lines�linercrrr
�
_get_dns_info�s&
���	�	��rucCs>t|�dkr|d|dkrdkrn|S|dd�}|S)zB
    Remove leading and trailing double quotes if they exist.
    r&r����")r,rHrrr
�_remove_quotes�s*�rxr#cCs|i}|s|Stjj�|d��&}tjtdtid�}|�|�|D]}|j	|||d�||<qWd�|S1s7wY|S)a;
    Get values for some options and a given section from a config file.

    :param section: Section Name
    :param options: List of options
    :param default_value: Default value if an option doesn't have a value. Default is empty string.
    :param filename: config file. Default is INI_FILE.
    :return:
    �rZunquoted)Z	dict_typeZ
converters)�fallbackN)
r rCrorp�configparserZRawConfigParserrrx�readfpZgetunquoted)�section�optionsZ
default_value�filename�results�config_fileZ
config_parser�optionrrr
�_load_config�s"
�
��
�
�
r�cCs\t|ddgd�}t|d�}t|d�}|dkr |dkrdSdS|dkr&dS|dkr,d	SdS)
z0
    return requestmode for given interface
    �linklocalenabled�dhcpenabledrvr&r_�	dhcp_only�linklocal_onlyrra)r��int)�	interface�settingsZlink_local_enabledZdhcp_enabledrrr
�_get_request_mode_infosr�cCs$t|dg�d��}|dvr|SdS)z0
    return adaptermode for given interface
    �mode)rd�ethercat�tcpip)r�rj)r�r�rrr
�_get_adapter_mode_info/sr�c	sg}tddg�d��}t�d|����tjj�d|�d���}|��}Wd�n1s.wYd�|D]}|�	d�rI|�
d�d	���nq7�D]1}|d
krSqL��|i�}|�d�rc||vrcqL||dvr}t
���fd
d�|dD��s}||g7}qL|S)z�
    Return possible adapter modes for a given interface using a blacklist.

    :param interface: interface name
    :param blacklist: given blacklist
    :return: list of possible adapter modes
    ZlvrtZAdditionalNetworkProtocolsz/sys/class/net/z/ueventNr#zDEVTYPE=�=r&r.�additional_protocol�namec3s@�|]}�d|dkr|�vp�d|dko|�kVqdS)r.�sys�ueventNr)�.0�
iface_type��	blacklistZsys_interface_pathZuevent_devtyperr
�	<genexpr>Ss��
�z._get_possible_adapter_modes.<locals>.<genexpr>�type)r�rjr3�readlinkr rCrorprq�
startswithr;rFrg�any)	r�r�Z
adapter_modesZ	protocolsZuevent_fileZuevent_linesrt�adapter_moder>rr�r
�_get_possible_adapter_modes7s8
��
�
��
�r�cCs|j|j|jdd�dgd�dd�dd�}d�|d	�d
��}tj�t�r�td|gd�td
�}|ddkrv|ddkrv|d�d�}|d|dd<|ddkrSdn|d�d�|dd<|d|dd<|d|dd<d|dd<|S|ddkr�d |dd<|S)!zh
    Return information about an interface from config file.

    :param interface: interface label
    NrvFrer_�rfr])rRrPrSrTrWrQr#rS�:�service_)rXrb�IPv4.method)rr�rZrX�/rrW�addressrb�''�,rcr&rGrBrUrar]�offrd)	r�rSr4r;r3r(�exists�INTERFACES_CONFIGr�)r�rk�hwaddr_section_numberZinformationZipv4_informationrrr
�_get_static_info\s<����
��r�c	Cs�ggdd�dgdgdd�dggd�dd�dddd	�d
�}|j|jt|j|�t|j�|jt@dkgd�t|j�d
�|jdd�d�S)z3
    return base details about given interface
    F)r�r�r�Zeth0�gadget)r��usb�wlanTr�r�)r�r�r�)r�rdr�r.r)r_r�r�rar�Nrv)rPrRZsupported_adapter_modesr�rTrWrS)r�r�r��flagsrr�rS)r�r�rrr
�_get_base_interface_info�s.
��
���	�r�cCs&t|�}dt|jdg�di|d<|S)z7
    return details about given ethercat interface
    Zmasterid�MasterIDr�)r�r�r�)r��base_informationrrr
�_get_ethercat_interface_info�s�r�cs`t|�}|dddkr7t|jgd��}|d|dd<|d|dd<|d	|dd
<|dg|dd<|S|d
r�|�|j�|dd<|�|j�|dd<d|dd
<t�|dd<tjj	�
dd��$}t�dj
|jd�tj�}|�|���}|s|dn|�d��Wd�n1s�wY�dur�t��dkr�d��fdd�tddd�D��|dd
<|S)z4
    return details about given tcpip interface
    rWr]ra)�
IP_Address�Subnet_Maskr\�DNS_Addressr�r�r�rGr\rUr�rcrTrVz/proc/net/routeryz'^{interface}\t[0]{{8}}\t([0-9A-Z]{{8}})�r�Nr&��.cs&g|]}tt�||d�d���qS)rB�)rr�)r��i�Ziface_gateway_hexrr
�
<listcomp>�s&z-_get_tcpip_interface_info.<locals>.<listcomp>�rv���)r�r�r�Z
sockaddrToStr�addrrGrur rCrorp�re�compiler'�	MULTILINE�search�read�groupr,r4�range)r�r�r�Z
route_file�pattern�matchrr�r
�_get_tcpip_interface_info�s<
�����	�r�cCs2t|j�}|dkr
t|�S|dkrt|�St|�S)z.
    return details about given interface
    rdr�)r�r�r�r�r�)r�r�rrr
�_get_interface_info�s
r�cCs�d}t|���D]G\}}t|t�r%t|�D]}|t|�d|d7}qqt|t�rAd�dd�|D��}|t|�d|d7}q|t|�dt|�d7}q|��S)z=
    converts a dictionary object into a list of strings
    r#�-�
r8cSsg|]}t|��qSr)r)r��itemrrr
r��sz#_dict_to_string.<locals>.<listcomp>z: )	�sortedr?r:�dict�_dict_to_stringr�listr4�
splitlines)Z
dictionary�ret�key�valrt�textrrr
r��s
�
r�cCs"t|j�}|dur
t|�St|�S)z�
    Return information about an interface even if it's not associated with a service.

    :param interface: interface label
    N)rNr�rnr�)r�r5rrr
�	_get_info�s
r�cCsBdd�t��D�}tddkrdttt|��iSdttt|��iS)z�
    Get details about all the interfaces on the minion

    :return: information about all interfaces omitting loopback
    :rtype: dictionary

    CLI Example:

    .. code-block:: bash

        salt '*' ip.get_interfaces_details
    cSsg|]}|jt@dkr|�qS)r)r�r)r�r�rrr
r�s
�z*get_interfaces_details.<locals>.<listcomp>rr�
interfaces)r�	getIfacesrr��mapr�r�)�_interfacesrrr
�get_interfaces_detailss
�r�cCs�t|�}t|d|dkr
dnd�|dkrtd�dStdd|�d	|���}|d
dkrBd�|dkr5d
nd||d�}tj�|��dS)aQ
    Enable or disable an interface on a legacy distro

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param interface: interface label
    :param new_state: up or down
    :return: True if the service was enabled, otherwise an exception will be thrown.
    :rtype: bool
    �ModerT�TCPIPZDisabledr��$system.set_reboot_required_witnessed�cmd.run_allzip link set r8�retcoderz#Couldn't {} interface {}. Error: {}�enable�disable�stderrT)r��_save_config�__salt__r'r r!r")r��	new_state�initial_mode�outrrrr
�_change_state_legacys
��r�TcCs�t��}t|_tj�|�r7ztjj	�
|d��
}|�|�Wd�n1s&wYWn
tjy6Ynwt
j|d�}|jdd�}d�|�d��}|�d|���r\|�d|���|�d|���|�d|��d|�|�d|��d	d
|���|�d|��dd�|r�|�d|��d
d�|�d|��dd�|�d|��dd�n
|�d|��dd�tjj	�
|d��}|�|�Wd�dS1s�wYdS)a
    Enable or disable dhcp for an interface which isn't a service (in a config file)

    :param interface: interface label
    :param enable_dhcp: True to enable dhcp and False to disable dhcp. Default is True
    :param filename: Config file name. Default is INTERFACES_CONFIG.
    ryN�r�rvr#r�r��MACr$�ethernet_cable_r%�ethernetzIPv4.Methodr^ZAutoConnectrrbr�rXr��wT)r{�ConfigParserr�optionxformr3r(r�r rCrorpr|�MissingSectionHeaderErrorrrKrSr4r;�has_section�remove_section�add_section�set�write)r�Zenable_dhcpr�parserr�rSr�rrr
�_change_dhcp_config2sF����
��r�cCsXtddkrt||�S|dd�t��D�vr#|dkrt|�St|d�Stj�d|����)a>
    Enable or disable an interface

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param interface: interface label
    :param new_state: up or down
    :return: True if the service was enabled, otherwise an exception will be thrown.
    :rtype: bool
    rrcS�g|]}|j�qSrr��r��xrrr
r�g�z!_change_state.<locals>.<listcomp>rTF�Invalid interface name: )rr�rr�r�r r!r")r�r�rrr
�
_change_stateZs
���rcC�
t|d�S)au
    Enable the specified interface

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the service was enabled, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.up interface-label
    rT�r�r�r�rrr
rTp�
rTcC�t|�S)ay
    Enable the specified interface

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the service was enabled, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.enable interface-label
    )rTr�rrr
r���r�cCr)a{
    Disable the specified interface

    Change adapter mode to Disabled. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the service was disabled, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.down interface-label
    �downrrrrr
r�rrcCr)a~
    Disable the specified interface

    Change adapter mode to Disabled. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the service was disabled, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.disable interface-label
    )rr�rrr
r��rr�cCs\t}|d|�d|�d|�d�7}td|�ddkr,d|�d	|�d
|�d�}tj�|��dS)
zD
    Helper function to persist a configuration in the ini file
    z --set section=z,token='z	',value='�'r�r�rzError: could not set z to z for r�N)�NIRTCFG_PATHr�r r!r")r}�tokenr>�cmd�exc_msgrrr
r��s�r�cCsNtddkr!t|�}t|dt�t|d|�|dkrtd�dStj�d��)	a�
    Configure specified adapter to use EtherCAT adapter mode. If successful, the target will need reboot if it doesn't
    already use EtherCAT adapter mode, otherwise will return true.

    :param interface: interface label
    :param master_id: EtherCAT Master ID
    :return: True if the settings were applied, otherwise an exception will be thrown.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.set_ethercat interface-label master-id
    rrr�r�r�r�TzEtherCAT is not supported)rr�r��NIRTCFG_ETHERCATr�r r!r")r�Z	master_idr�rrr
�set_ethercat�s
rcCst|�t|�dS)z)
    Disable and enable an interface
    N)r�r�r�rrr
�_restart�srcCs�tddkr-t|�}t|dd�t|dd�t|dd�|dkr'td	�d
St|�d
S|dd�t��D�vr<t|�Stj	�
d
|����)a�
    Configure specified adapter to use DHCP with linklocal fallback

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the settings were applied, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.set_dhcp_linklocal_all interface-label
    rrr�r�r�r	r�r�r�TcSr�rr�r�rrr
r�r�z*set_dhcp_linklocal_all.<locals>.<listcomp>r)rr�r�r�rrr�r�r r!r"�r�r�rrr
�set_dhcp_linklocal_all�s
�rcC�ftddkstj�d��t|�}t|dd�t|dd�t|dd	�|d
kr-td�dSt|�dS)
a�
    Configure specified adapter to use DHCP only

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the settings were applied, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.dhcp_only_all interface-label
    rr�Not supported in this versionr�r�r�r	r��0r�r�T�rr r!r"r�r�r�rrrrr
�set_dhcp_only_all�
�rcCr)
a�
    Configure specified adapter to use linklocal only

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :return: True if the settings were applied, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.linklocal_only_all interface-label
    rrrr�r�r�rr�r	r�r�Trrrrr
�set_linklocal_only_all$rrc	Ks�tj|d�}t��}t|_tj�t	�r=zt
jj�
t	d��
}|�|�Wd�n1s,wYWn
tjy<Ynw|jdd�}d�|�d��}|�d|���r\|�d|���|�d|���|�dd	�}|�d
d	�}|�dd	�}|�dd
�}	|�dd|���}
|�d|��d|�d|�d|���|�d|��d|	�|�d|��d|
�|�d|��d|�|�d|��dd�|�d|��dd�t
jj�
t	d��}|�|�Wd�dS1s�wYdS)aI
    Configure an interface that is not detected as a service by Connman (i.e. link is down)

    :param interface: interface label
    :param settings:
            - ip
            - netmask
            - gateway
            - dns
            - name
    :return: True if settings were applied successfully.
    :rtype: bool
    r�ryNrvr#r�r�rrVrGrUrcr�r�r�rXr�rbr$r�r%r�r�rZr�T)rrKr{r�rr�r3r(r�r�r rCrorpr|r�rSr4r;r�r�r�rgr�r�)r�r�r�r�rSr��
ip_addressrGrUZdns_serversr�rrr
�_configure_static_interfaceAsL����
��rc
Cst|||g�\}}|stj�|��|r+t|�\}}|s!tj�|��t|t�s+|�d�}tddkrtt	|�}t
|dd�t
|dd�t
|dd�t
|d	|�t
|d
|�t
|d|�|rct
|d|d
�|dkrntd�dSt|�dSt
|�}|s�|dd�t��D�vr�t|fi||r�d�|�nd||d���Stj�d|����t�tj�t|��}|�d�}	tjddd�|	d<tj|�dd�|	d<tj|�dd�|	d<tj|�dd�|	d<z|�d|	�|r�|�ddd�|D��WdSWdSt�y
}
zd �||
�}tj�|��d!}
~
ww)"a�
    Configure specified adapter to use ipv4 manual settings

    Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot.

    :param str interface: interface label
    :param str address: ipv4 address
    :param str netmask: ipv4 netmask
    :param str gateway: ipv4 gateway
    :param str nameservers: list of nameservers servers separated by spaces (Optional)
    :return: True if the settings were applied, otherwise an exception will be thrown.
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' ip.set_static_all interface-label address netmask gateway nameservers
    r8rrr�r�r�rr�r�r�r\r�rr�r�TcSr�rr�r�rrr
r��r�z"set_static_all.<locals>.<listcomp>r�r#)rrcrGrUrzIPv4.ConfigurationrZr&)Z
variant_levelrYrOr[zNameservers.ConfigurationcSsg|]}t�|���qSr)r�String)r��drrr
r��sz7Couldn't set manual settings for service: {}
Error: {}
N)rIr r!r"r@r:r�r;rr�r�r�rrNrr�rr4rr2r3r(r-rrrZset_property�	Exceptionr')r�r�rGrUrmrDrr�r5rWrr
rrr
�set_static_allqs|


����	�
�	�
����rcCs0t�}|dD]}|d|krt|�SqdS)z�
    Returns details about given interface.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.get_interface eth0
    r�rRN)r�r�)rLr�Z
_interfacerrr
�
get_interface�s
�r cKs�tddkrtj�d��|dkrtj�d|�d���d|vs$|ddkr)t|�n>|dd	kr<d
�|d�}tj�|��|d}|d}|d
}g}|��D]\}	}
d|	vsZd|	vr^||
7}qNt|||||�|rmt|�t	|�S)z�
    Build an interface script for a network interface.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.build_interface eth0 eth <settings>
    rr�Not supported in this version.ZethzInterface type not supported: r��protor^raz"Protocol type: {} is not supportedZipaddrrGrUrc�domain)
rr r!r"rr'r?rrTr )rLr��enabledr�r
r�rGrUrcr�r�rrr
�build_interface�s.

�
�r%cKs�tddkrtj�d��g}d|vr%|dtvrtdd�ntdd�d|vrM|d�d	d
�d}||d<td}||krMtd
|�|�d|���|S)z�
    Build the global network script.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.build_network_settings <settings>
    rrr!�
networkingzservice.enable�connmanzservice.disable�hostnamer�r&r�network.get_hostname�network.mod_hostname�	hostname=)rr r!r"�_CONFIG_TRUEr�r;r+)r�ZchangesZnew_hostnameZold_hostnamerrr
�build_network_settings�s
r-cCsZtddkrtj�d��g}t�dkrdnd}|�d|���td}|�d	|���|S)
z�
    Return the contents of the global network script.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.get_network_settings
    rrr!r�norznetworking=r)r+)rr r!r"rr+r�)r�r&r(rrr
�get_network_settingss
r/cKs�tddkrtj�d��d|vrd|d<d|vrd|d<d}|dtvr8d|vr1td	|d�}nt�d
�d}d}|dtvrHt�d�d}ntdd
�}t�	d�|oZtdd
�}|o^|S)z�
    Apply global network configuration.

    CLI Example:

    .. code-block:: bash

        salt '*' ip.apply_network_settings
    rrr!Zrequire_rebootFZapply_hostnameTr(r*zUThe network state sls is trying to apply hostname changes but no hostname is defined.zbThe network state sls is requiring a reboot of the system to properly apply network configuration.zservice.stopr'rBz
service.start)
rr r!r"r,r�rhri�time�sleep)r�Zhostname_res�res�stoprrr
�apply_network_settings-s0
��
r4)N)I�__doc__r{�loggingr3r�r0Zsalt.exceptionsr Zsalt.utils.filesZsalt.utils.validate.netr�ImportErrorrrZpyiface.ifreqioctlsrrZrequests.structuresr�	getLogger�__name__rhrr-r�r
ZINI_FILEr,rrrrr*r/r7r@rIrNrnrurxr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrTr�rr�r�rrrrrrrr r%r-r/r4rrrr
�<module>s�����

5
%)'"(


0Q(