Conditional resource creation / script execution


#1

Hello,

I would like to know how to write “conditional” parts for recipies.

For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file
and create it if necessary. Right now, the solution is rather inelegant

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end

while I would prefer

unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end

Is there a way to do this properly ?

– Sébastien


#2

I think using the Execute resource and specifying ‘creates’ while do what you want.

http://wiki.opscode.com/display/chef/Resources#Resources-Execute

On Jan 13, 2010, at 12:56 PM, Sébastien Pierre wrote:

Hello,

I would like to know how to write “conditional” parts for recipies.

For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file and create it if necessary. Right now, the solution is rather inelegant

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end

while I would prefer

unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end

Is there a way to do this properly ?

– Sébastien


#3

Something like this should work …

not_if do File.exists?(HOME + “/.ssh/id_dsa.pub”) end

Joshua

http://wiki.opscode.com/display/chef/Resources#Resources-Script

On Jan 13, 2010, at 12:56 PM, Sébastien Pierre wrote:

Hello,

I would like to know how to write “conditional” parts for recipies.

For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file and create it if necessary. Right now, the solution is rather inelegant

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end

while I would prefer

unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end

Is there a way to do this properly ?

– Sébastien


#4

Thanks,

I don’t know much of Ruby, so how would I chain the script to this
expression ?

– Sébastien

2010/1/13 Joshua Miller jassinpain@gmail.com

Something like this should work …

not_if do File.exists?(HOME + “/.ssh/id_dsa.pub”) end

Joshua

http://wiki.opscode.com/display/chef/Resources#Resources-Script

On Jan 13, 2010, at 12:56 PM, Sébastien Pierre wrote:

Hello,

I would like to know how to write “conditional” parts for recipies.

For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file
and create it if necessary. Right now, the solution is rather inelegant

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end

while I would prefer

unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end

Is there a way to do this properly ?

– Sébastien


#5

Sébastien,
Using creates is probably the best way:

script “ssh-keygen” do
creates “#{user_home}/.ssh/id_dsa.pub”
# other attributes
end

Script “inherits” from execute, so script can do everything execute can do.

If you prefer the not_if way, it’s pretty similar:

script “ssh-keygen” do
not_if {File.exist? “#{user_home}/.ssh/id_dsa.pub”}
# other attributes
end

HTH, and feel free to join us on #chef in freenode if you’d like more
real-time help.

Dan DeLeo

2010/1/13 Sébastien Pierre sebastien.pierre@gmail.com:

Thanks,
I don’t know much of Ruby, so how would I chain the script to this
expression ?
– Sébastien

2010/1/13 Joshua Miller jassinpain@gmail.com

Something like this should work …
not_if do File.exists?(HOME + “/.ssh/id_dsa.pub”) end

Joshua

http://wiki.opscode.com/display/chef/Resources#Resources-Script

On Jan 13, 2010, at 12:56 PM, Sébastien Pierre wrote:

Hello,
I would like to know how to write “conditional” parts for recipies.
For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file
and create it if necessary. Right now, the solution is rather inelegant
script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end
while I would prefer
unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end
Is there a way to do this properly ?
– Sébastien


#6

Something like this, only part I am not sure about is HOME, but based on I see it should work.

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code "mv ssh-temp/* .ssh/"
not_if do File.exists?(HOME + “/.ssh/id_dsa.pub”) end
end

On Jan 13, 2010, at 1:32 PM, Sébastien Pierre wrote:

Thanks,

I don’t know much of Ruby, so how would I chain the script to this expression ?

– Sébastien

2010/1/13 Joshua Miller jassinpain@gmail.com
Something like this should work …

not_if do File.exists?(HOME + “/.ssh/id_dsa.pub”) end

Joshua

http://wiki.opscode.com/display/chef/Resources#Resources-Script

On Jan 13, 2010, at 12:56 PM, Sébastien Pierre wrote:

Hello,

I would like to know how to write “conditional” parts for recipies.

For instance, I would like to check if there is a “~/.ssh/id_dsa.pub” file and create it if necessary. Right now, the solution is rather inelegant

script “ssh-keygen” do
interpreter "bash"
user "manage"
cwd (HOME)
code "mkdir ssh-temp"
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
code <<-EOH
if [ ! -f .ssh/id_dsa.pub ]; then
mv ssh-temp/* .ssh/
fi
rm -rf ssh-temp
EOH
end

while I would prefer

unless (File exists (HOME + “/.ssh/id_dsa.pub”) script do
interpreter "bash"
user "manage"
cwd (HOME)
code "ssh-keygen -t dsa -f ssh-temp/id_dsa << EOF\n\nEOF"
end

Is there a way to do this properly ?

– Sébastien


#7

OK, thanks to you all :slight_smile:

The “not_if” clause is perfect for my needs, as it allows more complex
conditions, like only do this if file does not contain this, or doesn’t have
this checksum.

– Sébastien