
    dM                     <   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c m	c 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mZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZm Z m!Z!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. dZ/d Z0 G d de1      Z2y)    N)	add_eventWALAEventOperation0initialize_event_logger_vminfo_common_parameters)ustr)
get_osutil)	GoalStateGoalStateProperties)get_protocol_util)setup_rdma_device)textutil)
AGENT_NAMEAGENT_LONG_NAMEAGENT_VERSIONDISTRO_NAMEDISTRO_VERSIONPY_VERSION_MAJORPY_VERSION_MINORPY_VERSION_MICRO)get_resourcedisk_handler)get_scvmm_handler)get_update_handler)get_provision_handler)get_rdma_handlerOPENSSL_FIPSc                      t               S N)DaemonHandler     =/usr/lib/python3/dist-packages/azurelinuxagent/daemon/main.pyget_daemon_handlerr!   0   s
    ?r   c                   >    e Zd ZdZd Zd
dZd Zd Zd Zd Z	d
d	Z
y)r   zO
    Main thread of daemon. It will invoke other threads to do actual work
    c                 0    d| _         t               | _        y )NT)runningr   osutilselfs    r    __init__zDaemonHandler.__init__9   s     lr   Nc                    t        j                  dt        t               t        j                  dt        t
               t        j                  dt        t        t               | j                          | j                          t        j                         rdt        j                  t        <   | j                   r 	 | j#                  |       | j                   ry y # t$        $ rs}t'        j(                  |      }t+        t,        dt/        |      t0        j2                         t        j4                  d       t7        j8                  d       Y d }~d }~ww xY w)	Nz{0} Version: {1}zOS: {0} {1}zPython: {0}.{1}.{2}1F)name
is_successmessageopzBDaemon ended with exception -- Sleep 15 seconds and restart daemon   )loggerinfor   r   r   r   r   r   r   	check_pidinitialize_environmentconfget_fips_enabledosenvironOPENSSL_FIPS_ENVIRONMENTr$   daemon	Exceptionr   format_exceptionr   r   r   r   UnhandledErrorwarntimesleep)r'   
child_argseerr_msgs       r    runzDaemonHandler.run=   s     	&GM;?)+;=MO_`##%
   "36BJJ/0llJ' ll  "33A6zeT']/>>@`a

2s   ;C 	E$A)EEc                    d}t        j                         }t        j                  j	                  |      rt        j                  |      }| j                  j                  |      r+t        j                  d|       t        j                  d       t        j                  |t        t        j                                      y)z'Check whether daemon is already runningNzDaemon is already running: {0}r   )r4   get_agent_pid_file_pathr6   pathisfilefileutil	read_filer%   check_pid_aliver0   r1   sysexit
write_filer   getpid)r'   pidpid_files      r    r2   zDaemonHandler.check_pid[   s|    //177>>(#$$X.C;;&&s+KK8#>HHQKHd299;&78r   c                 
   t        j                         }t        j                  j	                  |      rPdd l}t        j                  dj                  |             d| _	        |j                         }|j                          y y )Nr   zSDisabling the guest agent by sleeping forever; to re-enable, remove {0} and restartF)r4   get_disable_agent_file_pathr6   rF   exists	threadingr0   r=   formatr$   Eventwait)r'   agent_disabled_file_pathrT   disable_events       r    sleep_if_disabledzDaemonHandler.sleep_if_disabledh   sg    #'#C#C#E 77>>23KK ? 89; !DL%OO-M  4r   c                    t         j                  j                  t        j                               sQt        j                  t        j                         d       t        j                  t        j                                y y )Ni  )mode)r6   rF   isdirr4   get_lib_dirrH   mkdirchdirr&   s    r    r3   z$DaemonHandler.initialize_environments   sL    ww}}T--/0NN4++-E:HHT%%'( 1r   c                 N    | j                   j                         }t        |       y r   )protocol_utilget_protocolr   )r'   protocols     r    _initialize_telemetryz#DaemonHandler._initialize_telemetryy   s    %%2248Br   c                    t        j                  d       t               | _        t	               | _        t               | _        t               | _	        t               | _        t               | _        t        j                         r| j
                  j!                          t        j"                         r| j                  j!                          | j                  j%                          | j                  j!                          | j'                          t        j(                         r| j                  j+                         }| j                  j-                          t        j                  d       	 | j                  j/                         }t1        |t2        j4                        }t7        ||j8                         nt        j                  d       | j?                          t        j@                         r)t        j                  d       t        jB                          | jD                  r*| j                  jG                  |       | jD                  r)y y # t:        $ r"}t        j<                  d|z         Y d }~d }~ww xY w)Nz
Run daemonz.RDMA capabilities are enabled in configuration)goal_state_propertiesz Error setting up rdma device: %sz+RDMA capabilities are not enabled, skippingzbEnd of log to /dev/console. The agent will now check for updates and then will process extensions.)r@   )$r0   r1   r
   rb   r   scvmm_handlerr   resourcedisk_handlerr   rdma_handlerr   provision_handlerr   update_handlerr4   get_detect_scvmm_envrC   get_resourcedisk_formatclear_protocolre   enable_rdmaget_rdma_versioninstall_driver_if_neededrc   r   r	   SharedConfigr   shared_confr:   errorrZ   console_output_enableddisable_console_outputr$   
run_latest)r'   r@   
nd_versionrd   
goal_staterA   s         r    r9   zDaemonHandler.daemon}   s   L!.0.0$<$>!,.!6!802$$&""$'')%%))+ 	))+""$ 	""$ **;;=J668KKHIE
  --::<&xGZGgGgh
!*j.D.DE KKEF  ((*KK|}))+ll**j*A ll  E?!CDDEs   .AI 	I>I99I>r   )__name__
__module____qualname____doc__r(   rC   r2   rZ   r3   re   r9   r   r   r    r   r   4   s,    #<9	!)C7Br   r   )3r6   rK   r>   azurelinuxagent.common.confcommonr4   azurelinuxagent.common.loggerr0   %azurelinuxagent.common.utils.fileutilutilsrH   azurelinuxagent.common.eventr   r   r   azurelinuxagent.common.futurer   azurelinuxagent.common.osutilr   *azurelinuxagent.common.protocol.goal_stater   r	   $azurelinuxagent.common.protocol.utilr
   azurelinuxagent.common.rdmar   azurelinuxagent.common.utilsr   azurelinuxagent.common.versionr   r   r   r   r   r   r   r   #azurelinuxagent.daemon.resourcediskr   azurelinuxagent.daemon.scvmmr   azurelinuxagent.ga.updater   azurelinuxagent.pa.provisionr   azurelinuxagent.pa.rdmar   r8   r!   objectr   r   r   r    <module>r      sv   ( 
 
  * * . . 8 8 x x . 4 U B 9 1   I : 8 > 4) @BF @Br   