How to extend a lightweight provider in Chef

后端 未结 1 763
离开以前
离开以前 2021-01-06 02:37

I am creating a bunch of different Chef providers to deploy different types of applications. Chef\'s documentation for Extend a Lightweight Provider suggests it is possible

相关标签:
1条回答
  • 2021-01-06 03:36

    The Chef will automatically convert the LWRP DSL into a full-blown Ruby class at runtime. This is determined by the name of the cookbook followed by the name of the file (this is the same way the actual resource name is created).

    So if you have a cookbook named bacon and an LWRP in bacon/resources/eat.rb, the associated LWRP is bacon_eat. The associated class is the camel-cased, constantized version of that - Chef::Resource::BaconEat and Chef::Provider::BaconEat in this case.

    There is one exception to this pattern - default. "Default" is special in Chef land, as it doesn't get prefixed. So if you have a cookbook named bacon and an LWRP in bacon/resources/default.rb, the associated LWRP is bacon (not bacon_default). The associated class is the camel-cased, constantized version of that - Chef::Resource::Bacon and Chef::Provider::Bacon (not "BaconDefault") in this case.

    Okay, so why the backstory? In order to extend an LWRP, you want to inherit from the LWRP's class (Rubyism). So in your libraries/ directory, you want to extend your custom resource:

    class Chef
      class Resource::MyResource < Resource::Bacon # <- this
      end
    end
    

    So, in your example:

    class Chef
      class Resource::MyDeployRevision < Resource::DeployRevision
        def initialize(name, run_context = nil)
          super
    
          # This is what you'll use in the recipe DSL
          @resource_name = :my_deploy_revision
    
          # Things like default action and parameters are inherited from the parent
    
          # Set your default options here
          @repository = "http://my.svn.server/#{node['deployment']['project']}/branches/#{node.chef_environment}/"
          @user = 'deploy'
          @scm_provider = Chef::Provider::Subversion
          @svn_username = 'svn_user'
          @svn_password = 'password'
        end
      end
    end
    

    Then use my_deploy_revision in your recipes.

    0 讨论(0)
提交回复
热议问题