Slide 1

Slide 1 text

Django Deployments Done Right Peter Baumgartner Founder, Lincoln Loop DjangoCon US 2015

Slide 2

Slide 2 text

About Me

Slide 3

Slide 3 text

Founder at Lincoln Loop

Slide 4

Slide 4 text

Co-author of High Performance Django

Slide 5

Slide 5 text

Django Deployments Done Right

Slide 6

Slide 6 text

Python Deployments Done Right

Slide 7

Slide 7 text

Deployments Done Right

Slide 8

Slide 8 text

What is a deployment?

Slide 9

Slide 9 text

What is a deployment? • Noun: The way your servers and software are setup

Slide 10

Slide 10 text

What is a deployment? • Noun: The way your servers and software are setup • Verb: The techniques used to install and update your software on 
 the servers

Slide 11

Slide 11 text

Why do you want a good deployment?

Slide 12

Slide 12 text

Uptime!

Slide 13

Slide 13 text

Humans

Slide 14

Slide 14 text

Bad Deployments

Slide 15

Slide 15 text

Bad Deployments • Stressful • Cause of burnout • Employee turnover

Slide 16

Slide 16 text

Good Deployments

Slide 17

Slide 17 text

Good Deployments • Non-events • Empowering

Slide 18

Slide 18 text

Business

Slide 19

Slide 19 text

Good Deployments • Ship faster • new features • bug fixes • failure recovery • Happy Humans • more productive • less turnover

Slide 20

Slide 20 text

What makes a good deployment?

Slide 21

Slide 21 text

It works

Slide 22

Slide 22 text

It is reliable

Slide 23

Slide 23 text

It is boring

Slide 24

Slide 24 text

It is user-friendly

Slide 25

Slide 25 text

It is fast

Slide 26

Slide 26 text

It is not disruptive

Slide 27

Slide 27 text

It is idempotent & deterministic

Slide 28

Slide 28 text

How to…

Slide 29

Slide 29 text

Use Configuration Management!

Slide 30

Slide 30 text

Configuration Management • Use Salt or Ansible 
 (unless other in-house experience) • Fabric is not a configuration management tool • Use it for everything
 (setup, updates, deploys, etc.)

Slide 31

Slide 31 text

Pin Dependencies

Slide 32

Slide 32 text

Pin Dependencies • Install exact versions PyPI, e.g. Django==1.8.4 • Consider setup.py and install_requires over requirements.txt • Can’t use PyPI? • Setup your own (pip2pi + Nginx or S3) • Vendor • Use a repo that you own

Slide 33

Slide 33 text

Reload, Don’t Restart

Slide 34

Slide 34 text

Reload, Don’t Restart • Don’t drop requests during a deploy • service {nginx,varnish} reload • Gracefully reload WSGI servers
 (gunicorn, uWSGI, etc.) • Works out-of-the-box with Upstart • systemd?
 ExecReload=/bin/kill -HUP $MAINPID

Slide 35

Slide 35 text

Make it User- Friendly

Slide 36

Slide 36 text

Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate"

Slide 37

Slide 37 text

Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook -i production web.yml

Slide 38

Slide 38 text

Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook -i production web.yml

Slide 39

Slide 39 text

Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook -i production web.yml fab production deploy

Slide 40

Slide 40 text

ssh salt "sudo salt 'web*' state.highstate" ansible-playbook -i production web.yml fab production deploy Make it User-Friendly

Slide 41

Slide 41 text

Make it User-Friendly fab production deploy fab staging deploy:my-branch fab staging logs fab production status

Slide 42

Slide 42 text

Make it User-Friendly

Slide 43

Slide 43 text

Make it User-Friendly

Slide 44

Slide 44 text

Isolate Each Build

Slide 45

Slide 45 text

Isolate Each Build • New build/release = New virtualenv • Broken builds don't take down the site • No cruft from previous builds • Fast painless rollbacks

Slide 46

Slide 46 text

Isolate Each Build

Slide 47

Slide 47 text

Isolate Each Build

Slide 48

Slide 48 text

Isolate Each Build • Multiple servers?
 Build once, deploy everywhere 1. Build wheels 2. Bundle 3. Ship • See platter.pocoo.org

Slide 49

Slide 49 text

Database Migrations

Slide 50

Slide 50 text

Database Migrations • Maintain backwards compatibility
 (for a few releases) • Big database and lots of traffic? • Avoid long table locks • See bit.ly/db-mg

Slide 51

Slide 51 text

Track Releases

Slide 52

Slide 52 text

Track Releases • Store commit hash and timestamp • Invaluable for regression tracking • NewRelic, Opbeat, Slack, etc.

Slide 53

Slide 53 text

Always Be Improving

Slide 54

Slide 54 text

Always Be Improving • Remember to think of the humans! • Can failures be avoided? • Can you make it faster? • Can you make it easier?

Slide 55

Slide 55 text

Thank You! Peter Baumgartner @ipmb [email protected]