Search code examples
rubychef-infrachef-solochefspec

List all the declared packages in chef


I'm working on a infrastructure where some servers don't have access to the internet, so I have to push the packages to the local repo before declaring them to be installed on Chef.

However we've been on a situation where Chef failed to install a package since the package wasn't there on some boxes and it has been successful on some other boxes.

What I want to do is to run a Ruby/RSpec test before applying Chef config on the nodes to make sure the packages declared on the recipes do actually exist on the repo.

In order to do that I need to be able to list all the packages exists in the our recipes.

My question is: Is there anyway to list all the declared packages in Chef? I had a quick look at Chef::Platform and ChefSpec but unfortunately couldn't find anything useful to my problem.

Do you have any idea where is the best place to look at?


Solution

  • If you use ChefSpec you can find all the packages by calling chef_run.find_resources(:package) inside some test. See the source code. Like this:

    require 'chefspec'
    
    describe 'example::default' do
      let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
    
      it 'does something' do
        chef_run.find_resources(:package)...
      end
    end