LWRP DEVELOPMENT            Speaker:          Brian Bianco  Email: brian.bianco@gmail.com     Twitter: @brianwbianco  http...
RESOURCES• Resources represent a piece of system state• We supply resources with data• Ordering of resources is important•...
PROVIDERS•Providers are the underlying implementation of a resource•They are loaded by Chef::RunContext before Resources• ...
WHAT ARE LWRPS• A DSL provided to make writing resources and providers easier• Useful for creating your own resources and ...
FILE STRUCTURE                 Filename paths are relative to:                     /cookbooks/lwrpdemo      Filename      ...
LIGHTWEIGHT RESOURCESThe same directory resource if it were using the resource DSL
• Resources will look for providers by the same name (you can specify one with the provider resource attribute)• Actions d...
LIGHTWEIGHT PROVIDERSThe same provider if it were using the Provider DSL
load_current_resource•This method is responsible for loading the current state of  the resource into @current_resource• @c...
ANOTHER PROVIDER EXAMPLE         Python VirtualEnv Create Action  Checking to see if the virtual env exists already
NESTED RESOURCES• Resources can be nested inside of light weight providers• If those resources call updated_by_last action...
TIPS • Check out others cookbooks, especially the opscode teams• The default recipe should just make the resource availabl...
CONTRIBUTING BACK• http://wiki.opscode.com/display/chef/How+to+Contribute • Create a jira account • Sign the CLA • Submit ...
Lwrp presentation
Lwrp presentation
Lwrp presentation
Upcoming SlideShare
Loading in …5
×

Lwrp presentation

4,674 views
4,397 views

Published on

Published in: Technology, Business
2 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total views
4,674
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
45
Comments
2
Likes
5
Embeds 0
No embeds

No notes for slide
  • Lets first talk about resources and providers in general (and their ‘non light weight’ implementations)\nResources are a great way to abstract repeatable patterns\nShorcuts I may say => ivar = instance variable, DSL = domain specific language, \n
  • RunContext order -> Libraries, Providers, Resources, Attributes, Definitions, Recipes\nArgument after resource name is called the “Name attribute”\n\n\n
  • chef-0.10.10/lib/chef/resource/directory.rb\nRegular ruby class\nresource attributes are methods\nPath is the “name” attribute\n
  • Chef::Platform contains a HASH of default providers based on operating systems\n\n
  • All resource actions are named action_<action name>\nload_current_resource is responsible for determining the current state of the resource (where we are)\nnew resource is the state we want the resource in (where we want to be)\nRegular ruby class (This one inherits from Chef::Provider::File)\n\n
  • \n
  • \n
  • name_attribute is populated by the name given when the resource is called\ndefault_action is chef 0.10.10 and above (Will show you later how to do 0.10.8 and below)\nall resources get the :nothing action\nTwo parameters for “attribute” Name of the attribute and the validation parameters\n\n\n
  • Resource parameters (attributes) are not to be confused with node attributes!\nprovider Chef::Provider::Cookbookname::Providername or provider :my_other_provider\nList of validation parameters\n\n\n\n
  • You can use other chef resources inside of your providers\nYou can access data passed into the resource via new_resource.<attribute name>\nAll resource actions are implemented here!\nOnly one method for this DSL -> action\n\n
  • \n
  • Could also use: \nmyvar = Chef::ShellOut.new(“Some Command”)\nmyvar.run_command (runs the command)\nmyvar.stdout (the output)\n
  • \n
  • the run_context is only needed for the case of wanting to use methods like #user, #mount, #cookbook_file\nthe sub_run_context is a subset of resource executed in the provider (since you initialize its ResourceCollection)\nany? Passes the updated function to the set of resources, returns functions that respond with anything other than (nil,false)\nrun_context loads and tracks the context of a chef run\nconverge: iterates over the resource_collection in the run_context calling run_action for each resource\n
  • \n
  • \n
  • Lwrp presentation

    1. 1. LWRP DEVELOPMENT Speaker: Brian Bianco Email: brian.bianco@gmail.com Twitter: @brianwbianco http://github.com/brianbianco
    2. 2. RESOURCES• Resources represent a piece of system state• We supply resources with data• Ordering of resources is important• Chef ’s Recipe DSL creates resources• During the compile phase these resources are added to the resource collection Example of a resource in a recipe:
    3. 3. PROVIDERS•Providers are the underlying implementation of a resource•They are loaded by Chef::RunContext before Resources• Node attributes are NOT available to them (they can however be passed in via resource attributes)• Normal providers are selected by Chef::Platform
    4. 4. WHAT ARE LWRPS• A DSL provided to make writing resources and providers easier• Useful for creating your own resources and providers without having to implement the actual ruby classes• LWRP has two components: Resource & Provider• Great way to abstract some repeated pattern
    5. 5. FILE STRUCTURE Filename paths are relative to: /cookbooks/lwrpdemo Filename Resource / Provider Generated Class Name resources/default.rb lwrpdemo Chef::Resource::Lwrpdemo providers/default.rb lwrpdemo Chef::Provider::Lwrpdemoresources/directory.rb lwrpdemo_directory Chef::Resource::LwrpdemoDirectoryproviders/directory.rb lwrpdemo_directory Chef::Provider::LwrpdemoDirectory
    6. 6. LIGHTWEIGHT RESOURCESThe same directory resource if it were using the resource DSL
    7. 7. • Resources will look for providers by the same name (you can specify one with the provider resource attribute)• Actions defined in the resource correspond to action methods in the provider• Attributes correspond to methods for the resource (These are called parameters)• Validation parameters: :default :kind_of :required :regex :equal_to :name_attribute :callbacks :respond_to
    8. 8. LIGHTWEIGHT PROVIDERSThe same provider if it were using the Provider DSL
    9. 9. load_current_resource•This method is responsible for loading the current state of the resource into @current_resource• @current_resource = Chef::Resource::LwrpdemoDirectory.new(new_resource.name) updated_by_last_action •Call this method when the provider changes something • When called with true, will fire off notifications
    10. 10. ANOTHER PROVIDER EXAMPLE Python VirtualEnv Create Action Checking to see if the virtual env exists already
    11. 11. NESTED RESOURCES• Resources can be nested inside of light weight providers• If those resources call updated_by_last action your provider will not inherit that• Luckily there is a pattern you can use to solve this!• You can use the updated? method on any resource in the Chef::ResourceCollection
    12. 12. TIPS • Check out others cookbooks, especially the opscode teams• The default recipe should just make the resource available and setup any necessary files and configurations• The #chef channel on irc.freenode.org is full of helpful people • The mailing list is a great place to ask questions http://lists.opscode.com
    13. 13. CONTRIBUTING BACK• http://wiki.opscode.com/display/chef/How+to+Contribute • Create a jira account • Sign the CLA • Submit pull requests• Share you cookbooks on Github and community.opscode.com• Even the smallest bug fixes help

    ×