
    p
f<&                        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
 d dlmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ  e j8                  d
e j:                         e j8                  de j:                         e j8                  de j:                        gZdZd Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d de      Z'y)    N)get_dhcp_handler)add_periodicWALAEventOperation	add_event)ustr)ThreadHandlerInterface)
get_osutil)get_protocol_util)
AGENT_NAMECURRENT_VERSION)PeriodicOperationz^(.*)\.(\d+)\.(agentsManifest)$z^(.*)\.(\d+)\.(manifest\.xml)$z^(.*)\.(\d+)\.(xml)$2   c                      t               S N)
EnvHandler     8/usr/lib/python3/dist-packages/azurelinuxagent/ga/env.pyget_env_handlerr   .   s
    <r   c                   $     e Zd Z fdZd Z xZS )RemovePersistentNetworkRulesc                 ^    t         t        |   t        j                                || _        y r   )superr   __init__conf&get_remove_persistent_net_rules_periodosutilselfr   	__class__s     r   r   z%RemovePersistentNetworkRules.__init__3   s#    *D:4;f;f;hir   c                 8    | j                   j                          y r   )r   remove_rules_filesr   s    r   
_operationz'RemovePersistentNetworkRules._operation7   s    &&(r   __name__
__module____qualname__r   r$   __classcell__r    s   @r   r   r   2   s    )r   r   c                   *     e Zd Z fdZd Zd Z xZS )MonitorDhcpClientRestartc                     t         t        |   t        j                                || _        t               | _        | j                  j                          d| _	        g | _
        y NT)r   r,   r   r   &get_monitor_dhcp_client_restart_periodr   r   dhcp_handlerconf_routesdhcp_warning_enableddhcp_id_listr   s     r   r   z!MonitorDhcpClientRestart.__init__<   sP    &6t7b7b7de,.%%'$(!r   c                 j    t         j                        dk(  r j                          _        y t         fd j                  D              ry  j                         }t        |      dk7  rG| j                  k7  r7t	        j
                  d        j                  j                          | _        y y y )Nr   c              3   T   K   | ]  }j                   j                  |       ! y wr   )r   check_pid_alive).0pidr   s     r   	<genexpr>z6MonitorDhcpClientRestart._operation.<locals>.<genexpr>I   s      MCt{{**3/Ms   %(zBEnvMonitor: Detected dhcp client restart. Restoring routing table.)lenr3   _get_dhcp_client_pidallloggerinfor0   r1   )r   new_pids   ` r   r$   z#MonitorDhcpClientRestart._operationD   s    t  !Q& $ 9 9 ;DM4;L;LMM++-w<1D,=,=!=KK\]))+ 'D ">r   c                 X   g }	 t        | j                  j                               }t        |      dk(  r!| j                  rt        j                  d       t        |      dk7  | _        |S # t        $ r5}| j                  rt        j                  dt        |             Y d }~Nd }~ww xY w)Nr   zDhcp client is not running.z-Failed to get the PID of the DHCP client: {0})
sortedr   get_dhcp_pidr:   r2   r=   warn	Exceptionerrorr   )r   r8   	exceptions      r   r;   z-MonitorDhcpClientRestart._get_dhcp_client_pidR   s    		_ 1134C3x1}!:!:9:
 %(HM!
  	_((LdS\o^	_s   AA+ +	B)4+B$$B))r&   r'   r(   r   r$   r;   r)   r*   s   @r   r,   r,   ;   s    (r   r,   c                   $     e Zd Z fdZd Z xZS )EnableFirewallc                 z    t         t        |   t        j                                || _        || _        d| _        y )NF)r   rH   r   r   get_enable_firewall_period_osutil	_protocol _try_remove_legacy_firewall_rule)r   r   protocolr    s      r   r   zEnableFirewall.__init__f   s1    nd,T-L-L-NO!05-r   c                 \   | j                   s;| j                  j                  | j                  j	                                d| _         | j                  j                  | j                  j	                         t        j                               \  }}|rddj                  | j                  j                               }t        j                  |       t        t        t        t        j                   |d       t#        t        j$                  t        t        t        j                   |d       y )N)dst_ipT)rP   uidzKSuccessfully added Azure fabric firewall rules. Current Firewall rules:
{0}F)versionopmessage	log_event)rR   rS   
is_successrU   )rM   rK   remove_legacy_firewall_rulerL   get_endpointenable_firewallosgetuidformatget_firewall_listr=   r>   r   r   r   r   Firewallr   
EVERY_HOUR)r   successis_firewall_rules_updatedmsgs       r   r$   zEnableFirewall._operationl   s     44LL44DNN<W<W<Y4Z48D1-1\\-I-IQUQ_Q_QlQlQnNPiik .J .[** %`gghlhtht  iG  iG  iI  JCKKj/>P>Y>Ycfrwx#!**	r   r%   r*   s   @r   rH   rH   e   s    6r   rH   c                   (     e Zd ZdZ fdZd Z xZS )LogFirewallRulesz
    Log firewall rules state once a day.
    Goal is to capture the firewall state when the agent service startup,
    in addition to add more debug data and would be more useful long term.
    c                 ^    t         t        |   t        j                                || _        y r   )r   rd   r   r   get_firewall_rules_log_periodrK   r   s     r   r   zLogFirewallRules.__init__   s#    .t/Q/Q/STr   c                 |    t        j                  dj                  | j                  j	                                      y )NzCurrent Firewall rules:
{0})r=   r>   r\   rK   r]   r#   s    r   r$   zLogFirewallRules._operation   s'    299$,,:X:X:Z[\r   )r&   r'   r(   __doc__r   r$   r)   r*   s   @r   rd   rd      s    
]r   rd   c                   $     e Zd Z fdZd Z xZS )SetRootDeviceScsiTimeoutc                 ^    t         t        |   t        j                                || _        y r   )r   rj   r   r   #get_root_device_scsi_timeout_periodrK   r   s     r   r   z!SetRootDeviceScsiTimeout.__init__   s#    &6t7_7_7abr   c                 ^    | j                   j                  t        j                                y r   )rK   set_scsi_disks_timeoutr   get_root_device_scsi_timeoutr#   s    r   r$   z#SetRootDeviceScsiTimeout._operation   s    ++D,M,M,OPr   r%   r*   s   @r   rj   rj      s    Qr   rj   c                   $     e Zd Z fdZd Z xZS )MonitorHostNameChangesc                     t         t        |   t        j                                || _        | j
                  j                         | _        y r   )r   rq   r   r   get_monitor_hostname_periodrK   get_hostname_record	_hostnamer   s     r   r   zMonitorHostNameChanges.__init__   s7    $d4T5U5U5WX99;r   c                    t        j                         }|| j                  k7  r_t        j                  d| j                  |       | j
                  j                  |       | j
                  j                  |       || _        y y )Nz0EnvMonitor: Detected hostname change: {0} -> {1})socketgethostnameru   r=   r>   rK   set_hostnamepublish_hostname)r   curr_hostnames     r   r$   z!MonitorHostNameChanges._operation   sg    **,DNN*KKJ%' LL%%m4LL))-8*DN +r   r%   r*   s   @r   rq   rq      s    <
+r   rq   c                   H    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d Zd Zy	)
r   z
    Monitor changes to dhcp and hostname.
    If dhcp client process re-start has occurred, reset routes, dhcp with fabric.

    Monitor scsi disk.
    If new scsi disk found, set timeout
    c                  "    t         j                  S r   )r   _THREAD_NAMEr   r   r   get_thread_namezEnvHandler.get_thread_name   s    &&&r   c                 .    d| _         d | _        d | _        y r.   )stoppedhostname
env_threadr#   s    r   r   zEnvHandler.__init__   s    r   c                     | j                   s%t        j                  d       | j                          d| _         t        j                  d       | j	                          y )Nz"Stop existing env monitor service.FzStarting env monitor service.)r   r=   r>   stopstartr#   s    r   runzEnvHandler.run   s<    ||KK<=IIK34

r   c                 6    | j                   j                         S r   )r   is_aliver#   s    r   r   zEnvHandler.is_alive   s    ''))r   c                 
   t        j                  | j                        | _        | j                  j	                  d       | j                  j                  | j                                | j                  j                          y )N)targetT)	threadingThreaddaemonr   	setDaemonsetNamer   r   r#   s    r   r   zEnvHandler.start   sU    #**$++>!!$' 4 4 67r   c                 \   	 t               }|j                         }t               }t        |      t	        |      g}t        j                         r5|j                  t        ||             |j                  t        |             t        j                         |j                  t        |             t        j                         r|j                  t        |             | j                  s<	 |D ]  }|j                           	 t)        j*                  |       | j                  s;y y # t         $ r)}t#        j$                  dt'        |             Y d }~Qd }~ww xY w# t)        j*                  |       w xY w# t         $ r)}t#        j$                  dt'        |             Y d }~y d }~ww xY w)Nz[An error occurred in the environment thread main loop; will skip the current iteration.
{0}zFAn error occurred in the environment thread; will exit the thread.
{0})r
   get_protocolr	   r   r,   r   rY   appendrH   rd   ro   rj   get_monitor_hostnamerq   r   r   rD   r=   rE   r   r   sleep_until_next_operation)r   protocol_utilrN   r   periodic_operationsrS   es          r   r   zEnvHandler.daemon   su   	m ./M$113H\F -V4(0#
 ##%#**>&(+KL#**+;F+CD002>#**+CF+KL((*#**+A&+IJllV1 !!
 &@@ATU ll ! JLL!  BF  GH  BI  J  JJ &@@ATU 	mLLbdhijdkll	msT   C+E9 .D* E !E9 *	E3EE EE E66E9 9	F+F&&F+c                 `    d| _         | j                  | j                  j                          yy)zO
        Stop server communication and join the thread to main thread.
        TN)r   r   joinr#   s    r   r   zEnvHandler.stop   s*     ??&OO  " 'r   N)r&   r'   r(   rh   r~   staticmethodr   r   r   r   r   r   r   r   r   r   r   r      s@      L' '
* m@#r   r   )(rerZ   rw   r   azurelinuxagent.common.confcommonr   azurelinuxagent.common.loggerr=   azurelinuxagent.common.dhcpr   azurelinuxagent.common.eventr   r   r   azurelinuxagent.common.futurer   !azurelinuxagent.common.interfacesr   azurelinuxagent.common.osutilr	   $azurelinuxagent.common.protocol.utilr
   azurelinuxagent.common.versionr   r   %azurelinuxagent.ga.periodic_operationr   compile
IGNORECASECACHE_PATTERNSMAXIMUM_CACHED_FILESr   r   r,   rH   rd   rj   rq   r   r   r   r   <module>r      s   ( 
 	   * * . . 8 T T . D 4 B F C BJJ12==ABJJ0"--@BJJ&6  )#4 )'0 'T!& !H]( ]Q0 Q+. +"L#' L#r   