Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chef logging of wget

I have a chef recipe that looks something like:

Chef::Log.info('step1')
# do stuff

Chef::Log.info('step2')
bash "do_wget" do
  code <<-EOF
  wget somefile
  EOF
end

Chef::Log.info('step3')
# do stuff

The wget takes a while but the logging ends up looking like

step1    
step2    
step3
bash script runs #indicates that the bash script from step 2 is running

Is there a way to prevent the logging from step3 until the bash script is done executing?

like image 307
Jeff Storey Avatar asked Sep 26 '12 05:09

Jeff Storey


People also ask

How do I find Chef logs?

The simplest way to view a Chef log is to go to the instance's details page. The Logs section includes an entry for each event and Execute Recipes command. The following shows an instance's Logs section, with configure and setup commands, which correspond to Configure and Setup lifecycle events.

How do I know if a Chef is installed?

You can use which knife or which chef-client to determine (exist status 0 : command found, otherwise 1 ). While knife is used on the workstation, it should come with every installation of chef. But to be save, check for chef-client .

How do I install Chef packages?

Installing Packages from Third-Party RepoStep 1 − Edit the default recipe of the cookbook. Step 2 − Edit the metadata to add dependency on the apt cookbook. Step 3 − Upload the modified cookbook to the Chef server. Step 4 − Validate that the package you are trying to install, is not yet installed.

What is Chef infra client?

The chef client can be used in local mode to test cookbooks or in a single server setup, or it can be connected with a Chef Infra Server, a tool that can be used to centrally manage many clients on different VMs. It uses cookbooks to act as infrastructure as code and dictate the state of the system.


1 Answers

You should get acquainted with Anatomy of Chef Run. It has 2 stages: compiling and executing.

No action is taken on the resources in the recipes at compiling stage - each evaluated resource is just taken and put into the Resource Collection. Plain Ruby code outside of resources is evaluated, however.

In execution stage it actually evaluates Resource Collection. But by this moment all your logging messages are already printed out.

If you need to run ruby code (including Chef logging) on execution stage, there is a special resource for that Ruby Block

ruby_block "Add logging step3" do
  block do
    Chef::Log.info('step3')
  end
  action :create
end

Another way may be, is to actually execute resource in compiling stage:

bash "do_wget" do
  code "wget somefile"
  action :nothing
end.run_action :run

Action :nothing is set to avoid running this resource twice (once in every stage).

like image 101
Draco Ater Avatar answered Oct 03 '22 21:10

Draco Ater