Cloned from https://bitbucket.org/bertrandchenal/byrd and converted to git

Bertrand Chenal a0699e3ea9 Add requirement file 7 lat temu
examples 228adb7dbc Get rid of spur 7 lat temu
tests b96c3f8292 Raise exception when local file is not found 7 lat temu
README.md 3a2d48641b Clean README 7 lat temu
baker.py b96c3f8292 Raise exception when local file is not found 7 lat temu
requirements.txt a0699e3ea9 Add requirement file 7 lat temu
setup.py 90324a7c63 Init repo 7 lat temu

README.md

Baker

Baker is a simple deployment tool based on paramiko. The config file format is inspired by Sup (but not identical). In contrast to sup, Baker is meant to be invoked from any OS (aka Windows support). This project is in alpha stage, please handle carefully.

The name Baker is a reference to Chet Baker.

Quickstart

Basic Example

By default baker will use bk.yaml as config file:

networks:
  web:
    hosts:
      - web1.example.com
      - web2.example.com
  db:
    hosts:
      - db1.example.com
      - db2.example.com

tasks:
  health:
    desc: Get basic health info
    run: uptime

  time:
    desc: Print current time (on local machine)
    local: date -Iseconds
    once: true

Based on the above file, one can run the following operations (imagine that the INFO lines are colored):

$ bk time
INFO:2018-08-01 23:14:05: Load config bk.yaml
$ bk  time -v
INFO:2018-08-01 23:14:21: Load config bk.yaml
INFO:2018-08-01 23:14:21: Print current time (on local machine)
2018-08-01T23:14:21+02:00
$ bk health web -v
INFO:2018-08-01 23:14:25: Load config bk.yaml
INFO:2018-08-01 23:14:25: web1.example.com: Get basic health info
 23:14:26 up 7 days,  6:28,  4 users,  load average: 0,30, 0,26, 0,22
INFO:2018-08-01 23:14:26: web2.example.com: Get basic health info
  23:14:26 up 7 days,  6:28,  4 users,  load average: 0,30, 0,26, 0,22

You can also pass --dry-run (or -d) to print what would have been done:

$ bk web health --dry-run
INFO:2018-08-13 14:36:05: Load config bk.yaml
INFO:2018-08-13 14:36:05: web1.example.com: Get basic health info
INFO:2018-08-13 14:36:05: [DRY-RUN] uptime
INFO:2018-08-13 14:36:05: web2.example.com: Get basic health info
INFO:2018-08-13 14:36:05: [DRY-RUN] uptime

Multi-tasks

The following example shows how Baker handles environment variables, and how to assemble tasks.

tasks:
  echo:
    desc: Simple echo
    local: echo "{what}"
    once: true
    env:
      what: "ECHO!"

  echo-var:
    desc: Echo an env variable
    local: echo {my_var}
    once: true
    
  both:
    desc: Run both tasks
    multi:
      - task: echo
        export: my_var  # tells baker to use task ouput to set my_var
      - task: echo-var

We can then do the following:

$ bk both -v
INFO:2018-08-01 23:00:37: Load config bk.yaml
INFO:2018-08-01 23:00:37: Simple echo 
ECHO!
INFO:2018-08-01 23:00:37: Echo an env variable
ECHO!
$ bk both -e what="WHAT?" -v
INFO:2018-08-01 23:01:15: Load config bk.yaml
INFO:2018-08-01 23:01:15: Simple echo
WHAT?
INFO:2018-08-01 23:01:15: Echo an env variable
WHAT?

As you can see the export directive tells Baker to save the result of a command under a given environment variable.

Python

Python code can be added with a python directive:

  hello-python:
    desc: Says hello with python
    python: |
      for i in range(10):
          print('hello')
    once: true

Environement is used to format command, but it is also used to define the initial environement of command (duh!), so you can access it with the os module (for example print(os.envoron['MY_VAR']). It works for python directive but also for local and remote commands.

Assert

  hello-python:
    desc: Says hello with python
    python: |
      for i in range(10):
          print('hello')
    once: true
    assert: "len(stdout.splitlines()) == 10"

You can also add assert directives, they are run as a python eval within the current env, and can access two extra variables: stdout and stderr.

SSH Authentication

Currently, Baker only supports private key authentication. You can add an auth section that tells where to find your private key:

auth:
  ssh_private_key: path/to/deploy_key_rsa

On the following invocation, Baker will ask your passphrase for the key. This passphrase will be saved in your OS keyring (thanks to the keyring module.)

Load other config files

The top-level directive load allows to import other config files and merge them with the current one, so with bk.yaml containing:

load:
  - file: network.yaml
    as: net
tasks:
  echo-host:
    local: echo {host}

and network.yaml containing:

networks:
  web:
    hosts:
      - web1.example.com
      - web2.example.com

We can now run:

$ bk net/web echo-host --dry-run' -v
INFO:2018-08-14 11:27:33: Load config bk.yaml
INFO:2018-08-14 11:27:33: Load config network.yaml
INFO:2018-08-14 11:27:33: echo-host
INFO:2018-08-14 11:27:33: [DRY-RUN] echo web1.example.com
INFO:2018-08-14 11:27:33: echo-host
INFO:2018-08-14 11:27:33: [DRY-RUN] echo web2.example.com

Roadmap

  • Add doc about load directive (and namespacing)
  • Add a contrib directory with ready-made tasks for common operations
  • Add password-based auth, add per-network auth.