如何让Chef在运行其他配方之前运行apt-get update

时间:2012-02-12 05:51:41

标签: apt apt-get chef vagrant

现在我的Vagrantfile中有以下内容:

config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
    chef.add_recipe "chef-redis::source"
    chef.add_recipe "openssl"
    chef.add_recipe "git"
    chef.add_recipe "postgresql::server"
    chef.add_recipe "postgresql::client"
end

为了安装添加到我的recipe_list的软件,我需要让VM在安装其他软件之前发出 apt-get update

我的印象是,这是'apt'配方的一个特点 - 它会先运行更新。

执行 vagrant provision 时的输出是:

[Sat, 11 Feb 2012 22:20:03 -0800] INFO: *** Chef 0.10.2 ***
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Setting the run_list to ["recipe[apt]", "recipe[build-essential]", "recipe[chef-redis::source]", "recipe[openssl]", "recipe[git]", "recipe[postgresql::server]", "recipe[postgresql::client]", "recipe[vagrant-main]"] from JSON
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Run List is [recipe[apt], recipe[build-essential], recipe[chef-redis::source], recipe[openssl], recipe[git], recipe[postgresql::server], recipe[postgresql::client], recipe[vagrant-main]]
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Run List expands to [apt, build-essential, chef-redis::source, openssl, git, postgresql::server, postgresql::client, vagrant-main]
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Starting Chef Run for lucid32
[Sat, 11 Feb 2012 22:20:03 -0800] INFO: Processing package[postgresql-client] action install (postgresql::client line 37)
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: package[postgresql-client] (postgresql::client line 37) has had an error
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: Running exception handlers
[Sat, 11 Feb 2012 22:20:04 -0800] ERROR: Exception handlers complete
[Sat, 11 Feb 2012 22:20:04 -0800] FATAL: Stacktrace dumped to /tmp/vagrant-chef-1/chef-stacktrace.out
[Sat, 11 Feb 2012 22:20:04 -0800] FATAL: Chef::Exceptions::Exec: package[postgresql-client] (postgresql::client line 37) had an error: apt-get -q -y install postgresql-client=8.4.8-0ubuntu0.10.04 returned 100, expected 0

12 个答案:

答案 0 :(得分:31)

您可以在最开始包含apt配方:

include_recipe 'apt'

这将运行更新命令。

答案 1 :(得分:21)

apt-get update应该以您拥有它的方式运行。但是,配方只会每24小时更新一次:

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

答案 2 :(得分:10)

有三种资源可以在ubuntu系统上很好地完成这项工作,特别是我在12.04精确64位上使用。

  1. 当其他食谱需要

  2. 时,随意运行apt-get-update
  3. 安装update-notifier-common软件包,为我们提供更新时间戳

  4. 检查时间戳并定期更新。在这种情况下,在86400秒之后。

  5. 这是三个食谱。

    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
    

答案 3 :(得分:8)

看起来最新版本的opscode apt cookbook允许你在编译时运行它。

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

只要apt在运行列表中的其他编译时间烹饪书(如postgres)之前运行,这应该可以。

答案 4 :(得分:3)

我似乎已经能够通过应用以下补丁来解决问题:

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

答案 5 :(得分:2)

此处发布的许多其他答案可能会引发有关资源克隆的警告。

根据Apt cookbook documentation,您应该能够通过设置node['apt']['compile_time_update'] = true来实现这一目标,但是我自己从未对此方法有过多运气

这就是我做的事情:

这将加载原始apt-get update资源,并确保它在不向资源集合添加重复条目的情况下运行。这将导致apt-get update在编译阶段的每次Chef运行期间执行:

# First include the apt::default recipe (so that `apt-get update` is added to the collection)
include_recipe 'apt'

# Then load the `apt-get update` resource from the collection and run it
resources(execute: 'apt-get update').run_action(:run)

显然,您还希望在 metadata.rb 文件中加入apt食谱:

# ./metadata.rb
depends 'apt'

答案 6 :(得分:1)

解决问题的最简单,最直接的方法是应用以下补丁(h / t @ashchristopher):

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

问题是postgresql::client配方在postgresql/recipes/client.rb:39compile-time处的包资源上运行安装操作,而不是像正常那样运行时(不是Tim Potter) ,导致它们由Chef(并因此安装)在其他任何运行之前进行评估。

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)

我相信这是在为database cookbook的postgres提供者服务时完成的,这取决于postgresql食谱,并依赖于在编译之前安装的pg gem。应用上述补丁可能会破坏database食谱。

另一种替代解决方案是创建一个在编译时运行apt-get update的配方,并将其放在run_list食谱之前的postgresql中。最简单的形式可能是:

execute "apt-get update" do
  action :nothing
end.run_action(:install)

答案 7 :(得分:1)

如果没有修补,这是解决问题的通用方法,将在每次运行时更新:

bash "update-apt-repository" do
  user "root"
  code <<-EOH
  apt-get update
  EOH
end

值得考虑的是,在每次运行中,这样的运行会占用相当多的系统资源大约30秒;您可能想要一个名为recipe :: update_apt的特殊配方,您可以通过cron或其他事件运行,即

chef-client -o "recipe[yourrecipe::update_apt]"

答案 8 :(得分:1)

要在编译时运行apt-get update,请执行:

e = execute "apt-get update" do
  action :nothing
end

e.run_action(:run)

检查https://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time

答案 9 :(得分:0)

我有同样的情况,在我的情况下,apt cookbook在称为安装包的那个之后是第二个。把它留在这里也许有人会从中受益。检查运行列表,角色或其他任何地方的烹饪书的顺序。

答案 10 :(得分:0)

只是一个友好的提醒,在流浪者条款中添加所有这些食谱很快就会变得无法管理。

更好的模式是创建一个厨师角色.subtopic{ width:calc(100vw / 3 - 0.25vw); height:5vh; float:left; background:white; color:blue; left:0; overflow:hidden; border-bottom:0.5vh white; }

chef/my-fancy-role.rb

然后将此角色添加到# Name of the role should match the name of the file name "my-fancy-role" # Run list function we mentioned earlier run_list( "recipe[apt]", "recipe[build-essential]", "recipe[chef-redis::source]", "recipe[openssl]" ) 配置部分:

Vagrantfile

答案 11 :(得分:0)

对于最近的Chef版本,即版本14。 您也可以使用https://docs.chef.io/resource_apt_update.html

apt_update

以下输出是我的实验,它以本地模式(零)为Chef_14.5.33运行该资源:

curl -O https://packages.chef.io/files/stable/chef/14.5.33/ubuntu/18.04/chef_14.5.33-1_amd64.de
sudo dpkg -i chef_14.5.33-1_amd64.deb
mkdir -p cookbooks/hello/recipes/ && echo "apt_update" > cookbooks/hello/recipes/default.rb

sudo sh -c 'chef-client -z -o hello'
[2018-10-12T10:25:30+00:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 14.5.33
[2018-10-12T10:25:32+00:00] WARN: Run List override has been provided.
[2018-10-12T10:25:32+00:00] WARN: Run List override has been provided.
[2018-10-12T10:25:32+00:00] WARN: Original Run List: []
[2018-10-12T10:25:32+00:00] WARN: Original Run List: []
[2018-10-12T10:25:32+00:00] WARN: Overridden Run List: [recipe[hello]]
[2018-10-12T10:25:32+00:00] WARN: Overridden Run List: [recipe[hello]]
resolving cookbooks for run list: ["hello"]
Synchronizing Cookbooks:
  - hello (0.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * apt_update[] action periodic (up to date)
[2018-10-12T10:25:32+00:00] WARN: Skipping final node save because override_runlist was given
[2018-10-12T10:25:32+00:00] WARN: Skipping final node save because override_runlist was given

Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 01 seconds