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

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.