2

I'm using vagrant with chef-solo and berkshelf. To be honest I'm new to all three of them so maybe biting off more than I can chew here, but I'm trying to make a very simple cookbook which installs a postgres database.

The error run into first of all is: "Chef::Exceptions::Exec: package[postgresql-client] (postgresql::client line 36) had an error: Chef::Exceptions::Exec: apt-get -q -y install postgresql-client=9.1+129 returned 100, expected 0"

Here's the contents of cookbooks/transportapi/recipes/default.rb

#Set up a postgres database

include_recipe "database"
include_recipe "postgresql"

postgresql_database 'thedb' do
  connection(
    :host     => '127.0.0.1',
    :port     => 5432,
    :username => 'postgres',
    :password => node['postgresql']['password']['postgres']
  )
  template 'DEFAULT'
  encoding 'DEFAULT'
  tablespace 'DEFAULT'
  connection_limit '-1'
  owner 'postgres'
  action :create
end

I've tried to specify a dependency on 'database' and 'postgresql'. I also have a metadata.rb file (need both?) which has

depends 'database'
depends 'postgresql'

These are both cookbooks fetched by berkshelf from opscode repos. My Berksfile looks like this:

site :opscode

cookbook 'database'
cookbook 'postgresql'

cookbook 'transportapi', :path => './cookbooks/transportapi'

And here's the full output when I try to do 'vagrant up'

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise32'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[Berkshelf] This version of the Berkshelf plugin has not been fully tested on this version of Vagrant.
[Berkshelf] You should check for a newer version of vagrant-berkshelf.
[Berkshelf] If you encounter any errors with this version, please report them at https://github.com/RiotGames/vagrant-berkshelf/issues
[Berkshelf] You can also join the discussion in #berkshelf on Freenode.
[Berkshelf] Updating Vagrant's berkshelf: '/Users/harrywood/.berkshelf/default/vagrant/berkshelf-20131106-34531-1no9nl5-default'
[Berkshelf] Using postgresql (3.1.0)
[Berkshelf] Using transportapi (0.1.0) at './cookbooks/transportapi'
[Berkshelf] Using apt (2.3.0)
[Berkshelf] Using build-essential (1.4.2)
[Berkshelf] Using openssl (1.1.0)
[Berkshelf] Using database (1.5.2)
[Berkshelf] Using mysql (3.0.12)
[Berkshelf] Using aws (1.0.0)
[Berkshelf] Using xfs (1.1.0)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 80 => 8080 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: chef_solo...
Generating chef JSON and uploading...
Running chef-solo...
stdin: is not a tty
[2013-11-06T01:24:08+00:00] INFO: *** Chef 10.14.2 ***
[2013-11-06T01:24:08+00:00] INFO: Setting the run_list to ["recipe[transportapi]"] from JSON
[2013-11-06T01:24:08+00:00] INFO: Run List is [recipe[transportapi]]
[2013-11-06T01:24:08+00:00] INFO: Run List expands to [transportapi]
[2013-11-06T01:24:08+00:00] INFO: Starting Chef Run for precise32
[2013-11-06T01:24:08+00:00] INFO: Running start handlers
[2013-11-06T01:24:08+00:00] INFO: Start handlers complete.

================================================================================
Error executing action `install` on resource 'package[postgresql-client]'
================================================================================

Chef::Exceptions::Exec
----------------------
apt-get -q -y install postgresql-client=9.1+129 returned 100, expected 0

Resource Declaration:
---------------------
# In /tmp/vagrant-chef-1/chef-solo-1/cookbooks/postgresql/recipes/client.rb

 35: 
 36:   package pg_pack
 37: 

Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/postgresql/recipes/client.rb:36:in `from_file'

package("postgresql-client") do
  retry_delay 2
  retries 0
  recipe_name "client"
  action :install
  cookbook_name :postgresql
  package_name "postgresql-client"
end

[2013-11-06T01:24:16+00:00] ERROR: Running exception handlers
[2013-11-06T01:24:16+00:00] ERROR: Exception handlers complete
[2013-11-06T01:24:17+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2013-11-06T01:24:17+00:00] FATAL: Chef::Exceptions::Exec: package[postgresql-client] (postgresql::client line 36) had an error: Chef::Exceptions::Exec: apt-get -q -y install postgresql-client=9.1+129 returned 100, expected 0
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

Maybe 'apt' is not included properly. I wonder if the chef run list is supposed to show expanding to include dependencies. It just says "Run List expands to [transportapi]", which is the only recipe I'm adding in the vagrant file.

Harry Wood
  • 2,220
  • 2
  • 24
  • 46

1 Answers1

4

When you SSH into the VM and execute an apt-get update prior to vagrant provision I guess it works?

Then it is for sure the problem that the apt cache is outdated.

You thus should include the apt cookbook in your run list. This does an apt-get update (not during every single chef run, but once per day).

StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • hmmm. OK I think I've tried this before because, yes it gets me further, but then it fails with a different error to do with not having the 'pg' gem. Something more fundamental I'm doing wrong with chef I think, because I thought these dependencies were supposed to be resolved automatically. In my output it looks like this happens ok at the berkshelf level: "[Berkshelf] Using apt (2.3.0)" among other things. Shouldn't chef be then picking it up and adding it to the run list itself? – Harry Wood Nov 06 '13 at 10:42
  • @HarryWood - You should probably accept @StephenKing's answer and either search on the internet for the problem with the `pg` gem, or create a new question for that. The original issue is solved by including the `apt` cookbook (which is usually the case when `apt` exits with `status 100`). – cassianoleal Nov 06 '13 at 12:04
  • But why do I have to add apt cookbook to my run list? Actually it seems like the wrong thing to do because it just moves me onto the next dependency problem. I'll change the title of my question, because I've put too much emphasis on the immediate error message. – Harry Wood Nov 06 '13 at 12:20
  • You would have that `apt` recipe in kind of a base role or some debian-specific cookbook. I'm not yet using Berkshelf so much, so I can't say a lot about this. Sure, it feels awkward that the postgre cookbook has to depend on the apt cookbook. I'd go for something like that here: http://jgoodall.me/posts/2013/05/21/vagrant-chef-berkshelf/ - add another provisioner (that runs prior to chef) to your Vagrantfile and do the `apt-get update`. – StephenKing Nov 06 '13 at 16:09
  • @HarryWood The "pg problem" has been asked before and is solved here: http://stackoverflow.com/questions/18287581/issue-with-installing-postgres-on-vagrant-vm-using-chef-undefined-method-ruby/18288986#18288986 I would agree with cbl. Accept this answer, running an "apt update" on a new debian system is completely normal. The "apt" cookbook does this for you. – Mark O'Connor Nov 06 '13 at 23:49
  • 1
    OK I thought I was doing something wrong, but it seems the chef user community all happily ignore this bug in dependency resolution. Chatted to a chef expert in my office here who said the same. He always just bungs in 'apt' into his run list. So I will do the same, and move onto the next quirky glitch. It's not as clean as I was hoping. – Harry Wood Nov 07 '13 at 12:15
  • Well, how should chef decide, when to do an update? All the functionality comes through cookbooks. And in an environment where you have a base role including the apt cookbook everything is fine. It's just the problem with berkshelf. – StephenKing Nov 07 '13 at 17:38