Skip to content

Add ability to export rulesets as python eggs #13

@miraculixx

Description

@miraculixx

Utlimate goal is to be able to store, version and deploy rulesets between development, test and production environments. This story should implement the basics for this goal -- as soon as we can serialise/deserialise rules and rulesets, the other elements become trivial.

Expected behavior

  1. Rulesets can be exported into/built as python eggs
  2. Ruleset eggs are pip installable

Tasks

  1. implement a sample rule app that includes one rule for each type (Rule, LabmdaRule, Tableruleset)
  2. build an egg for this sample rule (manually)
  3. automate the process for building a ruleset egg as part of the django application, i.e. add a management command that does this
  4. write unit tests to verify the full export/import/execute cycle.

Implementation notes

import

a. Module rules

A module rule is simply a python class in the app's rules module/package. Hence importing (e.g. in a celery instance) means to find all Rule instances.

b. TableRulesets

Basically the same as with Module rules, assuming that each tablerule has been properly specified as shown below, and the tablerules.py is imported at startup.

export

Rules are either python modules or yaml specifications. Hence exporting a ruleset and building an egg from it involves these steps:

pythonic approach

  1. find all rules in a ruleset. create a ruleset.py that includes all selected Rulesets:
rulesets = {}

# repeat for all rulesets
rules = <build the list of rule instances in the exported ruleset>
rulesets[ruleset_slug] = Ruleset(*rules)
  1. for module rules, make sure to package the rules.py / rules module. for now assume that each rule is in the client app's rules module / name space package.
  2. for table rulesets, create an entry in tablerules.py:
# generated by rule_export at mm-dd-yyyy hh:mm
class TableRuleset<slug>(Tableruleset):
       __specs__ = """ 
       <yaml-code>
       """
  1. make sure tablerules.py gets imported by rulest.py.
  2. bump the version in the version file

I prefer the python approach. It may be helpful to code a full example egg by hand before automating it from the django models.

For completeness, this would be the non-pythonic approach. Yikes...

non python approach

  1. find all rules
  2. get a canonical representation of the rule in JSON:
{  
  "type" : "module" / "yaml",
  "path" : "path.to.module",
  "definition" : <serialized yml>
}
  1. create a python egg that contains all necessary modules plus the list of all rules as per above format in ruleset.json:
{
  "name" : name
  "version": version taken from egg setup.py
  "rules": [ list of rules as above]
}

The setup.py should have a version file with a version number that is incremented automatically on each build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions