java cookbook 能否用于安装 oracle java 的本地副本?

Can the java cookbook be used to install a local copy of oracle java?

我最近一直在尝试学习 chef,因为我打算用它来部署服务器配置和服务器应用软件。我在理解如何使用其他人的食谱时遇到问题。比如我要部署JDK8u31。我不知道如何实施这本食谱。 https://supermarket.chef.io/cookbooks/java

我阅读了说明并看到了以下内容

只需在您想要 Java 安装的任何地方包含 java 食谱,例如 运行 列表(食谱[java])或食谱(include_recipe 'java')

我试过

include_recipe 'java' 

在我的名为 common_java_server

的食谱中

然后

directory '/usr/lib/jvm/' do
  owner 'root'
  group 'root'
  mode '0644'
end




java_ark "jdk" do
    url 'http://download.oracle.com/otn-pub/java/jdk/8u31/jdk-8u31-linux-x64.bin'
    checksum  'a8603fa62045ce2164b26f7c04859cd548ffe0e33bfc979d9fa73df42e3b3365'
    app_home '/usr/lib/jvm/'
    bin_cmds ["java", "javac"]
    action :install
end

# set alternatives for java and javac commands
java_alternatives "set java alternatives" do
    java_location '/usr/local/java'
    bin_cmds ["java", "javac"]
    action :set
end

这是我得到的错误

Recipe Compile Error in /etc/chef/src/cookbooks/common/recipes/java_dev_server.rb
====

java cookbook is designed to support the installation of different Java variants. It's behaviour is controlled by node attributes. The defaults are in the cookbook并会安装OpenJDK.

因此,要安装 oracle JDK,您需要指定替代覆盖,这些将在 README

中讨论

你是怎么做到的?在 Chef 中你至少有两个选择:

  1. 包装食谱
  2. 角色

有关包装食谱的示例,请参阅我的其他答案。

  • How to use chef to update-alternatives for java using execute?

举个例子试试这个:

{
  "name": "java",
  "description": "Oracle java role",
  "override_attributes": {
    "java": {
      "jdk_version": 8,
      "install_flavor": "oracle",
      "oracle": {
        "accept_oracle_download_terms": true
      }
    }
  },
  "run_list": [
    "recipe[apt]",
    "recipe[java]"
  ]
}

将此角色添加到您的节点的 运行-列表中,然后将安装 OracleJDK。


测试 Oracle 安装的 Test Kitchen 项目JDK

以下是一个测试厨房示例,它将针对 ubuntu 和 centos

安装和测试 "java" 角色
├── Berksfile
├── .kitchen.yml
├── roles
│   └── java.json
└── test
    └── integration
        └── default
            └── serverspec
                └── java_spec.rb

安装 chefDK、vagrant 和运行以下命令

kitchen test

备注:

  • 获取测试厨房 运行ning 的最简单方法是同时安装 vagrant and chefdk

伯克斯文件

source "https://supermarket.chef.io"

cookbook "apt"
cookbook "java"

.kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: chef_zero
  require_chef_omnibus: 12.0.3
  client_rb:
    "Ohai::Config[:disabled_plugins] = [:GCE] #": 

platforms:
  - name: ubuntu-12.04
  - name: centos-6.4

suites:
  - name: default
    run_list:
      - role[java]

备注:

  • 特殊角色 "java" 添加到节点 运行-列表。
  • 此示例禁用 "gce" 插件。参见 issue 624

roles/java.json

见上文

test/integration/default/serverspec/java_spec.rb

require 'serverspec'

# Required by serverspec
set :backend, :exec

describe file('/usr/lib/jvm/java-8-oracle-amd64/release'), :if => os[:family] == "ubuntu" do
  it { should contain 'JAVA_VERSION="1.8.0_31"' }
end

describe file('/usr/lib/jvm/java/release'), :if => os[:family] == "redhat" do
  it { should contain 'JAVA_VERSION="1.8.0_31"' }
end