
                            d 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	 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Zej,                   ej.                  dee       ej0                  ej2                  j4                  ej2                  j6                  ej2                  j8                         G d dej:                                             Zy)z5Command for creating TPU node and GCE VM combination.    )absolute_import)division)unicode_literals)HttpConflictError)HttpNotFoundError)utils)base)flags)util)logzThis command is deprecated and will be removed in a future release. We recommend that you use the TPU-VM or Queued Resource API. See https://cloud.google.com/tpu/docs/managing-tpus-tpu-vm for more information.F)
is_removedwarningerrorc                   ,    e Zd ZdZed        Zd Zd Zy)Createa  Create Google Compute TPUs along with VMs.

  ## EXAMPLES

  To create both TPU and VM, run:

    $ {command} --name=test-execution-group --zone=test-zone
    --project=test-project --accelerator-type=v2-8 --tf-version=2.4.1

  To create both TPU and VM with additional flags, run:

    $ {command} --name=test-execution-group --zone=test-zone \
    --project=test-project --accelerator-type=v2-8 --tf-version=2.4.1 \
    --network=default --preemptible-vm --disk-size=100 \
    --machine-type=n1-standard-2 --use-dl-images

  To create a VM only before creating the TPU, run:

    $ {command} --name=test-execution-group-tpu-only --zone=test-zone \
    --project=test-project --accelerator-type=v2-8 --tf-version=2.4.1 --tpu-only

  To create the TPU only after the VM has been created, run:

    $ {command} --name=test-execution-group-tpu-only --zone=test-zone \
    --project=test-project --accelerator-type=v2-8 --tf-version=2.4.1 --vm-only
  c                    t        j                  |dd       t        j                  |       t        j                  |       t        j
                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                         j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                   |       t        j"                  |       t        j$                  |       y )Ntpucreate)resource_typeoperation_type)r
   AddZoneFlag
tpus_flagsAddTpuNameOverrideArgAddPreemptibleFlagAddTfVersionFlagAddVmOnlyFlagAddTpuOnlyFlagAddDeepLearningImagesFlagAddDryRunFlagGetAcceleratorTypeFlagAddToParserAddPreemptibleVmFlagAddPortForwardingFlagAddGceImageFlagAddDiskSizeFlagAddMachineTypeArgsAddNetworkArgsAddUseWithNotebook)clsparsers     3lib/surface/compute/tpus/execution_groups/create.pyArgszCreate.ArgsK   s    	fE(K$$V,!!&)'V$f%((0V$%%'33F;##F+$$V,v&v&!!&)f%!!&)    c           
         t         j                  j                  |       t         j                  j                  |       g }|j                  r| j                  |       |S t        j                  | j                               }|j                  s!	 |j                  |j                        |_        |j                  sS	 |j!                  |j"                  |j$                  |j                  |j                  |j&                  |j(                        }|j.                  st        j0                  | j                               }|j2                  }|s6|j4                  }|j6                  rd}|j9                  |j                  |      }	 |j!                  |j"                  |j                  |j:                  t=        j>                  |j@                        |jB                  ||j(                  |j6                        }|j                  s|jE                  |jG                  dj-                  |j"                                     |jI                  |j"                  |j                        }
t        jJ                         }|jM                  |
jN                         |j.                  sjG                  dj-                  |j"                              }|jE                  |       t        jP                  | j                               }|jE                  |jS                  ||             |S # t        $ r$ t        j                  j                  d       |cY S w xY w# t*        $ r= t        j                  j                  dj-                  |j"                               |cY S w xY w# t*        $ ri dj-                  |j"                        }	|j                  s|	dj-                  |j"                        z  }	t        j                  j                  |	       |cY S w xY w)Nz_Could not find stable Tensorflow version, please set tensorflow version flag using --tf-versionz:TPU Node with name:{} already exists, try a different nameTz5VM with name:{} already exists, try a different name.z> TPU Node:{} creation is underway and will need to be deleted.zCreating TPU node:{}zCreating GCE VM:{})*	tpu_utilsDefaultArgsValidateNameValidateZonedry_runDryRunTPUNodeReleaseTrack
tf_versionLatestStableTensorflowVersionzoner   r   errPrintvm_onlyr   nameaccelerator_typepreemptiblenetworkr   formattpu_onlyInstance	gce_imageuse_dl_imagesuse_with_notebook!ResolveImageFromTensorflowVersionmachine_typer   	BytesToGb	disk_sizepreemptible_vmappendWaitForOperationGetResourceManagerAddTpuUserAgentserviceAccountSSHSSHToInstance)selfargs	responsesr   tpu_operation_refinstancerD   rE   instance_operation_referr_msgtpu_noderesource_managerinstance_create_response
ssh_helpers                 r+   Runz
Create.Run^   s8   &&t,&&t,I||
kk$


D--/
0C??;;DIIF <<JJtyy'+'<'<doo'+yy$2B2B'+||5 ==##D$5$5$78h..i**!!->>OO],	!)IItyy$"3"3OODNN+T-@-@)LL$00"2 <<


02H2O2Oii3  DII.h"224&&x'>'>?==!)!:!:
 "6"="=dii"H"J/0==!2!2!45jz//6NOPm   G 	H   --3VDII->	@&  +,2F499,= 	||
 ,,2F499,=?'gs:    L =AM A1N *M
	M
ANNA/PPc           
         |j                   st        j                  j                  dj	                  |j
                  |j                  |j                  |j                  |j                               t        j                  j                  d       |j                  st        j                  j                  dj	                  |j
                  |j                  |j                  t        j                  |j                        |j                  |j                               t        j                  j                  dj	                  |j
                               y y )NzRCreating TPU with Name:{}, Accelerator type:{}, TF version:{}, Zone:{}, Network:{}z8Adding Storage and Logging access on TPU Service Accountz`Creating VM with Name:{}, Zone:{}, Machine Type:{}, Disk Size(GB):{}, Preemptible:{}, Network:{}zSSH to VM:{})r<   r   statusr;   rA   r=   r>   r7   r9   r@   rB   rH   r   rI   rJ   rK   )rT   rU   s     r+   r4   zCreate.DryRun   s    <<	jj  &tyy$2G2G'+		'+||!56
 
jj
DF ==	jj GGMv99dii1B1B ??4>>:D<O<O<<H)* 
jj~,,TYY78 r-   N)__name__
__module____qualname____doc__classmethodr,   r_   r4    r-   r+   r   r   (   s'    6 * *$DL9r-   r   N)re   
__future__r   r   r   apitools.base.py.exceptionsr   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper	   "googlecloudsdk.command_lib.computer
   'googlecloudsdk.command_lib.compute.tpusr   8googlecloudsdk.command_lib.compute.tpus.execution_groupsr   r/   googlecloudsdk.corer   _DEPRECATION_WARNINGDefaultUniverseOnly	DeprecateReleaseTracksr6   GABETAALPHACreateCommandr   rg   r-   r+   <module>rx      s    < &  ' 9 9 0 ( 4 G V #   
  D%%(($*;*;*@*@%%++-F9T F9-	  F9r-   