Trigger cron style jobs in Plone sites without passwords

For some time the plone.recipe.zope2instance added support to execute scripts in the context of a full "Zope 2 environment", by using it such as 

bin/instance run /path/to/script

This can be used to launch Python scripts with full support of the Zope machinery, connected to the Zeo server, etc. The script can be generated as a console script from any Plone addon. Here's a small snippet to be used to get a "fully integrated" Plone site:

HOST = 'www.example.com'
PLONE_PATH = '/Plone'    # physical Path of Plone website

def get_plone_site():
    import Zope2
    app = Zope2.app()
    from Testing.ZopeTestCase import utils
    utils._Z2HOST = HOST

    path = PLONE_PATH.split('/')
    plone = path[-1]

    app = utils.makerequest(app)
    app.REQUEST['PARENTS'] = [app]
    app.REQUEST.other['VirtualRootPhysicalPath'] = path
    from zope.globalrequest import setRequest
    setRequest(app.REQUEST)

    from AccessControl.SpecialUsers import system as user
    from AccessControl.SecurityManagement import newSecurityManager
    newSecurityManager(None, user)

    _site = app[plone]
    site = _site.__of__(app)

    from zope.site.hooks import setSite
    setSite(site)

    return site

def main():
    site = get_plone_site()
    site['portal_catalog'].searchResults(...)

Now the "main" function can be registered as console script in the setup.py of the package

setup(
...
      entry_points="""
      # -*- Entry points: -*-
      [console_scripts]
      myscript = my.package.scripts:main
      """,
...
)

 Note: There's also clockserver support in Zope, you can configure as:

<clock-server>
    # starts a clock which calls /foo/bar every 30 seconds
    method /foo/bar
    period 30
    user admin
    password 123
    host www.example.com
</clock-server>

What I don't like about this: it involves saving passwords in config files, which is not very maintainable. Also, the run-based method can be called "on demand", for example by scripts watching a RabbitMQ queue, etc.

Comments