自定义 Chef 资源以在 ubuntu 中启动 newrelic-infra

Custom Chef Resource to start newrelic-infra in ubuntu

最初我开始为我的项目寻找 Configure New Relic Infrastructure using Chef to setup newrelic infra chef cookbook 使用 chef solo 的项目,经过一些研究我发现食谱中的依赖项不再受支持。

所以我决定在食谱中编写一个自定义资源,这样我就可以利用 Chef 的幂等性。

我已经在 ubuntu 框中尝试 Install for Ubuntu 下的 following steps 并验证了新的 relic-infra 安装:

现在我正在尝试编写这样的厨师资源:

第 1 步:创建配置文件,并添加您的许可证密钥:

echo "license_key: YOUR_LICENSE_KEY" | sudo tee -a /etc/newrelic-infra.yml

在我的第 1 步食谱中添加了这个资源块:

file '/etc/newrelic-infra.yml' do
  content 'license_key: added_key_here'
  mode '0755'
  owner 'root'
  group 'root'
end

第 2 步:启用 New Relic 的 GPG 密钥:

curl https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg | sudo apt-key add -

在我的第 2 步食谱中添加了这个资源块:

apt_repository 'newrelic_key' do
  uri 'https://download.newrelic.com/infrastructure_agent/gpg'
  trusted true
  key 'https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg'
end

我通过使用以下命令列出密钥在本地框中验证了此步骤:

sudo apt-key list

第 3 步:使用适用于您的分发版本的命令创建代理的 apt 存储库:

printf "deb [arch=amd64] https://download.newrelic.com/infrastructure_agent/linux/apt bionic main" | sudo tee -a /etc/apt/sources.list.d/newrelic-infra.list

在我的第 3 步食谱中添加了这个资源块:

file '/etc/apt/sources.list.d/newrelic-infra.list' do
  content 'deb [arch=amd64] https://download.newrelic.com/infrastructure_agent/linux/apt bionic main'
  mode '0755'
  owner 'root'
  group 'root'
end

第 4 步:更新您的 apt 缓存和 运行 安装脚本:

sudo apt-get update
sudo apt-get install newrelic-infra -y 

在我的第 4 步食谱中添加了这个资源块:

apt_update
apt_package 'newrelic-infra'

错误:

但是安装失败并出现以下错误:

===============================================================================
    default:       Error executing action `update` on resource 'apt_update[newrelic-infra]'
    default:       ================================================================================
    default:       
    default:       Mixlib::ShellOut::ShellCommandFailed
    default:       ------------------------------------
    default:       execute[apt-get -q update] (/opt/chefdk/embedded/lib/ruby/gems/2.5.0/gems/chef-14.4.56/lib/chef/provider/apt_update.rb line 70) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '100'
    default:       ---- Begin output of ["apt-get", "-q", "update"] ----
    default:       STDOUT: 
    default:       
    default: STDERR: E: Malformed entry 1 in list file /etc/apt/sources.list.d/newrelic-infra.list (Component)
    default: 

  default:   
    default: * apt_package[newrelic-infra] action install
    default:     * No candidate version available for newrelic-infra
    default: 
    default:     
    default: ================================================================================
    default:     
    default: Error executing action `install` on resource 'apt_package[newrelic-infra]'
    default:     
    default: ================================================================================
    default:     
    default: 
    default: 
    default:     
    default: Chef::Exceptions::Package
    default:     -------------------------
    default:     No candidate version available for newrelic-infra
    default:     
    default:     Resource Declaration:
    default:     ---------------------
    default:     # In /etc/chef/local-mode-cache/cache/cookbooks/repo/recipes/default.rb
    default:     
    default:      38: apt_package 'newrelic-infra'
    default:      39: 
    default:     
    default:     Compiled Resource:
    default:     ------------------
    default:     # Declared in /etc/chef/local-mode-cache/cache/cookbooks/repo/recipes/default.rb:38:in `from_file'
    default:     
    default:     apt_package("newrelic-infra") do
    default:       package_name "newrelic-infra"
    default:       action [:install]
    default:       default_guard_interpreter :default
    default:     
    default:   declared_type :apt_package
    default: 
    default:     
    default:   cookbook_name "repo"
    default:       recipe_name "default"
    default:     end
    default:     
    default:     System Info:
    default:     ------------
    default:     chef_version=14.4.56
    default:     platform=ubuntu
    default:     platform_version=18.04
    default:     ruby=ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
    default:     program_name=/usr/bin/chef-solo
    default:     executable=/opt/chefdk/bin/chef-solo
    default:     
    default: 
    default: Running handlers:
    default: [2019-08-30T18:19:30+00:00] ERROR: Running exception handlers
    default: Running handlers complete
    default: [2019-08-30T18:19:30+00:00] ERROR: Exception handlers complete
    default: Chef Client failed. 6 resources updated in 48 seconds
    default: [2019-08-30T18:19:30+00:00] FATAL: Stacktrace dumped to /etc/chef/local-mode-cache/cache/chef-stacktrace.out
    default: [2019-08-30T18:19:30+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
    default: [2019-08-30T18:19:30+00:00] FATAL: Chef::Exceptions::Package: apt_package[newrelic-infra] (repo-deploy::default line 38) had an error: Chef::Exceptions::Package: No candidate version available for newrelic-infra

我 运行 我的 vag运行t 文件,它对每个步骤都有效,但在最后的安装步骤中失败...我做错了什么?任何故障排除提示都会有所帮助。谢谢!

首先,您可以利用 archdistribution.

使用 apt_repository 将步骤 2 和步骤 3 组合在一起

如果您阅读 apt_repository 文档,您会发现您甚至可以在第 4 步中删除 apt update

Adding a new repository will update the APT package cache immediately.

其次,回到你的问题...

通过查看您的日志,特别是

default:       execute[apt-get -q update] (/opt/chefdk/embedded/lib/ruby/gems/2.5.0/gems/chef-14.4.56/lib/chef/provider/apt_update.rb line 70) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '100'

您的节点上似乎没有 运行 chef-client,而是使用 chef-dk。确保您 运行ning chef-client(或现在的 chef infra 客户端)在您想要收敛的节点内。

您可以 运行 chef-client 在更高的日志级别通过指定 log_level

来揭示更多关于执行的信息

The level of logging to be stored in a log file. Possible levels: auto (default), debug, info, warn, error, or fatal. Default value: warn (when a terminal is available) or info (when a terminal is not available).

希望能帮到您解决问题

再次感谢@Mr 提供您的意见。如果有人想知道新遗迹基础设施的自定义资源,请看这里:

file '/etc/newrelic-infra.yml' do
  content 'license_key: added my license key here'
  mode '0755'
  owner 'root'
  group 'root'
end

apt_repository 'newrelic-infra' do
  uri 'https://download.newrelic.com/infrastructure_agent/linux/apt'
  trusted true
  arch 'amd64'
  components ['main']
  distribution 'bionic'
  key 'https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg'
end