How to get the result of a command in puppet?

1,313 views
Skip to first unread message

duff

unread,
Mar 14, 2011, 11:12:44 AM3/14/11
to Puppet Users
Hello, I am trying to export the latest tag of an svn repository to a
puppet client.
To do so, I would like to run the following command to get the latest
tag
/usr/bin/svn ls http://url_to_my_svn_repository/tags | /usr/bin/
tail -1

However, I couldn't find a way to store the result of an exec in a
variable.

Since the url to the repository will change depending on the project,
I decided to use a custom function and get it to run and return the
result of the command.

module Puppet::Parser::Functions
newfunction(:get_latest_tag, :type => :rvalue) do |args|
cmd = "/usr/bin/svn ls http://" + arg[0] + " | /usr/bin/
tail -1"
puts cmd
puts %x[ #{cmd} ]
puts 'the end'
end
end

which outputs (note that the command's result is missing):
/usr/bin/svn ls http://url_to_my_svn_repository/tags | /usr/bin/
tail -1

the end

When I run the %x[#{cmd}] statement in a simple ruby file, I get the
expected result.
If I run a simple echo statement instead of the {cmd}, the result of
the cmd is actually outputted

Any idea what's wrong in my custom function? How do I get the result
of a command in puppet?



Here is an excerpt of my manifest if you need a context.


subversion::deploy{'my_site':
repourl => 'my_site/tags',
#branch => '2011031401',
copydir => '/var/www/my_site'
}


$yedzu_svn = <<<url_to_my_svn_server>>>

class subversion {
package { subversion:
ensure => installed,
}
}

define subversion::workingcopy($repourl, $branch, $copydir) {
include subversion
file { "$copydir":
owner => nginx, group => nginx, mode => 755,
ensure => directory,
#notify => subversion::checkout{$branch}
}

# initial check out
subversion::checkout{
$branch :
repourl => $repourl,
branch => $branch,
copydir => $copydir,
require => File[$copydir]
}
}

define subversion::checkout($repourl, $branch, $copydir) {
include subversion
notice("/usr/bin/svn co --non-interactive http://$yedzu_svn/$repourl/$branch
$copydir/$branch")
exec { "svnco-$branch":
command => "/usr/bin/svn co --non-interactive
http://$yedzu_svn/$repourl/$branch $copydir/$branch",
creates => "$copydir/$branch/.svn",
require => [Package["subversion"]]
}
}

define subversion::deploy($repourl, $branch, $copydir,
$latest=false) {
include subversion

notice(get_latest_tag("$yedzu_svn/$repourl"))

subversion::workingcopy {
$branch:
repourl => $repourl,
branch => $branch,
copydir => $copydir,
#notify => subversion::symlink{$branch}
}


subversion::symlink{
$branch:
repourl => $repourl,
branch => $branch,
copydir => $copydir,
require => Exec["svnco-$branch"]
}

}

define subversion::symlink($repourl, $branch, $copydir){
notice("symlinking")
exec {
"new_current_$copydir/$branch":
command => "/bin/rm $copydir/current; /bin/ln -s
$copydir/$branch $copydir/current",
}

exec {
"current_$copydir/$branch":
command => "/bin/ln -s $copydir/$branch $copydir/
current",
}
} }

Brian Gallew

unread,
Mar 15, 2011, 2:37:45 AM3/15/11
to puppet...@googlegroups.com
On Mon, Mar 14, 2011 at 8:12 AM, duff <etienne...@googlemail.com> wrote:
Hello, I am trying to export the latest tag of an svn repository to a
puppet client.
To do so, I would like to run the following command to get the latest
tag
   /usr/bin/svn ls http://url_to_my_svn_repository/tags | /usr/bin/
tail -1

However, I couldn't find a way to store the result of an exec in a
variable.

Since the url to the repository will change depending on the project,
I decided to use a custom function and get it to run and return the
result of the command.

   module Puppet::Parser::Functions
       newfunction(:get_latest_tag, :type => :rvalue) do |args|
           cmd = "/usr/bin/svn ls http://" + arg[0] + " | /usr/bin/
tail -1"
           puts cmd
           puts %x[ #{cmd} ]
           puts 'the end'
       end
   end


If you are actually using "puts" in your function as indicated, then your return value should be "nil".  Have you tried your function this way:

module Puppet::Parser::Functions
  newfunction(:get_latest_tag, :type => :rvalue) do |args|
   %x["/usr/bin/svn ls http://" + arg[0] + " | /usr/bin/tail -1"]
  end
end

This is straight Ruby code, so the result of the function is the return value of the last statement/expression evaluated.

duff

unread,
Mar 15, 2011, 9:37:08 AM3/15/11
to puppet...@googlegroups.com
Hi Brian

The problem isn't the lack of output from the puppet custom function. I have written some that return results.
I used puts statements in the example to show the absence of result from the %x[] statement without having to write some convoluted manifest and puppet output.

The %x[] statement runs and returns a result if executed on the command line using 
    > ruby myfile.rb

        cmd = "/usr/bin/svn ls http://url_of_my_repositorys_tags | /usr/bin/tail -1"
        puts cmd
        puts %x[ #{cmd} ]

    outputs:
        my_tag_name/

Any ideas why the same command would stop running once wrapped in puppet?
If I run it using system(), I get "True" as the result, but that's not what I want

Brian Gallew

unread,
Mar 15, 2011, 12:07:07 PM3/15/11
to puppet...@googlegroups.com
In that case I've got nothing. Sorry.
Reply all
Reply to author
Forward
0 new messages