Beispiel #1
0
def _CreateExecuteResponse(client, messages, request, is_async, command_prefix):
  """Creates an ExecutePatchJobResponse message."""
  async_response = client.projects_patchJobs.Execute(request)

  patch_job_name = osconfig_command_utils.GetResourceName(async_response.name)

  if is_async:
    log.status.Print(
        'Execution in progress for patch job [{}]'.format(patch_job_name))
    log.status.Print(
        'Run the [{} describe] command to check the status of this execution.'
        .format(command_prefix))
    return async_response

  # Execute the patch job synchronously.
  patch_job_poller = osconfig_api_utils.Poller(client, messages)
  get_request = messages.OsconfigProjectsPatchJobsGetRequest(
      name=async_response.name)
  sync_response = waiter.WaitFor(
      patch_job_poller,
      get_request,
      custom_tracker=_CreateProgressTracker(patch_job_name),
      tracker_update_func=_UpdateProgressTracker,
      pre_start_sleep_ms=5000,
      exponential_sleep_multiplier=1,  # Constant poll rate of 5s.
      sleep_ms=5000,
  )
  log.status.Print(
      'Execution for patch job [{}] has completed with status [{}].'.format(
          patch_job_name, sync_response.state))
  log.status.Print('Run the [{} list-instance-details] command to view any '
                   'instance failure reasons.'.format(command_prefix))
  return sync_response
  def Run(self, args):
    project = properties.VALUES.core.project.GetOrFail()

    release_track = self.ReleaseTrack()
    client = osconfig_api_utils.GetClientInstance(release_track)
    messages = osconfig_api_utils.GetClientMessages(release_track)

    duration = six.text_type(args.duration) + 's' if args.duration else None
    filter_arg = 'id=*' if not args.instance_filter else args.instance_filter
    reboot_config = getattr(
        messages.PatchConfig.RebootConfigValueValuesEnum,
        args.reboot_config.upper()) if args.reboot_config else None
    retry_strategy = messages.RetryStrategy(
        enabled=True) if args.retry else None
    patch_config = messages.PatchConfig(
        rebootConfig=reboot_config,
        retryStrategy=retry_strategy,
        apt=_GetAptSettings(args, messages),
        windowsUpdate=_GetWindowsUpdateSettings(args, messages),
        yum=_GetYumSettings(args, messages),
        zypper=_GetZypperSettings(args, messages),
        preStep=_GetPrePostPatchStepSettings(
            args, messages, is_pre_patch_step=True),
        postStep=_GetPrePostPatchStepSettings(
            args, messages, is_pre_patch_step=False),
    )

    request = messages.OsconfigProjectsPatchJobsExecuteRequest(
        executePatchJobRequest=messages.ExecutePatchJobRequest(
            description=args.description,
            dryRun=args.dry_run,
            duration=duration,
            filter=filter_arg,
            patchConfig=patch_config,
        ),
        parent=osconfig_command_utils.GetProjectUriPath(project))
    async_response = client.projects_patchJobs.Execute(request)

    patch_job_name = osconfig_command_utils.GetPatchJobName(async_response.name)

    if args.async_:
      log.status.Print(
          'Execution in progress for patch job [{}]'.format(patch_job_name))
      log.status.Print(
          'Run the [{} describe] command to check the status of this execution.'
          .format(self._command_prefix))
      return async_response

    # Execute the patch job synchronously.
    patch_job_poller = osconfig_api_utils.Poller(client, messages)
    get_request = messages.OsconfigProjectsPatchJobsGetRequest(
        name=async_response.name)
    sync_response = waiter.WaitFor(
        patch_job_poller,
        get_request,
        custom_tracker=_GetProgressTracker(patch_job_name),
        tracker_update_func=_UpdateProgressTracker,
        pre_start_sleep_ms=5000,
        exponential_sleep_multiplier=1,  # Constant poll rate of 5s.
        sleep_ms=5000,
    )
    log.status.Print(
        'Execution for patch job [{}] has completed with status [{}].'.format(
            patch_job_name, sync_response.state))
    log.status.Print('Run the [{} list-instance-details] command to view any '
                     'instance failure reasons.'.format(self._command_prefix))
    return sync_response