Používam šéfkuchára na otestovanie svojich receptov. Snažím sa použiť knižnicu na spustenie e-mailu. Tu je to, čo som urobil.
Vytvoril som adresár knižníc v adresári kuchárskych kníh
cat /tmp/chefrepo/cookbooks/poc/libraries/helper.rb
require "net/smtp"
module HandlerSendEmail
class Helper
def send_email_on_run_failure(node_name)
message = "From: Chef <chef@chef.io>n"
message << "To: Grant <grantmc@chef.io>n"
message << "Subject: Chef run failedn"
message << "Date: #{Time.now.rfc2822}nn"
message << "Chef run failed on #{node_name}n"
Net::SMTP.start("localhost", 25) do |smtp|
smtp.send_message message, "chef@chef.io", "grantmc@chef.io"
end
end
end
end
A jednoduchý recept
cat /tmp/chefrepo/cookbooks/poc/recipes/default.rb
#
# Cookbook:: poc
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.
ruby_block "fail the run" do
block do
fail "deliberately fail the run"
end
end
Chef.event_handler do
on :run_failed do
HandlerSendEmail::Helper.new.send_email_on_run_failure(
Chef.run_context.node.name
)
end
end
Keď spúšťam šéfkuchára sólo chef-solo -c solo.rb -j node.json
dostávam
Running handlers:
[2017-09-29T12:49:09+00:00] ERROR: Running exception handlers
[2017-09-29T12:49:09+00:00] ERROR: Running exception handlers
Running handlers complete
[2017-09-29T12:49:09+00:00] ERROR: Exception handlers complete
[2017-09-29T12:49:09+00:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 03 seconds
[2017-09-29T12:49:09+00:00] ERROR: uninitialized constant #<Class:#<Chef::Recipe:0x0000000004875ee8>>::HandlerSendEmail
[2017-09-29T12:49:09+00:00] ERROR: uninitialized constant #<Class:#<Chef::Recipe:0x0000000004875ee8>>::HandlerSendEmail
[2017-09-29T12:49:09+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
[2017-09-29T12:49:09+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Všetko, čo mi chýba? Pokúsil som sa meniť umiestnenie adresárov knižníc na rovnakej úrovni ako v adresári kuchárskych kníh, ale to nefungovalo.
odpovede:
0 pre odpoveď č. 1Chýbajú vám príkazy na zmiešanie pomocného prostriedku. V spodnej časti súboru knižnice pridajte nasledujúce
Chef::Recipe.include(HelperSendEmail)
Chef::Resource.include(HelperSendEmail)
Tým sa sprístupní váš zdroj.
zdroj:https://www.sidorenko.io/post/2016/10/writing-helper-cookbooks-with-shared-functions/