I have been an unexpected experience with chef. Here is:
I declared my recipe to install postgres.
execute 'sudo apt-get install postgresql-9.1 -y --force-yes'
template "/etc/postgres/9.1/main/pg_hba.conf" do
source "pg_hba.conf.erb"
owner "postgres"
group "postgres"
mode 00600
notifies :restart, 'service[postgresql]', :immediately
subcribes:
end
service "postgresql" do
service_name "postgresql"
supports :restart => true, :status => true, :reload => true
action [:enable, :start]
end
But when I ran converge with test-kitchen.
Compiling Cookbooks...
Recipe: beesor-cookbook::postgresql
* execute[sudo apt-get install postgresql-9.1 -y --force-yes] action run [2014-03-14T17:37:28+00:00] INFO: Processing execute[sudo apt-get install postgresql-9.1 -y --force-yes] action run (beesor-cookbook::postgresql line 4)
Reading package lists...
Building dependency tree...
Reading state information...
postgresql-9.1 is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 31 not upgraded.
[2014-03-14T17:37:29+00:00] INFO: execute[sudo apt-get install postgresql-9.1 -y --force-yes] ran successfully
- execute sudo apt-get install postgresql-9.1 -y --force-yes
Converging 3 resources
* execute[sudo apt-get install postgresql-9.1 -y --force-yes] action nothing[2014-03-14T17:37:29+00:00] INFO: Processing execute[sudo apt-get install postgresql-9.1 -y --force-yes] action nothing (beesor-cookbook::postgresql line 4)
(skipped due to action :nothing)
* template[/etc/postgres/9.1/main/pg_hba.conf] action create[2014-03-14T17:37:29+00:00] INFO: Processing template[/etc/postgres/9.1/main/pg_hba.conf] action create (beesor-cookbook::postgresql line 8)
* Parent directory /etc/postgres/9.1/main does not exist.
================================================================================
Error executing action `create` on resource 'template[/etc/postgres/9.1/main/pg_hba.conf]'
================================================================================
Chef::Exceptions::EnclosingDirectoryDoesNotExist
------------------------------------------------
Parent directory /etc/postgres/9.1/main does not exist.
Resource Declaration:
---------------------
# In /tmp/kitchen/cookbooks/beesor-cookbook/recipes/postgresql.rb
8: template "/etc/postgres/9.1/main/pg_hba.conf" do
9: source "pg_hba.conf.erb"
10: owner "postgres"
11: group "postgres"
12: mode 00600
13: notifies :restart, 'service[postgresql]', :immediately
14: end
15:
Compiled Resource:
------------------
# Declared in /tmp/kitchen/cookbooks/beesor-cookbook/recipes/postgresql.rb:8:in `from_file'
template("/etc/postgres/9.1/main/pg_hba.conf") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
path "/etc/postgres/9.1/main/pg_hba.conf"
backup 5
atomic_update true
source "pg_hba.conf.erb"
cookbook_name :"beesor-cookbook"
recipe_name "postgresql"
owner "postgres"
group "postgres"
mode 384
end
[2014-03-14T17:37:29+00:00] INFO: Running queued delayed notifications before re-raising exception
[2014-03-14T17:37:29+00:00] ERROR: Running exception handlers
[2014-03-14T17:37:29+00:00] ERROR: Exception handlers complete
[2014-03-14T17:37:29+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
Chef Client failed. 1 resources updated
[2014-03-14T17:37:29+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Converge failed on instance <postgresql-ubuntu-1204>.
Please see .kitchen/logs/postgresql-ubuntu-1204.log for more details
------Exception-------
Class: Kitchen::ActionFailed
Message: SSH exited (1) for command: [sudo -E chef-solo --config /tmp/kitchen/solo.rb --json-attributes /tmp/kitchen/dna.json --log_level info]
I thought that Chef runs the resources when it had passed by each one in the recipe.
But it show me the opposite.
How does chef?
A few points here.
First, the output you pasted is not for the recipe code on top. It shows a resource declared with action :nothing
being run during compilation, suggesting you did something like:
execute 'sudo apt-get install postgresql-9.1 -y --force-yes' do
action :nothing
end.run_action(:run)
Which I believe you did while trying to fix you ordering issue (that was not there in the first place).
Second, Chef is doing exactly what you expect, and running each resource in the order they were declared in your recipe. If you look at the logs after the Converging 3 resources
line, you'll see it executing execute
with action :nothing
, then the template
, and lastly the service
.
You can also see before that, that it ran your execute
resource during compile time and didn't install any packages because "postgresql-9.1 is already the newest version
" (also clearly stated in the logs).
Third point, your recipe is probably failing because the postgresql-9.1
package does not create the directory you expect it to: "Parent directory /etc/postgres/9.1/main does not exist
".
Fourth, the idiomatic Chef way of installing a package is by using the package
resource:
package "postgresql-9.1" do
action :install
end
My suggestion: log into the machine you're deploying this recipe to, make sure that the postgresql-9.1
package is installed (or install it if it isn't), make sure that's the package you need, then find out where the pg_hba.conf
file is located for that particular Operating System. Then, fix your template
resource to point to the correct place.