Skip to main content

A generic way to have post-deploy actions done.

Project description

Django Post Deploy

This module adds a way automate release-specific post-deploy/post-migrate actions to your Django project.

Features

  • Allow actions to be scheduled with the deployment automation
    • But also give the oppurtunity to keep special actions out of the automation and have those executed manually.
  • Schedule the actions to be executed in Celery.
    • The task scheduler is configurable, so you can write your own.
  • Support for django_tenants.

Alternatives

Alternative to this module you can use:

  • Management tasks. Used once and in the future confuse developers "Must we also maintain this code?"
  • "Empty" migrations. That
    • may or may not activate all custom expected code for it...
    • may or may not depend on other migrations being completed.

Why use this module?

This module is of use for your application if you:

  • want to keep management commands clean of one-time-used code.
  • want to have release finishing tasks executed in a certain and known state of the application.

Example

# Inside a custom django module's post_deploy.py
from post_deploy import register_post_deploy

@register_post_deploy(auto=True)
def make_non_code_changes_to_complete_the_next_release():
    pass

@register_post_deploy(auto=False)
def this_action_must_be_triggered_manually():
    pass
# This line is added to your deployment automation script
python manage.py deploy --auto
# This line is executed when the time is right for specific actions
$ ./manage.py deploy --one core.this_action_must_be_triggered_manually

# Or like this: both auto and manual actions are scheduled:
$ ./manage.py deploy --all

# Inspect the status of the actions like so, to see if there are errors for any action.
$ ./manage.py deploy --report

Configuration

Out of the box the deploy management command runs the actions serially. However, if you use Celery you have to add this to your settings:

Celery

# inside your projects settings.py file:

POST_DEPLOY_CELERY_APP = 'module.path.to.your.projects.celery.app'
POST_DEPLOY_SCHEDULER_MANAGER = 'post_deploy.plugins.scheduler.celery.CeleryScheduler'

Custom scheduler manager

Below is a partial copy of othe Celery scheduler manager as an example on how to write one for your alternative task scheduler:

NOTICE: remember to configure your custom scheduler manager in your project settings.

from celery import Celery
from celery.result import AsyncResult

from post_deploy.plugins.scheduler import DefaultScheduler


class CeleryScheduler(DefaultScheduler):
    ...

    def task_ready(self, id):
        return AsyncResult(id=id, app=...).ready()

    def schedule(self, action_ids, context_kwargs):
        from post_deploy.tasks import deploy_task
        result = deploy_task.delay(action_ids, context_kwargs)
        return result.id

    ...

Django tenants

This module supports the django_tenants module. In order to make the post deploy actions tenant aware two things are different from normal operation. You have to (1) configure a context manager, and (2) the management command is a little different.

# inside your projects settings.py file:

POST_DEPLOY_CONTEXT_MANAGER = 'post_deploy.plugins.context.tenant.TenantContext'
# The tenant schema is transported to the celery task runner when the management
# command is executed in the given schema aware management commands. For example:

# all_tenants_command example:
$ ./manage.py all_tenants_command deploy --all

# tenant_command example:
$ ./manage.py tenant_command deploy --report --schema=public

Example on how to have actions behave different based on the selected schema:

# Inside a module's post_deploy.py
from django_tenants.utils import parse_tenant_config_path
from post_deploy import register_post_deploy

@register_post_deploy(auto=True)
def example_on_how_to_exclude_execution_schema_based():
    if parse_tenant_config_path("") == 'public':
        # Do 'public' specific operations.
        pass
  
    # else, proceed as required.
    ...

Technical details

  • This module provides a model, and therefore require a common relational database to be installed. There are however no relations between multiple models in this module, so it may be possible that it works with a non-relational database too. But it is not tested in non-relational database configurations.

License information

django_post_deploy (c) by Erlend ter Maat

django_post_deploy is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

You should have received a copy of the license along with this work. If not, see http://creativecommons.org/licenses/by-sa/4.0/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_post_deploy-1.1.tar.gz (15.1 kB view details)

Uploaded Source

File details

Details for the file django_post_deploy-1.1.tar.gz.

File metadata

  • Download URL: django_post_deploy-1.1.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.9

File hashes

Hashes for django_post_deploy-1.1.tar.gz
Algorithm Hash digest
SHA256 eb631f70363a5bfc6b748c82a3f071c7e72a05a7eb5981efeb9532050b19cbec
MD5 ed4cb00c63fc9e0781d04f174a5a5272
BLAKE2b-256 27731c6af99bb8a5bb07f43bb04e9d02ccde4c0fb56d4028e12cd2952a98a2b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page