fix(agent): improve install script command handling and add agent running check
Some checks failed
Docker Image CI (nightly) / build-nightly (push) Has been cancelled
Docker Image CI (nightly) / build-nightly-agent (push) Has been cancelled

This commit is contained in:
yusing 2025-05-17 08:51:22 +08:00
parent 78be9b1c71
commit 67a6b89ea5

View file

@ -2,6 +2,8 @@
set -e set -e
COMMAND="$1"
check_pkg() { check_pkg() {
if ! command -v "$1" &>/dev/null; then if ! command -v "$1" &>/dev/null; then
echo "$1 could not be found, please install it first" echo "$1 could not be found, please install it first"
@ -12,7 +14,7 @@ check_pkg() {
start-service() { start-service() {
systemctl daemon-reload systemctl daemon-reload
# if command is empty # if command is empty
if [ -z "$1" ]; then if [ -z "$COMMAND" ]; then
echo "Enabling and starting the agent service" echo "Enabling and starting the agent service"
else else
echo "Reloading the agent service" echo "Reloading the agent service"
@ -29,7 +31,7 @@ start-service() {
exit 1 exit 1
fi fi
# if command is empty # if command is empty
if [ -z "$1" ]; then if [ -z "$COMMAND" ]; then
echo "Agent installed successfully" echo "Agent installed successfully"
else else
echo "Agent updated successfully" echo "Agent updated successfully"
@ -55,7 +57,7 @@ else
fi fi
# check variables if command is empty # check variables if command is empty
if [ -z "$1" ]; then if [ -z "$COMMAND" ]; then
if [ -z "$AGENT_NAME" ]; then if [ -z "$AGENT_NAME" ]; then
echo "AGENT_NAME is not set" echo "AGENT_NAME is not set"
exit 1 exit 1
@ -113,7 +115,7 @@ if [ ! -w "$(dirname "$env_file")" ]; then
fi fi
# check if command is uninstall # check if command is uninstall
if [ "$1" = "uninstall" ]; then if [ "$COMMAND" = "uninstall" ]; then
echo "Uninstalling the agent" echo "Uninstalling the agent"
systemctl disable --now $name || true systemctl disable --now $name || true
rm -f $bin_path rm -f $bin_path
@ -139,13 +141,17 @@ bin_last_updated=$(date -d "$(echo "$asset" | jq -r '.updated_at')" +%s)
if [ -f "$bin_path" ]; then if [ -f "$bin_path" ]; then
bin_mod_time=$(stat -c %Y "$bin_path") bin_mod_time=$(stat -c %Y "$bin_path")
if [ "$bin_last_updated" -eq "$bin_mod_time" ]; then if [ "$bin_last_updated" -eq "$bin_mod_time" ]; then
echo "Binary is up to date" echo "Binary is already up to date, continue? (y/n)"
read -n 1 -r
if [ "$REPLY" != "y" ] && [ "$REPLY" != "Y" ]; then
echo "Aborting"
exit 0 exit 0
fi fi
fi fi
fi
# check if command is update # check if command is update
if [ "$1" = "update" ]; then if [ "$COMMAND" = "update" ]; then
echo "Stopping the agent" echo "Stopping the agent"
systemctl stop $name || true systemctl stop $name || true
fi fi
@ -156,6 +162,19 @@ if [ -z "$bin_url" ] || [ "$bin_url" = "null" ]; then
exit 1 exit 1
fi fi
# check if agent is already running
if systemctl is-active $name &>/dev/null; then
echo "Agent is already running, stopping it"
systemctl stop $name || true
sleep 1
if systemctl is-active $name &>/dev/null; then
echo "Agent is still running, please stop it manually"
exit 1
else
echo "Agent stopped successfully"
fi
fi
echo "Downloading the agent binary from $bin_url" echo "Downloading the agent binary from $bin_url"
if ! curl -L -f "$bin_url" -o $bin_path; then if ! curl -L -f "$bin_url" -o $bin_path; then
echo "Failed to download binary" echo "Failed to download binary"
@ -169,7 +188,7 @@ echo "Making the agent binary executable"
chmod +x $bin_path chmod +x $bin_path
# check if command is update # check if command is update
if [ "$1" = "update" ]; then if [ "$COMMAND" = "update" ]; then
echo "Starting the agent" echo "Starting the agent"
start-service start-service
exit 0 exit 0