how to do conditional check?

2,910 views
Skip to first unread message

Sans

unread,
Jun 10, 2011, 7:10:36 PM6/10/11
to puppet...@googlegroups.com
Dear all,

This is my script in the bash: how can I put it into  puppet?

#!/usr/bin/env bash

CONF_DIR="/var/mom_priv"
CPU_COUNT=`cat /proc/cpuinfo | grep siblings | uniq | cut -d\  -f2`

ideal_load_var=$(echo "scale=2; ${CPU_COUNT}+0.5" | bc)
max_load_var=$(echo "scale=2; ${CPU_COUNT}*1.2" | bc)

if [ -d "${CONF_DIR}" ]; then
cat << EOF > ${CONF_DIR}/config
\$ideal_load ${ideal_load_var}
\$max_load ${max_load_var}
EOF
fi

All I wann do is: Get the CPU_COUNT and then if  CONF_DIR exists, make sure  "$CONF_DIR/config" is present with the above lines. How should I write that for puppet? Cheers!!

Patrick

unread,
Jun 10, 2011, 8:14:16 PM6/10/11
to puppet...@googlegroups.com
I don't have a lot of time, but I can get you started.

Puppet way:
1) Create a File resource that creates the config directory.

2) Run "facter" at the command line and look for the name of the variable that tells how many processors the computer has.  That variable will exist when the catalog is compiled as a variable.

3) Use a template with File to create your config file.


Quicker way: (Not reccomended)
1) Copy that script to the computer using "File".
2) Run the script using Exec.

Sans

unread,
Jun 11, 2011, 10:53:01 AM6/11/11
to puppet...@googlegroups.com
Thanks kc! I probably didn't myself clear. I don't wanna create the "the config directory", in stead, if it exists, then make sure "config" file is present with those info. Cheers!!

Sans

unread,
Jun 13, 2011, 5:21:38 AM6/13/11
to Puppet Users
Any suggestion from anyone else? Is there a way to check "if a
directory (or file) already exists, then do something" in Puppet?
Cheers!!

Michael Knox

unread,
Jun 13, 2011, 6:25:15 AM6/13/11
to puppet...@googlegroups.com
Yes,
Create a custom fact that checks for the existence of the directory.

I have one that is as simple as ...
Facter.add("apde_available") do
setcode do
if File::directory?("/usr/local/APDE")
"true"
else
"false"
end
end
end

then in my manifest ...
if ($apde_available =~ /true/) {
...
}

Cheers

Darren Chamberlain

unread,
Jun 13, 2011, 12:49:21 PM6/13/11
to Puppet Users
* Sans <r.santanu.das at gmail.com> [2011/06/13 02:21]:

> Any suggestion from anyone else? Is there a way to check "if a
> directory (or file) already exists, then do something" in Puppet?
> Cheers!!

I use this pattern:

$_exists = inline_template("<%= File.exists?('$f') %>")
case $_exists {
"true": { ... }
"false": { ... }
}

I probably use it too much, in fact, but writing a custom fact for
every file I need to check becomes tedious and hard to maintain (we
do this in quite a few places).

For things like this, irb and inline_template are your friends.

--
It's not what you look at that matters, it's what you see.
-- Henry David Thoreau

Daniel Pittman

unread,
Jun 13, 2011, 1:18:12 PM6/13/11
to puppet...@googlegroups.com
On Mon, Jun 13, 2011 at 09:49, Darren Chamberlain <dar...@boston.com> wrote:
> * Sans <r.santanu.das at gmail.com> [2011/06/13 02:21]:
>
>> Any suggestion from anyone else? Is there a way to check "if a
>> directory (or file) already exists, then do something" in Puppet?
>> Cheers!!

This isn't a suggestion you will like, but: if it hurts when you do
that, don't do it™.

That sort of conditional action is the opposite of how Puppet is
designed to work. You can hack around it with custom facts, but you
are going to fight with the tool the whole time.

The "right" way to do this is that you include a class on all the
machines that *should* have that configuration directory, and file,
present. Inside the class, create the directory, put the config file
in place, and go from there. Puppet talks about the *desired* state
of the system, not the set of actions to take based on the current
state.

> I use this pattern:
>
>  $_exists = inline_template("<%= File.exists?('$f') %>")

So, er, templates (including inline_template) run on the *master*
machine. Your example will test if the file exists on the master for
every client catalog compiled – so, in almost all cases, won't work.
The catalog is *static* when it hits the client.

You really need to use a custom fact to do this: only facts contain
information and context about the client machine you are compiling
for, not anything you evaluate on the server, templates included.

> I probably use it too much, in fact, but writing a custom fact for
> every file I need to check becomes tedious and hard to maintain (we
> do this in quite a few places).

It does have the advantage that it would work, though, where this
pattern totally *can't*. :)

Regards,
Daniel
--
⎋ Puppet Labs Developer – http://puppetlabs.com
✉ Daniel Pittman <dan...@puppetlabs.com>
✆ Contact me via gtalk, email, or phone: +1 (877) 575-9775
♲ Made with 100 percent post-consumer electrons

Matthew Black

unread,
Jun 13, 2011, 1:50:51 PM6/13/11
to puppet...@googlegroups.com
After reading the other responses my question to you is what exactly are you
attempting to do?

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


Sans

unread,
Jun 13, 2011, 8:00:36 PM6/13/11
to Puppet Users
Well, the file I mention is actually one of the Torque (formerly PBS
batch system) "config" file (location: /var/torque/mom_priv/config),
which is auto generated by "yaim" but the thing is: if the file is
already there "yaim" won't touch it. Let's just say that I don't want
yaim to create this file (it messes it up very often and ended up with
wrong value) but wanna make sure that the file is in correct shape,
otherwise jobs won't run properly. On the other hand, that file
doesn't mean anything at all, if Torque is not install in the first
place. That's why I want to put that check in. Cheers!!

Matthew Black

unread,
Jun 13, 2011, 11:28:39 PM6/13/11
to puppet...@googlegroups.com
Sounds like what you want to do is to create a fact to find out that status of whether torque is installed or not.

As for the configuration file, without better understanding the contents of the file, I would in conjunction with the fact do a file resource surrounded by an if statement that utilizes the fact. If its something that is pretty much the same across the board except for some values, like host names or what not, then you could use templates to create the file and keep it that way.

so for example

if $torque_installed == true {
     file { '/var/torque/mom_priv/config':
         ensure => present,
         content => template(...)
      }
}

--

Sans

unread,
Jun 14, 2011, 3:00:42 AM6/14/11
to Puppet Users
Thanks Matthew!
I think that way it's better and simpler. But that makes me asking:
How to check if Torque (or any package in question) is installed or
not?
Another question: How write the template file to multiply the no. of
CPU-core by 1.2 (or whatever)?
cheers!!

Patrick

unread,
Jun 14, 2011, 9:48:36 AM6/14/11
to puppet...@googlegroups.com

On Jun 13, 2011, at 5:00 PM, Sans wrote:

> Well, the file I mention is actually one of the Torque (formerly PBS
> batch system) "config" file (location: /var/torque/mom_priv/config),
> which is auto generated by "yaim" but the thing is: if the file is
> already there "yaim" won't touch it. Let's just say that I don't want
> yaim to create this file (it messes it up very often and ended up with
> wrong value) but wanna make sure that the file is in correct shape,
> otherwise jobs won't run properly. On the other hand, that file
> doesn't mean anything at all, if Torque is not install in the first
> place. That's why I want to put that check in. Cheers!!
>

1) Why not use puppet to decide if Torque should be installed in the first place? Then you can use that logic to decide if the file should be created/put in place?
2) Does is matter if you create the file if the package isn't installed?

Sans

unread,
Jun 14, 2011, 10:00:49 AM6/14/11
to Puppet Users


On Jun 14, 2:48 pm, Patrick <kc7...@gmail.com> wrote:
>
> 1) Why not use puppet to decide if Torque should be installed in the first place?  Then you can use that logic to decide if the file should be created/put in place?

I can't: "torque" is a vital part of the middleware, which needs to be
installed and configured at the time of middleware installation. And n
the other hand, I use Puppet to prepare the environment for the
middleware to be installed (by yaim). After that installation (and
initial configuration), I wanna make sure that "config" file is always
there with correct values.

> 2) Does is matter if you create the file if the package isn't installed?
As I explained above, if the I create the [especially] the directory-
path, yaim will skip a things, thinking it's an upgrade or re-install,
even though installing for first time.

Hope, I've made it a bit clearer now. Cheers!!

Matthew Black

unread,
Jun 14, 2011, 1:12:26 PM6/14/11
to puppet...@googlegroups.com
I haven’t used torque or yaim but what you want to do in a fact is something
like this to determine installation.

if File.exists?('/path/to/config')

end

I don’t usually install anything on a system without doing it in puppet, so
I don’t typically write facts to find out if something is installed or not.


-----Original Message-----
From: puppet...@googlegroups.com [mailto:puppet...@googlegroups.com]
On Behalf Of Sans
Sent: Tuesday, June 14, 2011 10:01 AM
To: Puppet Users
Subject: [Puppet Users] Re: how to do conditional check?

--

Sans

unread,
Jun 14, 2011, 1:53:58 PM6/14/11
to Puppet Users
On Jun 14, 6:12 pm, "Matthew Black" <mjbl...@gmail.com> wrote:
> I haven’t used torque or yaim but what you want to do in a fact is something
> like this to determine installation.
>
> if File.exists?('/path/to/config')
>
Does it work for directory as well?

-San

Matthew Black

unread,
Jun 14, 2011, 3:59:45 PM6/14/11
to puppet...@googlegroups.com
Not sure if it does, I'm going with the assumption it would not.

Though there is a Dir.exists that you could use.

-----Original Message-----
From: puppet...@googlegroups.com [mailto:puppet...@googlegroups.com]
On Behalf Of Sans
Sent: Tuesday, June 14, 2011 1:54 PM
To: Puppet Users
Subject: [Puppet Users] Re: how to do conditional check?

-San

--

Sans

unread,
Jun 15, 2011, 9:32:54 AM6/15/11
to Puppet Users
Doesn't look like there is a "Dir.exists" but "File.exists" appears to
be working for directory as well. And there is a "File.directory",
which works as well.
Two ways of run a check for directories:

if File.exists?(d_path) && File.directory?(d_path)
or
if File.directory?(d_path)

One thing is: if the directory doesn't exist, I se this error on the
client:

err: Could not retrieve catalog from remote server: Error 400 on
SERVER: Failed to parse template w_nodes/mom_priv-config.tpl: Could
not find value for 'num_core' at /etc/puppet/modules/w_nodes/manifests/
init.pp:42 on node myserv.ac.uk


I do understand the meaning but is it normal? I thought if the above
directory doesn't exist, it'll simple skip the rest. cheers!!

Denmat

unread,
Jun 15, 2011, 5:01:00 PM6/15/11
to puppet...@googlegroups.com
Have a look at has_variable? and test on that first then (see puppet template docs).

Reply all
Reply to author
Forward
0 new messages