
                            S r SSKJr  SSKrSSKrSSKrSSKJrJrJ	r	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 jrSSS jjr " S S5      rg)z-Utility functions for clusters command group.    )annotationsN)AnyDictListSet)messages)
exceptions)concepts)
flag_types)	yaml_data)concept_parsers)presentation_specs)filesc                   [         R                  R                  S5      n[        R                  R                  UR                  5       SUS9n[        R                  " SUSSS9n[        R                  " U/5      R                  U 5        g)z&Adds a cluster name resource argument.z5cluster_director.clusters.projects_locations_clustersT)is_positionalapi_versionclusterz
        Name of the cluster resource.
        Formats: cluster | projects/{project}/locations/{locations}/clusters/{cluster}
      )nameconcept_specrequired
group_helpN)r   ResourceYAMLDataFromPathr
   ResourceSpecFromYamlGetDatar   ResourcePresentationSpecr   ConceptParserAddToParser)parserr   cluster_dataresource_specpresentation_specs        Alib/googlecloudsdk/command_lib/cluster_director/clusters/utils.pyAddClusterNameArgToParserr%   #   s    ++44=, ''00Dk 1 - )AA 
	 !2 34@@H    c                    U (       a  U [         R                  ;  a  [        R                  " SU  35      e[         R                  U    $ )z3Returns the cluster spec for the given API version.zUnsupported API version: )r    API_VERSION_TO_CLUSTER_FLAG_TYPEr	   ToolException)r   s    r$   GetClusterFlagTyper*   7   s?     	JGG	G

"
"%>{m#L
MM		4	4[	AAr&   c                     \ rS rSrSr  S. S/S jjrS rS rS rS r	S	 r
S
 rS rS r  S0     S1S jjrS rS rS rS rS rS rS2S jrS2S jrS3S jrS2S jrS2S jrS2S jrS2S jrS r S4 S5S jjr    S6S jrS rS7S jr S8S  jr!S9S! jr"S" r#          S:S# jr$        S;S$ jr%            S<S% jr&        S=S& jr'S' r(S( r)S) r*S* r+S+ r,S, r-S-r.g)>ClusterUtilA   z#Represents a cluster utility class.Nc                    Xl         X l        U R                   R                  R                  R	                  5       U l        X0l        U(       a  X@l        g[        5       U l        g)z&Initializes the cluster utility class.N)	argsmessage_moduleCONCEPTSr   Parsecluster_refexisting_clustersetupdate_mask)selfr/   r0   r4   r6   s        r$   __init__ClusterUtil.__init__D   sG     I(yy))11779D,0;Dr&   c                    [         R                  " U R                  R                  U R                  R
                  5      $ )z6Returns a cluster message from the config JSON string.)messages_utilDictToMessageWithErrorCheckr/   configr0   Cluster)r7   s    r$   MakeClusterFromConfig!ClusterUtil.MakeClusterFromConfigR   s0    44		$--55 r&   c                    U R                  5       nU R                  5       Ul        U R                  5       Ul        U R                  5       Ul        U R                  R                  U R                  U5      S9Ul
        U$ )z2Returns a cluster message from the granular flags.slurm)MakeClusterBasicMakeClusterNetworksnetworkResourcesMakeClusterStoragesstorageResourcesMakeClusterComputecomputeResourcesr0   OrchestratorMakeClusterSlurmOrchestratororchestratorr7   r   s     r$   MakeClusterClusterUtil.MakeClusterX   sv    ##%G#779G#779G#668G..;;//8 < G Nr&   c                   U R                   R                  R                  R                  5       nU R                  R                  UR                  5       S9nU R                   R                  S5      (       a  U R                   R                  Ul        U R                   R                  S5      (       aI  U R                  U R                   R                  U R                  R
                  R                  5      Ul
        U$ )z*Makes a cluster message with basic fields.)r   descriptionlabels)r/   r1   r   r2   r0   r>   NameIsSpecifiedrR   
MakeLabelsrS   LabelsValue)r7   r3   r   s      r$   rD   ClusterUtil.MakeClusterBasicc   s    ))$$,,224K!!)){/?/?/A)BGyy]++ II11gyyX&&
))

D//77CCgn Nr&   c                   U R                   R                  R                  5       nU R                  R	                  S5      (       a  U R                  R
                  R                  S5      nU R                  U5      nUR                  R                  U R                   R                  R                  R                  SU 3U R                   R                  U R                   R                  U R                   R                  UU R                  R
                  R                  S5      S9S9S9S95        U R                  R	                  S	5      (       a  U R                  R	                  S
5      (       a  U R                  R                  nU R                  U5      nUR                  R                  U R                   R                  R                  R                  SU 3U R                   R                  U R                   R                  U R                   R                  UU R!                  U R                  R"                  5      S9S9S9S95        U$ )z,Makes a cluster message with network fields.create_networkr   znet-rR   )networkrR   )
newNetworkr=   keyvaluer[   subnet)r[   
subnetwork)existingNetwork)r0   r>   NetworkResourcesValuer/   rU   rZ   get_GetNetworkNameadditionalPropertiesappendAdditionalPropertyNetworkResourceNetworkResourceConfigNewNetworkConfigr[   ExistingNetworkConfig_GetSubNetworkNamera   )r7   networks
network_idnetwork_names       r$   rE   ClusterUtil.MakeClusterNetworkso   s   ""**@@BHyy-..99++//7j))*5l##**



%
%
;
;
N
N%''77,,BB!%!4!4!E!E".&*ii&>&>&B&B+' "F " C  8 	 O  yyY''DII,A,A(,K,K99$$j))*5l##**



%
%
;
;
N
N%''77,,BB&*&9&9&O&O".%)%<%<TYY=M=M%N 'P ' C  8  O 
 Or&   c                   U R                   R                  R                  5       nSnU R                  R	                  S5      (       GaG  U R                  R
                   GH,  nU R                  U5      nUS-  nUR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  U R                   R                  U R                  UR                  S5      5      UR                  S5      U R                   R                  UR                  S5      UR                  S5      S9/UR                  S	5      UR                  S
5      S9S9S9S95        GM/     U R                  R	                  S5      (       a  U R                  R                    H  nU R                  U5      nUS-  nUR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  U R                   R#                  U R                  U5      S9S9S9S95        M     U R                  R	                  S5      (       Ga  U R                  R$                   H  nU R                  U5      nUS-  nUR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  U R                   R'                  U R)                  UR                  S5      5      UR                  S5      UR                  S5      UR                  S
5      S9S9S9S95        M     U R                  R	                  S5      (       a  U R                  R*                   H  nU R                  U5      nUS-  nUR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  U R                   R-                  U R)                  U5      S9S9S9S95        M     U R                  R	                  S5      (       Ga  U R                  R.                   GH  nU R                  U5      nUS-  nU R                   R1                  UR                  S5      S9nSU;   a  UR                  S5      Ul        UR2                  (       d3  SU;   a-  U R                   R5                  UR                  S5      S9Ul        UR2                  (       d@  UR6                  (       d/  U R                   R0                  R8                  R:                  Ul        SU;   a-  U R                   R=                  UR                  S5      S9Ul        UR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  US9S9S95        GM     U R                  R	                  S 5      (       a  U R                  R@                   H  nU R                  U5      nUS-  nUR                  R                  U R                   R                  R                  R                  UU R                   R                  U R                   R                  U R                   RC                  US9S!9S9S95        M     U$ )"z,Makes a cluster message with storage fields.
   create_filestores   r   tier
capacityGb	filesharerx   	fileShareprotocolrR   	filestorerw   
fileSharesr|   rR   newFilestorer]   r^   
filestoresr~   existingFilestorecreate_lustres
filesystemlustrer   rx   rR   	newLustrelustresr   existingLustrecreate_bucketsbucketstorageClassenableAutoclassenabled	enableHNS	newBucketbucketsexistingBucket)"r0   r>   StorageResourcesValuer/   rU   ru   _GetNextStorageIdrg   rh   ri   StorageResourceStorageResourceConfigNewFilestoreConfig_GetFilestoreNamere   FileShareConfigr   ExistingFilestoreConfigr   NewLustreConfig_GetLustreNamer   ExistingLustreConfigr   NewBucketConfigr   GcsAutoclassConfig	autoclassStorageClassValueValuesEnumSTANDARDGcsHierarchicalNamespaceConfighierarchicalNamespacer   ExistingBucketConfig)r7   storagesstorage_counterr~   
storage_idr   
gcs_bucketgcss           r$   rG   ClusterUtil.MakeClusterStorages   s   ""**@@BH Oyy011yy22)++O<
1%%,,''==PP))99..DD%)%8%8%K%K&*&<&< )f 5' "+v!6 $ 3 3 C C/8}}\/J.7mmK.H !D !"( &/]]:%>(1m(D &L & E  :  Q 	
 34 yy\**yy++)++O<
1%%,,''==PP))99..DD*.*=*=*U*U&*&<&<Y&G +V + E  :  Q 		
 , yy-..II,,&++O<
1%%,,''==PP))99..DD"&"5"5"E"E#'#6#6vzz&7I#J'-zz,'?'-zz,'?(.

=(A	 #F # E  : 	 Q 	
 -$ yyY''II%%&++O<
1%%,,''==PP))99..DD'+':':'O'O#'#6#6v#> (P ( E  :  Q 		
 & yy-..		00*++O<
1!!11>>&) 2 
 Z''^^N;#
$5$C--@@ nn%67 A #-
 !!11MMVV 
 *$!!@@$..5 A  
#
 	%%,,''==PP))99..DD"% E  :  Q 		
1 1D yyY''		))*++O<
1%%,,''==PP))99..DD'+':':'O'O#- (P ( E  :  Q 		
 * Or&   c           	        U R                   R                  S5      (       dv  U R                   R                  S5      (       dV  U R                   R                  S5      (       d6  U R                   R                  S5      (       d  [        R                  " S5      e[	        5       nU R
                  R                  R                  5       nU R                   R                  S5      (       a  U R                   R                   H{  nUR                  S5      nUR                  U5        UR                  R                  U R
                  R                  R                  R                  UU R                  U5      S95        M}     U R                   R                  S5      (       a  U R                   R                   H{  nUR                  S5      nUR                  U5        UR                  R                  U R
                  R                  R                  R                  UU R!                  U5      S95        M}     U R                   R                  S5      (       a  U R                   R"                   H{  nUR                  S5      nUR                  U5        UR                  R                  U R
                  R                  R                  R                  UU R%                  U5      S95        M}     U R                   R                  S5      (       a  U R                   R&                   H{  nUR                  S5      nUR                  U5        UR                  R                  U R
                  R                  R                  R                  UU R)                  U5      S95        M}     [+        U5      [+        UR                  5      :w  a  [        R                  " S5      eU$ )	z,Makes a cluster message with compute fields.on_demand_instancesspot_instancesreserved_instancesdws_flex_instanceszvAt least one of on_demand_instances, spot_instances, reserved_instances, or dws_flex_instances flag must be specified.idr^   z7Compute instances with duplicate ids are not supported.)r/   rU   r	   r)   r5   r0   r>   ComputeResourcesValuer   re   addrg   rh   ri   _MakeOnDemandComputeResourcer   _MakeSpotComputeResourcer   _MakeReservedComputeResourcer   _MakeDwsFlexComputeResourcelen)r7   compute_idscomputeinstance
compute_ids        r$   rI   ClusterUtil.MakeClusterCompute  s    II!!"788		%%&677		%%&:;;		%%&:;;$$O  %K!!))??AGyy233ii33(\\$'

#$$++''==PP77A Q 	
 4 yy-..ii..(\\$'

#$$++''==PP33H= Q 	
 / yy122ii22(\\$'

#$$++''==PP77A Q 	
 3 yy122ii22(\\$'

#$$++''==PP66x@ Q 	
 3 ;3w;;<<$$
C  Nr&   c                "   U R                   R                  5       nU R                  U5      nU R                  R	                  S5      (       aj  U R                  R
                   HP  nUR                  S5      nU R                  U5      nUR                  R                  U R                  XFU5      5        MR     U R                  R	                  S5      (       aG  U R                  R                   H-  nUR                  R                  U R                  U5      5        M/     U R                  R	                  S5      (       a  U R                  R                  Ul        U R                  R	                  S5      (       Ga  U R                  R                   nUR                  S5      nU R                   R#                  UR                  SS5      UUR                  S	5      UUR                  S
S5      UR                  SS5      U R%                  UR                  S5      5      U R'                  UR                  S5      U R                   R"                  R(                  S9S9Ul        UR*                  R,                  R                  U R/                  US95        U$ )z7Makes a cluster message with slurm orchestrator fields.slurm_node_sets	computeIdslurm_partitionsslurm_default_partitionslurm_login_nodemachineTypecountrv   zoneenableOSLoginTenablePublicIpsstartupScriptrS   
label_args	label_cls)r   r   r   storageConfigsenableOsLoginr   r   rS   machine_type)r0   SlurmOrchestrator_GetStorageConfigsr/   rU   r   re   _GetComputeMachineTypeFromArgsnodeSetsrh   _MakeSlurmNodeSetr   
partitions_MakeSlurmPartitionr   defaultPartitionr   SlurmLoginNodes_GetBashScriptrV   rW   
loginNodesdisksMakeDisk)	r7   r   rC   storage_configsnode_setr   r   	partition
login_nodes	            r$   rL   (ClusterUtil.MakeClusterSlurmOrchestrator[  s   113E(  yy.//ii//(\\+.
:::F""8?K	
 0 yy/00yy11) 8 8 CD 2 yy677#yy@@eyy/0099--j^^M2l,,<<w*"~~f%("=$..):DA++JNN?,KL#1++;;GG !  = e ##DMM|M$LMLr&   c                    U(       d  gU" [        UR                  5       5       VVs/ s H  u  p4UR                  X4S9PM     snnS9$ s  snnf )zReturns the labels message.Nr^   )rg   )sorteditemsri   )r7   r   r   r_   r`   s        r$   rV   ClusterUtil.MakeLabels  sU     %Z%5%5%78
8
 ((S(>8
 
s   A
c                l    SnUR                  S5      (       a  SnU R                  R                  UUUS9$ )zHReturns the disk message, defaults to boot disk with empty source image.zpd-standard)z
a3-megagpuza3-ultragpuz
a4-highgpuza4x-highgpuzhyperdisk-balanced)typebootsourceImage)
startswithr0   Disk)r7   r   r   source_image	disk_types        r$   r   ClusterUtil.MakeDisk  sM     IB  'i##  $  r&   c                P    U R                  5       nXR                  R                  4$ )z,Returns the cluster message from the config.)r?   r/   r6   rN   s     r$   MakeClusterPatchFromConfig&ClusterUtil.MakeClusterPatchFromConfig  s#    ((*GII))))r&   c                   U R                  5       nU R                  5       Ul        U R                  5       Ul        U R
                  R                  U R                  U5      S9Ul        USR                  [        U R                  5      5      4$ )z2Returns the cluster patch message and update mask.rB   ,)MakeClusterBasicPatchMakeClusterStoragesPatchrH   MakeClusterComputePatchrJ   r0   rK   !MakeClusterSlurmOrchestratorPatchrM   joinr   r6   rN   s     r$   MakeClusterPatchClusterUtil.MakeClusterPatch  s    ((*G#<<>G#;;=G..;;44W= < G CHHVD$4$45666r&   c                   U R                   R                  5       nU R                  R                  S5      (       a6  U R                  R                  Ul        U R
                  R                  S5        U R                  U R                  R                  5      nSnSnU R                  R                  S5      (       a1  U R                  R                   H  nU R                  XRU5        SnM     U R                  R                  S5      (       a'  UR                  U R                  R                  5        SnU(       aN  U R                  UU R                   R                  R                  S9Ul	        U R
                  R                  S5        U$ )	z0Makes a cluster patch message with basic fields.rR   FzLabel with key={0} not found.remove_labelsT
add_labelsr   rS   )r0   r>   r/   rU   rR   r6   r   _ConvertMessageToDictr4   rS   r  _RemoveKeyFromDictSpecupdater  rV   rW   )r7   r   rS   is_labels_updatedexception_messager_   s         r$   r   !ClusterUtil.MakeClusterBasicPatch  s7   !!))+Gyy]++ II11g
=)''(=(=(D(DEF7yy_--((###C1BC  ) yy\**mmDII(()''//;; ' gn 8$Nr&   c                <   U R                   R                  R                  5       nU R                  U R                  R
                  5      nSnU R                  R                  S5      (       GaC  U R                  R                   Vs1 s H  nU R                  U5      iM     nn[        5       n[        5       nUR                  5        H  u  pU	R                  n
SnU
(       a(  U
R                  (       a  U
R                  R                  nO.U
(       a'  U
R                  (       a  U
R                  R                  nX;   d  Mw  UR!                  U5        UR!                  U5        M     Xu:w  a,  XW-
  n["        R$                  " SSR'                  U5       35      eU H  nUR)                  U5        M     SnU R                  R                  S5      (       Ga8  [        U R                  R*                  5      n[        5       n[        5       nUR                  5        H  u  pU	R                  n
SnU
(       aO  U
R,                  (       a  U
R,                  R.                  nO'U
R0                  (       a  U
R0                  R.                  nX;   d  Mp  UR!                  U5        UR!                  U5        M     X:w  a>  X-
  n["        R$                  " SSR'                  [3        [5        U5      5      5       35      eU H  nUR)                  U5        M     SnU R                  R                  S	5      (       GaE  U R                  R6                   Vs1 s H  o@R9                  U5      iM     nn[        5       n[        5       nUR                  5        H  u  pU	R                  n
SnU
(       a(  U
R:                  (       a  U
R:                  R<                  nO.U
(       a'  U
R>                  (       a  U
R>                  R<                  nUU;   d  Mx  UR!                  U5        UR!                  U5        M     UU:w  a-  UU-
  n["        R$                  " S
SR'                  U5       35      eU H  nUR)                  U5        M     SnSnU(       a  URA                  5        Vs/ s HU  nURC                  S5      (       d  M  U[E        S5      S RG                  5       (       d  M>  [I        U[E        S5      S 5      PMW     nnU(       a  [K        U5      S-   nU R                  R                  S5      (       Ga  U R                  RL                   GH  nU RO                  U5      nUS-  nU R                  URQ                  S5      5      nURS                  5        H  n	U	R                  n
U
(       d  M  U
R                  (       a  U
R                  R                  U:X  d/  U
R                  (       d  MV  U
R                  R                  U:X  d  Mr  ["        R$                  " SU S35      e   U R                   RU                  U R                   RW                  U R                   RY                  UURQ                  S5      U R                   R[                  URQ                  S5      URQ                  S5      S9/URQ                  S5      URQ                  S5      S9S9S9X('   GM     SnU R                  R                  S5      (       Ga/  U R                  R\                   GH  nU RO                  U5      nUS-  nU R                  U5      nURS                  5        H  n	U	R                  n
U
(       d  M  U
R                  (       a  U
R                  R                  U:X  d/  U
R                  (       d  MV  U
R                  R                  U:X  d  Mr  ["        R$                  " SU S35      e   U R                   RU                  U R                   RW                  U R                   R_                  US9S9S9X('   GM     SnU R                  R                  S5      (       Ga  U R                  R`                   GHo  nU RO                  U5      nUS-  nU R9                  URQ                  S5      5      nURS                  5        H  n	U	R                  n
U
(       d  M  U
R:                  (       a  U
R:                  R<                  U:X  d/  U
R>                  (       d  MV  U
R>                  R<                  U:X  d  Mr  ["        R$                  " SU S35      e   U R                   RU                  U R                   RW                  U R                   Rc                  U R9                  URQ                  S5      5      URQ                  S 5      URQ                  S5      URQ                  S5      S!9S"9S9X('   GMr     SnU R                  R                  S#5      (       Ga/  U R                  Rd                   GH  nU RO                  U5      nUS-  nU R9                  U5      nURS                  5        H  n	U	R                  n
U
(       d  M  U
R:                  (       a  U
R:                  R<                  U:X  d/  U
R>                  (       d  MV  U
R>                  R<                  U:X  d  Mr  ["        R$                  " SU S35      e   U R                   RU                  U R                   RW                  U R                   Rg                  US$9S%9S9X('   GM     SnU R                  R                  S&5      (       Ga  U R                  Rh                   H  nU RO                  U5      nUS-  nURS                  5        H  n	U	R                  n
SnU
(       aO  U
R,                  (       a  U
R,                  R.                  nO'U
R0                  (       a  U
R0                  R.                  nUU:X  d  Mo  ["        R$                  " S'U S35      e   U R                   RU                  U R                   RW                  U R                   Rk                  US(9S)9S9X('   GM      SnU R                  R                  S*5      (       Ga!  U R                  Rl                   GH  nU RO                  U5      nUS-  nURQ                  S5      nURS                  5        H  n	U	R                  n
SnU
(       aO  U
R,                  (       a  U
R,                  R.                  nO'U
R0                  (       a  U
R0                  R.                  nUU:X  d  Mo  ["        R$                  " S'U S35      e   U R                   Ro                  URQ                  S5      S(9nS+U;   a  URQ                  S+5      Ul8        URp                  (       d3  S,U;   a-  U R                   Rs                  URQ                  S,5      S-9Ul:        URp                  (       d@  URt                  (       d/  U R                   Rn                  Rv                  Rx                  Ul8        S.U;   a-  U R                   R{                  URQ                  S.5      S-9Ul>        U R                   RU                  U R                   RW                  US/9S9X('   GM     SnU(       ar  UR                  5        VVs/ s H4  u  nnU R                   R                  R                  R                  UUS09PM6     snnUl@        U R                  R!                  S15        U$ s  snf s  snf s  snf s  snnf )2z2Makes a cluster patch message with storage fields.Fremove_filestore_instancesNzFilestore(s) not found: z, Tremove_storage_bucketsz#Cloud Storage bucket(s) not found: remove_lustre_instanceszLustre(s) not found: rt   storagerv   add_new_filestore_instancesr   z
Filestore z already exists.rw   rx   ry   rz   r|   rR   r}   r   r]   add_filestore_instancesr   r   add_new_lustre_instanceszLustre r   r   r   add_lustre_instancesr   r   add_storage_bucketszCloud Storage bucket r   r   add_new_storage_bucketsr   r   r   r   r   r^   storage_resources)Br0   r>   r   r	  r4   rH   r/   rU   r  r   r5   r   r=   r   r~   r   r   r	   r)   r  popr  r   r   r   r   listr  r   r   r   r   keysr   r   isdigitintmaxr  r   re   valuesr   r   r   r   r  r   r  r   r  r   r  r   r  r   r   r   r   r   r   r   r   ri   rg   r6   )r7   r  r   is_storage_updatedffilestores_to_removestorage_ids_to_removefound_filestoresr   storage_resourcer=   filestore_name	not_foundbuckets_to_removefound_bucketsbucket_namelustres_to_removefound_lustreslustre_namer   kstorage_idsr~   r   r   bucket_name_in_configr   b_namer   r_   r`   s                                  r$   r   $ClusterUtil.MakeClusterStoragesPatch  su   ++33IIK))..H yy9:: 99777a 
 
 
#7   "e*2..*:
&*!((f))!..88.00!33==.1

#
#J
/


~
. +; 
	1(;	&&&tyy';&<=
 	
 .*Z  .yy566dii>>?!eem*2..*:
&*!(( **11K$$ //66K+

#
#J
/


K
( +; 
	+%5	&&		&i1235
 	

 .*Z  .yy677*.))*K*K*KQ

a
 *K   "eem*2..*:
&*!((f&&((//+----44+++

#
#J
/


K
( +; 
+	+%5	&&#DIIi$8#9:
 	
 .*Z  .O ==?"a\\)$ #)*3y>+;)<)D)D)F ##aI !
""  
 
k*Q.yy:;;yy<<)++O<
1//	f0EF ( 1#**&V%%))33~E ***..88NJ **^,,<=  !2   $22BB&&<<!00CC,"v.++;;'0}}\'B&/mmK&@ <   ']]:6 )m < D  =   C  
) =H  yy677yy88)++O<
1//	: ( 1#**&V%%))33~E ***..88NJ **^,,<=  !2   $22BB&&<<"&"5"5"M"M, #N # =   C  
) 96  yy788II66&++O<
1))&**V*<= ( 1#**&VF$4$4$;$;{$J'''++22kA **+&67  !2  $22BB&&<<--==..vzz&/AB%zz,7%zz,7 &

= 9	 >  =   C 	 
# 76  yy344II22&++O<
1))&1 ( 1#**&VF$4$4$;$;{$J'''++22kA **+&67  !2  $22BB&&<<#22GG&  H   =   C  
# 30  yy233II11&++O<
1 ( 1#**&"&
&,&6&6&=&=#&&&,&;&;&B&B#"f,**'x/?@  !2  $22BB&&<<#22GG!  H   =   C  
% 22  yy677		99*++O<
1 nnV, ( 1#**&&''..f&&,,33f{"**'}4DE  !2 !!11>>&) 2 
 Z''^^N;#
$5$C--@@ nn%67 A #- !!11MMVV 
 *$!!@@$..5 A  
#
  $22BB&&<<s<K  C  
G :L  
 %NN,	0 -jc5 


%
%
;
;
N
NU O  -	0, ./Y	v<X0s$   |	|
|(||(;|c           	        U R                   R                  R                  5       nU R                  U R                  R
                  5      nSnSnSnU R                  R                  S5      (       a6  U R                  R                   H  nU R                  UUS/USU 3S9  SnM     U R                  R                  S	5      (       a6  U R                  R                   H  nU R                  UUS
/USU 3S9  SnM     U R                  R                  S5      (       a6  U R                  R                   H  nU R                  UUS/USU 3S9  SnM     U R                  R                  S5      (       a7  U R                  R                   H  nU R                  UUSS/USU 3S9  SnM     U R                  R                  S5      (       aO  U R                  R                   H5  nU R                  UR                  S5      UU R!                  U5      US9  SnM7     U R                  R                  S5      (       aO  U R                  R"                   H5  nU R                  UR                  S5      UU R%                  U5      US9  SnM7     U R                  R                  S5      (       aO  U R                  R&                   H5  nU R                  UR                  S5      UU R)                  U5      US9  SnM7     U R                  R                  S5      (       aO  U R                  R*                   H5  nU R                  UR                  S5      UU R-                  U5      US9  SnM7     U(       a  UR/                  5        VV	s/ s H2  u  pU R                   R                  R                  R1                  XS9PM4     sn	nUl        UR2                  (       d  [4        R6                  " S5      eU R8                  R;                  S5        U$ s  sn	nf )z:Makes a cluster compute patch message with compute fields.Fz(Compute instances with id={0} not found.z,Compute instances with id={0} already exist.remove_on_demand_instancesnewOnDemandInstancesz
On demand )r_   	dict_specattrskey_exception_messageattr_exception_messageTremove_spot_instancesnewSpotInstanceszSpot remove_reserved_instancesnewReservedInstancesz	Reserved remove_dws_flex_instancesnewDwsFlexInstancesnewFlexStartInstancesz	DWS Flex add_on_demand_instancesr   r_   r8  r`   r  add_spot_instancesadd_reserved_instancesadd_dws_flex_instancesr^   z"Compute instances cannot be empty.zcompute.resource_requests)r0   r>   r   r	  r4   rJ   r/   rU   r6  _RemoveKeyByAttrFromDictSpecr<  r>  r@  rC  _AddKeyToDictSpecre   r   rE  r   rF  r   rG  r   r   ri   rg   r	   r)   r6   r   )
r7   compute_resourcesr   is_compute_updatedex_msg_not_foundex_msg_already_existr   r   r_   r`   s
             r$   r  #ClusterUtil.MakeClusterComputePatch  s   ++33IIK(()>)>)O)OPGAIyy9::		<<*)))*"2%/0@/A#B 	* 	
 " = yy455		77*))%&"2%*+;*<#= 	* 	
 " 8 yy899		;;*)))*"2%./?.@#A 	* 	
 " < yy899		;;*))(*AB"2%./?.@#A 	* 	
 " < yy677ii77(T"33H=2	 	 	
 " 8 yy122ii22(T"//92	 	 	
 " 3 yy566ii66(T"33H=2	 	 	
 " 7 yy566ii66(T"228<2	 	 	
 " 7 
 $MMO	0 ,jc 


%
%
;
;
N
N O  ,	0, 33&&'KLL
670s   ?9Pc           
        U R                   R                  5       nU R                  R                  S5      (       a6  U R                  R                  Ul        U R                  R                  S5        U R                  U R                  R                  R                  R                  5      nSnSnSnU R                  R                  S5      (       a1  U R                  R                   H  nU R                  XsU5        SnM     U R                  R                  S5      (       a~  U R                  R                   Hd  nUR!                  S	5      nU R#                  XsU5      n	S
U;   a  UR!                  S
5      U	l        SU;   a  UR!                  S5      U	l        XU'   SnMf     U R                  R                  S5      (       a  U R                  R(                   H  nU R                  n
UR*                  (       a  UR*                  R,                  (       a  Un
U R/                  U
5      nUR!                  S5      nU R1                  XSS9nU R3                  UR!                  S	5      UU R5                  XU5      US9  SnM     U(       a`  [7        UR9                  5       5      Ul        UR                  (       d  [:        R<                  " S5      eU R                  R                  S5        U R                  U R                  R                  R                  R>                  5      nSnSnSnU R                  R                  S5      (       a1  U R                  R@                   H  nU R                  UX5        SnM     U R                  R                  S5      (       a  U R                  RB                   He  nUR!                  S	5      nU R#                  UX5      nSU;   a  UR!                  S5      Ul"        SU;   a  UR!                  S5      Ul#        UUU'   SnMg     U R                  R                  S5      (       aO  U R                  RH                   H5  nU R3                  UR!                  S	5      UU RK                  U5      US9  SnM7     U(       a`  [7        UR9                  5       5      Ul        UR>                  (       d  [:        R<                  " S5      eU R                  R                  S5        U R                  R                  S5      (       a  U R                  R                  R                  RL                  (       d  [:        R<                  " S5      eU R                  R                  R                  RL                  nU R                  RN                  nUR!                  S5      =nb  UUl(        UR!                  S5      =nb  U RS                  U5      Ul*        UUl&        U R                  R                  S5        U$ ) zCMakes a cluster slurm orchestrator patch message with slurm fields.r   z$orchestrator.slurm.default_partitionFz%Slurm nodesets with id={0} not found.z)Slurm nodesets with id={0} already exist.remove_slurm_node_setsTupdate_slurm_node_setsr   staticNodeCountmaxDynamicNodeCountadd_slurm_node_setsr   )use_existing_clusterrD  zSlurm nodesets cannot be empty.zorchestrator.slurm.node_setsz'Slurm partitions with id={0} not found.z+Slurm partitions with id={0} already exist.remove_slurm_partitionsupdate_slurm_partitions
nodesetIds	exclusiveadd_slurm_partitionsz!Slurm partitions cannot be empty.zorchestrator.slurm.partitionsupdate_slurm_login_nodezFLogin node is not part of existing cluster spec and cannot be updated.r   r   zorchestrator.slurm.login_nodes)+r0   r   r/   rU   r   r   r6   r   _ConvertSlurmMessageToDictr4   rM   rC   r   rP  r
  rQ  re   _GetValueFromDictSpecrR  rS  rT  rH   rg   r   !_GetComputeMachineTypeFromClusterrI  r   r  r!  r	   r)   r   rV  rW  
nodeSetIdsrY  rZ  r   r   r[  r   r   r   )r7   cluster_patchrC   r   is_node_sets_updatedrL  rM  node_set_idr   existing_node_setstorage_configs_sourcer   r   r   r   is_partitions_updatedpartition_idr   existing_partitionlogin_nodeslogin_node_patchr   startup_scripts                          r$   r  -ClusterUtil.MakeClusterSlurmOrchestratorPatch^  s@   113Eyy677#yy@@e
AB55**0099O !>Fyy56699+##*:	
  $	 :
 yy566ii66(ll4( 66*:
 (.6ll;L.M

+ H,2:,,#3

/ (9$# 7 yy233ii33(!%!6!6**..CC#0
 112HI\\+.
==D > 
 	T"%(( 3 	 	
  $) 4* O2245en^^&&'HII
9:66**00;; "@Hyy677));;,##*	
 !%	 <
 yy677yy88) }}T*!77*
 9$*3--*E

')#)2{)C

&);& $ 9 yy344yy55)d#&**952	 	 	
 !% 6 .5578e&&'JKK
:;yy677""//55@@&&
 	
 ))66<<GGk::#''0
0%	=!,00A
A.	N$($7$7$G!$e
;<Lr&   c                Z    U R                   R                  5       R                  nSU SU 3$ )zReturns the network name.	projects/z/global/networks/r3   Parent
projectsId)r7   r[   projects      r$   rf   ClusterUtil._GetNetworkName  s0    %%'22Gwi0	::r&   c                Z    U R                   R                  5       R                  nSU SU 3$ )zReturns the subnetwork name.rm  /rn  )r7   rb   rq  s      r$   rn   ClusterUtil._GetSubNetworkName  s/    %%'22Gwiq--r&   c                    SU 3$ )zReturns the next storage ID.r   )r7   r   s     r$   r   ClusterUtil._GetNextStorageId  s    _%&&r&   c                Z    U R                   R                  5       R                  nSU SU 3$ )zReturns the filestore name.rm  rt  rn  )r7   r~   rq  s      r$   r   ClusterUtil._GetFilestoreName  s/    %%'22Gwiq,,r&   c                Z    U R                   R                  5       R                  nSU SU 3$ )zReturns the Lustre name.rm  rt  rn  )r7   r   rq  s      r$   r   ClusterUtil._GetLustreName  s/    %%'22Gwiq))r&   c                    U R                   R                  5       R                  nUR                  S5      (       a  U$ SU SU 3$ )zReturns the reservation name.rm  rt  )r3   ro  rp  r   )r7   reservationrq  s      r$   _GetReservationNameClusterUtil._GetReservationName  sF    %%'22Gk**wiq..r&   c                    UR                  S5      n[        X"SS 5       H  u  p4US:X  d  M  U(       d  M  Us  $    [        R                  " SU S35      e)zReturns the reservation zone.rt  rv   NzoneszReservation z does not contain a zone.)splitzipr	   r)   )r7   r~  partscurrent_part	next_parts        r$   _GetReservationZoneClusterUtil._GetReservationZone  s`     c"E#&uABi#8		 YY $9 
"
"
{m#<= r&   c                   / nU R                   R                  S5      (       a%  UR                  U R                   R                  5        U R                   R                  S5      (       a%  UR                  U R                   R                  5        U R                   R                  S5      (       a%  UR                  U R                   R
                  5        U R                   R                  S5      (       a%  UR                  U R                   R                  5        U H+  nUR                  S5      U:X  d  M  UR                  S5      s  $    [        R                  " SU S35      e)	z+Returns the compute machine type from args.r   r   r   r   r   r   Compute instances with id= not found.)
r/   rU   extendr   r   r   r   re   r	   r)   )r7   r   	instancesr   s       r$   r   *ClusterUtil._GetComputeMachineTypeFromArgs  s
   Iyy233tyy445yy-..tyy//0yy122tyy334yy122tyy334	d	z	)||M**  
"
"
$ZL< r&   c                *   U(       a1  U R                  UR                  5      nX;   a  U R                  X5      $ U(       a;  U R                  U R                  R                  5      nX;   a  U R                  X5      $ [        R
                  " SU S35      e)z.Returns the compute machine type from cluster.r  r  )r	  rJ   _GetComputeMachineTyper4   r	   r)   )r7   r   r   rU  rJ  s        r$   r^  -ClusterUtil._GetComputeMachineTypeFromCluster  s     44W5M5MN		(**:II44



0
0 
	(**:II

"
"
$ZL< r&   c                   X!   nUR                   R                  (       a   UR                   R                  R                  $ UR                   R                  (       a   UR                   R                  R                  $ UR                   R                  (       a   UR                   R                  R                  $ UR                   R
                  (       a   UR                   R
                  R                  $ UR                   R                  (       a   UR                   R                  R                  $ [        R                  " S5      e)z8Returns the compute machine type from compute resources.z%Compute instances type not supported.)	r=   r7  r   r=  r?  rA  rB  r	   r)   )r7   r   rJ  compute_resources       r$   r  "ClusterUtil._GetComputeMachineType"  s     )433$$99EEE//$$55AAA33$$99EEE22$$88DDD44$$::FFF

"
"#J
KKr&   c                   / n[        UR                  R                  S S9nU(       a8  US   nUR                  U R                  R                  UR                  SS95        [        R                  " [        5      nUSS  GH  nSnUR                  (       Ga  UR                  R                  R                  (       d%  UR                  R                  R                  (       a  SUS	    3nUS	==   S-  ss'   OUR                  R                  R                  (       d%  UR                  R                  R                  (       a  S
US    3nUS==   S-  ss'   O_UR                  R                  R                   (       d%  UR                  R                  R"                  (       a  SUS    3nUS==   S-  ss'   U(       d  [$        R&                  " S5      eUR                  U R                  R                  UR                  US95        GM     U$ )zReturns the storage configs.c                    U R                   $ Nr_   )r  s    r$   <lambda>0ClusterUtil._GetStorageConfigs.<locals>.<lambda>8  s    GKKr&   r  r   z/home)r   
localMountrv   Nz/sharedr~   z/scratchr   z/datar   z'Storage configuration is not supported.)r   rH   rg   rh   r0   StorageConfigr_   collectionsdefaultdictr  r`   r=   r   r   r   r   r   r   r	   r)   )r7   r   r   sorted_storagesfirst_storagecountersr  local_mounts           r$   r   ClusterUtil._GetStorageConfigs3  s   ?AO  55'O %a(m



+
+""  ,  &&s+H"12&k	MM  --}}##55!(;"7!89+
;
1
$
MM  **}}##22"8H#5"67+
8

!
MM  **}}##22 234+
8

!
&&5
 	
 



+
+$ , 5 '@ r&   c                   U(       a  U R                  U5      (       d  U$ Un[        R                  R                  U5      (       d  [        R
                  " SU 35      e[        R                  R                  U5      (       a$  [        R                  R                  U5      (       d  [        R
                  " SU 35      e[        R                  " U5      $ )z>Returns the bash script if argument is a valid bash file path.z'Script file path must be absolute, got z'Script file not found at absolute path=)
_CheckIfBashFileFormatospathisabsr	   BadFileExceptionexistsisfiler   ReadFileContents)r7   	arg_valuer  s      r$   r   ClusterUtil._GetBashScripte  s    D77	BBD77==''3D6
:  77>>$rww~~d';';''3D6
:  !!$''r&   c                0    [         R                  " SU5      $ )z-Checks if the argument is a bash file format.z^\S*\.(sh|bash)$)rematch)r7   r  s     r$   r  "ClusterUtil._CheckIfBashFileFormatt  s    88'33r&   c                    U(       d  0 $ UR                    Vs0 s H  o"R                  UR                  _M     sn$ s  snf )zUConvert a message with list of type AdditionalProperty(key=str, value=Any) to a dict.)rg   r_   r`   r7   messageeachs      r$   r	  !ClusterUtil._ConvertMessageToDictx  s5    i-4-I-IJ-ITHHdjj -IJJJs    ;c                X    U(       d  0 $ U Vs0 s H  o"R                   U_M     sn$ s  snf )zIConvert a list of slurm message (SlurmNodeSet, SlurmPartition) to a dict.)r   r  s      r$   r\  &ClusterUtil._ConvertSlurmMessageToDict~  s(    i&-.gdGGTMg...s   'c                `    X;   a%  [         R                  " UR                  U5      5      eX2U'   g)zHAdds a cluster identifier (key) with value, if not present in dict spec.Nr	   r)   format)r7   r_   r8  r`   r  s        r$   rI  ClusterUtil._AddKeyToDictSpec  s.     $$%6%=%=c%BCCcNr&   c                z    X;  a%  [         R                  " UR                  U5      5      eUR                  U5        g)z<Removes a cluster identifier (key), if present in dict spec.N)r	   r)   r  r  r7   r_   r8  r  s       r$   r
  "ClusterUtil._RemoveKeyFromDictSpec  s3     $$%6%=%=c%BCCMM#r&   c                t  ^^ TT;  a%  [         R                  " UR                  T5      5      e[        TT   SS5      (       d%  [         R                  " UR                  T5      5      e[	        UU4S jU 5       5      (       d%  [         R                  " UR                  T5      5      eTR                  T5        g)zIRemoves a cluster identifier (key) by attribute, if present in dict spec.r=   Nc              3  X   >#    U  H  n[        TT   R                  US 5      v   M!     g 7fr  )getattrr=   ).0attrr8  r_   s     r$   	<genexpr>;ClusterUtil._RemoveKeyByAttrFromDictSpec.<locals>.<genexpr>  s'     Ledwy~,,dD99es   '*)r	   r)   r  r  anyr  )r7   r_   r8  r9  r:  r;  s    ``   r$   rH  (ClusterUtil._RemoveKeyByAttrFromDictSpec  s     )$$%:%A%A#%FGG9S>8T22$$%;%B%B3%GHHLeLLL$$%;%B%B3%GHHMM#r&   c                ^    X;  a%  [         R                  " UR                  U5      5      eX!   $ )zGReturns the value message by cluster identifier (key) from a dict spec.r  r  s       r$   r]  !ClusterUtil._GetValueFromDictSpec  s/     $$%6%=%=c%BCC>r&   c           
         U R                   R                  U R                   R                  U R                   R                  UR	                  S5      UR	                  S5      S9S9S9$ )zAMakes a cluster compute resource message for on demand instances.r   r   r   r   )r7  r]   )r0   ComputeResourceComputeResourceConfigNewOnDemandInstancesConfigre   r7   r   s     r$   r   (ClusterUtil._MakeOnDemandComputeResource  sk    ..""88!%!4!4!O!O\\&)$LL7 "P " 9 
 /  r&   c           
         U R                   R                  U R                   R                  U R                   R                  UR	                  S5      UR	                  S5      S9S9S9$ )z<Makes a cluster compute resource message for spot instances.r   r   r  )r=  r]   )r0   r  r  NewSpotInstancesConfigre   r  s     r$   r   $ClusterUtil._MakeSpotComputeResource  sk    ..""88!00GG\\&)$LL7 H  9 
 /  r&   c                b   UR                  S5      nU R                  R                  U R                  R                  U R                  R	                  U R                  U5      UR                  S5      U R                  U5      U R                  R                  R                  R                  S9S9S9$ )z@Makes a cluster compute resource message for reserved instances.r~  r   )r~  r   r   r   )r?  r]   )	re   r0   r  r  NewReservedInstancesConfigr  r  TypeValueValuesEnumSPECIFIC_RESERVATION)r7   r   r~  s      r$   r   (ClusterUtil._MakeReservedComputeResource  s    ,,}-K..""88!%!4!4!O!O 44[A$LL7--k:((CCWWll	 "P " 9 
 / 	 	r&   c                    U R                   R                  U R                   R                  U R                   R                  UR	                  S5      UR	                  S5      UR	                  S5      S9S9S9$ )z@Makes a cluster compute resource message for DWS Flex instances.r   r   maxDuration)r   r   r  )rA  r]   )r0   r  r  NewDWSFlexInstancesConfigre   r  s     r$   r   'ClusterUtil._MakeDwsFlexComputeResource  sw    ..""88 $ 3 3 M M\\&)$LL7$LL7 !N ! 9 
 /  r&   c                   U R                   R                  UR                  S5      UR                  S5      UR                  SS5      UR                  S5      UU R                  UR                  S5      5      U R	                  UR                  S5      U R                   R                  R
                  S9U R                  US	9S
9$ )z:Makes a cluster slurm node set message from node set args.r   r   rR  rv   rS  r   rS   r   r   )r   resourceRequestIdrR  rS  r   r   rS   bootDisk)r0   SlurmNodeSetre   r   rV   rW   r   )r7   r   r   r   s       r$   r   ClusterUtil._MakeSlurmNodeSet  s    ++<<",,{3 %6:$LL)>?&))(,,*GH||H-))66BB  
 L9 ,  r&   c                    U R                   R                  UR                  S5      UR                  S5      UR                  S5      S9$ )z<Makes a cluster slurm partition message from partition args.r   rX  rY  )r   r_  rY  )r0   SlurmPartitionre   )r7   r   s     r$   r   ClusterUtil._MakeSlurmPartition  sD    --====.--, .  r&   )r/   r3   r4   r0   r6   )NN)r6   zSet[str])T )r   strr   boolr   r  )returnr  )r   r  r  r  )F)r   r  )r   r  rJ  zDict[str, Any])r  r  r  z!str | exceptions.BadFileException)r  r  r  r  r  dict[str, Any])
r_   r  r8  r  r`   r   r  r  r  None | exceptions.ToolException)r_   r  r8  r  r  r  r  r  )r_   r  r8  r  r9  z	List[str]r:  r  r;  r  r  r  )r_   r  r8  r  r  r  r  zAny | exceptions.ToolException)/__name__
__module____qualname____firstlineno____doc__r8   r?   rO   rD   rE   rG   rI   rL   rV   r   r   r  r   r   r  r  rf   rn   r   r   r   r  r  r   r^  r  r   r   r  r	  r\  rI  r
  rH  r]  r   r   r   r   r   r   __static_attributes__rw  r&   r$   r,   r,   A   s   + "G
 G	
%NGR:x&P	 	  	$*
72un	]~tl;
.
'-
*
/	& <A$LL0>L"0d(4K/

  
 	

 
 '
!/DG&   	
 ! " '"!/DG%		
 r&   r,   r  r  )r  
__future__r   r  r  r  typingr   r   r   r   googlecloudsdk.api_lib.utilr   r;   googlecloudsdk.callioper	    googlecloudsdk.calliope.conceptsr
   4googlecloudsdk.command_lib.cluster_director.clustersr   $googlecloudsdk.command_lib.util.apisr   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.core.utilr   r%   r*   r,   rw  r&   r$   <module>r     sL     4 "  	 	 ' ' A . 5 K : D G *I(Bv vr&   