
    dB                     :   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Zd dl	Z	d dl
mc mZ d dlmc mZ d dlmZmZ d dlmZmZ d dlmc 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  G d d	e       Z! G d
 de!      Z"y)    N)ustr
bytebuffer)OSUtilError
CryptError)	CryptUtil)DefaultOSUtilc                   `     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Z xZS )Redhat6xOSUtilc                 8    t         t        |           d| _        y )NT)superr
   __init__jit_enabledself	__class__s    F/usr/lib/python3/dist-packages/azurelinuxagent/common/osutil/redhat.pyr   zRedhat6xOSUtil.__init__*   s    nd,.    c                 0    t        j                  dd      S )Nz/sbin/service networking startFchk_err	shellutilrunr   s    r   start_networkzRedhat6xOSUtil.start_network.       }}=uMMr   c                 0    t        j                  dd      S )Nz/sbin/service sshd condrestartFr   r   r   s    r   restart_ssh_servicez"Redhat6xOSUtil.restart_ssh_service1   r   r   c                 b    t        j                  dj                  | j                        d      S )Nz/sbin/service {0} stopFr   r   r   formatservice_namer   s    r   stop_agent_servicez!Redhat6xOSUtil.stop_agent_service4   s&    }}5<<T=N=NOY^__r   c                 b    t        j                  dj                  | j                        d      S )Nz/sbin/service {0} startFr   r    r   s    r   start_agent_servicez"Redhat6xOSUtil.start_agent_service7   s&    }}6==d>O>OPZ_``r   c                 b    t        j                  dj                  | j                        d      S )Nzchkconfig --add {0}Fr   r    r   s    r   register_agent_servicez%Redhat6xOSUtil.register_agent_service:   &    }}299$:K:KLV[\\r   c                 b    t        j                  dj                  | j                        d      S )Nzchkconfig --del {0}Fr   r    r   s    r   unregister_agent_servicez'Redhat6xOSUtil.unregister_agent_service=   r(   r   c                    t        j                  |      }	 t        t        j                               }|j                  |      }t        j                  ||       y # t        $ r}t        t        |            d }~ww xY wN)
fileutil	read_filer   confget_openssl_cmdasn1_to_sshr   r   r   append_file)r   
input_fileoutput_filepubkey	cryptutilssh_rsa_pubkeyes          r   openssl_to_opensshz!Redhat6xOSUtil.openssl_to_openssh@   sm    ##J/	'!$"6"6"89I&226:N 	[.9  	'd1g&&	's   .A 	A>%A99A>c                 (    | j                  ddg      S )Npidofdhclient)_get_dhcp_pidr   s    r   get_dhcp_pidzRedhat6xOSUtil.get_dhcp_pidJ   s    !!7J"788r   c                 z    t        j                  dddj                  |             | j                  d|gd       y)z,
        Set /etc/sysconfig/network
        z/etc/sysconfig/networkHOSTNAMEzHOSTNAME={0}hostnameF	log_errorN)r-   update_conf_filer!   _run_command_without_raising)r   rA   s     r   set_hostnamezRedhat6xOSUtil.set_hostnameM   sA     	!!":","0"7"7"A	C 	)):x*@E)Rr   c                     | j                         }dj                  |      }t        j                  |ddj                  |             y )Nz(/etc/sysconfig/network-scripts/ifcfg-{0}DHCP_HOSTNAMEzDHCP_HOSTNAME={0})get_if_namer!   r-   rD   )r   rA   ifnamefilepaths       r   set_dhcp_hostnamez Redhat6xOSUtil.set_dhcp_hostnameV   sB    !!#=DDVL!!("1"5"<"<X"F	Hr   c                 $    | j                  d      S )Nz#/var/lib/dhclient/dhclient-*.leasesget_endpoint_from_leases_pathr   s    r   get_dhcp_lease_endpointz&Redhat6xOSUtil.get_dhcp_lease_endpoint]   s    112WXXr   )__name__
__module____qualname__r   r   r   r#   r%   r'   r*   r9   r>   rF   rL   rP   __classcell__r   s   @r   r
   r
   (   sH     NN`a]]:9SHYr   r
   c                   V     e Zd Z fdZed        Zd Z fdZd Zd Z	d Z
d Z xZS )	RedhatOSUtilc                 T    t         t        |           | j                         | _        y r,   )r   rW   r   get_service_namer"   r   s    r   r   zRedhatOSUtil.__init__b   s!    lD*, 113r   c                       y)Nz/usr/lib/systemd/system r[   r   r   "get_systemd_unit_file_install_pathz/RedhatOSUtil.get_systemd_unit_file_install_pathf   s    (r   c                    dd|dg}	 t        j                  |d       y# t         j                  $ rL t        j                  dj                  dj                  |                   t        j                  | |       Y yw xY w)	z
        Unlike redhat 6.x, redhat 7.x will set hostname via hostnamectl
        Due to a bug in systemd in Centos-7.0, if this call fails, fallback
        to hostname.
        hostnamectlzset-hostnamez--staticFrB   z![{0}] failed, attempting fallback N)	r   run_commandCommandErrorloggerwarnr!   joinr   rF   )r   rA   hostnamectl_cmds      r   rF   zRedhatOSUtil.set_hostnamej   sm     ).(JO	7!!/UC%% 	7KK;BB388OC\]^&&tX6	7s     AA?>A?c                 V    t        j                  d       t        t        |   |       y)zI
        Restart NetworkManager first before publishing hostname
        zservice NetworkManager restartN)r   r   r   rW   publish_hostname)r   rA   r   s     r   rg   zRedhatOSUtil.publish_hostnamex   s!     	67lD28<r   c                 b    t        j                  dj                  | j                        d      S )Nzsystemctl enable {0}Fr   r    r   s    r   r'   z#RedhatOSUtil.register_agent_service   s&    }}3::4;L;LMW\]]r   c                 b    t        j                  dj                  | j                        d      S )Nzsystemctl disable {0}Fr   r    r   s    r   r*   z%RedhatOSUtil.unregister_agent_service   s&    }}4;;D<M<MNX]^^r   c                 2    t        j                  | ||       y r,   )r   r9   )r   r3   r4   s      r   r9   zRedhatOSUtil.openssl_to_openssh   s    ((z;Gr   c                 N    | j                  d      }|| j                  d      }|S )Nz"/var/lib/dhclient/dhclient-*.leasez(/var/lib/NetworkManager/dhclient-*.leaserN   )r   endpoints     r   rP   z$RedhatOSUtil.get_dhcp_lease_endpoint   s/    556Z[99:deHr   )rQ   rR   rS   r   staticmethodr\   rF   rg   r'   r*   r9   rP   rT   rU   s   @r   rW   rW   a   s=    4 ) )7=^_Hr   rW   c                   &     e Zd Z fdZddZ xZS )RedhatOSModernUtilc                 *    t         t        |           y r,   )r   ro   r   r   s    r   r   zRedhatOSModernUtil.__init__   s     $02r   c                 x   |dz   }t        d|      D ]  }t        j                  dj                  |            }|dk(  r yt	        j
                  dj                  ||             ||k  r:t	        j                  dj                  |             t        j                  |       t	        j
                  d        y)z
        Restart an interface by bouncing the link. systemd-networkd observes
        this event, and forces a renew of DHCP.
           z*ip link set {0} down && ip link set {0} upr   Nz&failed to restart {0}: return code {1}zretrying in {0} secondszexceeded restart retries)	ranger   r   r!   rb   rc   infotimesleep)r   rJ   retrieswaitretry_limitattemptreturn_codes          r   
restart_ifzRedhatOSModernUtil.restart_if   s    
 kQ, 		8G#--(T([([\b(cdKaKK@GGP[\]$5<<TBC

4 67		8r   )      )rQ   rR   rS   r   r|   rT   rU   s   @r   ro   ro      s    38r   ro   )#osrepwdshutilsocketarraystructfcntlru   base64azurelinuxagent.common.confcommonr/   azurelinuxagent.common.loggerrb   azurelinuxagent.common.futurer   r    azurelinuxagent.common.exceptionr   r   %azurelinuxagent.common.utils.fileutilutilsr-   &azurelinuxagent.common.utils.shellutilr   %azurelinuxagent.common.utils.textutiltextutil&azurelinuxagent.common.utils.cryptutilr   %azurelinuxagent.common.osutil.defaultr   r
   rW   ro   r[   r   r   <module>r      sw   & 
 	 
        * * . . : D 8 8 : : 8 8 < ?6Y] 6Yr/> /d8 8r   