Autodetect agent_major_version from agent_version
MODIFY
attributes/default.rb
attributes/default.rb
@@ -29,7 +29,7 @@ default['datadog']['api_key'] = nil
default['datadog']['application_key'] = nil
# Agent major version
-default['datadog']['agent_major_version'] = 7
+default['datadog']['agent_major_version'] = nil # nil to autodetect based on 'agent_version'
# Agent Version
# Default of `nil` will install latest version. On Windows, this will also upgrade to latest

MODIFY
libraries/recipe_helpers.rb
libraries/recipe_helpers.rb
@@ -2,6 +2,37 @@ class Chef
# Helper class for Datadog Chef recipes
class Datadog
class << self
+
+ def agent_version(node)
+ dd_agent_version = node['datadog']['agent_version']
+ if dd_agent_version.respond_to?(:each_pair)
+ platform_family = node['platform_family']
+ # Unless explicitly listed, treat fedora and amazon as rhel
+ if !dd_agent_version.include?(platform_family) && ['fedora', 'amazon'].include?(platform_family)
+ platform_family = 'rhel'
+ end
+ dd_agent_version = dd_agent_version[platform_family]
+ end
+ return dd_agent_version
+ end
+
+ def agent_major_version(node)
+ # user-specified values
+ agent_major_version = node['datadog']['agent_major_version']
+ agent_version = agent_version(node)
+
+ if !agent_version.nil?
+ epoch, major, minor, patch, suffix, release = agent_version.match(/([0-9]+:)?([0-9]+)\.([0-9]+)\.([0-9]+)([^-\s]+)?(?:-([0-9]+))?/).captures
+ if !agent_major_version.nil? && major.to_i != agent_major_version.to_i
+ raise "Provided (#{agent_major_version}) and deduced (#{major}) agent_major_version don't match"
+ end
+ return major.to_i
+ elsif !agent_major_version.nil?
+ return agent_major_version.to_i
+ end
+ return 7 #default
+ end
+
def api_key(node)
run_state_or_attribute(node, 'api_key')
end

MODIFY
recipes/_install-linux.rb
recipes/_install-linux.rb
# Install the Apt/Yum repository if enabled
include_recipe 'datadog::repository' if node['datadog']['installrepo']
-dd_agent_version = node['datadog']['agent_version']
-if dd_agent_version.respond_to?(:each_pair)
- platform_family = node['platform_family']
- # Unless explicitly listed, treat fedora and amazon as rhel
- if !dd_agent_version.include?(platform_family) && ['fedora', 'amazon'].include?(platform_family)
- platform_family = 'rhel'
- end
- dd_agent_version = dd_agent_version[platform_family]
-end
+dd_agent_version = Chef::Datadog.agent_version(node)
package_action = node['datadog']['agent_package_action']

MODIFY
recipes/_install-windows.rb
recipes/_install-windows.rb
# limitations under the License.
#
-dd_agent_version = node['datadog']['agent_version']
-if dd_agent_version.respond_to?(:each_pair)
- dd_agent_version = dd_agent_version['windows']
-end
+dd_agent_version = Chef::Datadog.agent_version(node)
if dd_agent_version.nil?
# Use latest
- agent_major_version = node['datadog']['agent_major_version']
- dd_agent_installer_basename = (agent_major_version.to_i == 5) ? 'ddagent-cli-latest' : "datadog-agent-#{agent_major_version}-latest.amd64"
+ agent_major_version = Chef::Datadog.agent_major_version(node)
+ dd_agent_installer_basename = (agent_major_version == 5) ? 'ddagent-cli-latest' : "datadog-agent-#{agent_major_version}-latest.amd64"
else
dd_agent_installer_prefix = (node['datadog']['windows_agent_installer_prefix'] || 'ddagent-cli')
dd_agent_installer_basename = "#{dd_agent_installer_prefix}-#{dd_agent_version}"

MODIFY
recipes/dd-agent.rb
recipes/dd-agent.rb
@@ -54,7 +54,7 @@ agent_start = node['datadog']['agent_start'] ? :start : :stop
# To add integration-specific configurations, add 'datadog::config_name' to
# the node's run_list and set the relevant attributes
#
-if node['datadog']['agent_major_version'].to_i > 5
+if Chef::Datadog.agent_major_version(node) > 5
include_recipe 'datadog::_agent6_config'
agent_config_dir = is_windows ? "#{ENV['ProgramData']}/Datadog" : '/etc/datadog-agent'
directory agent_config_dir do
@@ -123,7 +123,7 @@ end
# Common configuration
service_provider = nil
-if node['datadog']['agent_major_version'].to_i > 5 &&
+if Chef::Datadog.agent_major_version(node) > 5 &&
(((node['platform'] == 'amazon' || node['platform_family'] == 'amazon') && node['platform_version'].to_i != 2) ||
(node['platform'] == 'ubuntu' && node['platform_version'].to_f < 15.04) || # chef <11.14 doesn't use the correct service provider
(node['platform'] != 'amazon' && node['platform_family'] == 'rhel' && node['platform_version'].to_i < 7))
@@ -156,7 +156,7 @@ if system_probe_managed
ruby_block 'include system-probe' do
block do
# only load system-probe recipe if an agent 6/7 installation comes with it
- system_probe_supported = !is_windows && node['datadog']['agent_major_version'].to_i > 5
+ system_probe_supported = !is_windows && Chef::Datadog.agent_major_version(node) > 5
system_probe_installed = ::File.exist?('/opt/datadog-agent/embedded/bin/system-probe')
if system_probe_supported && system_probe_installed
run_context.include_recipe 'datadog::system-probe'

MODIFY
recipes/repository.rb
recipes/repository.rb
@@ -25,6 +25,8 @@ yum_a5_architecture_map = {
}
yum_a5_architecture_map.default = 'x86_64'
+agent_major_version = Chef::Datadog.agent_major_version(node)
+
case node['platform_family']
when 'debian'
include_recipe 'apt'
@@ -34,7 +36,7 @@ when 'debian'
action :install
end
- case node['datadog']['agent_major_version'].to_i
+ case agent_major_version
when 7
components = ['7']
when 6
@@ -42,7 +44,7 @@ when 'debian'
when 5
components = ['main']
else
- Chef::Log.error('agent_major_version not supported.')
+ Chef::Log.error("agent_major_version '#{agent_major_version}' not supported.")
end
retries = node['datadog']['aptrepo_retries']
@@ -92,13 +94,13 @@ when 'rhel', 'fedora', 'amazon'
else
# Older versions of yum embed M2Crypto with SSL that doesn't support TLS1.2
yum_protocol_a5 = node['platform_family'] == 'rhel' ? 'http' : 'https'
- case node['datadog']['agent_major_version'].to_i
+ case agent_major_version
when 6, 7
- baseurl = "https://yum.datadoghq.com/stable/#{node['datadog']['agent_major_version']}/#{node['kernel']['machine']}/"
+ baseurl = "https://yum.datadoghq.com/stable/#{agent_major_version}/#{node['kernel']['machine']}/"
when 5
baseurl = "#{yum_protocol_a5}://yum.datadoghq.com/rpm/#{yum_a5_architecture_map[node['kernel']['machine']]}/"
else
- Chef::Log.error('agent_major_version not supported.')
+ Chef::Log.error("agent_major_version '#{agent_major_version}' not supported.")
end
end
@@ -109,7 +111,7 @@ when 'rhel', 'fedora', 'amazon'
proxy node['datadog']['yumrepo_proxy']
proxy_username node['datadog']['yumrepo_proxy_username']
proxy_password node['datadog']['yumrepo_proxy_password']
- gpgkey node['datadog']['agent_major_version'].to_i < 7 ? node['datadog']['yumrepo_gpgkey'] : node['datadog']['yumrepo_gpgkey_new']
+ gpgkey agent_major_version < 7 ? node['datadog']['yumrepo_gpgkey'] : node['datadog']['yumrepo_gpgkey_new']
gpgcheck true
action :create
end
@@ -151,13 +153,13 @@ when 'suse'
if !node['datadog']['yumrepo_suse'].nil?
baseurl = node['datadog']['yumrepo_suse']
else
- case node['datadog']['agent_major_version'].to_i
+ case agent_major_version
when 6, 7
- baseurl = "https://yum.datadoghq.com/suse/stable/#{node['datadog']['agent_major_version']}/#{node['kernel']['machine']}/"
+ baseurl = "https://yum.datadoghq.com/suse/stable/#{agent_major_version}/#{node['kernel']['machine']}/"
when 5
baseurl = "https://yum.datadoghq.com/suse/rpm/#{yum_a5_architecture_map[node['kernel']['machine']]}/"
else
- Chef::Log.error('agent_major_version not supported.')
+ Chef::Log.error("agent_major_version '#{agent_major_version}' not supported.")
end
end
@@ -165,7 +167,7 @@ when 'suse'
zypper_repository 'datadog' do
description 'datadog'
baseurl baseurl
- gpgkey node['datadog']['agent_major_version'].to_i < 7 ? node['datadog']['yumrepo_gpgkey'] : node['datadog']['yumrepo_gpgkey_new']
+ gpgkey agent_major_version < 7 ? node['datadog']['yumrepo_gpgkey'] : node['datadog']['yumrepo_gpgkey_new']
gpgautoimportkeys false
gpgcheck false
action :create

MODIFY
recipes/system-probe.rb
recipes/system-probe.rb
@@ -51,7 +51,7 @@ end
# Common configuration
service_provider = nil
-if node['datadog']['agent_major_version'].to_i > 5 &&
+if Chef::Datadog.agent_major_version(node) > 5 &&
(((node['platform'] == 'amazon' || node['platform_family'] == 'amazon') && node['platform_version'].to_i != 2) ||
(node['platform'] == 'ubuntu' && node['platform_version'].to_f < 15.04) || # chef <11.14 doesn't use the correct service provider
(node['platform'] != 'amazon' && node['platform_family'] == 'rhel' && node['platform_version'].to_i < 7))

MODIFY
resources/integration.rb
resources/integration.rb
@@ -12,7 +12,7 @@ property :property_name, String, name_property: true
property :version, String, required: true
action :install do
- if node['datadog']['agent_major_version'].to_i == 5
+ if Chef::Datadog.agent_major_version(node) == 5
Chef::Log.error('The datadog_integration resource is not available with Agent v5.')
return
end
@@ -31,7 +31,7 @@ action :install do
end
action :remove do
- if node['datadog']['agent_major_version'].to_i == 5
+ if Chef::Datadog.agent_major_version(node) == 5
Chef::Log.error('The datadog_integration resource is not available with Agent v5.')
return
end

MODIFY
resources/monitor.rb
resources/monitor.rb
@@ -19,7 +19,7 @@ action :add do
template ::File.join(yaml_dir, "#{new_resource.name}.yaml") do
# On Windows Agent v5, set the permissions on conf files to Administrators.
if node['platform_family'] == 'windows'
- if node['datadog']['agent_major_version'].to_i > 5
+ if Chef::Datadog.agent_major_version(node) > 5
inherits true # Agent 6/7 rely on inheritance being enabled. Reset it in case it was disabled when installing Agent 5.
else
owner 'Administrators'
@@ -54,7 +54,7 @@ action :remove do
end
def yaml_dir
- is_agent5 = node['datadog']['agent_major_version'].to_i == 5
+ is_agent5 = Chef::Datadog.agent_major_version(node) == 5
is_windows = node['platform_family'] == 'windows'
if is_windows
"#{ENV['ProgramData']}/Datadog/conf.d"

Autodetect agent_major_version from agent_version