通过通用方式从变量配置提供程序 [英] Configure providers from variables, in a generic way

查看:61
本文介绍了通过通用方式从变量配置提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建配方,以通用的方式使用对象实例中的字段填充其属性?

How can I create a recipe that will populate its attributes using the fiels from an instance of an object in a generic way?

例如,请考虑以下内容配方:

As an example, consider the following recipe:

component = $auth_docker
docker_image component.name do
    registry component.registry
    tag component.tag
    action :pull
end

有50时

在Python中,我可能会实现一个看起来像这样的解决方案:

In Python, i would probably implement a solution that would look a bit like this:

docker_image = DockerImage(**$auth_docker)

或者,我会创建某种帮助函数来为我构建它:

Or, I would create some sort of helper function to build it for me:

def generate_docker_image_lwrp(attributes):
    lwrp = DockerImage()
    lwrp.registry = attributes.registry
    lwrp.tag = attributes.tag
    return lwrp

目标是减少配方维护。例如,今天早上,我想在所有提取图像的食谱上添加Chef的重试属性。我必须全部编辑-我不想要那样。我应该能够a)将属性添加到堆栈的JSON b)编辑Ruby包装器类,使其实例(即$ auth_docker)获得 retries字段,然后c)将retries属性添加到lwrp-generator。由于所有食谱都将使用相同的生成器,因此根本不需要编辑食谱。

The goal is to reduce maintenance on the recipes. For instance this morning I wanted to add Chef's "retries" attribute on all recipes that pull an image. I had to edit all of them - I don't want that. I should've been able to a) add the attribute to the stack's JSON b) edit the Ruby wrapper class so that instances of it (i.e.: $auth_docker) get the "retries" field, then c) add the retries attribute to the lwrp-generator. Since all recipes would use the same generator, recipes wouldn't need to be edited at all.

使用Chef可以做到这一点,并且通知仍然有效?

Is this possible using Chef, in a way that 'notifies' still work?

推荐答案

引用文档


定义是可在各种配方中重复使用的代码,类似于
的编译时宏。使用环绕内置厨师客户资源(文件,执行,模板,
等)的任意代码
来创建定义,方法是将这些资源声明为
,在食谱中声明。然后,在一个(或多个)
配方中使用定义,就好像它是一种资源一样。

A definition is code that is reused across recipes, similar to a compile-time macro. A definition is created using arbitrary code wrapped around built-in chef-client resources—file, execute, template, and so on—by declaring those resources into the definition as if they were declared in a recipe. A definition is then used in one (or more) recipes as if it were a resource.

尽管定义的行为就像一种资源,但有些关键区别b $ b存在。定义:

Though a definition behaves like a resource, some key differences exist. A definition:

不是菜谱的
/ definitions目录中定义的资源还是轻量级资源在
期间的资源加载之前加载厨师客户运行;这确保了该定义可用于所有可能需要的资源
可能不会通知
资源集合中的资源,因为在创建资源
集合本身之前已加载定义;但是,定义中的资源可能会通知
该定义中存在的资源自动
支持为何运行模式,这与轻量级资源不同,在整个资源和//中存在重复模式时使用定义
或需要简单的
直接方法。定义中可以包含的
资源数量没有限制:根据需要使用尽可能多的内置
Chef-client资源。

Is not a resource or a lightweight resource Is defined from within the /definitions directory of a cookbook Is loaded before resources during the chef-client run; this ensures the definition is available to all of the resources that may need it May not notify resources in the resource collection because a definition is loaded before the resource collection itself is created; however, a resource in a definition may notify a resource that exists within the same definition Automatically supports why-run mode, unlike lightweight resources Use a defintion when repeating patterns exist across resources and/or when a simple, direct approach is desired. There is no limit to the number of resources that may be included in a definition: use as many built-in chef-client resources as necessary.

即:您可以在仅用于此目的的库食谱中为此定义。

I.e: you can create a definition for this in a library cookbook used solely for this.

docker_library / defintions / default。 rb

docker_library/defintions/default.rb

define :my_docker_image, :component => nil do
  component = params[:component]
  docker_image component.name do
    registry component.registry
    tag component.tag
    action :pull
  end
end 

您的食谱中(需要依赖于元数据中的docker_library食谱) .rb):

in your recipes (need to have a depends on the docker_library cookbook in the metadata.rb):

my_component = $auth_docker
my_docker_image my_component.name do 
  component my_component
end

更完整的定义示例在 logrotate食谱

这篇关于通过通用方式从变量配置提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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