TDengine/packaging/tools/remove_client.sh
haoranchen c5bf416f4b
fix: install script bugs (chmod 666, insserv typo, process handling) (#35159)
* fix: install script bugs synced from 3.3.6 review

- install.sh: remove chmod 666 /etc/hosts, skip in non-root mode
- install.sh: fix insserv $1} stray brace typo
- install.sh: replace xargs -r with portable while-read in kill_process()
- install_client.sh: fix kill_client() multi-PID handling

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: address AI review - pipeline set -e safety and pgrep -x

- install.sh: add || : to while-read pipeline in kill_process() fallback
- install_client.sh: use pgrep -x for exact match, add || true for set -e safety

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(install): add systemd >= 232 check and linger hint for non-root

Non-root install requires systemd >= 232 for user service support.
Shows clear error on older systems (e.g., CentOS 7 with systemd 219).
Also adds linger reminder after successful non-root install if not enabled.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(packaging): read .install_path on upgrade, clean env on uninstall, fix remove_client non-root

- install.sh: Read .install_path in setup_env() to preserve custom install
  directory on upgrade without -d flag
- remove.sh: Add clean_env_file() to remove PATH/LD_LIBRARY_PATH from shell
  rc file on non-root uninstall
- remove_client.sh: Add user_mode detection, .install_path reading, correct
  paths for non-root, env cleanup. Previously completely broken for non-root.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(install): remove dangerous rm -rf of data/log/cfg link dirs in install_main_path

The rm -rf was introduced in PR #34172 assuming these paths are always
symlinks. When upgrading from older versions that used -d to install data
directly in ${installDir}/data (real directory), this would delete user data.

These rm calls are entirely unnecessary:
- cfg_link_dir and log_link_dir: ln -sf in install_config/install_log
  already overwrites existing symlinks
- data_link_dir: install_data() is NOT called during upgrade, so removing
  the symlink means it won't be recreated

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(install): validate .install_path against dangerous system paths

Reject corrupted .install_path values like '/', '/bin', '/usr', '/etc',
'/var' to prevent install_main_path from operating on system directories.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(uninstall): fix taosgen name mismatch and escape sed regex in clean_env_file

- remove_client.sh: taosgen_name was 'gen' (taosgen) but
  install_client.sh installs it as 'taosgen' (taostaosgen).
  Fixed to match installer naming.
- remove.sh & remove_client.sh: escape regex metacharacters (especially
  '.' in .local paths) when building sed patterns for clean_env_file to
  avoid accidentally removing unrelated export lines.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(install): use suffix check instead of blacklist for .install_path validation

Replace weak blacklist (/bin, /usr, /etc, /var) with a stronger check:
path must end with '/' (e.g., /taos). This is a natural whitelist
since install always sets installDir="${taosDir}/${PREFIX}", so any
legitimate .install_path will end with /taos.

Rejects /home, /opt, /root, /usr/local, or any other polluted value.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-21 14:06:20 +08:00

202 lines
6 KiB
Bash
Executable file

#!/bin/bash
#
# Script to stop the client and uninstall database, but retain the config and log files.
set -e
# set -x
RED='\033[0;31m'
GREEN='\033[1;32m'
NC='\033[0m'
verMode=edge
PREFIX="taos"
clientName="taos"
uninstallScript="rmtaos"
clientName2="taos"
productName2="TDengine"
productName="TDengine TSDB"
benchmarkName2="${clientName2}Benchmark"
demoName2="${clientName2}demo"
dumpName2="${clientName2}dump"
inspect_name="${clientName2}inspect"
taosgen_name="${clientName2}taosgen"
uninstallScript2="rm${clientName2}"
# User mode detection and path setup
if [ "$(id -u)" -ne 0 ]; then
user_mode=1
installDir="$HOME/${clientName2}"
bin_link_dir="$HOME/.local/bin"
lib_link_dir="$HOME/.local/lib"
lib64_link_dir="$HOME/.local/lib64"
inc_link_dir="$HOME/.local/include"
log_dir="$HOME/${clientName2}/log"
cfg_dir="$HOME/${clientName2}/cfg"
csudo=""
else
user_mode=0
installDir="/usr/local/${clientName2}"
bin_link_dir="/usr/bin"
lib_link_dir="/usr/lib"
lib64_link_dir="/usr/lib64"
inc_link_dir="/usr/include"
log_dir="/var/log/${clientName2}"
cfg_dir="/etc/${clientName2}"
csudo=""
if command -v sudo >/dev/null; then
csudo="sudo "
fi
fi
# Install path discovery from .install_path
_script_real="$(readlink -f "$0" 2>/dev/null || echo "$0")"
_script_dir="$(dirname "$_script_real")"
_guessed_dir=""
if [[ "$_script_dir" == */bin ]]; then
_guessed_dir="${_script_dir%/bin}"
elif [[ "$_script_dir" == */${clientName2} ]]; then
_guessed_dir="$_script_dir"
fi
if [ -n "${_guessed_dir:-}" ] && [ -f "${_guessed_dir}/.install_path" ]; then
installDir=$(cat "${_guessed_dir}/.install_path")
elif [ -f "/usr/local/${clientName2}/.install_path" ] && [ "$user_mode" -eq 0 ]; then
installDir=$(cat "/usr/local/${clientName2}/.install_path")
elif [ -f "$HOME/${clientName2}/.install_path" ] && [ "$user_mode" -eq 1 ]; then
installDir=$(cat "$HOME/${clientName2}/.install_path")
fi
install_main_dir=${installDir}
log_link_dir=${installDir}/log
cfg_link_dir=${installDir}/cfg
function kill_client() {
pid=$(ps -C ${clientName2} | grep -w ${clientName2} | grep -v $uninstallScript2 | awk '{print $1}')
if [ -n "$pid" ]; then
${csudo}kill -9 $pid || :
fi
}
function clean_bin() {
# Remove links
${csudo}rm -f ${bin_link_dir}/${clientName2} || :
${csudo}rm -f ${bin_link_dir}/${demoName2} || :
${csudo}rm -f ${bin_link_dir}/${benchmarkName2} || :
${csudo}rm -f ${bin_link_dir}/${dumpName2} || :
${csudo}rm -f ${bin_link_dir}/${uninstallScript2} || :
${csudo}rm -f ${bin_link_dir}/set_core || :
[ -L ${bin_link_dir}/${inspect_name} ] && ${csudo}rm -f ${bin_link_dir}/${inspect_name} || :
[ -L ${bin_link_dir}/${taosgen_name} ] && ${csudo}unlink ${bin_link_dir}/${taosgen_name} || :
if [ "$verMode" == "cluster" ] && [ "$clientName" != "$clientName2" ]; then
${csudo}rm -f ${bin_link_dir}/${clientName2} || :
${csudo}rm -f ${bin_link_dir}/${demoName2} || :
${csudo}rm -f ${bin_link_dir}/${benchmarkName2} || :
${csudo}rm -f ${bin_link_dir}/${dumpName2} || :
${csudo}rm -f ${bin_link_dir}/${uninstallScript2} || :
[ -L ${bin_link_dir}/${inspect_name} ] && ${csudo}rm -f ${bin_link_dir}/${inspect_name} || :
[ -L ${bin_link_dir}/${taosgen_name} ] && ${csudo}unlink ${bin_link_dir}/${taosgen_name} || :
fi
}
function clean_lib() {
# Remove links in lib and lib64 directories
for dir in "${lib_link_dir}" "${lib64_link_dir}"; do
if [ -d "$dir" ]; then
for pattern in "libtaos.*" "libtaosnative.*" "libtaosws.*"; do
${csudo}find "$dir" -name "$pattern" -exec ${csudo}rm -f {} \; || :
done
fi
done
}
function clean_header() {
# Remove link
${csudo}rm -f ${inc_link_dir}/taos.h || :
${csudo}rm -f ${inc_link_dir}/taosdef.h || :
${csudo}rm -f ${inc_link_dir}/taoserror.h || :
${csudo}rm -f ${inc_link_dir}/tdef.h || :
${csudo}rm -f ${inc_link_dir}/taosudf.h || :
${csudo}rm -f ${inc_link_dir}/taosws.h || :
}
function clean_config() {
# Remove link
${csudo}rm -f ${cfg_link_dir}/* || :
}
function clean_log() {
# Remove link
${csudo}rm -rf ${log_link_dir} || :
}
function clean_config_and_log_dir() {
# Remove link
echo "Do you want to remove all the log and configuration files? [y/n]"
read answer
if [ X$answer == X"y" ] || [ X$answer == X"Y" ]; then
confirmMsg="I confirm that I would like to delete all log and configuration files"
echo "Please enter '${confirmMsg}' to continue"
read answer
if [ X"$answer" == X"${confirmMsg}" ]; then
# Remove dir
rm -rf ${cfg_dir} || :
rm -rf ${log_dir} || :
else
echo "answer doesn't match, skip this step"
fi
fi
}
# Stop client.
kill_client
# Remove binary file and links
clean_bin
# Remove header file.
clean_header
# Remove lib file
clean_lib
# Remove link log directory
clean_log
# Remove link configuration file
clean_config
# Remove dir
clean_config_and_log_dir
${csudo}rm -rf ${install_main_dir}
# Clean env variables from shell rc file for non-root uninstall
function clean_env_file() {
if [ "$user_mode" -ne 1 ]; then
return 0
fi
local env_file=""
local login_shell="${SHELL##*/}"
if [ "$login_shell" = "zsh" ]; then
env_file="${HOME}/.zshrc"
elif [ "$login_shell" = "bash" ] || [ -z "$login_shell" ]; then
env_file="${HOME}/.bashrc"
else
env_file="${HOME}/.profile"
fi
if [ ! -f "$env_file" ]; then
return 0
fi
local tmp_file="${env_file}.tmp.$$"
local escaped_bin escaped_lib
escaped_bin=$(printf '%s' "${bin_link_dir}" | sed 's/[.[\\/^$*]/\\&/g')
escaped_lib=$(printf '%s' "${lib_link_dir}" | sed 's/[.[\\/^$*]/\\&/g')
sed -e "/^# ${productName} install path$/d" \
-e "\|^export PATH=\"${escaped_bin}:.*\"|d" \
-e "\|^export LD_LIBRARY_PATH=\"${escaped_lib}:.*\"|d" \
"$env_file" > "$tmp_file" && mv "$tmp_file" "$env_file" || rm -f "$tmp_file"
}
clean_env_file
echo -e "${GREEN}${productName2} client is removed successfully!${NC}"
echo