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

to_yaml in erb file is an unknown function #113

Closed PierreR closed 9 years ago

PierreR commented 9 years ago

Hi,

Would it be possible to implement to_yaml in erb file ?

Cheers,

bartavelle commented 9 years ago

There are two ways to do it : either write it in ruby, or in Haskell. If the ruby yaml library is more or less guaranteed to exist on the host running language-puppet, I believe this would be the right thing to do. Do you know if it's ruby's "base" ?

PierreR commented 9 years ago

I don't know ruby that much but my understanding is that it is in ruby "base":

http://ruby-doc.org/stdlib-2.0.0/libdoc/yaml/rdoc/YAML.html

Without installing more than ruby:

arch-docker ~ > irb
irb(main):001:0> to_yaml
NameError: undefined local variable or method `to_yaml' for main:Object
    from (irb):1
    from /usr/bin/irb:11:in `<main>'
irb(main):002:0> require 'yaml'
=> true
irb(main):003:0> to_yaml
=> "--- !ruby/object {}\n"
irb(main):004:0> 
bartavelle commented 9 years ago

That was easy : 0ac8b70c945b8590fe5747bd711b2a2fd37e961e

PierreR commented 9 years ago

Thanks !

PierreR commented 9 years ago

Trying it, I actually get this error

 ./check_role puppetserver_testing 

ERROR: (puppetserver_testing) uninitialized constant Encoding::ASCII_8BIT
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:435:in `binary?'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:306:in `visit_String'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:152:in `accept'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:376:in `block in visit_Hash'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:375:in `each'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:375:in `visit_Hash'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:152:in `accept'
/usr/lib/ruby/2.2.0/psych/visitors/yaml_tree.rb:116:in `push'
/usr/lib/ruby/2.2.0/psych.rb:409:in `dump'
/usr/lib/ruby/2.2.0/psych/core_ext.rb:14:in `psych_to_yaml'
(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'
/usr/local/bin/hrubyerb.rb:107:in `runFromContent'
/usr/local/bin/hrubyerb.rb:102:in `runFromFile'
 in ./modules/puppetdb/templates/routes.yaml.erb at # "./modules/puppetdb/manifests/master/routes.pp" (line 38, column 1

Any idea ?

bartavelle commented 9 years ago

No idea :) Perhaps try to juggle with your locales to see if this changes something ?

PierreR commented 9 years ago

I have a failry standard locale setting:

@arch-docker ~ > locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

If I tried:

irb(main):018:0> puts [['hello', 'world']].to_yaml
---
- - hello
  - world
=> nil

but if I do it in a elb file

<%= [['hello', 'world']].to_yaml %>

Then I have got the Encoding error. Is it working on your box ?

I will give a shot on another distro to be sure it not related.

bartavelle commented 9 years ago

Yeah I tried this before pushing :

$lala = [1,2,3,4,5]
file { '/tmp/x':
  content => inline_template('lalalal <%= @lala.to_yaml %>');
}
PierreR commented 9 years ago

Well this works for me too but this does not:

$lala = ['hello', 'world']
file { '/tmp/x':
  content => inline_template('lalalal <%= @lala.to_yaml %>');
}
bartavelle commented 9 years ago

I just pushed a "fix" (d25ff3d). This seems to work, but I am pretty sure this breaks things. Encoding::* should be available all the time, as it's built in into ruby ...

PierreR commented 9 years ago

Yep it does fix the problem but what a terrible fix indeed ;-)

Not sure if you want to close the issue, do you ?

bartavelle commented 9 years ago

I got it, it's a bug in the ruby interpreter initialization. I just uploaded a new hruby, I am checking language-puppet.

bartavelle commented 9 years ago

This time it should work without the terrible fix !

PierreR commented 9 years ago

Works nicely. Thx !