From 5169d0efb9f9e98a388494494260613dcd851b9d Mon Sep 17 00:00:00 2001 From: Jeff Cheng Date: Wed, 24 Mar 2021 12:53:35 -0400 Subject: [PATCH] Fix uninstall option in installer script (#194) --- .../packaging/installer/install.sh | 48 +++++++++++++------ .../packaging/tests/installer_test.py | 1 + 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/internal/buildscripts/packaging/installer/install.sh b/internal/buildscripts/packaging/installer/install.sh index c8f2267986..a429680e47 100755 --- a/internal/buildscripts/packaging/installer/install.sh +++ b/internal/buildscripts/packaging/installer/install.sh @@ -334,7 +334,7 @@ EOH } fluent_plugin_installed() { - local name="1" + local name="$1" td-agent-gem list "$name" --exact | grep -q "$name" } @@ -444,26 +444,44 @@ install() { uninstall() { case "$distro" in ubuntu|debian) - for agent in splunk-otel-collector td-agent; do - if command -v $agent >/dev/null 2>&1; then - apt-get remove $agent 2>&1 - echo "Successfully removed $agent" - else - echo "Unable to locate $agent" + for agent in otelcol td-agent; do + if command -v $agent 2>&1 >/dev/null; then + pkg="$agent" + if [ "$agent" = "otelcol" ]; then + pkg="splunk-otel-collector" + fi + if dpkg -s $pkg 2>&1 >/dev/null; then + apt-get remove -y $pkg 2>&1 + echo "Successfully removed the $pkg package" + else + agent_path="$( command -v agent )" + echo "$agent_path exists but the $pkg package is not installed" >&2 + echo "$agent_path needs to be manually removed/uninstalled" >&2 + exit 1 + fi fi done ;; amzn|centos|ol|rhel) - for agent in splunk-otel-collector td-agent; do - if command -v $agent >/dev/null 2>&1; then - if command -v yum >/dev/null 2>&1; then - yum remove $agent 2>&1 + for agent in otelcol td-agent; do + if command -v $agent 2>&1 >/dev/null; then + pkg="$agent" + if [ "$agent" = "otelcol" ]; then + pkg="splunk-otel-collector" + fi + if rpm -q $pkg 2>&1 >/dev/null; then + if command -v yum >/dev/null 2>&1; then + yum remove -y $pkg 2>&1 + else + dnf remove -y $pkg 2>&1 + fi + echo "Successfully removed the $pkg package" else - dnf remove $agent 2>&1 + agent_path="$( command -v agent )" + echo "$agent_path exists but the $pkg package is not installed" >&2 + echo "$agent_path needs to be manually removed/uninstalled" >&2 + exit 1 fi - echo "Successfully removed $agent" - else - echo "Unable to locate $agent" fi done ;; diff --git a/internal/buildscripts/packaging/tests/installer_test.py b/internal/buildscripts/packaging/tests/installer_test.py index 824322608b..5b393f3183 100644 --- a/internal/buildscripts/packaging/tests/installer_test.py +++ b/internal/buildscripts/packaging/tests/installer_test.py @@ -81,6 +81,7 @@ def test_installer(distro, version, memory_option): else: assert container.exec_run("systemctl status td-agent").exit_code != 0 + run_container_cmd(container, "sh -x /test/install.sh --uninstall") finally: run_container_cmd(container, "journalctl -u td-agent --no-pager") if container.exec_run("test -f /var/log/td-agent/td-agent.log").exit_code == 0: