Slide 1

Slide 1 text

Shut up and pipe! Unix-style Object Collaboration in Rack and Vagrant

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

paulh Paul Hinze jbelser Jeff Belser liz Liz Abinante nomitch Mike Nomitch jstern Jennifer Stern z Mike Ziwisky illstructure

Slide 6

Slide 6 text

Design

Slide 7

Slide 7 text

A B

Slide 8

Slide 8 text

A B

Slide 9

Slide 9 text

A B A B

Slide 10

Slide 10 text

But How?

Slide 11

Slide 11 text

Giving a Crap Mostly just by

Slide 12

Slide 12 text

Other People's Code

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Patterns

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Ceci n'est pas une pipe

Slide 22

Slide 22 text

Output interface = Input interface

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Composition

Slide 25

Slide 25 text

"Write programs to handle text streams, because that is a universal interface."

Slide 26

Slide 26 text

cat /var/log/system.log

Slide 27

Slide 27 text

cat /var/log/system.log |

Slide 28

Slide 28 text

cat /var/log/system.log | grep '^Jan'

Slide 29

Slide 29 text

cat /var/log/system.log | grep '^Jan' | awk '{ print $5 }'

Slide 30

Slide 30 text

cat /var/log/system.log | grep '^Jan' | awk '{ print $5 }' | sort | uniq -c

Slide 31

Slide 31 text

cat /var/log/system.log | grep '^Jan' | awk '{ print $5 }' | sort | uniq -c | sort -n

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

an Object that responds to #call #call accepts one Hash argument #call returns an Array with [status, headers, body] where and

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

app = ->(env) { [200, {}, 'Hello, World!'] } ! Rack::Handler::Thin.run(app) It's Simple!

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

app = ->(env) { [200, {}, 'Hello, World!'] } Rack::Handler.const_get( %i[ Thin WEBrick Unicorn Puma ].sample ).run(app) Any Server

Slide 40

Slide 40 text

app = [ BigOle::RailsApp.new, TinyClassInSameFile.new ].sample ! Rack::Handler::Thin.run(app) Any Application

Slide 41

Slide 41 text

Middleware

Slide 42

Slide 42 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 43

Slide 43 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 44

Slide 44 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 45

Slide 45 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 46

Slide 46 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 47

Slide 47 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 48

Slide 48 text

class YellIt def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app))

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

This is very Powerful!

Slide 52

Slide 52 text

class Rack::Logger def initialize(app, logger=Logger.new(STDOUT)) @app = app @logger = logger end ! def call(env) status, headers, body = @app.call(env) @logger.info("[#{Time.now}] #{status}") [status, headers, body] end end A logger

Slide 53

Slide 53 text

class Rack::ContentLength def initialize(app) @app = app end ! def call(env) status, headers, body = @app.call(env) ! if !headers['Content-Length'] headers['Content-Length'] = body.length end ! [status, headers, body] end end A header

Slide 54

Slide 54 text

class Rack::Runtime def initialize(app) @app = app end ! def call(env) start_time = Time.now status, headers, body = @app.call(env) request_time = Time.now - start_time ! headers['X-Runtime'] = "%0.6f" % request_time ! [status, headers, body] end end A timer

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) A auth!

Slide 57

Slide 57 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth)

Slide 58

Slide 58 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth)

Slide 59

Slide 59 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth)

Slide 60

Slide 60 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth)

Slide 61

Slide 61 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth)

Slide 62

Slide 62 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) Our app doesn't need to care about Authentication

Slide 63

Slide 63 text

class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass = app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) Amazing!

Slide 64

Slide 64 text

Composition

Slide 65

Slide 65 text

Rack::Logger.new( Rack::Auth::Basic.new( Rack::Runtime.new( Rack::ContentLength.new( app ) ), 'paulh', 'hello'), Logger.new(STDERR) )

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Rack::Logger.new( Rack::Auth::Basic.new( Rack::Runtime.new( Rack::ContentLength.new( app ) ), 'paulh', 'hello'), Logger.new(STDERR) )

Slide 68

Slide 68 text

class Rack::Builder def initialize(app, &block) @use = [] @app = app instance_eval(&block) end ! def use(middleware) @use.push ->(app) { middleware.new(app) } end ! def call(env) @use.reverse.inject(@app) { |app, middleware_builder| middleware_builder.call(app) }.call(env) end end ! built = Rack::Builder.new(app) do use Rack::Logger use Rack::ContentLength use Rack::Runtime end ! Rack::Handler::Thin.run(built)

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

rake middleware

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end

Slide 73

Slide 73 text

Vagrant::Action::Builder#insert_before ! Vagrant::Action::Builder#insert_after Extensibility!

Slide 74

Slide 74 text

def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end Extensibility!

Slide 75

Slide 75 text

def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end 1. 2. 3. 4. 5. 6. wait...

Slide 76

Slide 76 text

def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end Complexity escalate quickly can

Slide 77

Slide 77 text

B def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end A

Slide 78

Slide 78 text

Every tool has sharp edges

Slide 79

Slide 79 text

Ceci n'est pas une panacea

Slide 80

Slide 80 text

Design is worthwhile

Slide 81

Slide 81 text

Wild code can bring joy

Slide 82

Slide 82 text

Simplicity can be powerful

Slide 83

Slide 83 text

PIPE!