【发布时间】:2020-11-26 23:47:06
【问题描述】:
有没有办法在资源失败时创建信息文件“日志”?我尝试了以下方法,但 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 是否没有完成任务
【问题讨论】:
-
您能否在问题中显示属性
node['user']['option']的示例值?action :delete失败时出现的错误消息也会有所帮助。 -
有chef handlers可以在发生故障时在节点上生成错误报告(见ErrorReport Handler)。
-
完美的厨师经理完成了这项工作,谢谢!
-
嗨。很高兴看到解决方案。请在答案中更新它。它可能对其他人有帮助。
标签: ruby chef-infra