
    x[h                         S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
  \R                  " \5      rS\4S jr " S S	5      rg)
z#A module for common socket helpers.    N)suppress)performance)DEFAULT_RUN_DIRmessagec                    [         R                  R                  SS5      nU(       d  gUS   S:X  a  UR                  SSS5        OUS   S:w  a  [	        S	5      e[
        R
                  " [
        R                  [
        R                  [
        R                  -  5       n[        R                  S
[        U 5      5        UR                  U5        UR                  U R                  S5      5        SSS5        g! , (       d  f       g= f)zSSend 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C88
Dc                   @    \ rS rSrSrS\4S jrS\4S jrS rS r	S	r
g
)
SocketSync*   z<A two way synchronization protocol over Unix domain sockets.namesc           	      B   SU l         SU l        SU l        SU l        SU l        U Vs0 s HH  nU[
        R
                  " [
        R                  [
        R                  [
        R                  -  5      _MJ     snU l	        [        R                  " [         S3SSS9  U R                  R                  5        HP  u  p#[         SU S	3n[        [        5         [        R                   " U5        S
S
S
5        UR#                  U5        MR     g
s  snf ! , (       d  f       N)= f)a6  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_ok/share/z.sockN)stageremotefirst_exceptionsystemd_exit_codeexperienced_any_errorr   r   r   r   socketsr   makedirsr   itemsr   FileNotFoundErrorremovebind)selfr%   namer   r   s        r    __init__SocketSync.__init__-   s     
!!"%*"
 	
  &-- 1 1F4G4G G  	
 	'v.UTJ,,,,.JD,-WTF%@K+,		+& -IIk"	 /
 -,s   ADD
D	r*   c                 L    XR                   ;  a  [        SU 35      eXl        U $ )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*   )r5   r*   s     r    __call__SocketSync.__call__M   s+     $3E7;<<
    c                     [         R                  " [        R                  R	                  5       5      (       a  [
        R                  S5        gSU l        [        SU R                   S35        U R                  U R                     n[        R                  " SU R                   35         UR                  S5      u  o l        SSS5        SW:w  a+  U R                  SSS5        [!        S	[#        U5       S
35      e[$         SU R                   S3[#        U R                  5      :w  a+  U R                  SSS5        [!        SU R                   35      e[        SU R                   S35        U $ ! , (       d  f       N= f)zwWait 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: []r)   z-return.sockz Unexpected path to unix socket: zSTATUS=Running (z stage))r   isattysysstdinfilenor   r   r-   r!   r*   r/   r   Timedrecvfromr+   __exit__r:   r   r   )r5   r   chunks      r    	__enter__SocketSync.__enter__a   sK   
 99SYY%%'((HHL !",,0JJ<w@	
 ||DJJ'!8EF!%q!1E; G uMM$d+:3u:,aHII 

|<@CKKE
 

 MM$d+?}MNN$TZZL89! GFs   *E//
E=c                 :   SU R                    3nU(       ad  SU l        SU l        [        U5       SUR                   3nSnU R
                  (       d  XPl        [        R                  U5        [        SU 35        U R                  =(       d    [        U R                  5      U l        U R                  U R                      nUR                  U R                  5        UR                  SU SU R                   S	3R                  5       5        UR                  5         g)
z.Notify the socket that this stage is complete.z,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-   r.   reprtb_framer,   r   fatalr!   boolr/   r   r+   r   r   close)r5   exc_typeexc_valexc_tbr   statusr   s          r    rG   SocketSync.__exit__   s    @M%&D")-D&Wd6??*;<FB  '''-$IIfx()%)%?%? &
4""D
" ||DJJ'T[[!
 	WIXd&<&<%=Q?FFH	
 	

 r=   )r.   r,   r+   r/   r*   r-   N)__name__
__module____qualname____firstlineno____doc__r   r7   r;   rI   rG   __static_attributes__ r=   r    r#   r#   *   s)    F#s #@c (%N#r=   r#   )r[   loggingr   r   rB   
contextlibr   	cloudinitr   cloudinit.settingsr   	getLoggerrW   r   r   r!   r#   r]   r=   r    <module>rc      sE    )  	  
  ! .!.s .6A Ar=   