SlideShare a Scribd company logo
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.

More Related Content

PDF
Postgres 12 Cluster Database operations.
Vijay Kumar N
 
PDF
Hadoop spark performance comparison
arunkumar sadhasivam
 
PDF
Serve Meals, Not Ingredients (ChefConf 2015)
ThirdWaveInsights
 
PDF
Hadoop 2.0 cluster setup on ubuntu 14.04 (64 bit)
Nag Arvind Gudiseva
 
PDF
Automated Java Deployments With Rpm
Martin Jackson
 
DOCX
Moving 12c database from NON-ASM to ASM
Monowar Mukul
 
PDF
Colvin RMAN New Features
Enkitec
 
PDF
Introduction to scoop and its functions
Rupak Roy
 
Postgres 12 Cluster Database operations.
Vijay Kumar N
 
Hadoop spark performance comparison
arunkumar sadhasivam
 
Serve Meals, Not Ingredients (ChefConf 2015)
ThirdWaveInsights
 
Hadoop 2.0 cluster setup on ubuntu 14.04 (64 bit)
Nag Arvind Gudiseva
 
Automated Java Deployments With Rpm
Martin Jackson
 
Moving 12c database from NON-ASM to ASM
Monowar Mukul
 
Colvin RMAN New Features
Enkitec
 
Introduction to scoop and its functions
Rupak Roy
 

What's hot (19)

ODP
Java 7 Features and Enhancements
Gagan Agrawal
 
DOCX
Asm disk group migration from
Anar Godjaev
 
PDF
Postgresql 12 streaming replication hol
Vijay Kumar N
 
PDF
Open-E DSS V7 Asynchronous Data Replication over a LAN
open-e
 
PPTX
Session 04 -Pig Continued
AnandMHadoop
 
PDF
Build Automation 101
Martin Jackson
 
DOCX
Rman cloning when both directory and db name are same.
subhani shaik
 
DOC
Rman cloning guide
Amit87_dba
 
PDF
20141111 파이썬으로 Hadoop MR프로그래밍
Tae Young Lee
 
PDF
Open-E DSS V7 Synchronous Volume Replication over a LAN
open-e
 
PDF
HaskellとDebianの辛くて甘い関係
Kiwamu Okabe
 
PDF
Open-E DSS V7 Asynchronous Data Replication within a System
open-e
 
PDF
Linux class 10 15 oct 2021-6
Khawar Nehal [email protected]
 
PPTX
Session 04 pig - slides
AnandMHadoop
 
PDF
361 Rac
Emanuel Mateus
 
PDF
Linux class 9 15 oct 2021-5
Khawar Nehal [email protected]
 
PPTX
03 pig intro
Subhas Kumar Ghosh
 
PDF
341 Rac
Emanuel Mateus
 
PPTX
Building and Deploying Application to Apache Mesos
Joe Stein
 
Java 7 Features and Enhancements
Gagan Agrawal
 
Asm disk group migration from
Anar Godjaev
 
Postgresql 12 streaming replication hol
Vijay Kumar N
 
Open-E DSS V7 Asynchronous Data Replication over a LAN
open-e
 
Session 04 -Pig Continued
AnandMHadoop
 
Build Automation 101
Martin Jackson
 
Rman cloning when both directory and db name are same.
subhani shaik
 
Rman cloning guide
Amit87_dba
 
20141111 파이썬으로 Hadoop MR프로그래밍
Tae Young Lee
 
Open-E DSS V7 Synchronous Volume Replication over a LAN
open-e
 
HaskellとDebianの辛くて甘い関係
Kiwamu Okabe
 
Open-E DSS V7 Asynchronous Data Replication within a System
open-e
 
Linux class 10 15 oct 2021-6
Khawar Nehal [email protected]
 
Session 04 pig - slides
AnandMHadoop
 
Linux class 9 15 oct 2021-5
Khawar Nehal [email protected]
 
03 pig intro
Subhas Kumar Ghosh
 
Building and Deploying Application to Apache Mesos
Joe Stein
 
Ad

Similar to Understanding lwrp development (20)

PPTX
Puppet_training
Afroz Hussain
 
PPTX
Day02 a pi.
ABDEL RAHMAN KARIM
 
PDF
Automated infrastructure is on the menu
jtimberman
 
ODP
Puppet slides for intelligrape
Sharad Aggarwal
 
PDF
Python tools for testing web services over HTTP
Mykhailo Kolesnyk
 
PDF
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Puppet
 
PDF
TYPO3 Flow 2.0 (International PHP Conference 2013)
Robert Lemke
 
PDF
Pinterest like site using REST and Bottle
Gaurav Bhardwaj
 
PDF
Serve Meals, Not Ingredients - ChefConf 2015
Chef
 
PPTX
C++ PSM for DDS: Revised Submission
Rick Warren
 
PDF
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
OpenShift Origin
 
PDF
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
Wirabumi Software
 
PDF
Exploring Oracle Database 12c Multitenant best practices for your Cloud
dyahalom
 
PDF
Cqrs api v2
Brandon Mueller
 
PDF
Pandora FMS: Sun One webserver
Pandora FMS
 
PPTX
DevOps Hackathon: Session 3 - Test Driven Infrastructure
Antons Kranga
 
PDF
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
Puppet
 
PDF
Chef in the cloud [dbccg]
jtimberman
 
ODP
Introduction to OData
Mindfire Solutions
 
PDF
Embedding a Jupyter Notebook - SORSE.pdf
florinpico
 
Puppet_training
Afroz Hussain
 
Day02 a pi.
ABDEL RAHMAN KARIM
 
Automated infrastructure is on the menu
jtimberman
 
Puppet slides for intelligrape
Sharad Aggarwal
 
Python tools for testing web services over HTTP
Mykhailo Kolesnyk
 
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Puppet
 
TYPO3 Flow 2.0 (International PHP Conference 2013)
Robert Lemke
 
Pinterest like site using REST and Bottle
Gaurav Bhardwaj
 
Serve Meals, Not Ingredients - ChefConf 2015
Chef
 
C++ PSM for DDS: Revised Submission
Rick Warren
 
LinuxCon 2013 Steven Dake on Using Heat for autoscaling OpenShift on Openstack
OpenShift Origin
 
How To Install Openbravo ERP 2.50 MP43 in Ubuntu
Wirabumi Software
 
Exploring Oracle Database 12c Multitenant best practices for your Cloud
dyahalom
 
Cqrs api v2
Brandon Mueller
 
Pandora FMS: Sun One webserver
Pandora FMS
 
DevOps Hackathon: Session 3 - Test Driven Infrastructure
Antons Kranga
 
The Grand Puppet Sub-Systems Tour - Nicholas Fagerlund, Puppet Labs
Puppet
 
Chef in the cloud [dbccg]
jtimberman
 
Introduction to OData
Mindfire Solutions
 
Embedding a Jupyter Notebook - SORSE.pdf
florinpico
 
Ad

More from jtimberman (10)

PDF
Socal piggies-app-deploy
jtimberman
 
PDF
Oscon2011 tutorial
jtimberman
 
KEY
Agile services-dev opsdays
jtimberman
 
PDF
Velocity2011 chef-workshop
jtimberman
 
PDF
Cooking security sans@night
jtimberman
 
PDF
Mwrc2011 cookbook design patterns
jtimberman
 
PDF
tmux lightning talk mwrc
jtimberman
 
PDF
Fosdem chef-101-app-deploy
jtimberman
 
PDF
Data driven app deploys with chef frontdev
jtimberman
 
PDF
Derailed chef update-oct2010
jtimberman
 
Socal piggies-app-deploy
jtimberman
 
Oscon2011 tutorial
jtimberman
 
Agile services-dev opsdays
jtimberman
 
Velocity2011 chef-workshop
jtimberman
 
Cooking security sans@night
jtimberman
 
Mwrc2011 cookbook design patterns
jtimberman
 
tmux lightning talk mwrc
jtimberman
 
Fosdem chef-101-app-deploy
jtimberman
 
Data driven app deploys with chef frontdev
jtimberman
 
Derailed chef update-oct2010
jtimberman
 

Understanding lwrp development

  • 1. Copyright © 2010 Opscode, Inc - All Rights Reserved Speaker: ‣ [email protected] ‣ @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.