Hello there

I've posted on the user's group, but i'm starting to feel here might
be more appropriate since i want to do this in the scope of puppet,
rather than shell scripting.

The premise is simple, what's causing for me this need for this type
is that i can't find anything that does it for me (augeas was
mentioned but i don't get the red hat site's relevance and there is no
info on the puppet site). What i want to do is ensure that the
sshd_config contains a line that specifies the listen address. This
should be tied to the IP address that the host uses to communicate to
the puppet server, and thus it's primary IP, and what i gather the
fact $ipaddress denotes.

The problem is that i don't want to resort to sed and grep (or perl)
to find if a line needs replacing etc. and the guide at
http://reductivelabs.com/trac/puppet/wiki/PracticalTypes is almost
there, but at the end of the day is significantly simpler... in that
it just opens the file for append, whereas i want to replace something
in the middle.

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"

                        defaultto :leave

                        def retrieve
#                              not sure that return is needed ... i
note it is a recurring theme in the other types that ship with puppet
                                return File.readlines(resource
[:file]).map { |l| l.chomp }.include?(resource
[:old]) ? :replace : :leave
                        end

                        newvalue :replace do
                                lines = File.readlines(resource
[:file]) { |l| l.chomp  l == resource[:old] ? resource[:new] : l }

#                               i'm hoping ^ is the same as:
#                               lines = File.readlines(resource
[:file]) { |l| l.chomp }
#                               lines[lines.index(resource[:old])] =
resource[:new]
#                               although that last line is quite ugly,
and i'm not convinced it does what i want

                                File.open(resource[:file], 'w') { |fd|
fd.puts lines }
                        end

                        newvalue :leave do
                                #do nothing
                        end
                end
        end
end

ideally i would love to know how to test this quickly, and where to
get more information about the language constructs used.
http://www.ruby-doc.org/docs/ProgrammingRuby/ is quite good, but i
don't understand how the def retrieve gets used, why it get's called,
and how the defaultto is used...

of course, i also wouldn't object to the code that makes my thing
work, or the allegedly integrated augeas ... i guess it's one of those
"give a man a fish" vs "teach him how to fish" issues :)

Cheers

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to