suppose we have written a resource to untar a .tar file .
how can we skip the execution of that resource if that tar has already been unzipped.
suppose we have written a resource to untar a .tar file .
how can we skip the execution of that resource if that tar has already been unzipped.
You could use guards
https://docs.chef.io/resource_common.html#guards
For example:
not_if { ::File.exist?('/your/tar/file') }
the tar file that would be unzipped will give many folders on specific location,
and the tar is first being copied then its being unzipped. so we cannot apply this condition. is there any other way
If the simple file check with guard is not enough, then you could try the following cookbook
If you have multiple locations, you can also do multiple guard interpreters
not_if { ::File.exist?('/foo/') }
not_if { ::File.exist?('/bar/') }
If your tar has a metadata or some other text file that is unique pre version, you could check that.
For example, say your tar has a build.txt file in it
/foo/build.txt
build: 2017-10-29
Then you could read the file and compare versions, only untarting if the version doesn’t match
exec 'untar' do
command 'tar -xvf /tmp/foo.tgz -C /foo'
not_if { ::File.readlines('/foo/build.txt').grep('2017-10-29').empty?) }
end
Or if there is some other programatic way like fetching from an API, you could use ruby blocks and notifiers
exec 'untar' do
command 'tar -xvf /tmp/foo.tgz -C /foo'
action :nothing
end
ruby_block 'check_version' do
block
# Some ruby code to check version
end
notifies :run 'exec[untar], :immedietly
end