How to access plone 6 site from console script?

In setup.py of my plone add-on I defined a console script do_something:

    entry_points="""
      ...
      [console_scripts]
      do_something = my.addon.scripts.do_something
      ...

Now, in my script I want to access the plone site. How can I do this in Plone 6?

I need something similar to this get_plone_site we used in Plone 4.

Console scripts create a script in the buildout but they don't have access to the zope instance itself. They are run independently of the Zope process so if you need to interact with the Plone site you should use the REST API.

Instead, you can define a [zopectl.command] section in your setup.py, and this will create additional bin/instance XXXX commands which will have access to the Zope database through the app.Plone variable.

Here's some demo code:

setup.py

   [zopectl.command]
    reindex_catalog_indexes = addon.scripts.reindex_catalog_indexes:main

addon.scripts.reindex_catalog_indexes.py

from plone import api
from zope.component.hooks import setSite
import transaction

def main(app, *args):
    setSite(app.Plone)
    # ...
    transaction.commit()

2 Likes

Thank you very much, @erral!

@erral, one more question, please. I can't find instance in my bin folder. This is all I have:

root@blabla:/app# ls bin
Activate.ps1   diazocompiler	  fstail       mkwsgiinstance  pip3.11		     repozo	   rst2man.py		  rst2xml.py	  update_locale        zdaemon	    zope-sendmail
__pycache__    diazopreprocessor  futurize     mxdev	       plone-register-flags  roman	   rst2odt.py		  rstpep2html.py  waitress-serve       zeo-nagios
activate       diazorun		  i18nextract  normalizer      plone-register-icons  rst2html.py   rst2odt_prepstyles.py  runwsgi	  wheel		       zeoctl
activate.csh   fsdump		  icalendar    pasteurize      python		     rst2html4.py  rst2pseudoxml.py	  runzeo	  zconfig	       zeopack
activate.fish  fsoids		  jsonschema   pip	       python3		     rst2html5.py  rst2s5.py		  testldap	  zconfig_schema2html  zodbconvert
addzopeuser    fsrefs		  markdown_py  pip3	       python3.11	     rst2latex.py  rst2xetex.py		  unidecode	  zconsole	       zodbpack

I start my backend with ./docker-entrypoint.sh start command.

How can I run the zopectl.command in this case?

then it should be ./docker-entrypoint.sh yourscript right? In that case you should check whether docker-entrypoint supports arbitrary parameters or does specific things for start.

If you are using plone/plone-backend I can see here that docker-entrypoint.sh handles start specially:

So you may execute the full command in some other way. I also see there that the script used to start the site is zconsole so you may need to execute something like:

./docker-entrypoint.sh zconsole yourscript or something like that.

1 Like

Yes, my backend is based on plone/plone-backend. I tried with no success different things. :slight_smile:

Workaround:
I implemented this as a public browser view, protected by a token.
/@@my-view?token=tokenhere
The token value is saved in an ENV VAR.

My cronjob just has a curl command to trigger my view (which starts the script).

1 Like