Deepak
Deepak

Reputation: 417

Excecuting shell script from chef recipe

I have to execute a script from recipe based on the result which I have to get from the ubuntu node. I have to get the version of agent running and based on that I have to execute script. Below is the condition in recipe which I am running.

notifies :run, 'bash[uninstall CloudPassage]', :immediate
only_if { Mixlib::ShellOut.new("dpkg -s cphalo | grep Version | awk '{print $2}'" -lt "3.9.5").run_command.success? }

but it is giving a syntax. can anyone help me with the syntax to get version number from ubuntu node.

Syntax error:

FATAL: Cookbook file recipes/default.rb has a ruby syntax error:
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27:syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
FATAL: ...rsion | awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ...                               ^
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27: syntax error, unexpected ')', expecting '}'
FATAL: ... awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ...  

Upvotes: 0

Views: 417

Answers (2)

coderanger
coderanger

Reputation: 54211

The correct-er way to write this:

only_if { Gem::Requirement.create('< 3.9.5').satisfied_by?(Gem::Version.create(shell_out!('dpkg -s cphalo').stdout[/^Version: (.*)$/, 1])) }

Or something like that.

Upvotes: 1

J. Chomel
J. Chomel

Reputation: 8395

This script checks if cphalo version is less than (-lt ) 3.9.5.

  • get the line with cphalo reading Version:

     "dpkg -s cphalo | grep Version" 
    
  • then pipe it to awk and print second field ($2)

     | awk '{print $2}'" 
    
  • the result is compared to "3.9.5" thanks to -lt

     ">result<" -lt "3.9.5"
    

which returns true or false.

But you must not break your string like you do with internal double quotes. try the below:

" $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 "

or if square braces are needed:

" [ $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 ] "

Upvotes: 0

Related Questions