bartavelle / language-puppet

A library to work with Puppet manifests, test them and eventually replace everything ruby.
BSD 3-Clause "New" or "Revised" License
51 stars 8 forks source link

Are ext_funcs looked up in Erb templates ? #127

Closed PierreR closed 9 years ago

PierreR commented 9 years ago

In Puppet, functions defined in modulename/lib/puppet/parser/functions/ can be used inside erb templates by prefixing them by function_.

To support the apache puppetlabs module, I add bool2httpd into our haskell module Puppetlabs.

It still does not work and I am now under the impression that prefExtfuncs (including the one defined in lua files) won't be lookup in erb templates. Is this correct ?

If so, would it be hard to support this ?

bartavelle commented 9 years ago

This is correct, and I think this could be possible to support. I'll think about it.

bartavelle commented 9 years ago

Just pushed something ... does it help ?

bartavelle commented 9 years ago

It will return bogus answer for some functions though (like defined) because we don't have access to the puppet state from withing the templates.

PierreR commented 9 years ago

It does not work. It actually does not go into def method_missing(sname,*args,&block) when the error occurs. If I add a puts statement there, nothing with the prefix function_ gets printed (it just prints scope or file)

In case it helps here is the error:

ERROR: (puppet/foreman_testing) undefined method `function_bool2httpd' for #<Scope:0x007fd7a800bd38>
(erb):3:in `get_binding'
/usr/lib/ruby/2.2.0/erb.rb:863:in `eval'
/usr/lib/ruby/2.2.0/erb.rb:863:in `result'
bartavelle commented 9 years ago

Can you tell me how to reproduce your setup ? I suppose I need the apache module and your implementation of bool2httpd ?

PierreR commented 9 years ago

I have just committed the implementation of the function.

I was wondering if the following change is not required too ? (it does not fix the problem though)

--- a/Puppet/Daemon.hs
+++ b/Puppet/Daemon.hs
@@ -77,12 +77,12 @@ initDaemon prefs = do
     pfilecache    <- newFileCache
     let getStatements = parseFunction prefs pfilecache parserStats
     intr          <- startRubyInterpreter
-    getTemplate   <- initTemplateDaemon intr prefs templateStats
     hquery        <- case prefs ^. hieraPath of
                          Just p  -> either error id <$> startHiera p
                          Nothing -> return dummyHiera
     luacontainer <- initLuaMaster (T.pack (prefs ^. puppetPaths.modulesPath))
     let myprefs = prefs & prefExtFuncs %~ HM.union luacontainer
+    getTemplate   <- initTemplateDaemon intr myprefs templateStats
     return (DaemonMethods (gCatalog myprefs getStatements getTemplate catalogStats hquery) parserStats catalogStats templateStats)
bartavelle commented 9 years ago

Oh perhaps it is !

bartavelle commented 9 years ago

I tried something significantly more complicated here. I hope this doesn't break anything ...

PierreR commented 9 years ago

I am going further with it trying to test the new code as much as I can.

So far so good except that I have probably discovered another little bug.

puppetresources cannot resolve 1 == -1 or anything with a minus such as x == -y

Here is the code and the error: https://github.com/puppetlabs/puppetlabs-apache/blob/master/manifests/default_mods.pp#L16

ERROR: (puppet/foreman_testing) Don't know how to resolve this expression:
- (1) at # "./modules/apache/manifests/default_mods.pp" (line 16, column 9)
bartavelle commented 9 years ago

For all these years, this case was not handled ... this should be the case now.

PierreR commented 9 years ago

Thanks for the quick fix.

There is also a kind of a "debug" statement left. I am not sure if it is on purpose.

> puppetresources -p . -o pgserver_testing --hiera ./tests/hiera.yaml --pdbfile ./tests/facts.yaml -n postgresql94
("versioncmp",[PString "9.4",PString "9.1"],Right (PString "1"))
package {
  postgresql94: # "./modules/postgresql/manifests/client.pp" ( .....
bartavelle commented 9 years ago

woops ...

bartavelle commented 9 years ago

Fixed.

PierreR commented 9 years ago

Looks good. Thanks for the fix !