SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
Copyright © 2010 Opscode, Inc - All Rights Reserved
Speaker:
‣ joshua@opscode.com
‣ @jtimberman
‣ www.opscode.com
Joshua Timberman Technical Evangelist
1
Understanding LWRP
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved
Chef manages
Resources on Nodes
2
cookbook_file
template
service
package
deploy
git
http_request
link
ruby_block
log
bash
execute
remote_file
user
Tuesday, November 2, 2010
Resources are an abstraction we feed data into. When you write recipes in Chef, you create
resources of things you want to configure. You describe the state of some part of the system.
We’re going to talk about how this works in depth.
Copyright © 2010 Opscode, Inc - All Rights Reserved 3
gem_package "bluepill"
Tuesday, November 2, 2010
A very simple resource, simply tells Chef to install the bluepill package as a RubyGem.
Copyright © 2010 Opscode, Inc - All Rights Reserved 4
package "bluepill" do
provider Chef::Provider::Package::Rubygems
end
package "bluepill" do
provider gem_package
end
Tuesday, November 2, 2010
Likewise, these are both the equivalent of the previous gem_package, we’re just specifically
telling the package resource to use a particular provider. We’ll see why this matters later.
Copyright © 2010 Opscode, Inc - All Rights Reserved 5
template "/etc/bluepill/dnscache.pill" do
source "dnscache.pill.erb"
mode 0644
end
Tuesday, November 2, 2010
Another somewhat simple resource, specify a file to be written somewhere as a template.
Copyright © 2010 Opscode, Inc - All Rights Reserved 6
bluepill_service "dnscache" do
action [:enable,:load,:start]
end
Tuesday, November 2, 2010
Here’s a custom resource that specifies actions to run. We’ll talk in a bit how this works.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Resources take action
through Providers
7
Tuesday, November 2, 2010
The abstraction over the commands or API calls that will configure the resource to be in the
state you have defined.
Copyright © 2010 Opscode, Inc - All Rights Reserved 8
service "dnscache" do
provider bluepill_service
action [:enable, :load, :start]
end
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved 9
Chef’s Recipe DSL
creates resources
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved
Recipes evaluated in two phases
10
Compile Phase
Execution Phase
Tuesday, November 2, 2010
Going to leave out some of the details. The rest is in the code. :)
Copyright © 2010 Opscode, Inc - All Rights Reserved
Compile Phase
11
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved
Resources added to
ResourceCollection
12
Tuesday, November 2, 2010
When recipes are processed for Resources during the compile phase, they are added to the
resource collection. These can include..
Copyright © 2010 Opscode, Inc - All Rights Reserved
Chef::ResourceCollection
13
Resources and definitions
Hash of indexed resources
Definitions are replaced
Tuesday, November 2, 2010
Chef “recognizes” resources and definitions through method_missing. These are created as a
hash of numerically indexed resources. Definitions are replaced entirely by the resources they
contain.
Copyright © 2010 Opscode, Inc - All Rights Reserved 14
@resources_by_name={"file[/tmp/something]"=>0}
Tuesday, November 2, 2010
essential what the Resource Collection looks like.
Copyright © 2010 Opscode, Inc - All Rights Reserved 15
Chef::RunContext
Loads cookbook components
‣ Libraries
‣ Providers
‣ Resources
‣ Attributes
‣ Definitions
‣ Recipes
Tuesday, November 2, 2010
The order is important because of where you can put things, and when things are available.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Execution Phase
16
Tuesday, November 2, 2010
Once everything is loaded up and the recipes have been parsed for the resources they
contain, the runner starts the execution phase.
Copyright © 2010 Opscode, Inc - All Rights Reserved 17
Chef::Runner converges
the node
Tuesday, November 2, 2010
runs the provider actions specified by the resource
Copyright © 2010 Opscode, Inc - All Rights Reserved
Chef::Resource calls
run_action
18
Tuesday, November 2, 2010
run_action calls the method for that particular action in the provider
Copyright © 2010 Opscode, Inc - All Rights Reserved
Provider is chosen by
Chef::Platform
19
Tuesday, November 2, 2010
This is how platform specific providers are chosen like apt/yum, useradd, etc. It also contains
logic to get the provider for the resource..
Copyright © 2010 Opscode, Inc - All Rights Reserved
Chef::Platform finds the provider
20
Specifically named parameter
Platform assigned providers
Matching the resource name
Tuesday, November 2, 2010
Specifically named providers are like we saw earlier where the resource itself had the provider
parameter.
Platform assigned are the ones like apt/yum for packages.
Matching the resource name is how LWRPs get chosen.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Cookbook LWRP
21
Tuesday, November 2, 2010
LWRPs, or resources and providers written in this special DSL, reside in cookbooks.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Ruby DSL for writing
Resources & Providers
22
Tuesday, November 2, 2010
All LWRPs are, a Ruby DSL for writing resources and providers. This DSL is designed to be
easier to write and understand than the fully resource/provider code in the code included by
the Chef libraries.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Resources have states
23
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved
Resources have states
24
Resource DSL describe states
Two possible states
‣ Current (@current_resource)
‣ Desired (@new_resource)
Providers load_current_resource
Tuesday, November 2, 2010
The resource describes the state that some aspect of the system should be in by passing
parameters that configure it for that state.
Current and desired are the two states that a resource can be in.
Providers load_current_resource determines what state the resource is in.
Copyright © 2010 Opscode, Inc - All Rights Reserved 25
def load_current_resource
@bp = Chef::Resource::BluepillService.new(new_resource.name)
@bp.service_name(new_resource.service_name)
Chef::Log.debug("Checking status of service #{new_resource.service_name}")
begin
if run_command_with_systems_locale(:command => "#{node['bluepill']['bin']} status #
{new_resource.service_name}") == 0
@bp.running(true)
end
rescue Chef::Exceptions::Exec
@bp.running(false)
nil
end
if ::File.exists?("#{node['bluepill']['conf_dir']}/#{new_resource.service_name}.pill")
&& ::File.symlink?("#{node['bluepill']['init_dir']}/#{new_resource.service_name}")
@bp.enabled(true)
else
@bp.enabled(false)
end
end
Tuesday, November 2, 2010
The code is not important. What is important that we’re checking for some various states on
the system by running commands or code and setting “state” parameters.
Copyright © 2010 Opscode, Inc - All Rights Reserved 26
actions :start, :stop, :enable, :disable, :load, :restart
attribute :service_name, :name_attribute => true
attribute :enabled, :default => false
attribute :running, :default => false
attribute :variables, :kind_of => Hash
attribute :supports, :default => { :restart => true, :status => true }
Resource DSL
Tuesday, November 2, 2010
Actions correspond to ‘action’ methods in the provider.
Attributes correspond to methods for the resource. These are also called parameters and are
not node attributes.
Copyright © 2010 Opscode, Inc - All Rights Reserved
Provider DSL
27
action :start do
unless @bp.running
execute "/usr/bin/bluepill start #{new_resource.service_name}"
end
end
Tuesday, November 2, 2010
The provider DSL at a minimum has action methods. The action methods handle doing
whatever is needed to configure the resource to be in the declared state.
Earlier we set @bp_running to true or false depending on the current state, here we check.
Copyright © 2010 Opscode, Inc - All Rights Reserved 28
action :start do
unless @bp.running
execute "/usr/bin/bluepill start #{new_resource.service_name}"
end
end
actions :start, :stop, :enable, :disable, :load, :restart
attribute :service_name, :name_attribute => true
attribute :enabled, :default => false
attribute :running, :default => false
attribute :variables, :kind_of => Hash
attribute :supports, :default => { :restart => true, :status => true }
Tuesday, November 2, 2010
Lets break down the provider’s methods
Copyright © 2010 Opscode, Inc - All Rights Reserved 29
def load_current_resource
@bp = Chef::Resource::BluepillService.new(new_resource.name)
@bp.service_name(new_resource.service_name)
Chef::Log.debug("Checking status of service #{new_resource.service_name}")
begin
if run_command_with_systems_locale(:command => "#{node['bluepill']['bin']} status #
{new_resource.service_name}") == 0
@bp.running(true)
end
rescue Chef::Exceptions::Exec
@bp.running(false)
nil
end
if ::File.exists?("#{node['bluepill']['conf_dir']}/#{new_resource.service_name}.pill")
&& ::File.symlink?("#{node['bluepill']['init_dir']}/#{new_resource.service_name}")
@bp.enabled(true)
else
@bp.enabled(false)
end
end
actions :start, :stop, :enable, :disable, :load, :restart
attribute :service_name, :name_attribute => true
attribute :enabled, :default => false
attribute :running, :default => false
attribute :variables, :kind_of => Hash
attribute :supports, :default => { :restart => true, :status => true }
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved 30
action :start do
unless @bp.running
execute "/usr/bin/bluepill start #{new_resource.service_name}"
end
end
Tuesday, November 2, 2010
Copyright © 2010 Opscode, Inc - All Rights Reserved 31
action :start do
unless @bp.running
execute "/usr/bin/bluepill start #{new_resource.service_name}"
end
end
Chef Resources! New resource parameters
Tuesday, November 2, 2010
You can use chef resources inside LWRPs, too.
The goal: Replace definitions with LWRPs
Copyright © 2010 Opscode, Inc - All Rights Reserved
LWRPs in Opscode Cookbooks
32
aws_ebs_volume
aws_elastic_ip
bluepill_service
daemontools_service
dynect_rr
mysql_database
pacman_aur
pacman_group
samba_user
Tuesday, November 2, 2010
built from the name of the cookbook and the ruby file in the resources/providers directory.
these are the aws, bluepill, daemontools, dynect, mysql, pacman and samba cookbooks
respectively.

Contenu connexe

Tendances

Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and EnhancementsGagan Agrawal
 
Asm disk group migration from
Asm disk group migration from Asm disk group migration from
Asm disk group migration from Anar Godjaev
 
Postgresql 12 streaming replication hol
Postgresql 12 streaming replication holPostgresql 12 streaming replication hol
Postgresql 12 streaming replication holVijay Kumar N
 
Open-E DSS V7 Asynchronous Data Replication over a LAN
Open-E DSS V7 Asynchronous Data Replication over a LANOpen-E DSS V7 Asynchronous Data Replication over a LAN
Open-E DSS V7 Asynchronous Data Replication over a LANopen-e
 
Session 04 -Pig Continued
Session 04 -Pig ContinuedSession 04 -Pig Continued
Session 04 -Pig ContinuedAnandMHadoop
 
Rman cloning when both directory and db name are same.
Rman cloning when both directory and db name are same.Rman cloning when both directory and db name are same.
Rman cloning when both directory and db name are same.subhani shaik
 
Rman cloning guide
Rman cloning guideRman cloning guide
Rman cloning guideAmit87_dba
 
20141111 파이썬으로 Hadoop MR프로그래밍
20141111 파이썬으로 Hadoop MR프로그래밍20141111 파이썬으로 Hadoop MR프로그래밍
20141111 파이썬으로 Hadoop MR프로그래밍Tae Young Lee
 
Open-E DSS V7 Synchronous Volume Replication over a LAN
Open-E DSS V7 Synchronous Volume Replication over a LANOpen-E DSS V7 Synchronous Volume Replication over a LAN
Open-E DSS V7 Synchronous Volume Replication over a LANopen-e
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係Kiwamu Okabe
 
Open-E DSS V7 Asynchronous Data Replication within a System
Open-E DSS V7 Asynchronous Data Replication within a SystemOpen-E DSS V7 Asynchronous Data Replication within a System
Open-E DSS V7 Asynchronous Data Replication within a Systemopen-e
 
Session 04 pig - slides
Session 04   pig - slidesSession 04   pig - slides
Session 04 pig - slidesAnandMHadoop
 
Building and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosBuilding and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosJoe Stein
 

Tendances (19)

Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and Enhancements
 
Asm disk group migration from
Asm disk group migration from Asm disk group migration from
Asm disk group migration from
 
Postgresql 12 streaming replication hol
Postgresql 12 streaming replication holPostgresql 12 streaming replication hol
Postgresql 12 streaming replication hol
 
Open-E DSS V7 Asynchronous Data Replication over a LAN
Open-E DSS V7 Asynchronous Data Replication over a LANOpen-E DSS V7 Asynchronous Data Replication over a LAN
Open-E DSS V7 Asynchronous Data Replication over a LAN
 
Session 04 -Pig Continued
Session 04 -Pig ContinuedSession 04 -Pig Continued
Session 04 -Pig Continued
 
Build Automation 101
Build Automation 101Build Automation 101
Build Automation 101
 
Rman cloning when both directory and db name are same.
Rman cloning when both directory and db name are same.Rman cloning when both directory and db name are same.
Rman cloning when both directory and db name are same.
 
Rman cloning guide
Rman cloning guideRman cloning guide
Rman cloning guide
 
20141111 파이썬으로 Hadoop MR프로그래밍
20141111 파이썬으로 Hadoop MR프로그래밍20141111 파이썬으로 Hadoop MR프로그래밍
20141111 파이썬으로 Hadoop MR프로그래밍
 
Open-E DSS V7 Synchronous Volume Replication over a LAN
Open-E DSS V7 Synchronous Volume Replication over a LANOpen-E DSS V7 Synchronous Volume Replication over a LAN
Open-E DSS V7 Synchronous Volume Replication over a LAN
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
 
Open-E DSS V7 Asynchronous Data Replication within a System
Open-E DSS V7 Asynchronous Data Replication within a SystemOpen-E DSS V7 Asynchronous Data Replication within a System
Open-E DSS V7 Asynchronous Data Replication within a System
 
Linux class 10 15 oct 2021-6
Linux class 10   15 oct 2021-6Linux class 10   15 oct 2021-6
Linux class 10 15 oct 2021-6
 
Session 04 pig - slides
Session 04   pig - slidesSession 04   pig - slides
Session 04 pig - slides
 
361 Rac
361 Rac361 Rac
361 Rac
 
Linux class 9 15 oct 2021-5
Linux class 9   15 oct 2021-5Linux class 9   15 oct 2021-5
Linux class 9 15 oct 2021-5
 
03 pig intro
03 pig intro03 pig intro
03 pig intro
 
341 Rac
341 Rac341 Rac
341 Rac
 
Building and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosBuilding and Deploying Application to Apache Mesos
Building and Deploying Application to Apache Mesos
 

Similaire à Understanding lwrp development

Automated infrastructure is on the menu
Automated infrastructure is on the menuAutomated infrastructure is on the menu
Automated infrastructure is on the menujtimberman
 
Puppet slides for intelligrape
Puppet slides for intelligrapePuppet slides for intelligrape
Puppet slides for intelligrapeSharad Aggarwal
 
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTPMykhailo Kolesnyk
 
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014Puppet
 
TYPO3 Flow 2.0 (International PHP Conference 2013)
TYPO3 Flow 2.0 (International PHP Conference 2013)TYPO3 Flow 2.0 (International PHP Conference 2013)
TYPO3 Flow 2.0 (International PHP Conference 2013)Robert Lemke
 
Pinterest like site using REST and Bottle
Pinterest like site using REST and Bottle Pinterest like site using REST and Bottle
Pinterest like site using REST and Bottle Gaurav Bhardwaj
 
Serve Meals, Not Ingredients - ChefConf 2015
Serve Meals, Not Ingredients - ChefConf 2015Serve Meals, Not Ingredients - ChefConf 2015
Serve Meals, Not Ingredients - ChefConf 2015Chef
 
C++ PSM for DDS: Revised Submission
C++ PSM for DDS: Revised SubmissionC++ PSM for DDS: Revised Submission
C++ PSM for DDS: Revised SubmissionRick Warren
 
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on OpenstackLinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on OpenstackOpenShift Origin
 
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in UbuntuHow To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in UbuntuWirabumi Software
 
Exploring Oracle Database 12c Multitenant best practices for your Cloud
Exploring Oracle Database 12c Multitenant best practices for your CloudExploring Oracle Database 12c Multitenant best practices for your Cloud
Exploring Oracle Database 12c Multitenant best practices for your Clouddyahalom
 
Pandora FMS: Sun One webserver
Pandora FMS: Sun One webserverPandora FMS: Sun One webserver
Pandora FMS: Sun One webserverPandora FMS
 
DevOps Hackathon: Session 3 - Test Driven Infrastructure
DevOps Hackathon: Session 3 - Test Driven InfrastructureDevOps Hackathon: Session 3 - Test Driven Infrastructure
DevOps Hackathon: Session 3 - Test Driven InfrastructureAntons Kranga
 
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet LabsThe Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet LabsPuppet
 
Chef in the cloud [dbccg]
Chef in the cloud [dbccg]Chef in the cloud [dbccg]
Chef in the cloud [dbccg]jtimberman
 
Embedding a Jupyter Notebook - SORSE.pdf
Embedding a Jupyter Notebook - SORSE.pdfEmbedding a Jupyter Notebook - SORSE.pdf
Embedding a Jupyter Notebook - SORSE.pdfflorinpico
 

Similaire à Understanding lwrp development (20)

Puppet_training
Puppet_trainingPuppet_training
Puppet_training
 
Day02 a pi.
Day02   a pi.Day02   a pi.
Day02 a pi.
 
Automated infrastructure is on the menu
Automated infrastructure is on the menuAutomated infrastructure is on the menu
Automated infrastructure is on the menu
 
Puppet slides for intelligrape
Puppet slides for intelligrapePuppet slides for intelligrape
Puppet slides for intelligrape
 
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTP
 
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
 
TYPO3 Flow 2.0 (International PHP Conference 2013)
TYPO3 Flow 2.0 (International PHP Conference 2013)TYPO3 Flow 2.0 (International PHP Conference 2013)
TYPO3 Flow 2.0 (International PHP Conference 2013)
 
Pinterest like site using REST and Bottle
Pinterest like site using REST and Bottle Pinterest like site using REST and Bottle
Pinterest like site using REST and Bottle
 
Serve Meals, Not Ingredients - ChefConf 2015
Serve Meals, Not Ingredients - ChefConf 2015Serve Meals, Not Ingredients - ChefConf 2015
Serve Meals, Not Ingredients - ChefConf 2015
 
C++ PSM for DDS: Revised Submission
C++ PSM for DDS: Revised SubmissionC++ PSM for DDS: Revised Submission
C++ PSM for DDS: Revised Submission
 
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on OpenstackLinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
 
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in UbuntuHow To Install Openbravo ERP 2.50 MP43 in Ubuntu
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
 
Exploring Oracle Database 12c Multitenant best practices for your Cloud
Exploring Oracle Database 12c Multitenant best practices for your CloudExploring Oracle Database 12c Multitenant best practices for your Cloud
Exploring Oracle Database 12c Multitenant best practices for your Cloud
 
Cqrs api v2
Cqrs api v2Cqrs api v2
Cqrs api v2
 
Pandora FMS: Sun One webserver
Pandora FMS: Sun One webserverPandora FMS: Sun One webserver
Pandora FMS: Sun One webserver
 
DevOps Hackathon: Session 3 - Test Driven Infrastructure
DevOps Hackathon: Session 3 - Test Driven InfrastructureDevOps Hackathon: Session 3 - Test Driven Infrastructure
DevOps Hackathon: Session 3 - Test Driven Infrastructure
 
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet LabsThe Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
 
Chef in the cloud [dbccg]
Chef in the cloud [dbccg]Chef in the cloud [dbccg]
Chef in the cloud [dbccg]
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Embedding a Jupyter Notebook - SORSE.pdf
Embedding a Jupyter Notebook - SORSE.pdfEmbedding a Jupyter Notebook - SORSE.pdf
Embedding a Jupyter Notebook - SORSE.pdf
 

Plus de jtimberman

Socal piggies-app-deploy
Socal piggies-app-deploySocal piggies-app-deploy
Socal piggies-app-deployjtimberman
 
Oscon2011 tutorial
Oscon2011 tutorialOscon2011 tutorial
Oscon2011 tutorialjtimberman
 
Agile services-dev opsdays
Agile services-dev opsdaysAgile services-dev opsdays
Agile services-dev opsdaysjtimberman
 
Velocity2011 chef-workshop
Velocity2011 chef-workshopVelocity2011 chef-workshop
Velocity2011 chef-workshopjtimberman
 
Cooking security sans@night
Cooking security sans@nightCooking security sans@night
Cooking security sans@nightjtimberman
 
Mwrc2011 cookbook design patterns
Mwrc2011 cookbook design patternsMwrc2011 cookbook design patterns
Mwrc2011 cookbook design patternsjtimberman
 
tmux lightning talk mwrc
tmux lightning talk mwrctmux lightning talk mwrc
tmux lightning talk mwrcjtimberman
 
Fosdem chef-101-app-deploy
Fosdem chef-101-app-deployFosdem chef-101-app-deploy
Fosdem chef-101-app-deployjtimberman
 
Data driven app deploys with chef frontdev
Data driven app deploys with chef frontdevData driven app deploys with chef frontdev
Data driven app deploys with chef frontdevjtimberman
 
Derailed chef update-oct2010
Derailed chef update-oct2010Derailed chef update-oct2010
Derailed chef update-oct2010jtimberman
 

Plus de jtimberman (10)

Socal piggies-app-deploy
Socal piggies-app-deploySocal piggies-app-deploy
Socal piggies-app-deploy
 
Oscon2011 tutorial
Oscon2011 tutorialOscon2011 tutorial
Oscon2011 tutorial
 
Agile services-dev opsdays
Agile services-dev opsdaysAgile services-dev opsdays
Agile services-dev opsdays
 
Velocity2011 chef-workshop
Velocity2011 chef-workshopVelocity2011 chef-workshop
Velocity2011 chef-workshop
 
Cooking security sans@night
Cooking security sans@nightCooking security sans@night
Cooking security sans@night
 
Mwrc2011 cookbook design patterns
Mwrc2011 cookbook design patternsMwrc2011 cookbook design patterns
Mwrc2011 cookbook design patterns
 
tmux lightning talk mwrc
tmux lightning talk mwrctmux lightning talk mwrc
tmux lightning talk mwrc
 
Fosdem chef-101-app-deploy
Fosdem chef-101-app-deployFosdem chef-101-app-deploy
Fosdem chef-101-app-deploy
 
Data driven app deploys with chef frontdev
Data driven app deploys with chef frontdevData driven app deploys with chef frontdev
Data driven app deploys with chef frontdev
 
Derailed chef update-oct2010
Derailed chef update-oct2010Derailed chef update-oct2010
Derailed chef update-oct2010
 

Understanding lwrp development

  • 1. Copyright © 2010 Opscode, Inc - All Rights Reserved Speaker: ‣ joshua@opscode.com ‣ @jtimberman ‣ www.opscode.com Joshua Timberman Technical Evangelist 1 Understanding LWRP Tuesday, November 2, 2010
  • 2. Copyright © 2010 Opscode, Inc - All Rights Reserved Chef manages Resources on Nodes 2 cookbook_file template service package deploy git http_request link ruby_block log bash execute remote_file user Tuesday, November 2, 2010 Resources are an abstraction we feed data into. When you write recipes in Chef, you create resources of things you want to configure. You describe the state of some part of the system. We’re going to talk about how this works in depth.
  • 3. Copyright © 2010 Opscode, Inc - All Rights Reserved 3 gem_package "bluepill" Tuesday, November 2, 2010 A very simple resource, simply tells Chef to install the bluepill package as a RubyGem.
  • 4. Copyright © 2010 Opscode, Inc - All Rights Reserved 4 package "bluepill" do provider Chef::Provider::Package::Rubygems end package "bluepill" do provider gem_package end Tuesday, November 2, 2010 Likewise, these are both the equivalent of the previous gem_package, we’re just specifically telling the package resource to use a particular provider. We’ll see why this matters later.
  • 5. Copyright © 2010 Opscode, Inc - All Rights Reserved 5 template "/etc/bluepill/dnscache.pill" do source "dnscache.pill.erb" mode 0644 end Tuesday, November 2, 2010 Another somewhat simple resource, specify a file to be written somewhere as a template.
  • 6. Copyright © 2010 Opscode, Inc - All Rights Reserved 6 bluepill_service "dnscache" do action [:enable,:load,:start] end Tuesday, November 2, 2010 Here’s a custom resource that specifies actions to run. We’ll talk in a bit how this works.
  • 7. Copyright © 2010 Opscode, Inc - All Rights Reserved Resources take action through Providers 7 Tuesday, November 2, 2010 The abstraction over the commands or API calls that will configure the resource to be in the state you have defined.
  • 8. Copyright © 2010 Opscode, Inc - All Rights Reserved 8 service "dnscache" do provider bluepill_service action [:enable, :load, :start] end Tuesday, November 2, 2010
  • 9. Copyright © 2010 Opscode, Inc - All Rights Reserved 9 Chef’s Recipe DSL creates resources Tuesday, November 2, 2010
  • 10. Copyright © 2010 Opscode, Inc - All Rights Reserved Recipes evaluated in two phases 10 Compile Phase Execution Phase Tuesday, November 2, 2010 Going to leave out some of the details. The rest is in the code. :)
  • 11. Copyright © 2010 Opscode, Inc - All Rights Reserved Compile Phase 11 Tuesday, November 2, 2010
  • 12. Copyright © 2010 Opscode, Inc - All Rights Reserved Resources added to ResourceCollection 12 Tuesday, November 2, 2010 When recipes are processed for Resources during the compile phase, they are added to the resource collection. These can include..
  • 13. Copyright © 2010 Opscode, Inc - All Rights Reserved Chef::ResourceCollection 13 Resources and definitions Hash of indexed resources Definitions are replaced Tuesday, November 2, 2010 Chef “recognizes” resources and definitions through method_missing. These are created as a hash of numerically indexed resources. Definitions are replaced entirely by the resources they contain.
  • 14. Copyright © 2010 Opscode, Inc - All Rights Reserved 14 @resources_by_name={"file[/tmp/something]"=>0} Tuesday, November 2, 2010 essential what the Resource Collection looks like.
  • 15. Copyright © 2010 Opscode, Inc - All Rights Reserved 15 Chef::RunContext Loads cookbook components ‣ Libraries ‣ Providers ‣ Resources ‣ Attributes ‣ Definitions ‣ Recipes Tuesday, November 2, 2010 The order is important because of where you can put things, and when things are available.
  • 16. Copyright © 2010 Opscode, Inc - All Rights Reserved Execution Phase 16 Tuesday, November 2, 2010 Once everything is loaded up and the recipes have been parsed for the resources they contain, the runner starts the execution phase.
  • 17. Copyright © 2010 Opscode, Inc - All Rights Reserved 17 Chef::Runner converges the node Tuesday, November 2, 2010 runs the provider actions specified by the resource
  • 18. Copyright © 2010 Opscode, Inc - All Rights Reserved Chef::Resource calls run_action 18 Tuesday, November 2, 2010 run_action calls the method for that particular action in the provider
  • 19. Copyright © 2010 Opscode, Inc - All Rights Reserved Provider is chosen by Chef::Platform 19 Tuesday, November 2, 2010 This is how platform specific providers are chosen like apt/yum, useradd, etc. It also contains logic to get the provider for the resource..
  • 20. Copyright © 2010 Opscode, Inc - All Rights Reserved Chef::Platform finds the provider 20 Specifically named parameter Platform assigned providers Matching the resource name Tuesday, November 2, 2010 Specifically named providers are like we saw earlier where the resource itself had the provider parameter. Platform assigned are the ones like apt/yum for packages. Matching the resource name is how LWRPs get chosen.
  • 21. Copyright © 2010 Opscode, Inc - All Rights Reserved Cookbook LWRP 21 Tuesday, November 2, 2010 LWRPs, or resources and providers written in this special DSL, reside in cookbooks.
  • 22. Copyright © 2010 Opscode, Inc - All Rights Reserved Ruby DSL for writing Resources & Providers 22 Tuesday, November 2, 2010 All LWRPs are, a Ruby DSL for writing resources and providers. This DSL is designed to be easier to write and understand than the fully resource/provider code in the code included by the Chef libraries.
  • 23. Copyright © 2010 Opscode, Inc - All Rights Reserved Resources have states 23 Tuesday, November 2, 2010
  • 24. Copyright © 2010 Opscode, Inc - All Rights Reserved Resources have states 24 Resource DSL describe states Two possible states ‣ Current (@current_resource) ‣ Desired (@new_resource) Providers load_current_resource Tuesday, November 2, 2010 The resource describes the state that some aspect of the system should be in by passing parameters that configure it for that state. Current and desired are the two states that a resource can be in. Providers load_current_resource determines what state the resource is in.
  • 25. Copyright © 2010 Opscode, Inc - All Rights Reserved 25 def load_current_resource @bp = Chef::Resource::BluepillService.new(new_resource.name) @bp.service_name(new_resource.service_name) Chef::Log.debug("Checking status of service #{new_resource.service_name}") begin if run_command_with_systems_locale(:command => "#{node['bluepill']['bin']} status # {new_resource.service_name}") == 0 @bp.running(true) end rescue Chef::Exceptions::Exec @bp.running(false) nil end if ::File.exists?("#{node['bluepill']['conf_dir']}/#{new_resource.service_name}.pill") && ::File.symlink?("#{node['bluepill']['init_dir']}/#{new_resource.service_name}") @bp.enabled(true) else @bp.enabled(false) end end Tuesday, November 2, 2010 The code is not important. What is important that we’re checking for some various states on the system by running commands or code and setting “state” parameters.
  • 26. Copyright © 2010 Opscode, Inc - All Rights Reserved 26 actions :start, :stop, :enable, :disable, :load, :restart attribute :service_name, :name_attribute => true attribute :enabled, :default => false attribute :running, :default => false attribute :variables, :kind_of => Hash attribute :supports, :default => { :restart => true, :status => true } Resource DSL Tuesday, November 2, 2010 Actions correspond to ‘action’ methods in the provider. Attributes correspond to methods for the resource. These are also called parameters and are not node attributes.
  • 27. Copyright © 2010 Opscode, Inc - All Rights Reserved Provider DSL 27 action :start do unless @bp.running execute "/usr/bin/bluepill start #{new_resource.service_name}" end end Tuesday, November 2, 2010 The provider DSL at a minimum has action methods. The action methods handle doing whatever is needed to configure the resource to be in the declared state. Earlier we set @bp_running to true or false depending on the current state, here we check.
  • 28. Copyright © 2010 Opscode, Inc - All Rights Reserved 28 action :start do unless @bp.running execute "/usr/bin/bluepill start #{new_resource.service_name}" end end actions :start, :stop, :enable, :disable, :load, :restart attribute :service_name, :name_attribute => true attribute :enabled, :default => false attribute :running, :default => false attribute :variables, :kind_of => Hash attribute :supports, :default => { :restart => true, :status => true } Tuesday, November 2, 2010 Lets break down the provider’s methods
  • 29. Copyright © 2010 Opscode, Inc - All Rights Reserved 29 def load_current_resource @bp = Chef::Resource::BluepillService.new(new_resource.name) @bp.service_name(new_resource.service_name) Chef::Log.debug("Checking status of service #{new_resource.service_name}") begin if run_command_with_systems_locale(:command => "#{node['bluepill']['bin']} status # {new_resource.service_name}") == 0 @bp.running(true) end rescue Chef::Exceptions::Exec @bp.running(false) nil end if ::File.exists?("#{node['bluepill']['conf_dir']}/#{new_resource.service_name}.pill") && ::File.symlink?("#{node['bluepill']['init_dir']}/#{new_resource.service_name}") @bp.enabled(true) else @bp.enabled(false) end end actions :start, :stop, :enable, :disable, :load, :restart attribute :service_name, :name_attribute => true attribute :enabled, :default => false attribute :running, :default => false attribute :variables, :kind_of => Hash attribute :supports, :default => { :restart => true, :status => true } Tuesday, November 2, 2010
  • 30. Copyright © 2010 Opscode, Inc - All Rights Reserved 30 action :start do unless @bp.running execute "/usr/bin/bluepill start #{new_resource.service_name}" end end Tuesday, November 2, 2010
  • 31. Copyright © 2010 Opscode, Inc - All Rights Reserved 31 action :start do unless @bp.running execute "/usr/bin/bluepill start #{new_resource.service_name}" end end Chef Resources! New resource parameters Tuesday, November 2, 2010 You can use chef resources inside LWRPs, too. The goal: Replace definitions with LWRPs
  • 32. Copyright © 2010 Opscode, Inc - All Rights Reserved LWRPs in Opscode Cookbooks 32 aws_ebs_volume aws_elastic_ip bluepill_service daemontools_service dynect_rr mysql_database pacman_aur pacman_group samba_user Tuesday, November 2, 2010 built from the name of the cookbook and the ruby file in the resources/providers directory. these are the aws, bluepill, daemontools, dynect, mysql, pacman and samba cookbooks respectively.