Hello there

I found :
http://groups.google.com/group/puppet-users/browse_thread/thread/74194dbf969067cc/3fa06673d90b151e?lnk=gst&q=replace+line#3fa06673d90b151e

which deals with the subject of replacing lines in a file, but i might
be missing things but it looks to me that the current way of doing
this should be rather different:
http://reductivelabs.com/trac/puppet/wiki/PracticalTypes
http://reductivelabs.com/trac/puppet/wiki/CompleteResourceExample
http://reductivelabs.com/trac/puppet/wiki/CreatingCustomTypes

all detail types, which i understand go in $modulesdir or more
specifically in a subdir, which for me translates to /etc/puppet/
modules/custom/plugins/puppet/type

Long story short, i want to write a type, the way it should be done,
but am not having much joy. The problem is that the language the type
is written in does not conform what i know about ruby. Having coded in
ruby for a collective 2 hours or so now, clearly i'm not an old hand
at it, but thankfully http://www.ruby-doc.org/docs/ProgrammingRuby/ is
actually quite good.

The pseudo code of what i want is roughly:

for lines in file
do
  if lines == old_line then
    lines = new_line
  else
    # profit?
  end if
done

using the guide in practical types i get as far as:
[r...@puppetbeta type]$ cat replace_line.rb
module Puppet
        newtype(:replace_line) do
                @doc = "Replace a line (old) in a given file (file)
with a new line (new)."

                newparam(:name) do
                        desc "The name of the resource"
                end

                newparam(:file) do
                        desc "The file to be examined and possibly
modified"
                end

                newparam(:old) do
                        desc "The current, that is old, line which we
want to replace"
                end

                newparam(:new) do
                        desc "The future, that is new, line which we
want to have there instead"
                end

                newproperty(:ensure) do
                        desc "Whether the line needs to be replaced"

                        print("changing old: ", old, " to new: ", new)

                        def retrieve
                                File.readlines(resource[:file]).map { |
l|
                                        l.chomp
                                }.include?(resource
[:old] ? :needs_replacing :all_good
                        end

                        newvalue :all_good
                        newvalue :needs_replacing do
#                                File.open(resource[:file], 'r') { |
l|
#                                        l.readlines
#                                }
# ^ this is what i gather i should be doing from the guide but
# v this is what the ruby documentation says i should be doing
#                                begin
#                                        fileContents =
currentfile.readlines()
#                                rescue
#                                        # ... let's assume it just
works
#                                ensure
#                                        file.close
#                                end
#
#                                file = File.open(file, 'w')
#                                begin
#                                        for lines in fileContents
#                                                if lines == old then
#                                                        file.write
(new)
#                                                else
#                                                        file.write
(lines)
#                                                end
#                                        end
#                                rescue
#                                        # ... same as before
#                                ensure
#                                        file.close
#                                end
#                       end
                end
        end
end

Could someone point me at the doco that explains how these things are
constructed:
                                File.readlines(resource[:file]).map { |
l|
                                        l.chomp
                                }.include?(resource
[:old] ? :needs_replacing :all_good

i gather the |l| defines a variable that contains the lines read, and
the l.chomp does what it sounds like, but then ... i really dong get
what happens ... well, i guess it's a for loop which checks if each
line matches old, and depending on this sets a value ... but given my
minutes of ruby coding i can't see how this works.

When i get it working i promise to write doco / a recipe :)

Cheers

chakkerz

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to