customizing chef for fun and profit

45
Customizing Chef For Fun and Profit

Upload: jon-cowie

Post on 17-May-2015

4.520 views

Category:

Technology


0 download

DESCRIPTION

Chefconf 2014

TRANSCRIPT

Page 1: Customizing Chef for Fun and Profit

Customizing Chef

For Fun and Profit

Page 2: Customizing Chef for Fun and Profit

@jonlives

Jon Cowie

Sr Operations Engineer

Page 3: Customizing Chef for Fun and Profit
Page 4: Customizing Chef for Fun and Profit

@jonlives

Page 5: Customizing Chef for Fun and Profit

@jonlives

Page 6: Customizing Chef for Fun and Profit

@jonlives

Beginning of 2010 Today

Page 7: Customizing Chef for Fun and Profit

@jonlives

Chef at Etsy

Page 8: Customizing Chef for Fun and Profit

@jonlives

Chef at Etsy

• Open Source Chef Server

• ~1500 Nodes

• CentOS, some Mac OS X

Page 9: Customizing Chef for Fun and Profit

@jonlives

We Love Chef!

Page 10: Customizing Chef for Fun and Profit

@jonlives

We Know Best.

Page 11: Customizing Chef for Fun and Profit

@jonlives

Absorb what is useful.

Discard what is useless.

Page 12: Customizing Chef for Fun and Profit

@jonlives

“I am not smart enough to build an ontology … that

can encompass all the variations in infrastructure.

Nobody is, the world moves too fast.”

Page 13: Customizing Chef for Fun and Profit

@jonlives

There is no magic pill.

Page 14: Customizing Chef for Fun and Profit

@jonlives

You are the expert.

Page 15: Customizing Chef for Fun and Profit

@jonlives

Customizability

Page 16: Customizing Chef for Fun and Profit

@jonlives

Page 17: Customizing Chef for Fun and Profit

@jonlives

Thin Server

Page 18: Customizing Chef for Fun and Profit

@jonlives

Thick Client

Page 19: Customizing Chef for Fun and Profit

@jonlives

Page 20: Customizing Chef for Fun and Profit

@jonlives

Get Config Data / Initialize

• Load client.rb

• Server URL

• Auth Credentials

• Paths

• Run ohai

• Collect attributes

• Plugins!

Page 21: Customizing Chef for Fun and Profit

@jonlives

Ohai Plugins• Add attributes to your

node • Executed at start of

run • Single Ruby file • Highest precedence

Page 22: Customizing Chef for Fun and Profit

@jonlives

Ohai Plugins - v6

provides ‘awesome’ !

awesome Mash.new awesome[:sauce] = "Sricacha" awesome[:level] = "11"

Page 23: Customizing Chef for Fun and Profit

@jonlives

Ohai Plugins - v7 (Coming soon!)Ohai.plugin(:Awesome) do provides "awesome" collect_data(:darwin) do awesome Mash.new awesome[:sauce] = “Sriracha" awesome[:level] = 11 end end

Page 24: Customizing Chef for Fun and Profit

@jonlives

Ohai Plugins - client.rb / solo.rb

Ohai::Config[:plugin_path] << /<path>/plugins

Page 25: Customizing Chef for Fun and Profit

@jonlives

Authenticate / Register

• Authenticate to Server

• <clientkey>.pem

• Register if not already registered

• <validation>.pem

• If both fail, go directly to jail.

Page 26: Customizing Chef for Fun and Profit

@jonlives

Load / Build Node

• Download node

• Combine attributes

• Expand run_list

• Order matters!

• Create RunStatus object

• Run start handlers

Page 27: Customizing Chef for Fun and Profit

@jonlives

Handlers

• Respond to specific situations

• Start, Report, Exception • Common Code

Page 28: Customizing Chef for Fun and Profit

@jonlives

Handlers - Common Coderequire "chef/handler" !

class HandlerName < Chef::Handler def report # Ruby code goes here end end

Page 29: Customizing Chef for Fun and Profit

@jonlives

Handlers - client.rb / solo.rb

require “/path_to/mystarthandler.rb" !

my_start_handler = MyStartHandler.new !

start_handlers << my_start_handler

Page 30: Customizing Chef for Fun and Profit

@jonlives

Synchronize Cookbooks

• Get cookbook files list

• Only required recipes, files & templates

• Compares to local cache

• Download changes

Page 31: Customizing Chef for Fun and Profit

@jonlives

Set up RunContext

• Cookbook Collection

• Definitions List

• Events

• Notifications

• Node

• Resource Collection

• Empty for now…

Page 32: Customizing Chef for Fun and Profit

@jonlives

Events• Respond to interesting

events • Pub / Sub model • EventDispatch::Base • Formatter • Custom Subscriber

Page 33: Customizing Chef for Fun and Profit

@jonlives

Events - Custom Subscriberrequire “chef/event_dispatch/base" !

class AwesomeSubscriber < Chef::EventDispatch::Base def run_started(run_status) … end # Method for each interesting event end

Page 34: Customizing Chef for Fun and Profit

@jonlives

Events - Register Subscriberrequire "chef/handler" require “/path/awesome_subscriber.rb' !

class AwesomeSubscriberStartHandler < Chef::Handler def report event_dispatcher_subscriber = AwesomeSubscriber.new @run_status.events.register(event_dispatcher_subscriber) end end

Page 35: Customizing Chef for Fun and Profit

@jonlives

Load Cookbook Data

• Populate Resource Collection

• Libraries

• Attributes

• Resources / Providers

• Definitions

• Recipes

Page 36: Customizing Chef for Fun and Profit

@jonlives

Converge Node

• Actually changes node

• Apply resource collection

• Again, order matters!

Page 37: Customizing Chef for Fun and Profit

@jonlives

Finalize

• Successful Run

• Save Node

• Report Handlers

• Failed Run

• Exception Handlers

• 💩

Page 38: Customizing Chef for Fun and Profit

@jonlives

Report & Exception Handlersdef report if @run_status.success? # Do a happy dance elsif @run_status.failed? # Sad panda. end end

Page 39: Customizing Chef for Fun and Profit

@jonlives

Handlers - client.rb / solo.rbrequire “/var/chef/handlers/mystarthandler.rb"

require "/var/chef/handlers/mynewhandler.rb"

!

my_start_handler = MyStartHandler.new

my_new_handler = MyNewHandler.new

!

start_handlers << my_start_handler

report_handlers << my_new_handler

exception_handlers << my_new_handler

Page 40: Customizing Chef for Fun and Profit

@jonlives

That RunStatus Object Again…• Accessible to handlers

• Run status & timings

• Exception and Backtrace

• All & updated resources

• RunContext

• Node object

Page 41: Customizing Chef for Fun and Profit

@jonlives

Don’t fear the code!• https://github.com/opscode/chef

• lib/chef/handler.rb

• lib/chef/run_status.rb

• lib/chef/run_context.rb

• lib/chef/event_dispatch/base.rb

Page 42: Customizing Chef for Fun and Profit

@jonlives

Um…wat?

Page 43: Customizing Chef for Fun and Profit

@jonlives

Criteria for Customization

Simplicity

Modularity

Visibility

Maintainability

Scalability

Page 44: Customizing Chef for Fun and Profit

@jonlives

November 2014 !

http://jonliv.es/book !

O’Reilly Stand

Page 45: Customizing Chef for Fun and Profit

@jonlives

Thanks! Questions?

!

Office Hours @ 3.15, Marina Room @jonlives / http://jonliv.es / [email protected]