digitalocean / sample-functions-python-helloworld

24 stars 69 forks source link

library code for multiple python functions? #3

Open petrem opened 1 year ago

petrem commented 1 year ago

Suppose we have multiple functions that would share common, library code -- I'm sure this is a very common situation.

I currently ended up writing a minimal python project inside "lib" (the python package is also, inaptly, named lib -- apologies). I've ended up with a structure similar to the following:

.
├── .env
├── .gitignore
├── lib
│   └── lib
│       ├── pyproject.toml
│       └── src
│           └── lib
│               ├── __init__.py
│               ├── db.py
│               └── rest.py
├── packages
│   └── servers
│       ├── get_servers
│       │   ├── __deployer__.zip
│       │   ├── __main__.py
│       │   ├── build.sh
│       │   └── virtualenv
│       └── put_server
│           ├── __deployer__.zip
│           ├── __main__.py
│           ├── build.sh
│           └── virtualenv
└── project.yml

and each function's build.sh roughly doing

virtualenv --without-pip virtualenv
pip install ../../../lib/lib --target virtualenv/lib/python3.9/site-packages

Is there a better way? Could you add an example (or alter an existing example) with how to best achieve this?

rabbah commented 1 year ago

This is close. Is your repo public I can tweak it and send you a PR.

petrem commented 1 year ago

Thanks for the offer, @rabbah . I made it public here: https://gitlab.com/petrem/digitalocean-jamstack-tut

rabbah commented 1 year ago

Taking a look - for reference the build docs are here https://docs.digitalocean.com/products/functions/reference/build-process, they describe the build sequence when using a lib folder.

rabbah commented 1 year ago

I believe what you're currently missing is a .include file in each of the function directories to add the virtualenv directory>

> cat packages/servers/get_servers/.include
__main__.py
virtualenv

I didn't try this out (i will) but I suspect that's what you're currently missing from the deployment process.

rabbah commented 1 year ago

Here's the git patch.

> git diff b00e3f8a2153001e886bc86b93be0325de6d78e3
diff --git a/functions/packages/servers/get_servers/.include b/functions/packages/servers/get_servers/.include
new file mode 100644
index 0000000..e9c72cd
--- /dev/null
+++ b/functions/packages/servers/get_servers/.include
@@ -0,0 +1,2 @@
+__main__.py
+virtualenv
diff --git a/functions/packages/servers/put_server/.include b/functions/packages/servers/put_server/.include
new file mode 100644
index 0000000..e9c72cd
--- /dev/null
+++ b/functions/packages/servers/put_server/.include
@@ -0,0 +1,2 @@
+__main__.py
+virtualenv
DATABASE_URL="mongodb+srv://<redacted>/admin?tls=true&authSource=admin"  doctl sls deploy . --remote-build
Deploying '/projects/digitalocean-jamstack-tut/functions' ...
Submitted action 'servers/get_servers' for remote building and deployment in runtime python:default (id: 48226ca7d4ef44f0a26ca7d4ef74f0a1)
Submitted action 'servers/put_server' for remote building and deployment in runtime python:default (id: 6bb850e8a7ec4deab850e8a7eccdea7a)
Processing of action 'servers/get_servers' is still running remotely ...
Processing of action 'servers/put_server' is still running remotely ...
Processing of action 'servers/put_server' is still running remotely ...
Processing of action 'servers/get_servers' is still running remotely ...
Processing of action 'servers/get_servers' is still running remotely ...
Processing of action 'servers/put_server' is still running remotely ...
Deployment status recorded in '.deployed'

Deployed functions ('doctl sls fn get <funcName> --url' for URL):
  - servers/get_servers
  - servers/put_server
> doctl sls fn invoke servers/get_servers
{
  "body": []
}
petrem commented 1 year ago

Thank you for the patch. The linked code is purely for learning/tutorial, there would be a lot more to do for a real app, of course -- but making sure I only upload what is needed is definitely important.

So what you are saying is, basically, that this is the expected approach for adding library code to multiple functions (or perhaps in other words, there is no support for adding library code and that lib is just a support mechanism for building the functions, however we see fit?)

Thank you for looking into it.