
                             d Z ddlmZ ddlmZ ddlm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 dd
lmZ ddlmZ  G d dej                         Z G d de
j$                        Zy)z:Command for tailing the serial port output of an instance.    )absolute_import)division)unicode_literalsN)base_classes)arg_parsers)base)flags)
exceptions)logc                       e Zd ZdZy)TailSerialPortOutputExceptionz0An error occurred while tailing the serial port.N)__name__
__module____qualname____doc__     8lib/surface/compute/instances/tail_serial_port_output.pyr   r      s    8r   r   c                   2    e Zd ZdZddiZdZed        Zd Zy)TailSerialPortOutputag  Periodically fetch new output from a virtual machine instance's serial port and display it as it becomes available.

  {command} is used to tail the output from a Compute
  Engine virtual machine instance's serial port. The serial port output
  from the instance will be printed to standard output. This
  information can be useful for diagnostic purposes.
  EXAMPLESz
  To fetch new output from instance's serial port and display it, run:

    $ {command} example-instance --zone=us-central1-b
  
   c                     t         j                  j                  |        | j                  dt	        j
                  dd      d       y )Nz--port      a          Instances can support up to four serial port outputs, numbered 1 through
        4. By default, this command will return the output of the first serial
        port. Setting this flag will return the output of the requested serial
        port.
        )typehelp)r	   INSTANCE_ARGAddArgumentadd_argumentr   
BoundedInt)parsers    r   ArgszTailSerialPortOutput.Args5   sA    	""6*
##Aq)  r   c           
      J   t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t	        j                  |            }d }	 |j                  j                  d|j                  j                  |j                         |j                  |j                  ||j                         f}g }|j#                  |g|      }|r.t%        ddj'                  |D 	cg c]  }	|	d   	 c}	      z         |d   }
t(        j*                  j-                  |
j.                         t(        j*                  j1                          |
j2                  }|
j.                  st5        j6                  | j8                         .c c}	w )	N)scope_listerGetSerialPortOutput)instanceprojectportstartzone)requestserrors_to_collectz$Could not fetch serial port output: ,r   r   )r   ComputeApiHolderReleaseTrackclientr	   r   ResolveAsResource	resourcesGetInstanceZoneScopeListerapitools_client	instancesmessages*ComputeInstancesGetSerialPortOutputRequestNamer(   r)   r+   MakeRequestsr   joinr   outwritecontentsflushnexttimesleepPOLL_SLEEP_SECS)selfargsholderr1   instance_refr*   requesterrorsobjectserrorresults              r   RunzTailSerialPortOutput.RunB   sr   **4+<+<+>?F]]F%%77f55f= 8 ?L E
''11&KK*//1)11)) &++ L -.g f##9" $ $g 
+2HHF3F5eAhF3456 	6 qzf	ggmmFOO$	ggmmokke __

4''(7 " 4s   F N)	r   r   r   r   detailed_helprC   staticmethodr#   rM   r   r   r   r   r   #   s6      - /
 
$)r   r   )r   
__future__r   r   r   rA   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   r   ,googlecloudsdk.command_lib.compute.instancesr	   googlecloudsdk.corer
   r   Errorr   Commandr   r   r   r   <module>rW      sM    A &  '  7 / ( > * #9J$4$4 9C)4<< C)r   