Skip to content

Instantly share code, notes, and snippets.

@teohm
Last active December 31, 2015 12:29
Show Gist options
  • Save teohm/7986198 to your computer and use it in GitHub Desktop.
Save teohm/7986198 to your computer and use it in GitHub Desktop.

12 factor ..IIX(-___-*)*XII''

http://12factor.net/

  1. Codebase - One codebase tracked in revision control, many deploys
  2. Dependencies - Explicitly declare and isolate dependencies
  3. Config - Store config in the environment
  4. Backing Services - Treat backing services as attached resources
  5. Build, release, run - Strictly separate build and run stages
  6. Processes - Execute the app as one or more stateless processes
  7. Port binding - Export services via port binding
  8. Concurrency - Scale out via the process model
  9. Disposability - Maximize robustness with fast startup and graceful shutdown
  10. Dev/prod parity - Keep development, staging, and production as similar as possible
  11. Logs - Treat logs as event streams
  12. Admin processes - Run admin/management tasks as one-off processes

01. Codebase

  • 1 app in 1 git repo

02. Dependencies

gem install natives

natives install --gemfile=Gemfile

bundle install --deployment

Question: How to vendor system tools e.g. imagemagick, curl?

Perhaps.. docker?

03. Config

Ideas

  • a separate repo
  • store env files in repo
  • or store 1 env.yml, generate env files during deployment
# env.yml
default: &default
  DATABASE_URL: foo
  FOO: bar

development:
  <<: *default

staging:
  <<: *default
  DATABASE_URL: foo_staging
  BAR: foo

# yaml2env.rb
require 'yaml'
YAML.load_file('env.yml').each do |env_name, vars|
  File.open("env.#{env_name}", 'w') do |f|
    vars.each do |key, value|
      f.puts "#{key}=#{value}"
    end
  end
end
# Gemfile
gem 'dotenv-rails'

# .env.sample
DATABASE_URL=blablabla

# .gitignore
.env*
!.env.sample

04. Backing Services

Database

rm config/database.yml

# .env
DATABASE_URL=sqlite3://localhost/?database=db/{app}_development.sqlite3&pool=5&timeout=5000

DATABASE_URL=postgresql://postgres@localhost/{app}_development?encoding=unicode&pool=5&timeout=5000

DATABASE_URL=mysql://root@localhost/{app}_development?encoding=unicode&pool=5&timeout=5000

File storage

S3 API seems to be the de facto interface

https://code.google.com/p/mogilefs/ http://www.quora.com/Amazon-S3/Is-there-open-source-software-that-implements-Amazon-S3-plug-compatible-storage http://stackoverflow.com/questions/9210162/is-there-a-server-that-provides-an-amazon-s3-style-api-locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment