How can I get Chef to run apt-get update before running other recipes

后端 未结 12 1510
一生所求
一生所求 2021-01-31 02:01

Right now I have the following in my Vagrantfile:

config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = \"cookbooks\"
    chef.add_recipe \"apt\"
           


        
相关标签:
12条回答
  • 2021-01-31 02:52

    I had the same situation, and in my case, apt cookbook was second after the one which called installation of package. Just leaving it here so maybe someone will benefit from it. Check the order of cookbooks in your runlist, role or wherever else.

    0 讨论(0)
  • 2021-01-31 02:55

    apt-get update should be running first the way you have it. However, the recipe will only update once every 24 hours:

    execute "apt-get-update-periodic" do
      command "apt-get update"
      ignore_failure true
      only_if do
        File.exists?('/var/lib/apt/periodic/update-success-stamp') &&
        File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
      end
    end
    
    0 讨论(0)
  • 2021-01-31 02:56

    There are three resources that will do this nicely on an ubuntu system, specifically in use on 12.04 precise 64 bit by me.

    1. run apt-get-update at will when other recipes require

    2. install update-notifier-common package that gives us timestamps on updates

    3. check the timestamps and update periodically. In this case below after 86400 seconds.

    And here's those three recipes.

    execute "apt-get-update" do
      command "apt-get update"
      ignore_failure true
      action :nothing
    end
    
    package "update-notifier-common" do
      notifies :run, resources(:execute => "apt-get-update"), :immediately
    end
    
    execute "apt-get-update-periodic" do
      command "apt-get update"
      ignore_failure true
      only_if do
       File.exists?('/var/lib/apt/periodic/update-success-stamp') &&
       File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
      end
    end
    
    0 讨论(0)
  • 2021-01-31 02:56

    It looks like the latest version of the opscode apt cookbook allow you to run it at compile time.

    config.vm.provision :chef_solo do |chef|
      chef.cookbooks_path = "cookbooks"
      chef.add_recipe "apt"
      chef.json = { "apt" => {"compiletime" => true} }
    end
    

    As long as apt is run before other compiletime cookbooks (like postgres) in the run list, this should work.

    0 讨论(0)
  • 2021-01-31 02:58

    The simplest and most direct way to solve the problem is by applying the following patch (h/t @ashchristopher):

    https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81

    The problem is that the postgresql::client recipe runs the install action on the package resources at postgresql/recipes/client.rb:39 and 44 at compile-time rather than run-time like normal (h/t Tim Potter), causing them to be evaluated by Chef (and thus installed) before anything else runs.

    pg_packages.each do |pg_pack|
      package pg_pack do
        action :nothing
      end.run_action(:install)
    end
    
    gem_package "pg" do
      action :nothing
    end.run_action(:install)
    

    I believe this is done in service of the database cookbook's postgres provider, which depends on the postgresql cookbook and relies on the pg gem being installed before it will compile. Applying the above patch may break the database cookbook.

    The other alternative solution would be to create a recipe which runs apt-get update also at compile time and put it in your run_list before the postgresql cookbook. In its simplest form that would probably be something like:

    execute "apt-get update" do
      action :nothing
    end.run_action(:install)
    
    0 讨论(0)
  • 2021-01-31 03:02

    I seem to have been able to solve the issue by applying the following patch:

    https://github.com/wil/cookbooks/commit/a470a4f68602ec3bf3374830f4990a7e19e9de81

    0 讨论(0)
提交回复
热议问题