web-dev-qa-db-ja.com

他のレシピを実行する前にChefにapt-get updateを実行させるにはどうすればよいですか

現在、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」レシピの機能の1つであり、最初にアップデートを実行するという印象を受けました。

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
45
ashchristopher

次のパッチを適用することで問題を解決できたようです。

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

3
ashchristopher

最初にaptレシピを含めることができます。

include_recipe 'apt'

これにより、更新コマンドが実行されます。

31
rtacconi

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
21
Gabe Evans

これをubuntuシステムでうまく実行する3つのリソースがあります。具体的には12.04の正確な64ビットで使用しています。

  1. 他のレシピが必要なときに意のままにapt-get-updateを実行する

  2. 更新のタイムスタンプを提供するupdate-notifier-commonパッケージをインストールします

  3. タイムスタンプを確認し、定期的に更新してください。この場合、86400秒後に以下になります。

そして、これらの3つのレシピがあります。

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
10
jmontross

Opscode apt cookbookの最新バージョンでは、コンパイル時に実行できるように見えます。

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

実行リストにある他のコンパイル時クックブック(postgresなど)の前にaptが実行されている限り、これは機能するはずです。

8
Jake

ここに投稿された他の多くの回答は、リソースのクローン作成に関する警告を引き起こす可能性があります。

Apt cookbook documentation によれば、node['apt']['compile_time_update'] = trueを設定することでこれを実現できるはずですが、これは自分自身にアプローチします

代わりに私が行うことは次のとおりです。

これにより、元のapt-get updateリソースがロードされ、リソースコレクションに重複したエントリを追加せずに実行されます。これにより、コンパイルフェーズ中のすべてのChef実行中にapt-get updateが実行されます。

# 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'
2
Eric Herot

コンパイル時に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

1
Rudger

問題を解決する最も簡単で直接的な方法は、次のパッチ(h/t @ashchristopher)を適用することです。

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

問題は、postgresql::clientレシピが postgresql/recipes/client.rb:39 でパッケージリソースに対してインストールアクションを実行し、 compile-time で44通常の(h/t 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 cookbookに依存し、pg gemに依存していますコンパイルする前にインストールされます。上記のパッチを適用すると、databaseクックブックが破損する場合があります。

他の代替ソリューションは、コンパイル時にapt-get updateを実行するレシピを作成し、postgresqlクックブックの前のrun_listに配置することです。最も単純な形式では、おそらく次のようになります。

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

パッチを適用しない場合、これは実行のたびに更新される問題に対する一般的なアプローチです。

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

このような実行は、実行ごとに約30秒間かなりのシステムリソースを消費することを考慮する価値があります。 cronまたは他のイベントを介して実行したrecipe :: update_aptという特別なレシピが必要な場合があります。

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

最新の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
0
zdk

これらのすべてのレシピをVagrantプロビジョニング内に追加すると、すぐに管理不能になる可能性があることを簡単に思い出してください。

より良いパターンは、シェフの役割を作成することです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プロビジョニングセクションに追加します。

config.vm.provision :chef_solo do |chef|
  chef.roles_path = "chef/roles"
  chef.cookbooks_path = ["chef/site-cookbooks", "chef/cookbooks"]
  chef.add_role "my-fancy-role"
end
0
lurscher

私も同じ状況にありました。私の場合、aptクックブックはパッケージのインストールと呼ばれるものに次いで2番目でした。ここに置いておくだけで、誰かが恩恵を受けるかもしれません。ランリスト、ロール、またはその他の場所でクックブックの順序を確認してください。

0
kaboom