
                            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SKJr  SSKJr  SSKJr   " S S\R<                  5      rg)z Command for spanner samples run.    )absolute_import)division)unicode_literalsN)
exceptions)	databases)arg_parsers)base)samples)execution_utils)log)
properties)	resources)backend)init)workloadc                   V    \ rS rSrSrS\R                  " S5      0r\S 5       r	S r
Srg)	Run'   a  Run the given Cloud Spanner sample app.

Each Cloud Spanner sample application includes a backend gRPC service
backed by a Cloud Spanner database and a workload script that generates
service traffic. This command creates and initializes the Cloud Spanner
database and runs both the backend service and workload script.

These sample apps are open source and available at
https://github.com/GoogleCloudPlatform/cloud-spanner-samples.

To see a list of available sample apps, run:

    $ {parent_command} list
EXAMPLESz          To run 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SS9  U R                  SS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.
appnamez$The sample app name, e.g. "finance".)help--instance-idTz1The Cloud Spanner instance ID for the sample app.)requiredtyper   --database-idzBID of the new Cloud Spanner database to create for the sample app.)r   r   z
--duration1hzKDuration of time allowed to run the sample app before stopping the service.)defaultr   r   z	--cleanup
store_truez1Delete the instance after running the sample app.)actionr   r   z--skip-initFz7Use an existing database instead of creating a new one.N)add_argumentstrr   Duration)parsers    "lib/surface/spanner/samples/run.pyArgsRun.Args@   s     	(NO
@	  B
   
 !!#	   A	  D
 G	  J    c           	      $
   UR                   n [        R                  " U5        UR                  n[        R                  R                  R                  R                  5       n[        R                  R                  USU0SS9nUR                  b  UR                  nO[        R                   " U5      nUR"                  nUR$                  n	 [&        R(                  " U5        [*        R,                  R/                  SR1                  XrS95        U	(       a:  [        R                  R                  UUUS	.S
S9n
 [2        R4                  " U
5        On U R;                  5       [<        R:                  R>                  :X  a   [&        R@                  " UR                   5        [&        RB                  " UR                   Xg5        [D        RF                  " XRUU5      n URI                  S5        SR1                  U5      $ ! [         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! [6        R8                   a6    UR                  c  SOSn[        R
                  " USR1                  XtS95      ef = f! [         a  n[        R
                  " SU5      eSnAff = f! [J        RL                   a     Of = f[O        [P        RP                  " 5       5      nX-   n[R        RT                  " USS9n[P        RV                  " S5        [*        R,                  R/                  SR1                  XS95         [J        RX                  " 5          URI                  U5        SSS5        O! , (       d  f       O= f! [Z         aB    UR]                  5         UR]                  5         [*        R,                  R/                  S5         Oe[J        RL                   aO    UR]                  5         UR]                  5         [*        R,                  R/                  SR1                  US95         Of = fUR^                  (       a  [*        R,                  R/                  SR1                  U5      5        [        R                  R                  U[        R                  R                  R                  R                  UR`                  S.S
S9n
[2        Rb                  " U
5        [*        R,                  R/                  S5        g)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.
APPNAMEN
projectsIdzspanner.projects.instances)params
collectionr   z@Initializing database '{database_id}' for sample app '{appname}')database_idr   )instancesIdr+   z$spanner.projects.instances.databasesr   z|Database '{database_id}' does not exist in instance '{instance_id}'. Re-run this command without `--skip-init` to create it.)r.   instance_id   z\The {} sample app backend gRPC service failed to start, is another instance already running?T)capture_logs   zd
Generating workload for database, start timestamp: {now}, end timestamp: {later}. Press ^C to stop.)nowlaterz2Backend gRPC service and workload generator killedzDBackend gRPC service and workload generator killed after {duration}s)durationzDeleting database '{}')r+   r/   Done)2r   r
   check_appname
ValueErrorcalliope_exceptionsBadArgumentExceptionr0   r   VALUEScoreproject	GetOrFailr   REGISTRYParser.   get_db_id_for_appr6   	skip_initsamples_initcheck_instancer   statusPrintformatr   Getapitools_exceptionsHttpNotFoundErrorReleaseTrackr	   ALPHAdownload_sample_filescheck_create_dbsamples_backendrun_backendwaitr   TIMEOUT_EXPIRED_ERRinttimesamples_workloadrun_workloadsleepRaisesKeyboardInterruptKeyboardInterrupt	terminatecleanupr/   Delete)selfargsr   exr0   r>   instance_refr.   r6   rC   database_refbad_flagbe_procr4   r5   wl_procs                   r%   r   Run.Rund   sA    llGDG$ ""K$$,,668G%%++'
 0 , 1L #$$k--g6k}}HIJ!!+. JJJ	K	9; ''--
(# < . =l
Cl#L$"3"3"9"99

,
,T\\
:$$T\\<M ))'K*57Gll1o&&,fWo7q  D44YCCD&  J44_bIIJ  !22 C''/ $5D 	!66 '   BC 	CC  L!66KKL .. 
 diik
CNE++G$GGJJqMJJ	00630LN&224X 544 M	jjKL.. &	jj
P686$&& ||	jj/66{CD''--
&--22::DD)55 < . =l |$JJV
s   G8 H! 8I
 A-J !K  8
HHH!
I+II
A
J
J=!J88J= KKN )N;N 
NN N A	Q AQQ N)__name__
__module____qualname____firstlineno____doc__textwrapdedentdetailed_helpstaticmethodr&   r   __static_attributes__rg   r(   r%   r   r   '   sA      
//  - !J !JFnr(   r   ) rl   
__future__r   r   r   rm   rU   apitools.base.pyr   rJ   googlecloudsdk.api_lib.spannerr   googlecloudsdk.callioper   r	   r:   "googlecloudsdk.command_lib.spannerr
   googlecloudsdk.corer   r   r   r   surface.spanner.samplesr   rP   r   rD   r   rV   Commandr   rg   r(   r%   <module>rz      sR    ' &  '   > 4 / ( E 6 / # * ) > 8 @k$,, kr(   