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

标签 ruby ubuntu chef-infra erb chef-solo

最初我开始查看 Configure New Relic Infrastructure using Chef设置newrelic infra chef cookbook对于我使用 chef solo 的项目,经过一些研究,我发现 Recipe 中的依赖项不再受支持。

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

我试过following steps 下为 Ubuntu 安装 在我的 ubuntu 盒子中并验证了新的 relic-infra 安装:

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

Step1:创建一个配置文件,并添加您的许可证 key :

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

在我的第 1 步配方中添加了此资源 block :
file '/etc/newrelic-infra.yml' do
  content 'license_key: added_key_here'
  mode '0755'
  owner 'root'
  group 'root'
end

Step2:启用New Relic的GPG key :
curl https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg | sudo apt-key add -

在我的第 2 步配方中添加了此资源 block :
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

我通过使用以下命令列出 key 在本地框中验证了此步骤:
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 步配方中添加了此资源 block :
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 步配方中添加了此资源 block :
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

我运行了我的流浪文件,它成功地适用于每一步,但在最后的安装步骤中失败了......我做错了什么?任何故障排除提示都会有所帮助。谢谢!

最佳答案

首先,您可以使用 apt_repository 将步骤 2 和 3 组合在一起通过使用 archdistribution .

如果您阅读 apt_repository文档,您可以看到您甚至可以删除 apt update在第 4 步

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。确保您正在运行 chef-client (或现在的 Chef 基础设施客户端)在您想要聚合的节点内。

你可以运行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).



我希望它能帮助你解决你的问题

关于ruby - 自定义 Chef 资源在 ubuntu 中启动 newrelic-infra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731697/

相关文章:

git - 如何在 git 中为 berkshelf 设置 Recipe ?

ubuntu - 使用 Chef 和 Knife 引导一个 ubuntu ec2 实例

ruby-on-rails - 如何使用 Chef 预编译 Assets ?

ruby - 我如何将参数从命令行传递给rake然后rspec

linux - 我的 AWS 服务器(debian 或 Linux)需要什么包格式

ubuntu - 在 Ubuntu : ERROR: cannot find a JRE or JDK 上设置 Artifactory

ubuntu - 如何让 Hyper V 检测我的 Ubuntu VM 的 IP 地址

javascript - Rails 递归地包含 javascripts Assets 文件夹

Ruby - 用于速率限制的访问响应 header (Help Scout)

ruby-on-rails - 如何在 RSpec 中重用上下文?