source_util_2.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. def SetFunctionSourceProps(function, function_ref, source_arg, stage_bucket):
  2. """Add sources to function.
  3. Args:
  4. function: The function to add a source to.
  5. function_ref: The reference to the function.
  6. source_arg: Location of source code to deploy.
  7. stage_bucket: The name of the Google Cloud Storage bucket where source code
  8. will be stored.
  9. Returns:
  10. A list of fields on the function that have been changed.
  11. """
  12. function.sourceArchiveUrl = None
  13. function.sourceRepository = None
  14. function.sourceUploadUrl = None
  15. messages = api_util.GetApiMessagesModule()
  16. if source_arg is None:
  17. source_arg = '.'
  18. source_arg = source_arg or '.'
  19. if source_arg.startswith('gs://'):
  20. if not source_arg.endswith('.zip'):
  21. # Users may have .zip archives with unusual names, and we don't want to
  22. # prevent those from being deployed; the deployment should go through so
  23. # just warn here.
  24. log.warning(
  25. '[{}] does not end with extension `.zip`. '
  26. 'The `--source` argument must designate the zipped source archive '
  27. 'when providing a Google Cloud Storage URI.'.format(source_arg))
  28. function.sourceArchiveUrl = source_arg
  29. return ['sourceArchiveUrl']
  30. elif source_arg.startswith('https://'):
  31. function.sourceRepository = messages.SourceRepository(
  32. url=_AddDefaultBranch(source_arg)
  33. )
  34. return ['sourceRepository']
  35. with file_utils.TemporaryDirectory() as tmp_dir:
  36. zip_file = _CreateSourcesZipFile(tmp_dir, source_arg)
  37. service = api_util.GetApiClientInstance().projects_locations_functions
  38. upload_url = UploadFile(
  39. zip_file, stage_bucket, messages, service, function_ref)
  40. if upload_url.startswith('gs://'):
  41. function.sourceArchiveUrl = upload_url
  42. return ['sourceArchiveUrl']
  43. else:
  44. function.sourceUploadUrl = upload_url
  45. return ['sourceUploadUrl']