Search code examples
vagrantchef-infraberkshelfchef-zero

Vagrant chef provision with Berkshelf ignores environment versions


Our current setup is that we want to re-use a chef cookbook provided by our server administrators to provision our development environments. Problem is, that we cannot change the original cookbook nor want to use the exact recipes (due to some parts not required).

In contrast to the live infrastructure, we will be using Vagrant-managed VMs - confired as close as possible to the live infrastructure. So the plan is to create a simple cookbook on our end, requireing the original server cookbook from our server provider, and call the required recipies.

That all is already working quite fine, the cookbook is downloaded from github (private repo) as a Berkshelf dependency and our custom cookbook just calls the recipes we want to run.

The respective Vagrantfile as simple as this:

Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-6.7"

  config.omnibus.chef_version = :latest
  config.berkshelf.enabled = true

  config.vm.provision :chef_zero do |chef|
    chef.cookbooks_path = ['site-cookbooks']
    chef.data_bags_path = "data_bags"
    chef.nodes_path = "nodes"
    chef.roles_path = "roles"
    chef.environments_path = "environments"

    chef.add_role "cms"
    chef.environment = "development"
  end
end

And our Berksfile is only:

source 'https://supermarket.chef.io'

cookbook '1000-server', git: '[email protected]:somewhere/1000-server.git'
cookbook 'main', path: './site-cookbooks/main'

The overall project structure is as follows:

Root
│   Berksfile
│   Berksfile.lock
│   Vagrantfile
│
├───environments
│       .gitkeep
│       development.json
│
├───nodes
│       .gitkeep
│       vagrant-caa3c589.json
│
├───roles
│       .gitkeep
│       cms.rb
│
└───site-cookbooks
    │   .gitkeep
    │
    └───main
        │   Berksfile
        │   metadata.rb
        │
        ├───attributes
        ├───files
        │   └───default
        ├───libraries
        ├───providers
        ├───recipes
        │       cms.rb
        │       default.rb
        │       _automatic_updates.rb
        │       _common_packages.rb
        │       _motd.rb
        │
        ├───resources
        └───templates
            └───default
                    motd.erb

Now the trick is, that all dependencies in the metadata.rb of the 1000-server cookbook are not version constrained. This is left to the environment.

metadata.rb snip:

depends 'apt'
depends 'automatic_updates'

development.json (from the main cookbook) snip

  "cookbook_versions": {
    "apt": "= 2.9.2",
    "automatic_updates": "= 0.2.0",
    ....
  }

Other environments may use different versions of course. This approach works quite fine in the live environment deployed with regular Chef.

For the development part using Vagrant, we run into problems though. During Vagrant's provision phase, the environment specific versions seem to be ignored. Only when chef-zero then runs on the machine itself, the environment requests a different version that provided.

This becomes apparent for the apt cookbook. During vagrant provisioning, version 4.0.0 (which is the latest) is vendored. Yet when the development role is converging, there is a version conflict as this environment requires version 2.9.2.

The (partially truncated) log of vagrant up is:

$ vagrant up
Bringing machine 'default' up with 'vmware_workstation' provider...
    default: The Berkshelf shelf is at "C:/Users/fge/.berkshelf/vagrant-berkshelf/shelves/berkshelf20160614-11060-184lksc-default"
==> default: Sharing cookbooks with VM
==> default: Cloning VMware VM: 'bento/centos-6.7'. This can take some time...
==> default: Checking if box 'bento/centos-6.7' is up to date...
==> default: Verifying vmnet devices are healthy...
==> default: Updating Vagrant's Berkshelf...
==> default: Resolving cookbook dependencies...
==> default: Fetching 'main' from source at site-cookbooks/main
==> default: Using 1000-server (1.0.19) from [email protected]:***.git (at master)
==> default: Using apt (4.0.0)
** snip ** 
==> default: Vendoring apt (4.0.0) to C:/Users/fge/.berkshelf/vagrant-berkshelf/shelves/berkshelf20160614-11060-184lksc-default/apt
==> default: Auto-generating node name for Chef...
==> default: Preparing network adapters...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Starting the VMware VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 192.168.218.131:22
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Forwarding ports...
    default: -- 22 => 2200
==> default: Configuring network adapters within the VM...
==> default: Waiting for HGFS kernel module to load...
==> default: Enabling and configuring shared folders...
    default: -- C:/Users/fge/Documents/Projects/chef-playground: /vagrant
    default: -- C:/Users/fge/Documents/Projects/chef-playground/roles: /tmp/vagrant-chef/c7b0e759b945769c9873959f131fb7b0/roles
    default: -- C:/Users/fge/Documents/Projects/chef-playground/nodes: /tmp/vagrant-chef/8e970870f311a0ae043f0f7907363289/nodes
    default: -- C:/Users/fge/.berkshelf/vagrant-berkshelf/shelves/berkshelf20160614-11060-184lksc-default: /tmp/vagrant-chef/3bc7c75ac04521a146fc361c5902825a/cookbooks
    default: -- C:/Users/fge/Documents/Projects/chef-playground/data_bags: /tmp/vagrant-chef/d6e666738afcd2f3bf01b2db89232370/data_bags
    default: -- C:/Users/fge/Documents/Projects/chef-playground/environments: /tmp/vagrant-chef/d1bb590869371296e6ed41178f8b3a2c/environments
==> default: Installing Chef 12.11.18 Omnibus package...
** snip **
==> default: Thank you for installing Chef!
==> default: Running provisioner: chef_zero...
==> default: Detected Chef (latest) is already installed
==> default: Generating chef JSON and uploading...
==> default: Running chef-client (local-mode)...
==> default: [2016-06-14T09:16:29+00:00] INFO: Started chef-zero at chefzero://localhost:8889 with repository at /tmp/vagrant-chef/3bc7c75ac04521a146fc361c5902825a
==> default:   One version per cookbook
==> default:   data_bags at /tmp/vagrant-chef/d6e666738afcd2f3bf01b2db89232370/data_bags
==> default:   environments at /tmp/vagrant-chef/d1bb590869371296e6ed41178f8b3a2c/environments
==> default:   roles at /tmp/vagrant-chef/c7b0e759b945769c9873959f131fb7b0/roles
==> default:   nodes at /tmp/vagrant-chef/8e970870f311a0ae043f0f7907363289/nodes
==> default:
==> default: [2016-06-14T09:16:29+00:00] INFO: Forking chef instance to converge...
==> default: Starting Chef Client, version 12.11.18
==> default: [2016-06-14T09:16:29+00:00] INFO: *** Chef 12.11.18 ***
==> default: [2016-06-14T09:16:29+00:00] INFO: Platform: x86_64-linux
==> default: [2016-06-14T09:16:29+00:00] INFO: Chef-client pid: 4479
==> default: [2016-06-14T09:16:31+00:00] INFO: GET /organizations/chef/nodes/vagrant-caa3c589
==> default: [2016-06-14T09:16:31+00:00] INFO: #<ChefZero::RestErrorResponse: 404: Object not found: chefzero://localhost:8889/nodes/vagrant-caa3c589>
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_base.rb:91:in `rescue in get_data'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_base.rb:83:in `get_data'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/endpoints/rest_object_endpoint.rb:18:in `get'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_base.rb:62:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_router.rb:24:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:664:in `block in app'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:336:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:336:in `handle_socketless_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/socketless_server_map.rb:87:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/socketless_server_map.rb:33:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http/socketless_chef_zero_client.rb:154:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:305:in `block in send_http_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:336:in `block in retrying_http_errors'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:334:in `loop'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:334:in `retrying_http_errors'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:299:in `send_http_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:144:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:111:in `get'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/node.rb:604:in `load'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/node.rb:588:in `find_or_create'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/policy_builder/dynamic.rb:72:in `load_node'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/client.rb:467:in `load_node'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/client.rb:269:in `run'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:286:in `block in fork_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:274:in `fork'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:274:in `fork_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:239:in `block in run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/local_mode.rb:44:in `with_server_connectivity'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:227:in `run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:456:in `sleep_then_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:443:in `block in interval_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:442:in `loop'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:442:in `interval_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:426:in `run_application'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:59:in `run'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/bin/chef-client:26:in `<top (required)>'
==> default: /usr/bin/chef-client:52:in `load'
==> default: /usr/bin/chef-client:52:in `<main>'
==> default: [2016-06-14T09:16:31+00:00] INFO: HTTP Request Returned 404 Not Found: Object not found: chefzero://localhost:8889/nodes/vagrant-caa3c589
==> default: [2016-06-14T09:16:31+00:00] INFO: POST /organizations/chef/nodes
==> default: --- POST BODY ---
==> default: {"name":"vagrant-caa3c589","chef_environment":"development","json_class":"Chef::Node","automatic":{},"normal":{},"chef_type":"node","default":{},"override":{},"run_list":[]}
==> default: --- END POST BODY ---
==> default: [2016-06-14T09:16:31+00:00] INFO: Setting the run_list to ["role[cms]"] from CLI options
==> default: [2016-06-14T09:16:31+00:00] INFO: GET /organizations/chef/roles/cms
==> default: [2016-06-14T09:16:31+00:00] INFO: GET /organizations/chef/environments/development
==> default: [2016-06-14T09:16:31+00:00] INFO: Run List is [role[cms]]
==> default: [2016-06-14T09:16:31+00:00] INFO: Run List expands to [main::cms]
==> default: [2016-06-14T09:16:31+00:00] INFO: Starting Chef Run for vagrant-caa3c589
==> default: [2016-06-14T09:16:31+00:00] INFO: Running start handlers
==> default: [2016-06-14T09:16:31+00:00] INFO: Start handlers complete.
==> default: resolving cookbooks for run list: ["main::cms"]
==> default: [2016-06-14T09:16:31+00:00] INFO: POST /organizations/chef/environments/development/cookbook_versions
==> default: --- POST BODY ---
==> default: {"run_list":["main::cms"]}
==> default: --- END POST BODY ---
==> default: [2016-06-14T09:16:59+00:00] INFO: #<ChefZero::RestErrorResponse: 412: Could not satisfy version constraints for: apt>
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb:42:in `post'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_base.rb:62:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/rest_router.rb:24:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:664:in `block in app'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:336:in `call'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/server.rb:336:in `handle_socketless_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/socketless_server_map.rb:87:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-zero-4.6.2/lib/chef_zero/socketless_server_map.rb:33:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http/socketless_chef_zero_client.rb:154:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:305:in `block in send_http_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:336:in `block in retrying_http_errors'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:334:in `loop'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:334:in `retrying_http_errors'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:299:in `send_http_request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:144:in `request'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/http.rb:127:in `post'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/policy_builder/expand_node_object.rb:204:in `sync_cookbooks'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/policy_builder/expand_node_object.rb:83:in `setup_run_context'
==> default: /opt/chef/embedded/lib/ruby/2.1.0/forwardable.rb:183:in `setup_run_context'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/client.rb:510:in `setup_run_context'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/client.rb:280:in `run'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:286:in `block in fork_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:274:in `fork'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:274:in `fork_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:239:in `block in run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/local_mode.rb:44:in `with_server_connectivity'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:227:in `run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:456:in `sleep_then_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:443:in `block in interval_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:442:in `loop'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:442:in `interval_run_chef_client'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application/client.rb:426:in `run_application'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/lib/chef/application.rb:59:in `run'
==> default: /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18/bin/chef-client:26:in `<top (required)>'
==> default: /usr/bin/chef-client:52:in `load'
==> default: /usr/bin/chef-client:52:in `<main>'
==> default: [2016-06-14T09:16:59+00:00] INFO: HTTP Request Returned 412 Precondition Failed: Could not satisfy version constraints for: apt
==> default:
==> default: ================================================================================
==> default: Error Resolving Cookbooks for Run List:
==> default: ================================================================================
==> default:
==> default:
==> default: Missing Cookbooks:
==> default: ------------------
==> default: Could not satisfy version constraints for: apt
==> default:
==> default:
==> default: Expanded Run List:
==> default: ------------------
==> default: * main::cms
==> default:
==> default:
==> default: Platform:
==> default: ---------
==> default: x86_64-linux
==> default:
==> default:
==> default:
==> default: Running handlers:
==> default: [2016-06-14T09:16:59+00:00] ERROR: Running exception handlers
==> default: Running handlers complete
==> default:
==> default: [2016-06-14T09:16:59+00:00] ERROR: Exception handlers complete
==> default: Chef Client failed. 0 resources updated in 29 seconds
==> default: [2016-06-14T09:16:59+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2016-06-14T09:16:59+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
==> default: [2016-06-14T09:16:59+00:00] ERROR: 412 "Precondition Failed"
==> default: [2016-06-14T09:16:59+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

I sure could pin these dependencies in our metadata.rb main file, but that kind of would defeat the purpose here I guess and would not allow us to switch to a different environment (e.g. production) nor always use the same versions as by the 1000-server cookbook (or a different version of the 1000-server coookbook)

I have seen this guy having the same problem for kitchen, but I guess this won't work for Vagrant. Any ideas on how this could be solved for environments?

Any ideas on how this could be solved? Or is this something we


Solution

  • The version constraint solver in Chef Zero is intentionally very simplistic and not as full-featured as the real Chef Server. Environment-level constraints are generally for production-y overrides et al, while things actually required for a given cookbook to function belong in the relevant metadata.rb file. You might also want to look at the berks apply command to run the sync in the other direction.