Skip to content

Smart Policies

Adán Sánchez de Pedro Crespo edited this page Jul 28, 2016 · 16 revisions

Smart Policies


Smart Policies are scripts that receive notifications every time a watched file changes. Policies trigger actions such as emailing someone, reverting changes or shutting the system down.

Currently you can write policies in Javascript, CoffeeScript or IcedCoffeeScript.

Policies are Node.js packages downloaded from public git repositories. Anyone can take any available policy, fork it and improve it.

Policies are parameterizable. Each policy package can define customizable "fields" to suit different monitoring needs.

When the watcher starts, it runs every policy inside a sandboxed virtual machine. Such virtual machines run in the background, waiting the watched files to change.

Every time a watched file gets modified, the watcher generates a diff. Such diff is then sent to every one of the modified file's policies.

Ready-to-use policies

  • Mailer: sends an email with the diff between previous version of a file and current one.
  • Backup: backs up a file to a different location every time it gets modified.
  • Rollbacker: rolls modified files back to the previous version.
  • Shutdown: shuts the whole system down.
  • Stamper: embeds your files into the blockchain.
  • Zapier (coming soon): connects your file events to a whole world of apps.

Writing your own policies

Policy package structure

  • main.js / main.coffee / main.iced: Main entry point for the policy script.
  • package.json: Package manifest.
  • LICENSE: (optional)
  • README.md: (optional)

package.json example

The policy object is compulsory and must contain the language attribute. Possible values are javascript, coffeescript or icedcoffeescript.

The policy.params is optional and defines variables that you can customize when you attach the policy to a watched file. Their values are passed to the policy script's constructor.

{
  "name": "policy-mailer",
  "version": "0.0.1",
  "dependencies": {
    "email": "0.2.6"
  },
  "policy": {
    "language": "coffeescript",
    "params": {
      "from": {
        "type": "email",
        "label": "Sender address"
      },
      "to": {
        "type": "email",
        "label": "Recipient address"
      }
    }
  }
}

Policy script example

You can name your main class as you prefer as long as you export it by assigning it to module.exports.

Email = require('email').Email

module.exports = class Policy
  constructor : (params) ->
    @file = params.filename
    @from = params.from
    @to = params.to

  receiver : (changes) =>
    msg = new Email
      from: @from
      to: @to
      subject: """File "#{@file}" has been modified"""
      body: """File "#{@file}" has been modified.
The changes are:
#{JSON.stringify changes}
"""
    msg.send()
Clone this wiki locally