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:
[root@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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---
On Wed, Jan 14, 2009 at 09:38:42PM -0800, chakkerz wrote:> > which deals with the subject of replacing lines in a file,You might want to look at Augeas. It''s pretty neat, and the latest puppet version has it built in. -Robin -- They say: "The first AIs will be built by the military as weapons." And I''m thinking: "Does it even occur to you to try for something other than the default outcome?" -- http://shorl.com/tydruhedufogre http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/ --~--~---------~--~----~------------~-------~--~----~ 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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s what I use which has been pulled from various answers in the
forum.
Bascially there''s a function for deleting a line, appending a line if
not
already there and near the bottom one for editing a line. So the way this
works is that this file gets run as part of the base_class to set up the
definition statements and then whenever you need to make a mod such as
replacing a line just include the commented bit at the end (uncommenting it
first, of course)
#
# edit_file.pp
case $operatingsystem {
RedHat,suse: { $sedcommand="/bin/sed" }
solaris: { $sedcommand="/opt/csw/bin/gsed"}
}
define delete_line($file, $pattern) {
exec { "$sedcommand -r -i ''/$pattern/d'' $file":
onlyif => "/bin/grep ''$pattern''
''$file''",
}
}
# call the above somewhere else using the following:-
# import "edit_file.pp"
# delete_line { title:
# file => "/path/to/file",
# pattern => "InsertRegExpHere",
# }
define append_if_no_such_line($file, $line, $refreshonly =
''false'') {
exec { "/bin/echo ''$line'' >>
''$file''":
#unless => "/bin/grep -Fxqe ''$line''
''$file''",
unless => "/bin/grep ''$line''
''$file''",
path => "/bin",
refreshonly => $refreshonly,
}
}
# call the above somewhere else using the following:-
# append_if_no_such_line{ motd:
# file => "/etc/motd",
# line => "Configured with Puppet!"}
define replace_line($file, $old_pattern, $new_pattern) {
exec { "$sedcommand -r -i
''s/$old_pattern/$new_pattern/'' $file":
onlyif => "/bin/grep ''$old_pattern''
''$file''",
}
}
# call the above somewhere else using the following:-
# import "edit_file.pp"
# replace_line { title:
# file => "/path/to/file",
# old_pattern => "InsertRegExpHere",
# new_pattern => "InsertRegExpHere",
# }
Rgds
Paul
2009/1/15 Robin Lee Powell <rlpowell@digitalkingdom.org>
>
> On Wed, Jan 14, 2009 at 09:38:42PM -0800, chakkerz wrote:
> >
> > which deals with the subject of replacing lines in a file,
>
> You might want to look at Augeas. It''s pretty neat, and the
latest
> puppet version has it built in.
>
> -Robin
>
>
> --
> They say: "The first AIs will be built by the military as
weapons."
> And I''m thinking: "Does it even occur to you to try for
something
> other than the default outcome?" -- http://shorl.com/tydruhedufogre
>
http://www.digitalkingdom.org/~rlpowell/<http://www.digitalkingdom.org/%7Erlpowell/>***
> http://www.lojban.org/
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---