
    p
f                         d dl Z d dlZd dlmc mZ d dlmc mc mZ d dlmc mc m	Z	 d dl
mZ d dlmZ  G d de      Zy)    N)DefaultOSUtil)NetworkInterfaceCardc                        e Zd Z fdZddZddZd ZddZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZddZ xZS )OpenWRTOSUtilc                     t         t        |           d| _        d| _        t        j                  d      | _        d| _        y )Nz/etc/waagent.confudhcpcz^\d+:\s+(\w+):\s+(.*)$T)	superr   __init__agent_conf_file_pathdhclient_namerecompileip_command_outputjit_enabled)self	__class__s    G/usr/lib/python3/dist-packages/azurelinuxagent/common/osutil/openwrt.pyr
   zOpenWRTOSUtil.__init__   s:    mT+-$7!%!#,E!F    c                 .    t        j                  d       y )Nz!eject is not supported on OpenWRT)loggerwarn)r   chk_errs     r   	eject_dvdzOpenWRTOSUtil.eject_dvd%   s    78r   c                 T   | j                  |      }|t        j                  d|       y|
dd|ddd|g}ndd|ddg}t        j                  j                  d      st        j                  d       ||j                  d	|g       | j                  |d
j                  |             y)z5
        Create user account with 'username'
        Nz%User {0} already exists, skip useradduseraddz-mz-sz/bin/ashz-ez/homez-cz!Failed to create user account:{0})err_msg)
get_userentryr   infoospathexistsmkdirextend _run_command_raising_OSUtilErrorformat)r   username
expirationcomment	userentrycmds         r   r   zOpenWRTOSUtil.useradd(   s     &&x0	 KK?J!dHdJjQCdHdJ?Cww~~g&HHWJJg'--c;^;e;efn;o-pr   c                 <    | j                  d| j                  g      S )Npidof)_get_dhcp_pidr   r   s    r   get_dhcp_pidzOpenWRTOSUtil.get_dhcp_pid=   s    !!7D,>,>"?@@r   c                    |ryi }t        j                  ddd      \  }}|dk7  r't        j                  dj	                  ||             i S |j                         D ]O  }| j                  j                  |      }|s!|j                  d      }t        ||j                  d            ||<   Q | j                  |d	t        j                  d
       | j                  |dt        j                  d       |S )z
        Capture NIC state (IPv4 and IPv6 addresses plus link state).

        :return: Dictionary of NIC state objects, with the NIC name as key
        :rtype: dict(str,NetworkInformationCard)
         z
ip -o linkF)r   log_cmdr   z.Could not fetch NIC link info; status {0}, {1}      zip -o -f inet addresszan IPv4 addresszip -o -f inet6 addresszan IPv6 address)	shellutilrun_get_outputr   verboser%   
splitlinesr   matchgroupr   _update_nic_stateadd_ipv4add_ipv6)r   	as_stringstatestatusoutputentryresultnames           r   get_nic_statezOpenWRTOSUtil.get_nic_state@   s     "11,W\]Q;NNKRRSY[abcI&&( 	JE++11%8F||A24aId		J 	u&=?S?\?\^opu&>@T@]@]_pqr   c                 V   t        j                  |d      \  }}|dk7  ry|j                         D ]v  }| j                  j	                  |      }|s!|j                  d      }	|	|v r |||	   |j                  d             Rt        j                  dj                  |	|             x y)a  
        Update the state of NICs based on the output of a specified ip subcommand.

        :param dict(str, NetworkInterfaceCard) state: Dictionary of NIC state objects
        :param str ip_command: The ip command to run
        :param handler: A method on the NetworkInterfaceCard class
        :param str description: Description of the particular information being added to the state
        Tr   r   Nr3   r4   z'Interface {0} has {1} but no link state)	r5   r6   r8   r   r9   r:   r   errorr%   )
r   r?   
ip_commandhandlerdescriptionr@   rA   rB   rC   interface_names
             r   r;   zOpenWRTOSUtil._update_nic_state]   s     #11*dKQ;&&( 	pE++11%8F!'a!U*E.16<<?CLL!J!Q!QR`bm!no	pr   c                      y N r.   s    r   is_dhcp_enabledzOpenWRTOSUtil.is_dhcp_enableds       r   c                      y rN   rO   r.   s    r   start_dhcp_servicez OpenWRTOSUtil.start_dhcp_servicev   rQ   r   c                      y rN   rO   r.   s    r   stop_dhcp_servicezOpenWRTOSUtil.stop_dhcp_servicey   rQ   r   c                 0    t        j                  dd      S )Nz/etc/init.d/network startTrG   )r5   runr.   s    r   start_networkzOpenWRTOSUtil.start_network|   s    }}8$GGr   c                     t         j                  j                  d      rt        j                  dd      S t        j                  d       y )Nz/etc/init.d/sshdz/etc/init.d/sshd restartTrG   zsshd service does not exists)r   r    r!   r5   rW   r   r   r.   s    r   restart_ssh_servicez!OpenWRTOSUtil.restart_ssh_service   s2    77>>,-==!;TJJKK67r   c                 b    t        j                  dj                  | j                        d      S )Nz/etc/init.d/{0} stopTrG   r5   rW   r%   service_namer.   s    r   stop_agent_servicez OpenWRTOSUtil.stop_agent_service   s&    }}3::4;L;LMW[\\r   c                 b    t        j                  dj                  | j                        d      S )Nz/etc/init.d/{0} startTrG   r\   r.   s    r   start_agent_servicez!OpenWRTOSUtil.start_agent_service   s&    }}4;;D<M<MNX\]]r   c                 b    t        j                  dj                  | j                        d      S )Nz/etc/init.d/{0} enableTrG   r\   r.   s    r   register_agent_servicez$OpenWRTOSUtil.register_agent_service   s&    }}5<<T=N=NOY]^^r   c                 b    t        j                  dj                  | j                        d      S )Nz/etc/init.d/{0} disableTrG   r\   r.   s    r   unregister_agent_servicez&OpenWRTOSUtil.unregister_agent_service   s&    }}6==d>O>OPZ^__r   c                     t        j                  d|       dddj                  |      gg dddgg}| j                  |dd	       y )
Nz/etc/hostnameucisetzsystem.@system[0].hostname={0})rf   commitsystemz/etc/init.d/systemreloadF)	log_errorcontinue_on_error)fileutil
write_filer%   &_run_multiple_commands_without_raising)r   hostnamecommandss      r   set_hostnamezOpenWRTOSUtil.set_hostname   sP    OX6E#C#J#J8#TUWr)84633Haf3gr   c                      y rN   rO   )r   rules_filess     r   remove_rules_filesz OpenWRTOSUtil.remove_rules_files   rQ   r   )T)NN)F)r1   )__name__
__module____qualname__r
   r   r   r/   rE   r;   rP   rS   rU   rX   rZ   r^   r`   rb   rd   rr   ru   __classcell__)r   s   @r   r   r      sa     9q*A:p,H8]^_`hr   r   )r   r   azurelinuxagent.common.loggercommonr   &azurelinuxagent.common.utils.shellutilutilsr5   %azurelinuxagent.common.utils.fileutilrm   %azurelinuxagent.common.osutil.defaultr   (azurelinuxagent.common.utils.networkutilr   r   rO   r   r   <module>r      s3   ( 
 	 . . : : 8 8 ? I}M }r   