Skip to content

Commit

Permalink
Clean up csfle server ports
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Oct 1, 2024
1 parent aeebb86 commit 4060193
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .evergreen/csfle/stop-servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ if [ -f "kmip_pids.pid" ]; then
rm kmip_pids.pid
fi
popd

# Forcibly kill the process listening on the desired ports, most likely
# left running from a previous task.
. "$SCRIPT_DIR/../process-utils.sh"
for port in 5698 9000 9001 9002 8080; do
killport $port 9
done
46 changes: 46 additions & 0 deletions .evergreen/process-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
#
# process-utils.sh
#
# Usage:
# . /path/to/process]-utils.sh
#
# This file defines the following functions:
# - killport
# These functions may be invoked from any working directory.


# killport
#
# Usage:
# killport 8889
#
# Parameters:
# "$1": The port of the process to kill.
# "$2": The signal to send, defaults to SIGTERM.
#
# Kill the process listening on the given port.
killport() {
local -r port="${1:?'killport requires a port'}"
local -r signal="${2:-15}"
local pid=""

if [[ "${OSTYPE:?}" == cygwin || "${OSTYPE:?}" == msys ]]; then
for pid in $(netstat -ano | grep ":$port .* LISTENING" | awk '{print $5}' | tr -d '[:space:]'); do
taskkill /F /T /PID "$pid" || true
done
elif [ -x "$(command -v lsof)" ]; then
for pid in $(lsof -t "-i:$port" || true); do
kill "$pid" -${signal} || true
done
elif [ -x "$(command -v fuser)" ]; then
fuser --kill -${signal} "$port/tcp" || true
elif [ -x "$(command -v ss)" ]; then
for pid in $(ss -tlnp "sport = :$port" | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]'); do
kill "$pid" -${signal} || true
done
else
echo "Unable to identify the OS (${OSTYPE:?}) or find necessary utilities (fuser/lsof/ss) to kill the process."
exit 1
fi
}
35 changes: 1 addition & 34 deletions .evergreen/start-orchestration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,12 @@ if [[ "${OSTYPE:?}" == cygwin ]]; then
ORCHESTRATION_ARGUMENTS="$ORCHESTRATION_ARGUMENTS -s wsgiref"
fi


# killport
#
# Usage:
# killport 8889
#
# Parameters:
# "$1": The port of the process to kill.
#
# Kill the process listening on the given port.
killport() {
local -r port="${1:?'killport requires a port'}"
local pid=""

if [[ "${OSTYPE:?}" == cygwin || "${OSTYPE:?}" == msys ]]; then
for pid in $(netstat -ano | grep ":$port .* LISTENING" | awk '{print $5}' | tr -d '[:space:]'); do
taskkill /F /T /PID "$pid" || true
done
elif [ -x "$(command -v lsof)" ]; then
for pid in $(lsof -t "-i:$port" || true); do
kill "$pid" || true
done
elif [ -x "$(command -v fuser)" ]; then
fuser --kill -SIGTERM "$port/tcp" || true
elif [ -x "$(command -v ss)" ]; then
for pid in $(ss -tlnp "sport = :$port" | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]'); do
kill "$pid" || true
done
else
echo "Unable to identify the OS (${OSTYPE:?}) or find necessary utilities (fuser/lsof/ss) to kill the process."
exit 1
fi
}

# Forcibly kill the process listening on port 8889, most likely a wild
# mongo-orchestration left running from a previous task.
# NOTE: Killing mongo-orchestration with SIGTERM gives it a chance to
# cleanup any running mongo servers.
# Also kill any leftover mongo processes on common ports as a backup.
. "$det_evergreen_dir/process-utils.sh"
for port in 8889 27017 27018 27019 27217 27218 27219 1026; do
killport $port
done
Expand Down

0 comments on commit 4060193

Please sign in to comment.