Support multiple notifies in definitions

2.4k views Asked by At

This is a followup question from Configure providers from variables, in a generic way

I wrapped some providers into definitions and I was wondering how to handle the notifies. I succeeded in writing a cheap implementation where I can pass an array of arrays, similar to this:

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

And then in my definition I have something like this:

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

Surely there is a better way of doing this? I was hoping that I could keep the same kind of format in the recipe:

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

But when I do this, only the last notifies makes it to my definition.

1

There are 1 answers

2
Tensibai On BEST ANSWER

Sticking with definitions you already have the better way I think.

An option to achieve what you wish would be to turn your definition into a LWRP, this will create a custom resource which can takes notify attributes as any other chef resource.

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

I may be wrong on the type for the component, I let you adapt to your specific case. Written from memory, I may have forget something, there's more documentation here on LWRP.

And same as before in recipe, just that the LWRP name will be derived from cookbook name and file name, so in this case:

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