Chef - Dir.exists? guard treating symlink as directory
Asked Answered
D

1

6

I have a recipe which deletes an empty logs directory, then replaces it with a symlink in the next step.

directory "#{ENV['GS_HOME']}/logs/" do
  action :delete
  only_if { ::Dir.exists?("#{ENV['GS_HOME']}/logs/") }
end

It works the first time around but on the next chef-client run when it should not delete the item which is now a link to another directory, I receive an error:

Errno::ENOTDIR
--------------
Not a directory @ dir_s_rmdir ...

Why does the guard appear to treat the link as a dir and not skip, but then the resource action recognizes it correctly, not as one, and fails? What is the best way around this?

Divergence answered 28/8, 2015 at 16:30 Comment(1)
Possible answer: https://mcmap.net/q/1276941/-how-to-check-if-the-directory-is-symlink-in-chefDespotic
D
5

The first time, the guard checks if it's a directory. Consequent run it can check if the file directory is a symlink. Try

directory "#{ENV['GS_HOME']}/logs/" do
  action :delete
  only_if { ::Dir.exist?("#{ENV['GS_HOME']}/logs/") || !::File.symlink?("#{ENV['GS_HOME']}/logs/") }
end
Despotic answered 28/8, 2015 at 16:52 Comment(1)
Yes, thank you! Worked perfectly after removing the final slash on the File class path.Divergence

© 2022 - 2024 — McMap. All rights reserved.