collective / cookiecutter-plone-starter

Cookiecutter Plone Starter is a framework for jumpstarting Plone 6 projects quickly.
MIT License
17 stars 10 forks source link

Allow complete override of instance.yaml in backend with instance-local.yaml #145

Open fredvd opened 3 weeks ago

fredvd commented 3 weeks ago

Sometines the solutions are so simple and almost there in front of you. I was frustrated a lot while working on projects with a local backend that I couldn't change the storage backend to zeo, of move it to another folder without touching instance.yaml, which is in git. (I even created separate 'build-dev-fs' targets in 1-2 local projects.).

Can we add this to the cookiecutter template? But please can someone check if this is correct Makefile-ish? It took me 20 minutes just to add/fix an if check :blush:

https://github.com/plone/demo.plone.org/pull/39

https://github.com/plone/demo.plone.org/pull/39/files#diff-5024268fbb0b80c2fca407bd63672f2e0e8c2b61e87f3bdd361e6a7a41a9dda3R99-R104

fredvd commented 3 weeks ago

PR in https://github.com/collective/cookiecutter-plone-starter/pull/146

jensens commented 3 weeks ago

This works, but I think we can be way more flexible using environment variables.

I use cookiecutter-zope-instance new helper transform_from_environment.py before startup in the entrypoint.

This needs a download of cookiecutter, cookiecutter-zope-instance and transform_from_environment as part of the image to not be bound to a working internet connection or github at container startup.

Here an example from my custom OCI image (I use different folders, but you get the idea):

# ...
ENV COOKIECUTTER_ZOPE_INSTANCE_VERSION 2.0.1
ENV ZOPE_TEMPLATE=/site/deployment/cookiecutter-zope-instance.zip
ENV ZOPE_CONFIGURATION_FILE /data/instance-from-environment.yaml

RUN \
# ...
  # actually I use mxmake and do "make packages cookiecutter &&\"
  # but here this would be enough:
  pip install "cookiecutter==2.6.0" &&\
  wget -O $ZOPE_TEMPLATE https://github.com/plone/cookiecutter-zope-instance/archive/refs/tags/${COOKIECUTTER_ZOPE_INSTANCE_VERSION}.zip &&\
  wget -O deployment/transform_from_environment.py https://raw.githubusercontent.com/plone/cookiecutter-zope-instance/${COOKIECUTTER_ZOPE_INSTANCE_VERSION}/helpers/transform_from_environment.py &&\
  chmod u+x deployment/transform_from_environment.py &&\
# ...

Then in the entrypoint, before anything else

#!/bin/bash
set -e

echo "Plone OCI-Image entrypoint entered"
echo "... generate instance from environment"
/app/bin/python /site/deployment/transform_from_environment.py -o $ZOPE_CONFIGURATION_FILE

# actually I do "make zope-instance" here, but this would do it:
/app/bin/cookiecutter -f --no-input  --config-file $(ZOPE_CONFIGURATION_FILE) --output-dir $(ZOPE_BASE_FOLDER) $(ZOPE_TEMPLATE)

After this, in my docker compose file I set a bunch of environment variables like so:

    environment:
      INSTANCE_location_clienthome: /data
      INSTANCE_wsgi_listen: 0.0.0.0:8080
      INSTANCE_wsgi_fast_listen: ""
      INSTANCE_wsgi_threads: ${ZOPE_THREADS}
      INSTANCE_debug_mode: "false"
      INSTANCE_initial_user_name: admin
      INSTANCE_initial_user_password: foobar
      INSTANCE_db_storage: relstorage
      INSTANCE_db_blobs_mode: cache
      INSTANCE_db_cache_size: ${ZODB_CACHE_SIZE_OBJECTS?unset}
      INSTANCE_db_cache_size_bytes: ${ZODB_CACHE_SIZE_BYTES?unset}
      INSTANCE_db_relstorage: postgresql
      INSTANCE_db_relstorage_keep_history: "false"
      INSTANCE_db_relstorage_postgresql_dsn: host='db' dbname='plone' user='plone' password='verysecret'
      INSTANCE_db_relstorage_cache_local_mb: ${RELSTORAGE_LOCAL_MB?unset}