
    `                        S 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KJr  SSKJr  SSKrS rS rS rS rS rS r S r!S r"S r#S r$S r%S r&S r'S r(S r)S  r*S! r+S" r,S# r-S$ r.S% r/S& r0S' r1S( r2S) r3S* r4S+ r5S, r6S- r7\Rp                  \Rr                  " \Rt                  Rv                  5       " S. S/\Rx                  5      5       5       r=\Rp                  \Rr                  " \Rt                  R|                  5       " S0 S1\=5      5       5       r?g)2zHImplements command to execute an OS patch on the specified VM instances.    )absolute_import)division)unicode_literals)utils)waiter)actions)arg_parsers)base)
exceptions)	arg_utils)log)
properties)progress_tracker)resource_projectorNc                 n   U R                  SSSS9  U R                  SS9nUR                  SSS	[        R                  " 5       S
S9  UR                  SS[        R                  " 5       SS9  UR                  SS[        R                  " 5       SS9  UR                  SS[        R                  " 5       SS9  g)zBAdds instance filter flags to a mutually exclusive argument group.z--instance-filter-all
store_truez3A filter that targets all instances in the project.actionhelpzU    Individual filters. The targeted instances must meet all criteria specified.
    r   z--instance-filter-group-labelsappendz	KEY=VALUEaq        A filter that represents a label set. Targeted instances must have all
      specified labels in this set. For example, "env=prod and app=web".

      This flag can be repeated. Targeted instances must have at least one of
      these label sets. This allows targeting of disparate groups, for example,
      "(env=prod and app=web) or (env=staging and app=web)".)r   metavartyper   z--instance-filter-zonesINSTANCE_FILTER_ZONESzw      A filter that targets instances in any of the specified zones. Leave empty
      to target instances in any zone.r   r   r   z--instance-filter-namesINSTANCE_FILTER_NAMESae        A filter that targets instances of any of the specified names. Instances
      are specified by the URI in the form
      "zones/<ZONE>/instances/<INSTANCE_NAME>",
      "projects/<PROJECT_ID>/zones/<ZONE>/instances/<INSTANCE_NAME>", or
      "https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/<ZONE>/instances/<INSTANCE_NAME>".
      z--instance-filter-name-prefixesINSTANCE_FILTER_NAME_PREFIXESzo      A filter that targets instances whose name starts with one of these
      prefixes. For example, "prod-".N)add_argument	add_groupr	   ArgDictArgList)mutually_exclusive_groupindividual_filters_groups     3lib/surface/compute/os_config/patch_jobs/execute.py_AddCommonInstanceFilterFlagsr%   $   s    ''D ( 
 6?? F? 	 ''& @ (  ''% *	 (  ''% 
	 (  '''- )	 (     c                 :    U R                  SSS9n[        U5        g)z9Adds top-level argument flags for the Beta and GA tracks.T/Filters for selecting which instances to patch:requiredr   N)add_mutually_exclusive_groupr%   parserinstance_filter_groups     r$   _AddTopLevelArgumentsr/   Y   s)     ==< >    56r&   c                     U R                  SSS9nUR                  S[        S[        R                  " SSSSS	9S
9  [        U5        U R                  SSSS9  g)z2Adds top-level argument flags for the Alpha track.Tr(   r)   z--instance-filterz      Filter expression for selecting the instances to patch. Patching supports
      the same filter mechanisms as `gcloud compute instances list`, allowing
      one to patch specific instances by name, zone, label, or other criteria.
      z          {flag_name} is deprecated; use individual filter flags instead. See
          the command help text for more details.Fstore)warnremovedr   )r   r   r   z--retryr   z      Specifies whether to attempt to retry, within the duration window, if
      patching initially fails. If omitted, the agent uses its default retry
      strategy.r   N)r+   r   strr   DeprecationActionr%   r,   s     r$   _AddTopLevelArgumentsAlphar6   b   s     ==< > 
 $$

 &&
5  %    56  r&   c           	      |   [         R                  " SSS9nUR                  [         R                  " SSSSS.S	95        [         R                  " S
SS9nUR                  [         R                  " SSS95        UR                  [         R                  " SSS95        UR                  U5        UR                  U 5        g)z'Adds top-level patch rollout arguments.Fz*Rollout configurations for this patch job:)mutexr   z--rollout-modezMode of the rollout.a                Patches are applied one zone at a time. The patch job begins in
              the region with the lowest number of targeted VMs. Within the
              region, patching begins in the zone with the lowest number of
              targeted VMs. If multiple regions (or zones within a region) have
              the same number of targeted VMs, a tie-breaker is achieved by
              sorting the regions or zones in alphabetical order.z9Patches are applied to VMs in all zones at the same time.)zzone-by-zonezconcurrent-zoneshelp_strchoicesTz      Disruption budget for this rollout. A running VM with an active agent is
      considered disrupted if its patching operation fails anytime between the
      time the agent is notified until the patch process completes.z--rollout-disruption-budgetz6Number of VMs per zone to disrupt at any given moment.r   z#--rollout-disruption-budget-percentz          Percentage of VMs per zone to disrupt at any given moment. The number
          of VMs calculated from multiplying the percentage by the total number
          of VMs in a zone is rounded up.N)r
   ArgumentGroupAddArgumentChoiceArgumentArgumentAddToParser)r-   rollout_groupdisruption_budget_groups      r$   _AddPatchRolloutArgumentsrC      s    $$DF-

)E N	" !..GH %%
mm
'G	
 %%
mm
/-	 34F#r&   c                 R   [         R                  R                  U 5        U R                  S[        SS9  U R                  S[        SS9  U R                  SSSS	9  U R                  S
[
        R                  " 5       SS9  [         R                  " SSSSSS.S9R                  U 5        g)z-Adds top-level argument flags for all tracks.z--descriptionz%Textual description of the patch job.)r   r   z--display-namezADisplay name for this patch job. This does not have to be unique.z	--dry-runr   z      Whether to execute this patch job as a dry run. If this patch job is a dry
      run, instances are contacted, but the patch is not run.r   z
--durationa        Total duration in which the patch job must complete. If the patch does not
      complete in this time, the process times out. While some instances might
      still be running the patch, they will not continue to work after
      completing the current step. See $ gcloud topic datetimes for information
      on specifying absolute time durations.

      If unspecified, the job stays active until all instances complete the
      patch.z--reboot-configzPost-patch reboot settings.z          The agent decides if a reboot is necessary by checking signals such as
          registry keys or '/var/run/reboot-required'.z5Always reboot the machine after the update completes.z4Never reboot the machine after the update completes.)defaultalwaysneverr9   N)r
   
ASYNC_FLAGr@   r   r4   r	   Durationr>   r-   s    r$   _AddCommonTopLevelArgumentsrK      s    //f%C&M  ON  
 	A   	!   ,: JH	 Kr&   c                     U R                  SS9nUR                  SSSS9  UR                  5       nUR                  SS[        R                  " 5       S	S
9  UR                  SS[        R                  " 5       SS
9  g)zAdds Apt setting flags.z"Settings for machines running Apt:r   z
--apt-distr   z      If specified, machines running Apt use the `apt-get dist-upgrade` command;
      otherwise the `apt-get upgrade` command is used.r   z--apt-excludesAPT_EXCLUDESz(List of packages to exclude from update.r   z--apt-exclusive-packagesAPT_EXCLUSIVE_PACKAGES      An exclusive list of packages to be updated. These are the only packages
      that will be updated. If these packages are not installed, they will be
      ignored.N)r   r   r+   r	   r!   )r-   	apt_groupr"   s      r$   _AddAptGroupArgumentsrQ      s    $HI):   'CCE'' 9	 (  '' & 	 ( r&   c                     U R                  SS9nUR                  SS9nUR                  SS[        R                  " / SQS9SS	9  UR                  S
S[        R                  " 5       SS	9  UR                  SS[        R                  " 5       SS	9  g)zAdds Windows setting flags.z&Settings for machines running Windows:r   zWindows patch optionsz--windows-classificationsWINDOWS_CLASSIFICATIONS)	criticalsecurity
definitiondriverzfeature-packzservice-packtoolzupdate-rollupupdate)r;   a        List of classifications to use to restrict the Windows update. Only
      patches of the given classifications are applied. If omitted, a default
      Windows update is performed. For more information on classifications,
      see: https://support.microsoft.com/en-us/help/824684r   z--windows-excludesWINDOWS_EXCLUDESzTOptional list of Knowledge Base (KB) IDs to exclude from the
      update operation.z--windows-exclusive-patchesWINDOWS_EXCLUSIVE_PATCHESzx      An exclusive list of Knowledge Base (KB) IDs to be updated. These are the
      only patches that will be updated.Nr+   r   r   r	   r!   )r-   	win_groupnon_exclusive_groups      r$   _AddWinGroupArgumentsr_      s    113 2 5)!++1H+I""!' ( 	> #  ""  	 #  #) ,	  r&   c                    U R                  SS9nUR                  SS9nUR                  SSSS9  UR                  SSS	S9  UR                  S
S[        R                  " 5       SS9  UR                  SS[        R                  " 5       SS9  g)zAdds Yum setting flags.z"Settings for machines running Yum:r   zYum patch optionsz--yum-securityr   za      If specified, machines running Yum append the `--security` flag to the
      patch command.r   z--yum-minimalz{      If specified, machines running Yum use the command `yum update-minimal`;
      otherwise the patch uses `yum-update`.z--yum-excludesYUM_EXCLUDESz      Optional list of packages to exclude from updating. If this argument is
      specified, machines running Yum exclude the given list of packages using
      the Yum `--exclude` flag.r   z--yum-exclusive-packagesYUM_EXCLUSIVE_PACKAGESrO   Nr\   )r-   	yum_groupr^   s      r$   _AddYumGroupArgumentsrd     s    11/ 2 1)!++1D+E"" #  ""0 #  "" #	 #   & 	  r&   c                    U R                  SS9nUR                  S5      nUR                  SS[        R                  " 5       SS9  UR                  SS	[        R                  " 5       S
S9  UR                  SSSS9  UR                  SSSS9  UR                  SS[        R                  " 5       SS9  UR                  SS[        R                  " 5       SS9  g)zAdds Zypper setting flags.z%Settings for machines running Zypper:r   zZypper patch optionsz--zypper-categoriesZYPPER_CATEGORIESz      If specified, machines running Zypper install only patches with the
      specified categories. Categories include security, recommended, and
      feature.r   z--zypper-severitiesZYPPER_SEVERITIESz      If specified, machines running Zypper install only patch with the
      specified severities. Severities include critical, important, moderate,
      and low.z--zypper-with-optionalr   zc      If specified, machines running Zypper add the `--with-optional` flag to
      `zypper patch`.r   z--zypper-with-updateza      If specified, machines running Zypper add the `--with-update` flag to
      `zypper patch`.z--zypper-excludesZYPPER_EXCLUDESzB      List of Zypper patches to exclude from the patch job.
      z--zypper-exclusive-patchesZYPPER_EXCLUSIVE_PATCHESz      An exclusive list of patches to be updated. These are the only patches
      that will be installed using the 'zypper patch patch:<patch_name>'
      command.Nr\   )r-   zypper_groupr^   s      r$   _AddZypperGroupArgumentsrk   C  s   442 5 4,$../EF""! 	 #  ""! 	 #  "" #  "" #  "" 
	 #  "( 	  r&   c                 <   U R                  SS9nUR                  SSS9  UR                  S[        R                  " [        S9SSS	9  U R                  S
S9nUR                  SSS9  UR                  S[        R                  " [        S9SSS	9  U R                  SS9nUR                  SSS9  UR                  S[        R                  " [        S9SSS	9  U R                  SS9nUR                  SSS9  UR                  S[        R                  " [        S9SSS	9  g)z#Adds pre-/post-patch setting flags.z+Pre-patch step settings for Linux machines:r   z--pre-patch-linux-executablea        A set of commands to run on a Linux machine before an OS patch begins.
      Commands must be supplied in a file. If the file contains a shell script,
      include the shebang line.

      The path to the file must be supplied in one of the following formats:

      An absolute path of the file on the local filesystem.

      A URI for a Google Cloud Storage object with a generation number.
      z--pre-patch-linux-success-codes)element_typePRE_PATCH_LINUX_SUCCESS_CODESz      Additional exit codes that the executable can return to indicate a
      successful run. The default exit code for success is 0.)r   r   r   z,Post-patch step settings for Linux machines:z--post-patch-linux-executablea        A set of commands to run on a Linux machine after an OS patch completes.
      Commands must be supplied in a file. If the file contains a shell script,
      include the shebang line.

      The path to the file must be supplied in one of the following formats:

      An absolute path of the file on the local filesystem.

      A URI for a Google Cloud Storage object with a generation number.
      z --post-patch-linux-success-codesPOST_PATCH_LINUX_SUCCESS_CODESz-Pre-patch step settings for Windows machines:z--pre-patch-windows-executablea        A set of commands to run on a Windows machine before an OS patch begins.
      Commands must be supplied in a file. If the file contains a PowerShell
      script, include the .ps1 file extension. The PowerShell script executes
      with flags `-NonInteractive`, `-NoProfile`, and `-ExecutionPolicy Bypass`.

      The path to the file must be supplied in one of the following formats:

      An absolute path of the file on the local filesystem.

      A URI for a Google Cloud Storage object with a generation number.
      z!--pre-patch-windows-success-codesPRE_PATCH_WINDOWS_SUCCESS_CODESz.Post-patch step settings for Windows machines:z--post-patch-windows-executablea        A set of commands to run on a Windows machine after an OS patch completes.
      Commands must be supplied in a file. If the file contains a PowerShell
      script, include the .ps1 file extension. The PowerShell script executes
      with flags `-NonInteractive`, `-NoProfile`, and `-ExecutionPolicy Bypass`.

      The path to the file must be supplied in one of the following formats:

      An absolute path of the file on the local filesystem.

      A URI for a Google Cloud Storage object with a generation number.
      z"--post-patch-windows-success-codes POST_PATCH_WINDOWS_SUCCESS_CODESN)r   r   r	   r!   int)r-   pre_patch_linux_grouppost_patch_linux_grouppre_patch_windows_grouppost_patch_windows_groups        r$   _AddPrePostStepArgumentsrw   {  s    **8 + :$$$

 %  $$'C0-A	 %  "++9 , ;%%%

 &  %%(C0.A	 &  #,,: - <&&&
 '  &&)C0/A	 '  $--; . ='''
 (  ''*C00A	 ( r&   c                     [        U 5        [        U 5        [        U 5        [        U 5        [	        U 5        U R                  SSSS9  U R                  SSSS9  g)z%Adds all patch config argument flags.z--mig-instances-allowedr   z[        If set, patching of VMs that are part of a managed instance group (MIG) is allowed.r   z--skip-unpatchable-vmsa%          If set, enables enhanced reporting for the patch job:

        1. Allows the patch job to skip unpatchable instances, reporting them as
        SKIPPED. An instance can be unpatchable for two reasons:

          a. The instance runs Container-Optimized OS (COS), which cannot be
          patched.

          b. The instance is part of a managed instance group (MIG), and
          patching MIG instances is disabled in the patch job's configuration
          (`--mig-instances-allowed` flag is not set).

        2. Reports the patch job as SUCCEEDED if it completes without errors,
        even if some instances were SKIPPED.

        3. Reports the patch job as COMPLETED_WITH_INACTIVE_VMS if it completes
        without errors, but some instances were INACTIVE and were not patched.
        N)rQ   rd   r_   rk   rw   r   rJ   s    r$   _AddPatchConfigArgumentsry     si    6"6"_   	  r&   c                    [        U R                  U R                  U R                  /5      (       d  gUR	                  U R                  (       a   UR                  R
                  R                  OUR                  R
                  R                  U R                  (       a  U R                  O/ U R                  (       a  U R                  S9$ / S9$ )z4Creates an AptSettings message from input arguments.N)r   excludesexclusivePackages)anyapt_distapt_excludesapt_exclusive_packagesAptSettingsTypeValueValuesEnumDISTTYPE_UNSPECIFIEDargsmessagess     r$   _CreateAptSettingsr   	  s    	dmmT..0K0KL	M	M			<@MM83388..??$($5$5t  2		$	$ 33	 
 
. .
 +- 
 
. .r&   c                    [        U R                  U R                  U R                  /5      (       d  gUR                  R
                  nU R                  (       a1  U R                   Vs/ s H  n[        R                  " X25      PM     snO/ nUR	                  UU R                  (       a  U R                  O/ U R                  (       a  U R                  S9$ / S9$ s  snf )z=Creates a WindowsUpdateSettings message from input arguments.N)classificationsr{   exclusivePatches)r}   windows_classificationswindows_excludeswindows_exclusive_patchesWindowsUpdateSettings'ClassificationsValueListEntryValuesEnumr   ChoiceToEnum)r   r   enumscr   s        r$   _CreateWindowsUpdateSettingsr     s    	
""D$9$9
$$ 
 
 

(
(
P
P% ## 150L0L0L1iQ&0L)+  
	'	'%(,(=(=t$$2		'	' 55 
( 
  .0	 
( 
 s   ( Cc                 @   [        U R                  U R                  U R                  U R                  /5      (       d  gUR                  U R                  (       a  U R                  O/ U R                  U R                  U R                  (       a  U R                  S9$ / S9$ )z3Creates a YumSettings message from input arguments.N)r{   minimalrU   r|   )r}   yum_excludesyum_minimalyum_securityyum_exclusive_packagesYumSettingsr   s     r$   _CreateYumSettingsr   *  s    	
))4+<+<
!! 
 
 			$($5$5t  2  		$	$ 33	 
 
 
 +- 
 
 r&   c           	         [        U R                  U R                  U R                  U R                  U R
                  U R                  /5      (       d  gUR                  U R                  (       a  U R                  O/ U R                  (       a  U R                  O/ U R                  U R                  U R
                  (       a  U R
                  O/ U R                  (       a  U R                  S9$ / S9$ )z6Creates a ZypperSettings message from input arguments.N)
categories
severitieswithOptional
withUpdater{   r   )r}   zypper_categorieszypper_severitieszypper_with_optionalzypper_with_updatezypper_excludeszypper_exclusive_patchesZypperSettingsr   s     r$   _CreateZypperSettingsr   ;  s    	
d44d6O6O
t33
## 
 

 		 	 +/+A+A''r+/+A+A''r,,(('+';';t##		&	& 44 
! 
  -/ 
! 
 r&   c                     UR                  S5      (       a   U R                  R                  R                  $ U R                  R                  R                  $ )z:Returns the ExecStepConfig interpreter based on file path.z.ps1)endswithExecStepConfigInterpreterValueValuesEnum
POWERSHELLSHELL)r   paths     r$   $_GetWindowsExecStepConfigInterpreterr   O  sA    	]]6""==HHH""==CCCr&   c                 n   U R                   R                  R                  n[        R                  " X5      nU(       aJ  U(       a  [        XS   5      nU R                  U R                  US   US   US   S9U(       a  UUS9$ / US9$ U(       a  [        X5      nU R                  UU(       a  UUS9$ / US9$ )z7Creates an ExecStepConfig message from input arguments.objectbucketgenerationNumber)r   r   r   )	gcsObjectallowedSuccessCodesinterpreter)	localPathr   r   )r   r   INTERPRETER_UNSPECIFIEDosconfig_command_utilsGetGcsParamsr   	GcsObject)r   arg_namer   allowed_success_codes
is_windowsr   
gcs_paramss          r$   _CreateExecStepConfigr   W  s     ''BBZZ+%228B*8
x(*k""$$h'h''(:; % 
 ! 2 # 	 	 ') # 	 	 8Hk""  2	 #   ')	 #  r&   c                 n    U(       a.  U(       d&  [         R                  " USR                  U 5      5      egg)z=Validates the relation between pre-/post-patch setting flags.z[{}] must also be specified.N)r   InvalidArgumentExceptionformat)executable_arg_nameexecutable_argsuccess_codes_arg_namesuccess_codes_args       r$   _ValidatePrePostPatchStepArgsr   u  s8     ~

-
-&--.ABD D  .r&   c                    U(       a  [        U R                  U R                  U R                  U R                  /5      (       d  g[        SU R                  SU R                  5        [        SU R                  SU R                  5        S=p4U R                  (       a!  [        USU R                  U R                  SS9nU R                  (       a!  [        USU R                  U R                  SS9nUR                  UUS	9$ [        U R                  U R                  U R                  U R                  /5      (       d  g[        S
U R                  SU R                  5        [        SU R                  SU R                  5        S=pVU R                  (       a!  [        US
U R                  U R                  SS9nU R                  (       a!  [        USU R                  U R                  SS9nUR                  UUS	9$ )z1Creates an ExecStep message from input arguments.Nzpre-patch-linux-executablezpre-patch-linux-success-codeszpre-patch-windows-executablezpre-patch-windows-success-codesF)r   T)linuxExecStepConfigwindowsExecStepConfigzpost-patch-linux-executablezpost-patch-linux-success-codeszpost-patch-windows-executablez post-patch-windows-success-codes)r}   pre_patch_linux_executablepre_patch_linux_success_codespre_patch_windows_executablepre_patch_windows_success_codesr   r   ExecSteppost_patch_linux_executablepost_patch_linux_success_codespost_patch_windows_executable post_patch_windows_success_codes)r   r   is_pre_patch_steppre_patch_linux_step_configpre_patch_windows_step_configpost_patch_linux_step_configpost_patch_windows_step_configs          r$   _CreatePrePostPatchStepSettingsr   ~  s   '')K)K))4+O+O   !">"&"A"A"A"&"D"DF ""@"&"C"C"C"&"F"FH
 CGF&&$9

&

)
)

,
,%! ((&;

(

+
+

.
.'# 7;   
 (($*M*M**--  
 !"?"&"B"B"B"&"E"EG ""A"&"D"D"D"&"G"GI
 EIH ''%:

'

*
*

-
-&" ))'<

)

,
,

/
/($ 8<   r&   c                     [         R                  " SSS9[         R                  " SSS9/n[         R                  " SR                  U 5      US9$ )zACreates a progress tracker to display patch status synchronously.zGenerating instance details...pre-summary)keyzReporting instance details...with-summaryzExecuting patch job [{0}])messagestages)r   StageStagedProgressTrackerr   )patch_job_namer   s     r$   _CreateProgressTrackerr     sX     
*?
)~?& 
	/	/)00@
Q Qr&   c                    [         R                   Vs0 s H  o"S_M     nn[         R                  R                  5        H"  u  pBXA;   a  [	        X   5      OSnX2   U-   X2'   M$     SR                  UR                  5        VVs/ s H.  u  p&SR                  XbR                  R                  5       5      PM0     snn5      nSR                  U U5      $ s  snf s  snnf )z@Constructs a message to be displayed during synchronous execute.r   z, z{} {}z{:.1f}% complete with {})	r   InstanceDetailsStatesINSTANCE_DETAILS_KEY_MAPitemsrr   joinr   namelower)percent_completeinstance_details_jsonstateinstance_statesr   num_instancesnuminstance_details_strs           r$   _CreateExecutionUpdateMessager     s     3HHH5QhH   +CCIIKjc'*'C "$IJ ,3mCO L
 '--/$/*% nnS****,-/$  
$	*	*+;+?
A A$s   C5C
c                    [         R                  " UR                  5      nU(       a  US:X  a;  U R                  S5      (       d  U R	                  S5        gU R                  SS5        g[        UR                  U5      nU R                  S5      (       a6  U R                  SS5        U R	                  S5        U R                  SU5        gU R                  SU5        g)zUpdates the progress tracker on screen based on patch job details.

Args:
  tracker: Progress tracker to be updated.
  patch_job: Patch job being executed.
z{}r   zPlease wait...zDone!r   N)	r   MakeSerializableinstanceDetailsSummary	IsRunning
StartStageUpdateStager   percentCompleteCompleteStage)tracker	patch_jobunused_statusdetails_jsondetails_strs        r$   _UpdateProgressTrackerr    s     $44&&(,	-]++'-)9:/	0I0I0<>K''M73(.+6.+6r&   c                 n    U R                   (       a#  [        R                  " U R                   5      S-   $ S$ )z$Returns a formatted duration string.sN)durationsix	text_type)r   s    r$   _GetDurationr	    s%    /3}}t}}	%	+F$Fr&   c                 V   U R                   (       a8  [        UR                  R                  U R                   R	                  5       5      OSnUR                  U[        X5      [        X5      [        X5      [        X5      [        XSS9[        XSS9U R                  U R                  S9	$ )z3Creates a PatchConfig message from input arguments.NT)r   F)	rebootConfigaptwindowsUpdateyumzypperpreSteppostStepmigInstancesAllowedskipUnpatchableVms)reboot_configgetattrPatchConfigRebootConfigValueValuesEnumupperr   r   r   r   r   mig_instances_allowedskip_unpatchable_vms)r   r   r  s      r$   _CreatePatchConfigr    s     &*%7%7 66
 "=A  
		 
T
,0@
T
,"42-
D2.
E34422 
 
 r&   c           	      B   / nU H  n/ nUR                  5        H7  u  pUR                  U R                  R                  R	                  XS95        M9     UR                  U R                  U R                  R                  US9S95        M     U R                  UUUUUS9$ )z:Creates a PatchInstanceFilter message from its components.)r   value)additionalProperties)labels)allgroupLabelszones	instancesinstanceNamePrefixes)r   r   PatchInstanceFilterGroupLabelLabelsValueAdditionalPropertyPatchInstanceFilter)r   
filter_allfilter_group_labelsfilter_zonesfilter_namesfilter_name_prefixesgroup_labelsgroup_labelpairsr   r  s              r$   _CreatePatchInstanceFilterr1    s     ,(kE!'')
ll

0
0
<
<
O
O P $% * ..99EE%* F , 	/ 	-. ) 
	%	%
/ 
& 
 r&   c           	         [        U R                  U R                  U R                  /5      (       d  gU R                  (       a9  U R                  (       d(  U R                  (       d  [        R
                  " SS5      eU R                  (       a(  U R                  (       d  [        R
                  " SS5      eU R                  (       a(  U R                  (       d  [        R
                  " SS5      eUR                  R                  nUR                  [        R                  " U R                  U5      UR                  U R                  (       a  [        U R                  5      OSU R                  (       a  [        U R                  5      OSS9S9$ )	z4Creates a PatchRollout message from input arguments.Nzrollout-modezZ[rollout-disruption-budget] or [rollout-disruption-budget-percent] must also be specified.zrollout-disruption-budgetz&[rollout-mode] must also be specified.z!rollout-disruption-budget-percent)fixedpercent)modedisruptionBudget)r}   rollout_moderollout_disruption_budget!rollout_disruption_budget_percentr   r   PatchRolloutModeValueValuesEnumr   r   FixedOrPercentrr   )r   r   rollout_modess      r$   _CreatePatchRolloutr>  4  sP   	
77
,, 
 
 	 > > $ F F

-
-	"# #
 
##D,=,=

-
-#%MO O 
++D4E4E

-
-+02 2 '';;-			!!$"3"3]C..++ D2231533 d<<=9=	 / ? 
 
@ @r&   c                 
   [        U UU	U
UU5      nU(       a7  U R                  U R                  UUUUUUUS9[        R                  " U5      S9$ U R                  U R                  UUUUUUS9[        R                  " U5      S9$ )z5Creates an ExecuteRequest message for the Beta track.descriptiondisplayNamedryRunr  instanceFilterpatchConfigrolloutexecutePatchJobRequestparent)rA  rB  rC  r  rD  rE  )r1  'OsconfigProjectsPatchJobsExecuteRequestExecutePatchJobRequestr   GetProjectUriPath)r   projectrA  dry_runr  patch_configpatch_rolloutdisplay_namer)  r*  r+  r,  r-  patch_instance_filters                 r$   _CreateExecuteRequestrS  V  s    
 5 ;;'>>#$0$!  ?  
 &77@ < 
B 
B ;;'>>#$0$  ?  
 &77@ < 	B 	Br&   c                 P   U(       a7  U R                  U R                  UUUUUUUS9[        R                  " U5      S9$ [	        XXU/5      (       dD  U R                  U R                  UUUUU R                  SS9UUS9[        R                  " U5      S9$ [        XX#XEUXxU	XU5      $ )z6Creates an ExecuteRequest message for the Alpha track.)rA  rB  rC  r  filterrE  rF  rG  T)r   r@  )rJ  rK  r   rL  r}   r(  rS  )r   rM  rA  rN  r  rO  rP  rQ  r)  r*  r+  r,  r-  filter_expressions                 r$   _CreateExecuteRequestAlpharW  }  s     ;;'>>#$$$!  ?  
 &77@ < 
B 
B |   ;;'>>#$#77D7A$!  ?  
 &77@ < 
B 
B !K!)!-;N!-!5	7 7r&   c           
         U R                   R                  U5      n[        R                  " UR                  5      nU(       a^  [
        R                  R                  SR                  U5      5        [
        R                  R                  SR                  U5      5        U$ [        R                  " X5      nUR                  UR                  S9n[        R                  " UU[        U5      [        SSSS9n	[
        R                  R                  SR                  XiR                   5      5        [
        R                  R                  SR                  U5      5        U	$ )	z+Creates an ExecutePatchJobResponse message.z(Execution in progress for patch job [{}]zDRun the [{} describe] command to check the status of this execution.)r   i     )custom_trackertracker_update_funcpre_start_sleep_msexponential_sleep_multipliersleep_msz<Execution for patch job [{}] has completed with status [{}].zPRun the [{} list-instance-details] command to view any instance failure reasons.)projects_patchJobsExecuter   GetResourceNamer   r   statusPrintr   osconfig_api_utilsPoller#OsconfigProjectsPatchJobsGetRequestr   WaitForr   r  r   )
clientr   requestis_asynccommand_prefixasync_responser   patch_job_pollerget_requestsync_responses
             r$   _CreateExecuteResponserp    s'   ,,44W=.)99.:M:MN.JJ299.IKJJN		!  (..v@<< =  +..+N;0#$- **DKK
--/0 ** //5vn/EG	r&   c                   :    \ rS rSrSrSS0rSr\S 5       rS r	Sr
g	)
r`  i  2Execute an OS patch on the specified VM instances.EXAMPLESa]        To start a patch job named `my patch job` that patches all instances in the
      current project, run:

            $ {command} --display-name="my patch job" --instance-filter-all

      To patch an instance named `instance-1` in the `us-east1-b` zone, run:

            $ {command} --instance-filter-names="zones/us-east1-b/instances/instance-1"

      To patch all instances in the `us-central1-b` and `europe-west1-d` zones, run:

            $ {command} --instance-filter-zones="us-central1-b,europe-west1-d"

      To patch all instances where the `env` label is `test` and `app` label is
      `web`, run:

            $ {command} --instance-filter-group-labels="env=test,app=web"

      To patch all instances where the `env` label is `test` and `app` label is
      `web` or where the `env` label is `staging` and `app` label is `web`, run:

            $ {command} --instance-filter-group-labels="env=test,app=web" --instance-filter-group-labels="env=staging,app=web"

      To apply security and critical patches to Windows instances with the prefix
      `windows-` in the instance name, run:

            $ {command} --instance-filter-name-prefixes="windows-" --windows-classifications=SECURITY,CRITICAL

      To update only `KB4339284` on Windows instances with the prefix `windows-` in
      the instance name, run:

            $ {command} --instance-filter-name-prefixes="windows-" --windows-exclusive-patches=4339284

      To patch all instances in the current project and specify scripts to run
      pre-patch and post-patch, run:

            $ {command} --instance-filter-all --pre-patch-linux-executable="/bin/script" --pre-patch-linux-success-codes=0,200 --pre-patch-windows-executable="C:\Users\user\script.ps1" --post-patch-linux-executable="gs://my-bucket/linux-script#123" --post-patch-windows-executable="gs://my-bucket/windows-script#678"

      To patch all instances zone-by-zone with no more than 50 percent of the
      instances in the same zone disrupted at a given time, run:

            $ {command} --instance-filter-all --rollout-mode=zone-by-zone --rollout-disruption-budget-percent=50
      z#gcloud compute os-config patch-jobsc                 \    [        U 5        [        U 5        [        U 5        [        U 5        g N)r/   rK   ry   rC   rJ   s    r$   ArgsExecute.Args   s"    &!'V$f%r&   c                    [         R                  R                  R                  R	                  5       nU R                  5       n[        R                  " U5      n[        R                  " U5      n[        U5      n[        X5      n[        X5      n[        UUUR                  UR                  UUUUR                  UR                   UR"                  (       a  UR"                  O/ UR$                  (       a  UR$                  O/ UR&                  (       a  UR&                  O/ UR(                  (       a  UR(                  O/ 5      n	[+        XEXR,                  U R.                  5      $ ru  )r   VALUEScorerM  	GetOrFailReleaseTrackrd  GetClientInstanceGetClientMessagesr	  r  r>  rS  rA  rN  rQ  instance_filter_allinstance_filter_group_labelsinstance_filter_zonesinstance_filter_namesinstance_filter_name_prefixesrp  async__command_prefix)
selfr   rM  release_trackrh  r   r  rO  rP  ri  s
             r$   RunExecute.Run  s   $$,,668G%%'M11-@F!33MBHD!H%d5L'7M#  ,, 	))24&*&@&@""b&*&@&@""b-- 	**35G" "&G[["&"6"68 8r&    N)__name__
__module____qualname____firstlineno____doc__detailed_helpr  staticmethodrv  r  __static_attributes__r  r&   r$   r`  r`    s7     ; +
.-` :/& &8r&   r`  c                       \ rS rSrSrSrSrg)ExecuteBetai'  rr  z(gcloud beta compute os-config patch-jobsr  N)r  r  r  r  r  r  r  r  r&   r$   r  r  '  s     ;>/r&   r  )@r  
__future__r   r   r   (googlecloudsdk.api_lib.compute.os_configr   rd  googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r	   r
   r   ,googlecloudsdk.command_lib.compute.os_configr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   r  r%   r/   r6   rC   rK   rQ   r_   rd   rk   rw   ry   r   r   r   r   r   r   r   r   r   r   r  r	  r  r1  r>  rS  rW  rp  UniverseCompatibleReleaseTracksr|  GACommandr`  BETAr  r  r&   r$   <module>r     sk   O &  ' P . + / ( . X : # * 8 ; 
2j7B)$X+\8 F$N5peP#L
.("(D<DHV	QA&72G
*2@D$BN&7R F D%%(()Y8dll Y8 * Y8x D%%**+?' ? , ?r&   