rubyubuntuchef-infraerbchef-solo

Custom Chef Resource to start newrelic-infra in ubuntu


Initially I started looking at Configure New Relic Infrastructure using Chef to setup newrelic infra chef cookbook for my project using chef solo, after some research I found that the dependencies in the cookbook is no longer supported.

So I decided to write a custom resource in the recipe so I can utilize chef idempotency.

I have tried the following steps under Install for Ubuntu in my ubuntu box and verified new relic-infra installation:

Now I am trying write a chef resource like this:

Step1: Create a configuration file, and add your license key:

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

Added this resource block in my recipe for Step1:

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

Step2: Enable New Relic's GPG key:

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

Added this resource block in my recipe for Step2:

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

I verified this step in the local box by listing the keys using this command:

sudo apt-key list

Step3: Create the agent's apt repo using the command for your distribution version:

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

Added this resource block in my recipe for Step3:

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

Step4: Update your apt cache and Run the install script:

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

Added this resource block in my recipe for Step4:

apt_update
apt_package 'newrelic-infra'

Error:

But the install fails with the following error:

===============================================================================
    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

I ran my vagrant file and it works for every step successfully but fails in the final install step... What am I doing wrong? Any troubleshooting tips would be helpful. Thanks!


Solution

  • firstly, you can combine together steps 2 and 3 using apt_repository by utilizing the arch and distribution.

    if you read apt_repository documentation, you can see that you can even drop the apt update in step 4

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

    secondly, back to your question...

    from looking at your logs, specifically

    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'
    

    it appears that you do not run chef-client on your node, rather you are using chef-dk. make sure that you are running chef-client (or chef infra client nowadays) within the node that you like to converge.

    you can run chef-client in higher log level to reveal more about the execution by specifying the 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).

    i hope it will help you solving your issue