【问题标题】:Support multiple notifies in definitions支持定义中的多个通知
【发布时间】:2015-08-24 10:31:00
【问题描述】:

这是Configure providers from variables, in a generic way的后续问题

我将一些提供程序包装到定义中,我想知道如何处理通知。我成功地编写了一个廉价的实现,我可以传递一个数组数组,类似于:

provider_definition name do
    component $some_component
    notifies [[:redeploy, "docker_container[some_container]", :immediately],
              [:redeploy, "docker_container[some_other_cntr]", :delayed]]
end

然后在我的定义中我有这样的东西:

params[:notifies].each do |notify_action, resource_name, notify_time|
    notifies notify_action, resource_name, notify_time
end

肯定有更好的方法吗?我希望我可以在食谱中保持同样的格式:

provider_definition name do
    component $some_component
    notifies :redeploy, "docker_container[some_container]", :immediately
    notifies :redeploy, "docker_container[some_other_cntr]", :delayed
end

但是当我这样做时,只有最后的通知才符合我的定义。

【问题讨论】:

    标签: chef-infra


    【解决方案1】:

    坚持你已经有我认为更好的定义。

    实现您希望的一个选项是将您的定义转换为 LWRP,这将创建一个自定义资源,该资源可以像任何其他厨师资源一样采用通知属性。

    docker_library/resources/my_deploy.rb

    action :deploy
    default_action :deploy
    
    attribute :component, kind_of: Hash, required: true
    

    docker_library/provider/my_deploy.rb

    action :deploy do
      params = new_resource.component
      docker_image params['name'] do
        registry params['registry']
        tag params['tag']
        action :pull
      end
    end
    

    我可能对组件的类型有误,我让你适应你的具体情况。凭记忆写的,我可能忘记了什么,LWRP上有更多文档here

    和之前的菜谱一样,只是 LWRP 名称将来自食谱名称和文件名,所以在这种情况下:

    my_component = $auth_docker
    docker_library_my_deploy my_component.name do 
      component my_component
      notifies :redeploy, "docker_container[some_container]", :immediately
      notifies :redeploy, "docker_container[some_other_cntr]", :delayed
    end
    

    【讨论】:

    • 再次感谢!太糟糕了,没有办法多次指定相同的属性。
    • 我忘记了配方示例中的通知;)
    猜你喜欢
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多