如何在Chef中扩展轻量级提供程序 [英] How to extend a lightweight provider in Chef

查看:52
本文介绍了如何在Chef中扩展轻量级提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建许多不同的Chef提供程序来部署不同类型的应用程序。 Chef的扩展轻量级提供程序的文档建议有可能,但实际上并未说明该怎么做。该页面表明也许需要调用 mixin ,但是我不知道代码在 / libraries 或如何将代码实际包含在 / providers 下。

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 but doesn't actually say what to do. That page suggests that perhaps a call to mixin is needed, but I don't know what structure my code should have in the file under /libraries or how to actually include that code in something under /providers.

这里是我想做的事的例子。

Here are the examples of what I want to do.

在我的基类中, / libraries

repository "http://my.svn.server/#{deployment[:project]}/branches/#{node[:chef_environment]}/"
user "deploy"
scm_provider Chef::Provider::Subversion
svn_username "svn_user"
svn_password "password"

在我的Torquebox Rails应用程序部署提供商中:

In my provider for Torquebox Rails app deployments:

deploy_revision "/my/deployment/directory/#{deployment[:project]}" do
  # Magically mixin the code from libraries
  environment "RAILS_ENV" => node[:chef_environment]
  restart_command "rake torquebox:deploy"
end

然后当然还有其他类型的提供程序,用于不同类型的应用程序。

And then of course other types of providers for different types of applications.

有人可以为此指出正确的方向吗?

Can anyone point me in the right direction on this? Is there documentation somewhere I'm missing?

推荐答案

厨师会在运行时自动将LWRP DSL转换为成熟的Ruby类。这由食谱名称决定,再由文件名称决定(这与创建实际资源名称的方式相同)。

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).

因此,如果您有一个名为 bacon 的菜谱和 bacon / resources / eat.rb 中的LWRP,相关的LWRP为 bacon_eat 。关联的是驼峰式的常量化版本- Chef :: Resource :: BaconEat Chef: :Provider :: BaconEat

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.

此模式有一个例外-默认。 默认在Chef地域是特殊的,因为它带有前缀。因此,如果您有一本名为 bacon 的食谱,并且在 bacon / resources / default.rb 中有一个LWRP,则关联的LWRP为培根(不是 bacon_default )。关联的是驼峰式的常量化版本- Chef :: Resource :: Bacon Chef: :Provider :: Bacon (不是 BaconDefault)。

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.

好的,那为什么要讲故事呢?为了扩展LWRP,您想继承LWRP的类(Rubyism)。因此,在您的 libraries / 目录中,您想扩展自定义资源:

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

在您的示例中:

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

然后在食谱中使用 my_deploy_revision

这篇关于如何在Chef中扩展轻量级提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆