Testing for mounted fs, and trying shell_out


#1

hello. i’d like to test that a dir path is separately mounted filesystem.
and if it is, place a config file there via a template. what are the best
ways to do that?

just today i tried to poke at using shell_out, drawing from examples from
other cookbooks. but i’m failing at that. i’m also new to wielding
begin/rescue. my intention is for the chef-client run to fail if if the
mount point isn’t mounted.

is shell_out a decent way to test the mount? any advice?

my recipe snippet and error:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
include Chef::Mixin::ShellOut

if node[‘ec2’]
begin
ebs_mounted = true if shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]”)
Chef::Log.info(“HIGGS-BOSON: /ebs is mounted.”)
rescue
Chef::Log.fatal(“HIGGS-BOSON: /ebs is not mounted.”)
raise
end
end

[Tue, 25 Sep 2012 19:36:08 +0000] DEBUG: Re-raising exception: NameError - Cannot find a resource for include on centos version 5.8
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource_platform_map.rb:129:in get' /opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:667:inresource_for_platform’
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:684:in resource_for_node' /opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/recipe_definition_dsl_core.rb:58:inmethod_missing’
/var/cache/chef/cookbooks/cupcake-percona/recipes/percona-setup.rb:14:in from_file' /opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:ininstance_eval’
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in from_file' /opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/cookbook_version.rb:578:inload_recipe’

i can place the config file under the right circumstances using only_if within
a template resource block. this does the right thing, but it is silent about
it. i’d prefer it fail, or notify me. i guess i could set up a chef_handler for
the notification, but i’d like to hear thoughts on my attempt with shellout and
begin/rescue.

template “#{node[‘thing’][‘conf_dir’]}/config.conf” do
source "config.conf.erb"
owner "root"
mode "0644"
only_if "grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]"
end

thanks,
kallen


#2

You can’t mix a module into the chef recipe DSL, the way it is evaluating
is trying to find a Chef resource for ‘include’ statement.

Try to require then Chef::Mixin:: ShellOut.shell_out “foo”

The Higgs Boson logging messages always crack me up.

Cheers,

AJ
On Sep 26, 2012 8:18 AM, kallen@groknaut.net wrote:

hello. i’d like to test that a dir path is separately mounted filesystem.
and if it is, place a config file there via a template. what are the best
ways to do that?

just today i tried to poke at using shell_out, drawing from examples from
other cookbooks. but i’m failing at that. i’m also new to wielding
begin/rescue. my intention is for the chef-client run to fail if if the
mount point isn’t mounted.

is shell_out a decent way to test the mount? any advice?

my recipe snippet and error:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
include Chef::Mixin::ShellOut

if node[‘ec2’]
begin
ebs_mounted = true if shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”)
Chef::Log.info(“HIGGS-BOSON: /ebs is mounted.”)
rescue
Chef::Log.fatal(“HIGGS-BOSON: /ebs is not mounted.”)
raise
end
end

[Tue, 25 Sep 2012 19:36:08 +0000] DEBUG: Re-raising exception: NameError -
Cannot find a resource for include on centos version 5.8
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource_platform_map.rb:129:in
`get’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:667:in
`resource_for_platform’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:684:in
`resource_for_node’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/recipe_definition_dsl_core.rb:58:in
method_missing' /var/cache/chef/cookbooks/cupcake-percona/recipes/percona-setup.rb:14:infrom_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`instance_eval’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`from_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/cookbook_version.rb:578:in
`load_recipe’

i can place the config file under the right circumstances using only_if
within
a template resource block. this does the right thing, but it is silent
about
it. i’d prefer it fail, or notify me. i guess i could set up a
chef_handler for
the notification, but i’d like to hear thoughts on my attempt with
shellout and
begin/rescue.

template “#{node[‘thing’][‘conf_dir’]}/config.conf” do
source "config.conf.erb"
owner "root"
mode "0644"
only_if "grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]"
end

thanks,
kallen


#3

On Wed, 26 Sep 2012, AJ Christensen wrote:

You can’t mix a module into the chef recipe DSL, the way it is evaluating
is trying to find a Chef resource for ‘include’ statement.

Try to require then Chef::Mixin:: ShellOut.shell_out “foo”

cool… so my approach to the task using shell_out and begin/rescue is
sane, if not good? i’ll try to get back to this soon to get it to work.
yay context-switching.

The Higgs Boson logging messages always crack me up.

:> … yeah, it gives me something easy to grep for.

Cheers,

AJ
On Sep 26, 2012 8:18 AM, kallen@groknaut.net wrote:

hello. i’d like to test that a dir path is separately mounted filesystem.
and if it is, place a config file there via a template. what are the best
ways to do that?

just today i tried to poke at using shell_out, drawing from examples from
other cookbooks. but i’m failing at that. i’m also new to wielding
begin/rescue. my intention is for the chef-client run to fail if if the
mount point isn’t mounted.

is shell_out a decent way to test the mount? any advice?

my recipe snippet and error:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
include Chef::Mixin::ShellOut

if node[‘ec2’]
begin
ebs_mounted = true if shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”)
Chef::Log.info(“HIGGS-BOSON: /ebs is mounted.”)
rescue
Chef::Log.fatal(“HIGGS-BOSON: /ebs is not mounted.”)
raise
end
end

[Tue, 25 Sep 2012 19:36:08 +0000] DEBUG: Re-raising exception: NameError -
Cannot find a resource for include on centos version 5.8
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource_platform_map.rb:129:in
`get’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:667:in
`resource_for_platform’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:684:in
`resource_for_node’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/recipe_definition_dsl_core.rb:58:in
method_missing' /var/cache/chef/cookbooks/cupcake-percona/recipes/percona-setup.rb:14:infrom_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`instance_eval’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`from_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/cookbook_version.rb:578:in
`load_recipe’

i can place the config file under the right circumstances using only_if
within
a template resource block. this does the right thing, but it is silent
about
it. i’d prefer it fail, or notify me. i guess i could set up a
chef_handler for
the notification, but i’d like to hear thoughts on my attempt with
shellout and
begin/rescue.

template “#{node[‘thing’][‘conf_dir’]}/config.conf” do
source "config.conf.erb"
owner "root"
mode "0644"
only_if "grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]"
end

thanks,
kallen


#4

On Tue, Sep 25, 2012 at 4:43 PM, kallen@groknaut.net wrote:

cool… so my approach to the task using shell_out and begin/rescue is
sane, if not good?

Seems like you’re expecting shell_out to raise an exception if the
grep command doesn’t find the mount. That’s not how shell_out works,
so your rescue block will never run.
Instead you should be checking #exitstatus:

if node[‘ec2’]
ebs_mounted = shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”).exitstatus.zero?
end

Thanks,
Carl


#5

ShellOut also has a #shell_out! (bang) method, which checks the exit status:

https://github.com/opscode/chef/blob/master/chef/lib/chef/mixin/shell_out.rb#L34-38

Cheers,

AJ

On 26 September 2012 10:10, Brasic, Carl carl.brasic@pearson.com wrote:

On Tue, Sep 25, 2012 at 4:43 PM, kallen@groknaut.net wrote:

cool… so my approach to the task using shell_out and begin/rescue is
sane, if not good?

Seems like you’re expecting shell_out to raise an exception if the
grep command doesn’t find the mount. That’s not how shell_out works,
so your rescue block will never run.
Instead you should be checking #exitstatus:

if node[‘ec2’]
ebs_mounted = shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”).exitstatus.zero?
end

Thanks,
Carl


#6

On Tue, Sep 25, 2012 at 3:10 PM, Brasic, Carl carl.brasic@pearson.com wrote:

On Tue, Sep 25, 2012 at 4:43 PM, kallen@groknaut.net wrote:

cool… so my approach to the task using shell_out and begin/rescue is
sane, if not good?

Seems like you’re expecting shell_out to raise an exception if the
grep command doesn’t find the mount. That’s not how shell_out works,
so your rescue block will never run.
Instead you should be checking #exitstatus:

if node[‘ec2’]
ebs_mounted = shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”).exitstatus.zero?
end

Why not just open /proc/mounts and read it?

Shelling out of ruby code to run a grep pipe seems overly complicated
and fragile.

Google for “ruby recipe check for string in file”

  • Booker C. Bense

#7

On Tue, Sep 25, 2012 at 3:18 PM, Booker Bense bbense@gmail.com wrote:

Why not just open /proc/mounts and read it?

Shelling out of ruby code to run a grep pipe seems overly complicated
and fragile.

if node[‘ecs’]
mnt = Array.new
File.open ‘/proc/mounts’ do |io|
mnt = io.grep(//ebs.*md[1-9]/ )
end
if ( mnt.empty?)
# Fail
else
# Create awesome
end
end

If you can write your shell_out in a few lines of Ruby, it is almost
always faster, simpler and more robust. It is very easy to generate a
false positive error return from a shell command. Because what you’re
actually testing is the execution of the shell, not the actual result.

“The Ruby Way” is an excellent source of recipes for doing basic shell stuff.

  • Booker C. Bense

#8

On Wed, 26 Sep 2012, AJ Christensen wrote:

You can’t mix a module into the chef recipe DSL, the way it is evaluating
is trying to find a Chef resource for ‘include’ statement.

Try to require then Chef::Mixin:: ShellOut.shell_out “foo”

hi. i probably won’t use shell_out for my task of detecting a mounted fs, but
i’d still like to be able to wield shell_out in case i want it in the future.
and i’m still failing:

i get this error:

[Wed, 26 Sep 2012 18:40:40 +0000] DEBUG: NoMethodError: undefined method `shell_out’ for Chef::Mixin::ShellOut:Module

when my recipe is:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
ebs_mounted = Chef::Mixin::ShellOut.shell_out “grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]”

halp? many thanks,
kallen

hello. i’d like to test that a dir path is separately mounted filesystem.
and if it is, place a config file there via a template. what are the best
ways to do that?

just today i tried to poke at using shell_out, drawing from examples from
other cookbooks. but i’m failing at that. i’m also new to wielding
begin/rescue. my intention is for the chef-client run to fail if if the
mount point isn’t mounted.

is shell_out a decent way to test the mount? any advice?

my recipe snippet and error:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
include Chef::Mixin::ShellOut

if node[‘ec2’]
begin
ebs_mounted = true if shell_out(“grep /ebs /proc/mounts 2>&1 | grep -q
md[1-9]”)
Chef::Log.info(“HIGGS-BOSON: /ebs is mounted.”)
rescue
Chef::Log.fatal(“HIGGS-BOSON: /ebs is not mounted.”)
raise
end
end

[Tue, 25 Sep 2012 19:36:08 +0000] DEBUG: Re-raising exception: NameError -
Cannot find a resource for include on centos version 5.8
/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource_platform_map.rb:129:in
`get’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:667:in
`resource_for_platform’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:684:in
`resource_for_node’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/recipe_definition_dsl_core.rb:58:in
method_missing' /var/cache/chef/cookbooks/cupcake-percona/recipes/percona-setup.rb:14:infrom_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`instance_eval’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in
`from_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/cookbook_version.rb:578:in
`load_recipe’

i can place the config file under the right circumstances using only_if
within
a template resource block. this does the right thing, but it is silent
about
it. i’d prefer it fail, or notify me. i guess i could set up a
chef_handler for
the notification, but i’d like to hear thoughts on my attempt with
shellout and
begin/rescue.

template “#{node[‘thing’][‘conf_dir’]}/config.conf” do
source "config.conf.erb"
owner "root"
mode "0644"
only_if "grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]"
end

thanks,
kallen


#9

You want to use the shell_out class, not the mixin

https://github.com/opscode/chef/blob/master/chef/lib/chef/shell_out.rb
On Sep 26, 2012 2:51 PM, kallen@groknaut.net wrote:

On Wed, 26 Sep 2012, AJ Christensen wrote:

You can’t mix a module into the chef recipe DSL, the way it is evaluating
is trying to find a Chef resource for ‘include’ statement.

Try to require then Chef::Mixin:: ShellOut.shell_out “foo”

hi. i probably won’t use shell_out for my task of detecting a mounted fs,
but
i’d still like to be able to wield shell_out in case i want it in the
future.
and i’m still failing:

i get this error:

[Wed, 26 Sep 2012 18:40:40 +0000] DEBUG: NoMethodError: undefined method
`shell_out’ for Chef::Mixin::ShellOut:Module

when my recipe is:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
ebs_mounted = Chef::Mixin::ShellOut.shell_out “grep /ebs /proc/mounts 2>&1
| grep -q md[1-9]”

halp? many thanks,
kallen

hello. i’d like to test that a dir path is separately mounted
filesystem.

and if it is, place a config file there via a template. what are the
best

ways to do that?

just today i tried to poke at using shell_out, drawing from examples
from

other cookbooks. but i’m failing at that. i’m also new to wielding
begin/rescue. my intention is for the chef-client run to fail if if the
mount point isn’t mounted.

is shell_out a decent way to test the mount? any advice?

my recipe snippet and error:

require 'chef/mixin/shell_out’
require 'chef/mixin/language’
include Chef::Mixin::ShellOut

if node[‘ec2’]
begin
ebs_mounted = true if shell_out("grep /ebs /proc/mounts 2>&1 |
grep -q

md[1-9]")
Chef::Log.info(“HIGGS-BOSON: /ebs is mounted.”)
rescue
Chef::Log.fatal(“HIGGS-BOSON: /ebs is not mounted.”)
raise
end
end

[Tue, 25 Sep 2012 19:36:08 +0000] DEBUG: Re-raising exception:
NameError -

Cannot find a resource for include on centos version 5.8

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource_platform_map.rb:129:in

`get’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:667:in

`resource_for_platform’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/resource.rb:684:in

`resource_for_node’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/recipe_definition_dsl_core.rb:58:in

`method_missing’

/var/cache/chef/cookbooks/cupcake-percona/recipes/percona-setup.rb:14:in

`from_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in

`instance_eval’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/mixin/from_file.rb:30:in

`from_file’

/opt/opscode/embedded/lib/ruby/gems/1.9.1/gems/chef-0.10.10/lib/chef/cookbook_version.rb:578:in

`load_recipe’

i can place the config file under the right circumstances using only_if
within
a template resource block. this does the right thing, but it is silent
about
it. i’d prefer it fail, or notify me. i guess i could set up a
chef_handler for
the notification, but i’d like to hear thoughts on my attempt with
shellout and
begin/rescue.

template “#{node[‘thing’][‘conf_dir’]}/config.conf” do
source "config.conf.erb"
owner "root"
mode "0644"
only_if "grep /ebs /proc/mounts 2>&1 | grep -q md[1-9]"
end

thanks,
kallen


#10

On Thursday, September 27, 2012 at 4:32 AM, andi abes wrote:

You want to use the shell_out class, not the mixin
https://github.com/opscode/chef/blob/master/chef/lib/chef/shell_out.rb

You can use the mixin, you either need to include it into the Chef::Recipe class, or use extend (instead of include) in an individual recipe file.

HTH,


Daniel DeLeo


#11

Well I was exceedingly unhelpful in this thread – sorry for the misinformation.

Cheers,

AJ

On 28 September 2012 02:18, Daniel DeLeo dan@kallistec.com wrote:

On Thursday, September 27, 2012 at 4:32 AM, andi abes wrote:

You want to use the shell_out class, not the mixin

https://github.com/opscode/chef/blob/master/chef/lib/chef/shell_out.rb

You can use the mixin, you either need to include it into the Chef::Recipe
class, or use extend (instead of include) in an individual recipe file.

HTH,


Daniel DeLeo