
    thg                         d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ  ej                  e      ZdefdZ G d	 d
      Zy)z#A module for common socket helpers.    N)suppress)Dict)performance)DEFAULT_RUN_DIRmessagec                    t         j                  j                  dd      }|sy|d   dk(  r|j                  ddd       n|d   dk7  rt	        d	      t        j
                  t
        j                  t
        j                  t
        j                  z        5 }t        j                  d
t        |              |j                  |       |j                  | j                  d             ddd       y# 1 sw Y   yxY w)z[Send a sd_notify message.

    :param message: sd-notify message (must be valid ascii)
    NOTIFY_SOCKET Nr   @    /zUnsupported socket typezSending sd_notify(%s)ascii)osenvirongetreplaceOSErrorsocketAF_UNIX
SOCK_DGRAMSOCK_CLOEXECLOGinfostrconnectsendallencode)r   socket_pathsocks      2/usr/lib/python3/dist-packages/cloudinit/socket.py	sd_notifyr"      s    
 **.."5K	Q3	Cq) 
Q3	/00	))F,?,??
 .	(#g,7[!W^^G,-. . .s   AC00C9c                   4    e Zd ZdZdefdZdefdZd Zd Zy)	
SocketSyncz<A two way synchronization protocol over Unix domain sockets.namesc           	      P   d| _         d| _        d| _        d| _        |D ci c]F  }|t	        j                  t        j
                  t        j                  t        j                  z        H c}| _        i | _	        t        j                  t         ddd       | j                  j                         D ]_  \  }}t         d| d	}t        t              5  t        j                   |       d
d
d
       |j#                  |       |j%                          a y
c c}w # 1 sw Y   2xY w)an  Initialize a synchronization context.

        1) Ensure that the socket directory exists.
        2) Bind a socket for each stage.

        Binding the sockets on initialization allows receipt of stage
        "start" notifications prior to the cloud-init stage being ready to
        start.

        :param names: stage names, used as a unique identifiers
        r
   r   Fz/sharei  T)modeexist_okz/share/z.sockN)stagefirst_exceptionsystemd_exit_codeexperienced_any_errorr   r   SOCK_STREAMr   socketsconnectionsr   makedirsr   itemsr   FileNotFoundErrorremovebindlisten)selfr%   namer    r   s        r!   __init__zSocketSync.__init__.   s    
!!"%*"
 	
  &-- 2 2V5H5H H 
 68
'v.UTJ,,,,. 	JD$,-WTF%@K+, '		+&'IIk"KKM	
' 's   ADDD%	r)   c                 L    || j                   vrt        d|       || _        | S )a  Set the stage before entering context.

        This enables the context manager to be initialized separately from
        each stage synchronization.

        :param stage: the name of a stage to synchronize

        Example:
            sync = SocketSync("stage 1", "stage 2"):
            with sync("stage 1"):
                pass
            with sync("stage 2"):
                pass
        zInvalid stage name: )r.   
ValueErrorr)   )r6   r)   s     r!   __call__zSocketSync.__call__O   s/     $3E7;<<
    c                    t        j                  t        j                  j	                               rt
        j                  d       yd| _        t        d| j                   d       | j                  | j                     }t        j                  d| j                         5  |j                         \  }}|j                  d      \  }}|| j                  | j                  <   ddd       dk7  r+| j!                  ddd       t#        d	t%        |       d
      t        d| j                   d       | S # 1 sw Y   TxY w)zWait until a message has been received on this stage's socket.

        Once the message has been received, enter the context.
        z:Stdin is a tty, so skipping stage synchronization protocolNr   zDSTATUS=Waiting on external services to complete before starting the z stage.zWaiting to start stage    s   startzReceived invalid message: []zSTATUS=Running (z stage))r   isattysysstdinfilenor   r   r+   r"   r)   r.   r   Timedacceptrecvfromr/   __exit__r:   r   )r6   r    
connection_chunks        r!   	__enter__zSocketSync.__enter__c   s   
 99SYY%%'(HHL !",,0JJ<w@	
 ||DJJ'!8EF 	6 KKMMJ!**1-HE1+5DTZZ(	6
 uMM$d+:3u:,aHII$TZZL89	6 	6s   #AD77E c                 L   t        j                  t        j                  j	                               ryd| j
                   }|r`d| _        d| _        t        |       d|j                   }d}| j                  s|| _
        t        j                  |       t        d|        | j                  xs t        | j                        | _        | j                  | j
                     }|j!                  d| d	| j                   d
j#                                |j%                          y)z.Notify the socket that this stage is complete.Nz,Completed socket interaction for boot stage r   Tz in zkfatal error, run "systemctl status cloud-init-main.service" and "cloud-init status --long" for more detailszSTATUS=zecho 'z'; exit ;)r   r@   rA   rB   rC   r)   r+   r,   reprtb_framer*   r   fatalr"   boolr/   r   r   close)r6   exc_typeexc_valexc_tbr   statusr    s          r!   rG   zSocketSync.__exit__   s   99SYY%%'(@M%&D")-D&Wd6??*;<FB  '''-$IIfx()%)%?%? &
4""D
" 

+
 	WIXd&<&<%=Q?FFH	
 	

 r<   N)	__name__
__module____qualname____doc__r   r8   r;   rK   rG    r<   r!   r$   r$   +   s)    Fs Bc ( D%r<   r$   )rZ   loggingr   r   rA   
contextlibr   typingr   	cloudinitr   cloudinit.settingsr   	getLoggerrW   r   r   r"   r$   r[   r<   r!   <module>rb      sJ    )  	  
   ! .g!.s .6 r<   