puppetlabs / ruby-hocon

A Ruby port of the Typesafe Config library.
Apache License 2.0
34 stars 30 forks source link

Hocon file includes with puppet? #119

Open krissik opened 5 years ago

krissik commented 5 years ago

The readme says that file includes are unsupported. What does that mean? I found a implementation of file include at /lib/hocon/impl/simple_includer.rb. It seems to work if I use it in a ruby script, readhocon.rb:

require 'hocon'
conf = Hocon.load("hocon-test-with-include.conf")
puts "Here's the setting: #{conf}"

hocon-test-with-include.conf:

{
    include file("hocon-include.conf")
    include file("hocon-include.json")
}

hocon-include.conf:

{
    "j": "foo",
    "k": "bar",
    "l": "lo"
}

hocon-include.json:

{
    "l": "loo",
    "m": "mar"
}

Run script

> ruby readhocon.rb
Here's the setting: {"j"=>"foo", "k"=>"bar", "l"=>"loo", "m"=>"mar"}

But when I try to use includes with puppet it is not working. Puppet complains about missing parameters which are defined in an included file:

 Error while evaluating a Function Call, Class[Profile::Haproxy]: expects a value for parameter 'cluster_name'

Can I use HOCON file includes with puppet? Are there documentation or any examples about it?

Environment

MikaelSmith commented 5 years ago

I don't see anything obvious that would cause that to fail in Puppet. Maybe it has to do with how the file name is resolved to an absolute path for include file('foo').

Can you share more context about how you're using Hocon in your Puppet code, and how that makes its way to the cluster_name parameter of Profile::Haproxy? Presumably it works when you inline the settings?

MikaelSmith commented 5 years ago

Yeah, just ran a test where the files are in a different directory. include file('foo') resolves from your current working directory, not the directory where the including config lives. That may be what they mean by "doesn't work". Hocon also just silently ignores files it can't find and returns an empty object, so these errors can be annoying to find.

MikaelSmith commented 5 years ago

I'm not really sure of a better way to do this in Hocon. I think the Java version relies on having everything in a resource path and loads relative paths from there, but Ruby doesn't have that. I think this should be filed as a bug with Puppet (or Puppet Server) if you're trying to use it with one of Puppet's configs.

If you're using a custom function that loads Hocon, I'd suggest something like

conf = Dir.chdir('/path/to/configs') do
  Hocon.load('conf')
end