d.ansimov
d.ansimov

Reputation: 2171

String parsing via ruby (chef)

Need to do install on centos 6 logrotate 3.8.7 out of sources, via chef. Using simple execute. But also need to check for existing installation. How can I do that thing, if I can't parse logrotate --version output.

logrotate --version | tr -cd [:digit:]

&

logrotate --version | tr -d "logrotate"

and awk - no use..

Even if I don't parse output, chef can't compare it with my variable..

My recipe is:

ver = `logrotate --version`

if ver.eql? "logrotate 3.8.7"
    puts "nothing to do"
else

bash "logrotate-source-install" do
    user "root"
    group "root"
    cwd "/tmp"
    code <<-EOH
        cd /tmp
        yum -y install gettext popt-devel
        wget https://fedorahosted.org/releases/l/o/logrotate/logrotate-3.8.7.tar.gz
        tar xf logrotate-3.8.7.tar.gz
        cd logrotate-3.8.7
        gmake
    gmake install
    EOH
end

end

Thx in advance.

Upd.

actual_ver = `logrotate --version 2>&1 | awk '{print $2}'`
ver = "3.8.7"

if actual_ver == ver
    puts "nothing to do"
else

bash "logrotate-source-install" do ...

Parsing done, but chef can't recognize output..

Upvotes: 0

Views: 1017

Answers (3)

rastasheep
rastasheep

Reputation: 11232

You could use attribute only_if to guard this resource for idempotence. Also, good idea would be to pull version to node attribute like:

default['logrotate']['version'] = '3.8.7'

and then use it in recepie:

version = default['logrotate']['version']

bash "logrotate-source-install" do
    user "root"
    group "root"
    cwd "/tmp"
    code <<-EOH
        yum -y install gettext popt-devel
        wget https://fedorahosted.org/releases/l/o/logrotate/logrotate-#{version}.tar.gz
        tar xf logrotate-#{version}.tar.gz
        cd logrotate-#{version}
        gmake
        gmake install
    EOH
    not_if "[ ${version} = \"$(logrotate --version 2>&1 | awk '{print $2}')\" ]"
end

Upvotes: 2

coderanger
coderanger

Reputation: 54251

Better version of this:

bash "logrotate-source-install" do
    user "root"
    group "root"
    cwd "/tmp"
    code <<-EOH
        cd /tmp
        yum -y install gettext popt-devel
        wget https://fedorahosted.org/releases/l/o/logrotate/logrotate-3.8.7.tar.gz
        tar xf logrotate-3.8.7.tar.gz
        cd logrotate-3.8.7
        gmake
        gmake install
    EOH
    only_if do
      version = shell_out('logrotate --version')
      version.error? || version.stdout.split.last != '3.8.7'
    end
end

This uses the only_if guard clause to control if the resource converges or not, and uses the shell_out helper instead of Ruby's shell execute syntax. This will be more widely portable, automatically splits stdout and stderr, and will correctly run the install if the command fails. Also doesn't depend on awk.

Upvotes: 1

d.ansimov
d.ansimov

Reputation: 2171

String parsing via ruby do the thing:

actual_ver = `logrotate --version 2>&1 | awk '{print $2}'`
ver = "387"

if actual_ver.delete('^0-9') == ver
    puts "nothing to do"
else

bash "logrotate-source-install" do
    user "root"
    group "root"
    cwd "/tmp"
    code <<-EOH
        cd /tmp
        yum -y install gettext popt-devel
        wget https://fedorahosted.org/releases/l/o/logrotate/logrotate-3.8.7.tar.gz
        tar xf logrotate-3.8.7.tar.gz
        cd logrotate-3.8.7
        gmake
        gmake install
    EOH
end

end

Upvotes: 0

Related Questions