Beispiel #1
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      None

    Raises:
      FunctionsError: If the user doesn't confirm on prompt.
    """
        client = self.context['functions_client']
        messages = self.context['functions_messages']
        project = properties.VALUES.core.project.Get(required=True)
        name = 'projects/{0}/regions/{1}/functions/{2}'.format(
            project, args.region, args.name)

        prompt_message = 'Resource [{0}] will be deleted.'.format(name)
        if not console_io.PromptContinue(message=prompt_message):
            raise exceptions.FunctionsError('Deletion aborted by user.')
        # TODO(user): Use resources.py here after b/21908671 is fixed.
        op = client.projects_regions_functions.Delete(
            messages.CloudfunctionsProjectsRegionsFunctionsDeleteRequest(
                name=name))
        operations.Wait(op, messages, client)
        log.DeletedResource(name)
Beispiel #2
0
def _WaitForOperation(client, get_request, message):
    """Wait for an operation to complete.

  No operation is done instantly. Wait for it to finish following this logic:
  * we wait 1s (jitter is also 1s)
  * we query service
  * if the operation is not finished we loop to first point
  * wait limit is 380s - if we get to that point it means something is wrong
        and we can throw an exception

  Args:
    client:  The client used to make requests.
    get_request: A GetOperatioRequest message.
    message: str, The string to print while polling.

  Returns:
    True if the operation succeeded without error.

  Raises:
    FunctionsError: If the operation takes more than 380s.
  """

    with console_io.ProgressTracker(message, autotick=False) as pt:
        # This is actually linear retryer.
        retryer = retry.Retryer(exponential_sleep_multiplier=1,
                                max_wait_ms=MAX_WAIT_MS,
                                wait_ceiling_ms=WAIT_CEILING_MS)
        try:
            retryer.RetryOnResult(_GetOperationStatus, [client, get_request],
                                  {'progress_tracker': pt},
                                  should_retry_if=None,
                                  sleep_ms=SLEEP_MS)
        except retry.WaitException:
            raise exceptions.FunctionsError(
                'Operation {0} is taking too long'.format(get_request.name))
Beispiel #3
0
 def _CheckArgs(self, args):
     # This function should raise ArgumentParsingError, but:
     # 1. ArgumentParsingError requires the  argument returned from add_argument)
     #    and Args() method is static. So there is no elegant way to save it
     #    to be reused here.
     # 2. _CheckArgs() is invoked from Run() and ArgumentParsingError thrown
     #    from Run are not caught.
     if util.IsCloudRepoPath(args.source):
         if args.bucket is not None:
             raise exceptions.FunctionsError(
                 'argument --bucket: not allowed when argument --source points '
                 'to a repository')
     elif args.bucket is None:
         raise exceptions.FunctionsError(
             'argument --bucket: required when argument --source points '
             'to a local directory')
Beispiel #4
0
 def _PrepareSourcesOnGcs(self, args):
     remote_zip_file = self._GenerateFileName(args)
     # args.bucket is not None: Enforced in _CheckArgs().
     gcs_url = storage.BuildRemoteDestination(args.bucket, remote_zip_file)
     with file_utils.TemporaryDirectory() as tmp_dir:
         zip_file = self._CreateZipFile(tmp_dir, args)
         if self._UploadFile(zip_file, gcs_url) != 0:
             raise exceptions.FunctionsError('Function upload failed.')
     return gcs_url
Beispiel #5
0
 def _DeployFunction(self, name, location, args, deploy_method):
     remote_zip_file = self._GenerateFileName(args)
     bucket_name = self._GenerateBucketName(args)
     gcs_url = storage.BuildRemoteDestination(bucket_name, remote_zip_file)
     function = self._GenerateFunction(name, gcs_url, args)
     with file_utils.TemporaryDirectory() as tmp_dir:
         zip_file = self._CreateZipFile(tmp_dir, args)
         if self._UploadFile(zip_file, gcs_url) != 0:
             raise exceptions.FunctionsError('Function upload failed.')
     return deploy_method(location, function)
Beispiel #6
0
def _GetOperationStatus(client, get_request, progress_tracker=None):
    """Helper function for getting the status of an operation.

  Args:
    client: The client used to make requests.
    get_request: A GetOperationRequest message.
    progress_tracker: console_io.ProgressTracker, A reference for the progress
        tracker to tick, in case this function is used in a Retryer.

  Returns:
    True if the operation succeeded without error.
    False if the operation is not yet done.

  Raises:
    FunctionsError: If the operation is finished with error.
  """
    if progress_tracker:
        progress_tracker.Tick()
    op = client.operations.Get(get_request)
    if op.done and op.error:
        raise exceptions.FunctionsError(util.GetOperationError(op.error))
    return op.done