[chef] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: any plan for partial templates in Chef?


Chronological Thread 
  • From: Peter Norton < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: any plan for partial templates in Chef?
  • Date: Wed, 14 Nov 2012 17:31:59 -0500

All of this looks better than my nginx hacks to provide mini-templates in the default attributes:

attributes/default.rb:

default[:nginx][:knewton_common_nginx_client_opts] = proc { |n|
r <<EBLOCK
        client_body_buffer_size      1m;
        client_header_buffer_size    1k;
        client_max_body_size         #{n[:nginx][:client_max_body_size]};
        large_client_header_buffers  4 8k;
EBLOCK
}

recipes/my_nginx.rb:

template "ordered_include.conf" do
  path "#{node[:nginx][:dir]}/ordered_include.conf"
  source "ordered_include.conf.erb"
  owner  "myuser"
  group  "mygroup"
  mode   "0644"
  variables({
                :chef_warning_header              => node[:nginx][:chef_warning_header].call(s_node), # uses cluster, needs s_node
                :my_common_nginx_client_opts => node[:nginx][:knewton_common_nginx_client_opts].call(node
), # uses :nginx, needs node.
                :my_common_gzip_opts         => node[:nginx][:my_common_gzip_opts].call(node),
                :https_common_proxy_opts          => node[:nginx][:https_common_proxy_opts].call(node),
                :http_common_proxy_opts           => node[:nginx][:http_common_proxy_opts].call(node),
                :upstream_servers                 => upstream_servers,
                :enabled_sites_conf               => enabled_configs,
                :sites_available_dir              => node[:nginx][:sites_available],
            })
end

etc.

So how will the proposal to introduce partials make it clearer what's happening (clearer than this, anyway) to the new chef-user who just wants to download a cookbook and have it work?  And break in a clearer manner?

-Peter

On Wed, Nov 14, 2012 at 4:57 PM, Bryan Berry < " target="_blank"> > wrote:

Gosh I am embarrassed that I didn't notice kallistec's note on conf.d/ files.  I was totally consumed with the needs of enterprise java apps :p

On Nov 14, 2012 10:52 PM, "Andrea Campi" < " target="_blank"> > wrote:
On Wed, Nov 14, 2012 at 3:29 AM, Daniel DeLeo < " target="_blank"> > wrote:

About the implementation, using the lambda as the partial finder is clever, but it'd be nice if it could be done with less magic.


On Wed, Nov 14, 2012 at 8:36 AM, Bryan Berry < " target="_blank"> > wrote:
echoing Dan's comment on the lambda, why not just make  partial_resolver  a method inside Chef::Mixin::Template::ChefContext next to your render method ?   Do you need the late binding that the lambda gives you?  

Chef::Mixin::Template::ChefContext is a "clean slate" that is very careful to expose as little data as possible to the template, so unfortunately that wouldn't work.
Chef::Mixin::Template is not possible either, since by design it doesn't know anything about nodes, run contexts and so on. That has value in unit testing and futher.

So a closure is a given, I think.

What I plan to do to make this more readable is to turn the body of the lambda into a proper method of Chef::Provider::Template. We will still need to pass a proc / lambda into the options hash, but it will be less magic.



also, why do you have an if statement in the following, do you expect the partial_resolver to be pluggable or somehow converted to nil ? 

+          if @partial_resolver
+            template_location = @partial_resolver.call(partial_name)
+            eruby = Erubis::Eruby.new(IO.read(template_location))
+            output = eruby.evaluate(self)
+          else
+            "foo"
+          end

Yes, kinda. This is defining an API of sorts—it enables any caller of Chef::Mixin::Template::ChefContext to specify this partial_resolver, but they don't have to, it's up to them.

If I dropped the if and somebody invoked this "incorrectly", you would get something hard to debug.
If instead I replaced "foo" with something like:

raise "You cannot render a partial in this context"

at least you would have a clear hint of what went wrong.


But I have to say, this is pretty hypotetical: in the tree today we have exactly two uses for this mixin: Chef::Provider::Template, and specs.





Archive powered by MHonArc 2.6.16.

§