Remove source file and datadog-signing-keys package when removing the Agent
MODIFY
libraries/recipe_helpers.rb
libraries/recipe_helpers.rb
@@ -21,6 +21,13 @@ class Chef
datadog-iot-agent
].freeze
+ # This method stores a variable that is used across recipes so we needed a place to define it.
+ # Global variables like this are usually set in the attributes/default.rb file for users to edit them.
+ # We don't want this variable to be changed by the user though, hence the place.
+ def apt_sources_list_file
+ '/etc/apt/sources.list.d/datadog.list'
+ end
+
def chef_version_ge?(version)
Gem::Requirement.new(">= #{version}").satisfied_by?(Gem::Version.new(Chef::VERSION))
end

MODIFY
recipes/remove-dd-agent.rb
recipes/remove-dd-agent.rb
# On Windows, this recipe works only with Chef >= 12.6
+# Importing apt_sources_list_file variable from recipe_helpers.rb
+apt_sources_list_file = Chef::Datadog.apt_sources_list_file
+
case node['os']
when 'linux'
+ # First remove the datadog-agent package
package 'datadog-agent' do
action :purge
end
+
+ # Then remove the installation files (depending on the OS: sources_list file, datadog-signing-keys package...)
+ case node['platform_family']
+ when 'amazon', 'rhel', 'fedora' # 'rhel' includes redhat, centos, rocky, scientific and almalinux
+ yum_repository 'datadog' do
+ action :remove
+ end
+ when 'debian' # 'debian' includes debian and ubuntu
+ apt_package 'datadog-signing-keys' do
+ action :purge
+ end
+ file apt_sources_list_file do
+ action :delete
+ end
+ when 'suse'
+ zypper_repository 'datadog' do
+ action :remove
+ end
+ end
+
when 'windows'
+ # Remove the datadog-agent package (no need to remove other files as for Linux)
package 'Datadog Agent' do
action :remove
end

MODIFY
recipes/repository.rb
recipes/repository.rb
@@ -37,7 +37,8 @@ apt_gpg_keys = {
}
apt_trusted_d_keyring = '/etc/apt/trusted.gpg.d/datadog-archive-keyring.gpg'
apt_usr_share_keyring = '/usr/share/keyrings/datadog-archive-keyring.gpg'
-apt_sources_list_file = '/etc/apt/sources.list.d/datadog.list'
+# Importing apt_sources_list_file variable from recipe_helpers.rb
+apt_sources_list_file = Chef::Datadog.apt_sources_list_file
apt_repo_uri = 'https://apt.datadoghq.com'
# DATADOG_RPM_KEY_CURRENT always contains the key that is used to sign repodata and latest packages

ADD
spec/remove-dd-agent_spec.rb
+# Copyright:: 2011-Present, Datadog
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'spec_helper'
+
+# Importing apt_sources_list_file variable from recipe_helpers.rb
+apt_sources_list_file = Chef::Datadog.apt_sources_list_file
+
+describe 'datadog::remove-dd-agent' do
+ context 'when on Ubuntu' do
+ let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '20.04').converge(described_recipe) }
+
+ it 'purges the datadog-agent package' do
+ expect(chef_run).to purge_package('datadog-agent')
+ end
+
+ it 'removes apt_sources_list_file and purges the datadog-signing-keys package' do
+ expect(chef_run).to purge_apt_package('datadog-signing-keys')
+ expect(chef_run).to delete_file(apt_sources_list_file)
+ end
+ end
+
+ context 'when on Amazon Linux' do
+ let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'amazon', version: '2').converge(described_recipe) }
+
+ it 'purges the datadog-agent package' do
+ expect(chef_run).to purge_package('datadog-agent')
+ end
+
+ it 'removes the datadog yum repository' do
+ expect(chef_run).to remove_yum_repository('datadog')
+ end
+ end
+
+ context 'when on Suse' do
+ let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'suse', version: '12.5').converge(described_recipe) }
+
+ it 'purges the datadog-agent package' do
+ expect(chef_run).to purge_package('datadog-agent')
+ end
+
+ it 'removes the datadog zypper repository' do
+ expect(chef_run).to remove_zypper_repository('datadog')
+ end
+ end
+
+ context 'when on Windows' do
+ let(:chef_run) { ChefSpec::SoloRunner.new(platform: 'windows', version: '2016').converge(described_recipe) }
+
+ it 'removes the Datadog Agent package' do
+ expect(chef_run).to remove_package('Datadog Agent')
+ end
+ end
+end

Remove source file and datadog-signing-keys package when removing the Agent (#865)