Chef-server 如何在目录操作删除失败时执行资源?

Chef-server How can i execute a resource when direcotory action delete fails?

有没有办法在资源失败时创建信息文件'log'?我已经尝试了以下但 ingnore_failure 使退出状态为 0 所以 bash 不会被执行

示例值

node.default['user']['option'] = ['delete','/home/vagrant/app/libs']
include_recipe 'user_configure::default'
# so the developer want to delete the folder /libs
# but i don't want to let him as the folder libs is not empty

# node.default['user']['option'] = ['delete','/home/vagrant/app/libs'] 
# the array working like that, [0] will always be the action and then the folders

然后我将另一本食谱称为 user_configure

case node.default['user']['option'][0]
when /create/i
      node.default['user']['dirs'].each do |dir|
      directory "#{dir}" do
      action :create
      recursive true
      end
    end
when /delete/i
  include_recipe '::delete'
when /modify/i
  include_recipe '::modify'
end
i = 1
while i < node.default['user']['option'].length
    directory "#{node.default['user']['option'][i]}" do
    action :delete
    ignore_failure true
    i += 1
    end
end


bash 'Cheking for error' do
    code <<-EOH
    if [ $? -ne 0 ]
        then
         echo 'Something want wrong with the chef-client at deleting folders!' >> /vagrant/$HOSTNAME_"#{Time.now}".txt
        fi
    EOH
  end

主厨客户端错误

Starting Chef Infra Client, version 16.6.14
Patents: https://www.chef.io/patents
resolving cookbooks for run list: ["nginx"]
Synchronizing Cookbooks:
  - nginx (1.0.0)
  - user_configure (1.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: user_configure::delete
  * directory[/home/vagrant/app/libs] action delete
    
    ================================================================================
    Error executing action `delete` on resource 'directory[/home/vagrant/app/libs]'
    ================================================================================
    
    Errno::ENOTEMPTY
    ----------------
    Directory not empty @ dir_s_rmdir - /home/vagrant/app/libs
    
    Resource Declaration:
    ---------------------
    # In /home/tomriddle/.chef/cache/cookbooks/user_configure/recipes/delete.rb
    
      3:     directory "#{node.default['user']['option'][i]}" do
      4:     action :delete
      5:     i += 1
      6:     end
      7: end
    
    Compiled Resource:
    ------------------
    # Declared in /home/tomriddle/.chef/cache/cookbooks/user_configure/recipes/delete.rb:3:in `from_file'
    
    directory("/home/vagrant/app/libs") do
      action [:delete]
      default_guard_interpreter :default
      declared_type :directory
      cookbook_name "user_configure"
      recipe_name "delete"
      path "/home/vagrant/app/libs"
      owner nil
      group nil
      mode nil
    end
    
    System Info:
    ------------
    chef_version=16.6.14
    platform=ubuntu
    platform_version=20.04
    ruby=ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
    program_name=/usr/bin/chef-client
    executable=/opt/chef/bin/chef-client
    

Running handlers:
[2020-11-25T23:03:21-08:00] ERROR: Running exception handlers
Running handlers complete
[2020-11-25T23:03:21-08:00] ERROR: Exception handlers complete
Chef Infra Client failed. 0 resources updated in 03 seconds
[2020-11-25T23:03:21-08:00] FATAL: Stacktrace dumped to /home/tomriddle/.chef/cache/chef-stacktrace.out
[2020-11-25T23:03:21-08:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2020-11-25T23:03:21-08:00] FATAL: Errno::ENOTEMPTY: directory[/home/vagrant/app/libs] (user_configure::delete line 3) had an error: Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir - /home/vagrant/app/libs

有没有办法解决这个错误? 我的主要目标是,当我失败时,我创建一个日志文件并将其移动到文件夹错误的 ftp 服务器中,以便开发人员可以查看 chef-client 是否未完成任务

感谢seshandri_c,经过一些研究这里是解决方案

#on top of your recipe
directory '/root/errors_handle' do
  recursive true
end

cookbook_file "/root/errors_handle/log_handler.rb" do
  source 'log_handler.rb'
end

chef_handler 'LogModule::LogHandler' do
  source "/root/errors_handle/log_handler.rb"
  action :enable
end

然后在你的 cookbook_name/files/default


module LogModule
    class LogHandler < Chef::Handler
        def report
            if !success?
                Chef::Log.fatal('The chef-client has failed')
                status = "chef-client failed at #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} for client #{node.name} at resource #{run_status.exception}\n"
                File.write("/vagrant/#{node.name}_#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}.log", status)  
            else
            end
        end
    end
end