LWRP, append text to file only if it doesnt comtain it


#1

is there any LWRP way to do append a text to a file if it does NOT contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#2

Use someara’s line cookbook: https://supermarket.chef.io/cookbooks/line

append_if_no_line “make sure a line is in dangerfile” do
path "/tmp/dangerfile"
line "HI THERE I AM STRING"
end

Consider searching the supermarket for cookbooks before sending a
message to every Chef community mailing list user like this. Thanks in
advance.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:18 AM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#3

that is beautiful ! btw I am looking at the source code, why somera has put
all the code in the library folder ? shouldn’t it have resource and
provider folder? is it old way of doing it?

On Tue, Mar 10, 2015 at 12:26 PM, AJ Christensen <
aj@junglistheavy.industries> wrote:

Use someara’s line cookbook: https://supermarket.chef.io/cookbooks/line

append_if_no_line “make sure a line is in dangerfile” do
path "/tmp/dangerfile"
line "HI THERE I AM STRING"
end

Consider searching the supermarket for cookbooks before sending a
message to every Chef community mailing list user like this. Thanks in
advance.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:18 AM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT
contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#4

This is the new “Heavy Weight Resource Provider” post-LWRP paradigm.
It’s more or less like a rubygem in cookbook form and is not subject
to some of the complexities of developing reusable resources with the
LWRP system.

In addition, LWRP and HWRP can be combined during implementation:
Subclassing of other providers and advanced Ruby OO capabilities are
afforded in the HWRP variants. I like to make light weight resources
(because the DSL is friendly) and then extended/heavy-weight reusable
provider implementations.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:45 AM, Medya medya.gh@gmail.com wrote:

that is beautiful ! btw I am looking at the source code, why somera has put
all the code in the library folder ? shouldn’t it have resource and provider
folder? is it old way of doing it?

On Tue, Mar 10, 2015 at 12:26 PM, AJ Christensen
aj@junglistheavy.industries wrote:

Use someara’s line cookbook: https://supermarket.chef.io/cookbooks/line

append_if_no_line “make sure a line is in dangerfile” do
path "/tmp/dangerfile"
line "HI THERE I AM STRING"
end

Consider searching the supermarket for cookbooks before sending a
message to every Chef community mailing list user like this. Thanks in
advance.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:18 AM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT
contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#5

and you can combine these as well. an lwrp is a subclass of LWRPBase , with
instance eval. Chef does the evaluation magic. if you inherit straight from
LWRPBase, you can use the standard attribute etc method. :slight_smile:

On Tue, Mar 10, 2015 at 10:56 AM, AJ Christensen <
aj@junglistheavy.industries> wrote:

This is the new “Heavy Weight Resource Provider” post-LWRP paradigm.
It’s more or less like a rubygem in cookbook form and is not subject
to some of the complexities of developing reusable resources with the
LWRP system.

In addition, LWRP and HWRP can be combined during implementation:
Subclassing of other providers and advanced Ruby OO capabilities are
afforded in the HWRP variants. I like to make light weight resources
(because the DSL is friendly) and then extended/heavy-weight reusable
provider implementations.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:45 AM, Medya medya.gh@gmail.com wrote:

that is beautiful ! btw I am looking at the source code, why somera has
put
all the code in the library folder ? shouldn’t it have resource and
provider
folder? is it old way of doing it?

On Tue, Mar 10, 2015 at 12:26 PM, AJ Christensen
aj@junglistheavy.industries wrote:

Use someara’s line cookbook: https://supermarket.chef.io/cookbooks/line

append_if_no_line “make sure a line is in dangerfile” do
path "/tmp/dangerfile"
line "HI THERE I AM STRING"
end

Consider searching the supermarket for cookbooks before sending a
message to every Chef community mailing list user like this. Thanks in
advance.

cheers,

–aj

On Wed, Mar 11, 2015 at 6:18 AM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT
contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#6

Hi,
I have used Chef::Util::FileEdit for things like this in the past.
Docs are here:
http://www.rubydoc.info/gems/chef/Chef/Util/FileEdit#insert_line_if_no_match-instance_method

If it works, is there a reason not to do it this way?
Thanks,
Yonah

On Tue, Mar 10, 2015 at 7:18 PM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#7

That Chef::Util::FileEdit code is very antiquated (and bound to Chef
Core releases for bug-fixes) and probably makes up some if not most of
the logic encapsulated by Sean’s line cookbook, although it is plain
to see that it does not re-use the Chef::Util code (IMO, the right
choice.)

I’d recommend the latter. Use whatever works for you.

cheers,

–aj

On Wed, Mar 11, 2015 at 7:37 AM, Yonah Russ opscode@yonahruss.com wrote:

Hi,
I have used Chef::Util::FileEdit for things like this in the past.
Docs are here:
http://www.rubydoc.info/gems/chef/Chef/Util/FileEdit#insert_line_if_no_match-instance_method

If it works, is there a reason not to do it this way?
Thanks,
Yonah

On Tue, Mar 10, 2015 at 7:18 PM, Medya medya.gh@gmail.com wrote:

is there any LWRP way to do append a text to a file if it does NOT contain
it ?

for exmaple

add PEERDNS=no to this file
/etc/sysconfig/network-scripts/ifcfg-eth0

only if it is not there


#8

A cookbook I came upon by chance and don’t hear anything about is the “file” cookbook (yes, the name sounds confusing) [1]. It uses Chef::Util::FileEdit under the covers. I’ve always wondered why the line cookbook doesn’t do that, and re-implements that logic. The main reason I use the file cookbook over line is because I could never get notifications to work with the line cookbook (I’m not sure that they’re even supported).

[1] https://github.com/jenssegers/chef-file

Thanks,
Ameir


#9

Hi.

I wrote this cookbook years ago as a way to learn how to write resources.

The main problem with Chef::Util::FileEdit, is that it’s a pure Ruby
library, and not a Chef resource.

Chef resources are convergent (test-and-repair), and can be marked as
"updated" for notifications/subscriptions.

I initially tried to use Chef::Util::FileEdit, but had trouble getting
it to tell me when it made changes.
Also, it leaves .bak files all over the place. (probably a bug)

-s

On Tue, Mar 10, 2015 at 2:49 PM, Ameir A. ameirh@gmail.com wrote:

A cookbook I came upon by chance and don’t hear anything about is the “file” cookbook (yes, the name sounds confusing) [1]. It uses Chef::Util::FileEdit under the covers. I’ve always wondered why the line cookbook doesn’t do that, and re-implements that logic. The main reason I use the file cookbook over line is because I could never get notifications to work with the line cookbook (I’m not sure that they’re even supported).

[1] https://github.com/jenssegers/chef-file

Thanks,
Ameir