Ask Your Question
0

download file if another file does not exist

asked 2015-10-29 13:47:51 -0500

jcriehl gravatar image

I would like to install some software via puppet. The software exists in a tarball. The basic idea is to download the tarball, untar it, delete the tarball. Deleting the tarball is necessary, because of space considerations.

While I can have the install (untar) command work correctly (i.e. if the target directory exists, do nothing, otherwise execute the untar command), I cannot seem to prevent puppet from re-downloading the tarball if it gets deleted. Based on some other items that I googled, I have tried many variations of something like this:

    exec { 'tpstest':
            command => "/bin/true",
            onlyif => "test ! -d /TPS",
            path => ["/bin","/usr/bin"],
    }
    file { "/tps.el6.64.tar.gz":
            ensure => file,
            source => "puppet:///modules/dswtps/tps.el6.64.tar.gz",
            mode => '0644',
            require => Exec["tpstest"],
    }

I have tried changing the logic and attributes in the tpstext command, but nothing works. What I want to do is prevent the tarball (tps.el6.64.tar.gz) from being downloaded if the ultimate target (/TPS) already exists.

any ideas?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

answered 2015-10-30 17:35:24 -0500

If you're prepared to replace the tarball with an empty marker file after installation rather than removing it, then you could use the replace attribute of the file resource set to false to prevent re-download.

You may also want an exec resource to remove the empty marker file if the installed directory is missing.

For example

exec { 'tpstest':
  command => 'rm /tps.el6.64.tar.gz',
  onlyif  => 'test ! -d /TPS -a -f /tps.el6.64.tar.gz -a ! -s /tps.el6.64.tar.gz',
  path    => ["/bin","/usr/bin"],
}

file { '/tps.el6.64.tar.gz':
  ensure  => file,
  mode    => '0644',
  replace => false,
  require => Exec['tpstest'],
  source  => 'puppet:///modules/dswtps/tps.el6.64.tar.gz',
}

exec { 'tpsinstall':
  command => 'tar xf /tps.el6.64.tar.gz',
  creates => '/TPS',
  cwd     => '/',
  path    => ["/bin","/usr/bin"],
  require => File['/tps.el6.64.tar.gz'],
}

exec { 'tpsclean':
  command => 'cat /dev/null > /tps.el6.64.tar.gz',
  onlyif  => 'test -f /tps.el6.64.tar.gz -a -s /tps.el6.64.tar.gz',
  path    => ["/bin","/usr/bin"],
  require => Exec['tpsinstall'],
}
edit flag offensive delete link more

Comments

thanks. I did not see the 'replace => false'. that helped.

jcriehl gravatar imagejcriehl ( 2015-11-02 18:00:29 -0500 )edit
0

answered 2015-10-30 13:10:48 -0500

JohnsonEarls gravatar image

updated 2015-10-30 13:26:06 -0500

Try changing your exec resource to actually fail if the directory exists. Either...

exec { 'tpstest':
    command => '/bin/false' ,
    onlyif => 'test -d /TPS' ,
    path => [ '/bin', '/usr/bin' ] ,
}

or, more simply,

exec { 'tpstest':
    command => 'test ! -d /TPS' ,
    path => [ '/bin', '/usr/bin' ] ,
}

That way, when the directory exists, the exec will fail and the dependent resources will not be applied. However, this will fill your puppet log with failure notices for the exec resource and skipping warnings for the dependent resources.


Another "tricker" solution (requires Puppet 3.5 or better) is to use the validate_cmd part of file. Leave out the tpstest exec entirely, and try something like this instead:

file { "/tps.el6.64.tar.gz":
        ensure => file,
        source => "puppet:///modules/dswtps/tps.el6.64.tar.gz",
        mode => '0644',
        validate_cmd => 'test ! -d /TPS',
}

(You may need to fully-qualify the path to the test command.)

This will result in the following log file messages on the agent side:

Error: Execution of 'test ! -d /TPS' returned 1:
Error: /Stage[main]/…/File[/tps.el6.64.tar.gz]/ensure: change from absent to file failed: Execution of 'test ! -d /TPS' returned 1:
edit flag offensive delete link more

Comments

It seems odd to me that a test return would trigger an error message. I guess I can see that you are testing return codes, and a non-zero return would trigger an error message. It is a bit irksome to see x messages 24 times a day. (yes, we read our logs). thanks.

jcriehl gravatar imagejcriehl ( 2015-11-02 18:04:35 -0500 )edit
0

answered 2015-10-30 00:44:13 -0500

lupin gravatar image

Using file resource it will only make sure that file exists if it doesn't exist. However, if you can use other method to download the file, e.g. rsync/curl then you can drive it using exec resource with conditional like unless/only_if as your other resource.

edit flag offensive delete link more

Comments

thanks. another item to consider.

jcriehl gravatar imagejcriehl ( 2015-11-02 18:05:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-10-29 13:47:51 -0500

Seen: 1,572 times

Last updated: Oct 30 '15