每次我运行厨师 - 客户端,它下载JDK需要5分钟。怎么跳?

时间:2012-10-19 19:31:02

标签: ruby chef

即使我在一个盒子上运行chef-client,它也会等待大约5分钟来下载JDK,即使它之前已经完成了。有没有办法解决这个问题?

[Mon, 22 Oct 2012 13:17:46 -0500] INFO: Processing remote_file[/var/chef/cache/jdk-1.6-u30-linux-amd64.rpm] action create_if_missing (sun_java::default line 18)
[Mon, 22 Oct 2012 13:17:48 -0500] INFO: Processing package[jdk-1.6-u30-linux-amd64.rpm] action install (sun_java::default line 25)
[Mon, 22 Oct 2012 13:21:07 -0500] INFO: package[jdk-1.6-u30-linux-amd64.rpm] installed version 1.6.0_30-fcs
[Mon, 22 Oct 2012 13:21:07 -0500] INFO: package[jdk-1.6-u30-linux-amd64.rpm] sending run action to bash[update-alternatives java] (immediate)
[Mon, 22 Oct 2012 13:21:07 -0500] INFO: Processing bash[update-alternatives java] action run (sun_java::default line 40)

配方如下:

urlVersion = "1."+node["sun_java"]["version"].sub(/[u]/, "-u")
node.default["sun_java"]["rpm_url"] = "http://***/#{urlVersion}/jdk-#{urlVersion}-linux-#{node["sun_java"]["arch"]}.rpm"

#Check that we are using the .rpm file because of the recent change
if File.extname(File.basename(node["sun_java"]["rpm_url"]))!=".rpm"
  raise "You must use the jdk*.rpm file to install the Sun JDK. You can get it from the jdk*-rpm.bin file by running the command './jdk*-rpm.bin -x'"
end

javaRPM = File.basename(node["sun_java"]["rpm_url"])

remote_file "#{Chef::Config[:file_cache_path]}/#{javaRPM}" do
  action :create_if_missing
  source node["sun_java"]["rpm_url"]
  mode "0755"
  backup false
end

package javaRPM do
  action :install
  source "#{Chef::Config[:file_cache_path]}/#{javaRPM}"
  options "--nogpgcheck" # sun/oracle doesn't sign their RPMs o_O
  notifies :run, "bash[update-alternatives java]", :immediately
end

javaHomeFolder = "/usr/java/jdk1.#{node["sun_java"]["version"].sub(/[u]/, ".0_")}"
jdkFolder = "#{javaHomeFolder}/bin"
slaveString = ""

node["sun_java"]["update_slaves"].each do |java_bin|
  slaveString = slaveString + "--slave \"/usr/bin/#{java_bin}\" \"#{java_bin}\" \"#{jdkFolder}/#{java_bin}\" "
end

bash "update-alternatives java" do
  action :nothing
  code <<-EOH
    update-alternatives --install "/usr/bin/java" "java" "#{jdkFolder}/java" 1 #{slaveString}
    update-alternatives --set java #{jdkFolder}/java
  EOH
end

#Remove old environment then notify new environment to be created
ruby_block "delete_environement" do
        block do
            editBashrc = Chef::Util::FileEdit.new("/etc/profile")
                editBashrc.search_file_delete_line(/^.*#JAVA_HOME environment settings.*$/)
                editBashrc.search_file_delete_line(/^.*#Auto-generated by Chef Cookbook sun_java.*$/)
                editBashrc.search_file_delete_line(/^.*export JAVA_HOME=.*$/)
                editBashrc.write_file 
        end
        action :create
end

#create environment of root user
execute "create_environment" do
  user "root"
  command "echo -e '#JAVA_HOME environment settings\n#Auto-generated by Chef Cookbook sun_java\nexport JAVA_HOME=#{javaHomeFolder}' >> /etc/profile"
end

1 个答案:

答案 0 :(得分:1)

这不是下载需要5分钟。似乎rpm每次都重新安装包(输出缩短和注释):

[Mon, 22 Oct 2012 13:17:46 -0500] Processing remote_file # Download the file
[Mon, 22 Oct 2012 13:17:48 -0500] Processing package # Install the file 2 secs later
[Mon, 22 Oct 2012 13:21:07 -0500] package installed # 3+ minutes later

我不知道rpm,但Debian的软件包管理默认跳过已安装的软件包。

所以我猜您的选择是:

  1. 配置厨师更详细。这样,您可以使用所有选项跟踪调用,并查看是否通过从shell运行相同的命令来重新生成行为。
  2. 找出rpm的行为与Debian dpkg的行为方式是否相同(见上文)。如果没有,请告诉它跳过已安装的软件包。
  3. 检查包装版本。如果您的包版本在每次运行时都相同,请转到(1)。如果不是,请查明是否可以rpm跳过类似的版本。
  4. 不要使用package资源,而是手动安装软件包(仅在所有其他选项都失败时使用)。例如,您可以使用execute resource并创建一个指示文件,告诉Chef您已经安装了包。
  5. 选项#4的伪代码:

    execute "install Java by hand" do
      command "rpm install <pkg> && touch /home/user/java_installed"
      creates "/home/user/java_installed"
    end