
                            S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr	  SSK
Jr  SSKJr  SS	KJr  SSKJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  S rS rSS jr   SS jr " S S\R:                  5      rg)z$Command for spanner samples backend.    )absolute_import)division)unicode_literalsN)
exceptions)	databases)arg_parsers)base)samples)execution_utils)
properties)	resources)initc                 $    SR                  U 5      $ )N{}-backend.log)formatappnames    &lib/surface/spanner/samples/backend.py_get_logfile_namer   $   s    		 	 	))    c                     U [         R                  ;  a  [        SR                  U 5      5      e[        R
                  R                  [         R                  " U 5      [         R                  U    R                  5      $ )NzUnknown sample app '{}')	r
   APPS
ValueErrorr   ospathjoinget_local_bin_pathbackend_binr   s    r   _get_popen_jarr   (   s[    GLL 
.55g>
??	  )7<<+@+L+L
N Nr   c                 T   Uc  [         R                  " U5      nSR                  U 5      SR                  U5      SR                  U5      /nUb   UR                  SR                  U5      5        [         R                  " U5      [
        R                  :X  a  UR                  S5        U$ )z&Get formatted args for server command.z--spanner_project_id={}z--spanner_instance_id={}z--spanner_database_id={}z	--port={}z--spanner_use_pg)r
   get_db_id_for_appr   appendget_database_dialectr   DATABASE_DIALECT_POSTGRESQL)projectr   instance_iddatabase_idportflagss         r   _get_popen_argsr*   2   s    ++G4K&&w/ ''4 ''4%
 
	LL##D)*!!778	LL#$	,r   c           	      .   SS/nUR                  [        U5      5        UR                  [        XX#U5      5        U(       a=  [        R
                  R                  [        R                  SR                  U5      5      OSn[        R                  " Xg5      $ )a  Run the backend service executable for the given sample app.

Args:
  project: str, Name of the GCP project.
  appname: str, Name of the sample app.
  instance_id: str, Cloud Spanner instance ID.
  database_id: str, Cloud Spanner database ID.
  port: int, Port to run the service on.
  capture_logs: bool, Whether to save logs to disk or print to stdout.

Returns:
  subprocess.Popen or execution_utils.SubprocessTimeoutWrapper, The running
    subprocess.
javaz-jarr   N)r"   r   extendr*   r   r   r   r
   SAMPLES_LOG_PATHr   run_proc)r%   r   r&   r'   r(   capture_logs	proc_argscapture_logs_fns           r   run_backendr3   C   s    ( v)>'*+g$GI 
 	ggll7++-=-D-DW-MN  
		)	55r   c                   V    \ rS rSrSrS\R                  " S5      0r\S 5       r	S r
Srg)	Backenda   a  Run the backend gRPC service for the given Cloud Spanner sample app.

This command starts the backend gRPC service for the given sample
application. Before starting the service, create the database and load any
initial data with:

    $ {parent_command} init APPNAME --instance-id=INSTANCE_ID

After starting the service, generate traffic with:

    $ {parent_command} workload APPNAME

To run all three steps together, use:

    $ {parent_command} run APPNAME --instance-id=INSTANCE_ID
EXAMPLESz          To run the backend gRPC service for the 'finance' sample app using
          instance 'my-instance', run:

          $ {command} finance --instance-id=my-instance
        c                     U R                  SSS9  U R                  SS[        SS9  U R                  S[        S	S
9  U R                  SS[        R                  " 5       SS9  U R                  S[        SS
9  g)zArgs is called by calliope to gather arguments for this command.

Args:
  parser: An argparse parser that you can use to add arguments that go on
    the command line after this command. Positional arguments are allowed.
r   z$The sample app name, e.g. "finance".)help--instance-idTz1The Cloud Spanner instance ID for the sample app.)requiredtyper9   --database-idz1The Cloud Spanner database ID for the sample app.)r<   r9   z
--duration1hz<Duration of time allowed to run before stopping the service.)defaultr<   r9   z--portz'Port on which to receive gRPC requests.N)add_argumentstrr   Durationint)parsers    r   ArgsBackend.Args}   s     	(NO
@	  B
 @  B !!#L	  O
 s"K  Nr   c                    UR                   n [        R                  " U5        [        R                  R                  R                  R                  5       nUR                  n [        R                  " U5        UR                  b  UR                  nO[        R                  " U5      n[         R"                  R%                  UUUS.SS9n [&        R(                  " U5        [3        XBXQR                  UR4                  5      n [6        R8                  " 5          UR;                  UR<                  5        SSS5        g! [         a  n[        R
                  " SU5      eSnAff = f! [         a  n[        R
                  " SU5      eSnAff = f! [*        R,                   aN  nUR                  b  [        R
                  " SU5      e[        R.                  " SR1                  U5      5      eSnAff = f! , (       d  f       g= f! [>         a    URA                  5          g	[6        RB                   a,    URA                  5         S
R1                  UR<                  S9s $ f = f)zThis is what gets called when the user runs this command.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    command invocation.

Returns:
  Some value that we want to have printed later.
APPNAMENr:   )
projectsIdinstancesIdz$spanner.projects.instances.databases)params
collectionr=   zKDatabase {} doesn't exist. Did you run `gcloud spanner samples init` first?zBackend gRPC service killedz0Backend gRPC service timed out after {duration}s)duration)"r   r
   check_appnamer   calliope_exceptionsBadArgumentExceptionr   VALUEScorer%   	GetOrFailr&   samples_initcheck_instancer'   r!   r   REGISTRYParser   Getapitools_exceptionsHttpNotFoundErrorSpannerSamplesErrorr   r3   r(   r   RaisesKeyboardInterruptwaitrM   KeyboardInterrupt	terminateTIMEOUT_EXPIRED_ERR)	selfargsr   exr%   r&   r'   database_refprocs	            r   RunBackend.Run   s    llGDG$ $$,,668G""KJ!!+. #$$k--g6k%%++!&
 : , ;L0mmL! w6F6Fyy"D	"224		$--  5 Y  D44YCCD  J44_bIIJ  00 0				%!66KK))!6+.0 	0	0 54   +
nn*.. "
nn?FF== G " ""s~   D> #E' F H G55H >
E$EE$'
F1FFG2$A	G--G25
H?H H I!"<I! I! N)__name__
__module____qualname____firstlineno____doc__textwrapdedentdetailed_helpstaticmethodrE   rf   __static_attributes__rh   r   r   r5   r5   a   s?    $ 
//  - N N29r   r5   )NN)NNF)rm   
__future__r   r   r   r   rn   apitools.base.pyr   rY   googlecloudsdk.api_lib.spannerr   googlecloudsdk.callioper   r	   rO   "googlecloudsdk.command_lib.spannerr
   googlecloudsdk.corer   r   r   surface.spanner.samplesr   rT   r   r   r*   r3   Commandr5   rh   r   r   <module>r{      si    + &  ' 	  > 4 / ( E 6 / * ) 8*N( !"6<odll or   