google-cloud-platformgoogle-deployment-manager

How do you programmatically import files in Google Deployment Manager


I'm looking at this example which uses deployment manager to deploy a cloud function: https://github.com/GoogleCloudPlatform/deploymentmanager-samples/tree/master/examples/v2/cloud_functions

One of the things that makes this unwieldy and not very usable is this, it requires you explicitly import EVERY file in the function:

imports:
- path: cloud_function.py
# The function code will be defined for the files in function/
- path: function/index.js
- path: function/package.json 

It's not acceptable to have to add to that every time there is a new file. Deployment Manager also does not support wildcards.

How can you import files programmatically?

This is the part of cloud_function.py that references the imported files, I tried to just use a string but it seems that import actually copies files somewhere? How do I do that programmatically so I don't need to explicitly define every individual file?

  files = ["function/index.js","function/package.json"] # this does not work if these files have not been declared via "import"
  #for imp in ctx.imports:
  for imp in files:
    if imp.startswith(ctx.properties['codeLocation']):
      zip_file.writestr(imp[len(ctx.properties['codeLocation']):],
                        ctx.imports[imp])

Solution

  • You have to turn on globbing, in this way config files can now use glob patterns in import paths:

    gcloud config set deployment_manager/glob_imports True

    Here are examples where entire folders can be added:

    imports:
    - path: templates/simple_frontend.py
      name: simple_frontend.py
    
    # Helper classes
    - path: helper/*.py
    
    # Configuration files
    - path: configs/*.yaml
    - path: configs/*/*.yaml
    

    The full documentation can be found here.