10

我目前正在使用 chef 来安装 RPM JDK 包,但问题是即使已经下载并安装了包,它每次都会这样做。我尝试在安装前找到一个检查选项,但没有。有没有办法解决这个问题,我可以跳过已经安装的包?Debian 的包管理默认会跳过已安装的包,但 RPM 包管理器似乎没有这样做。

[Tue, 23 Oct 2012 10:34:32 -0500] INFO: Processing remote_file[/var/chef/cache/jdk-1.6-u30-linux-amd64.rpm] action create_if_missing (sun_java::default line 18)
[Tue, 23 Oct 2012 10:34:36 -0500] INFO: Processing package[jdk-1.6-u30-linux-amd64.rpm] action upgrade (sun_java::default line 25)
[Tue, 23 Oct 2012 10:37:15 -0500] INFO: Processing bash[update-alternatives java] action nothing (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
4

5 回答 5

16

我知道这是旧的,但我相信你想要:

remote_file "your-remote-file" do
  ...
  not_if "rpm -qa | grep -qx 'your-package'"
end
于 2013-07-09T17:30:05.747 回答
3

我对 RPM 不熟悉,但您可以查看Chef 如何了解软件包是否已安装(load_current_resource 方法)。你可以在你的配方中实现类似的东西,并将这个条件添加到 remote_file:

remote_file "#{Chef::Config[:file_cache_path]}/#{javaRPM}" do
  not_if { [your_code_that_checks_if_package_installed] }
  ...
end
于 2012-10-24T08:02:44.563 回答
1

Chef 为rpm_packages提供了资源。你可以找到很多例子来展示厨师如何验证是否安装了一个包

于 2014-01-17T17:17:57.467 回答
1

您可以先删除软件包使用ignore_failure然后安装它

package 'package_name'
  ignore_failure true
  action         :remove
end

然后抓取文件

remote_file localPath do
    source packageUrl
    mode 0644
    checksum checkSum
end

然后安装包

package packageName do
      source localPath
      action :install
      allow_downgrade true
end

这适用于任何包类型。理想情况下,对于 rpm,您不需要删除该软件包。allow_downgrade应该管用。但这对我不起作用。

用于校验和curl packageUrl | shasum -a 256

于 2015-07-14T00:17:47.550 回答
1

not_if还有另一种方法,请参见下面的示例

execute 'yum -y install ntp' do
  not_if "rpm -qa | grep 'ntp'"
end
于 2018-09-05T19:55:21.977 回答