|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# |
| 18 | +# Common functions for CI scripts |
| 19 | +# |
| 20 | + |
| 21 | +# |
| 22 | +# Set initial port number for client/server applications to be updated with |
| 23 | +# function below |
| 24 | +# |
| 25 | +tcp_port_range=1000 |
| 26 | +min_port_number=10500 |
| 27 | +max_port_number=65535 |
| 28 | + |
| 29 | +# GITLAB CI |
| 30 | +if [ -n "$CI_CONCURRENT_ID" ]; then |
| 31 | + nixl_concurrent_id=$CI_CONCURRENT_ID |
| 32 | +# Jenkins CI |
| 33 | +elif [ -n "$EXECUTOR_NUMBER" ]; then |
| 34 | + nixl_concurrent_id=$EXECUTOR_NUMBER |
| 35 | +else |
| 36 | + # Fallback to random number if both CI_CONCURRENT_ID and EXECUTOR_NUMBER are not set |
| 37 | + nixl_concurrent_id=$((RANDOM % $(((max_port_number - min_port_number) / tcp_port_range)))) |
| 38 | +fi |
| 39 | + |
| 40 | +echo nixl_concurrent_id="$nixl_concurrent_id" |
| 41 | + |
| 42 | +# First half of the port range is used for shell script tests |
| 43 | +tcp_port_min=$((min_port_number + nixl_concurrent_id * tcp_port_range)) |
| 44 | +tcp_port_max=$((tcp_port_min + tcp_port_range / 2)) |
| 45 | + |
| 46 | +get_next_tcp_port() { |
| 47 | + local port_file="/tmp/nixl_tcp_port_${nixl_concurrent_id}" |
| 48 | + |
| 49 | + if [ ! -f "$port_file" ]; then |
| 50 | + echo "$tcp_port_min" > "$port_file" |
| 51 | + fi |
| 52 | + |
| 53 | + local current_port |
| 54 | + current_port=$(cat "$port_file") |
| 55 | + local next_port=$((current_port + 1)) |
| 56 | + |
| 57 | + # Check if the port is already in use |
| 58 | + while ss -tuln | grep -q :$next_port; do |
| 59 | + next_port=$((next_port + 1)) |
| 60 | + done |
| 61 | + |
| 62 | + if [ "$next_port" -ge "$tcp_port_max" ]; then |
| 63 | + next_port="$tcp_port_min" |
| 64 | + fi |
| 65 | + |
| 66 | + echo "$next_port" > "$port_file" |
| 67 | + |
| 68 | + echo "$next_port" |
| 69 | +} |
| 70 | + |
| 71 | +# Second half of the port range is used for gtest |
| 72 | +gtest_offset=$((tcp_port_range / 2)) |
| 73 | +# shellcheck disable=SC2034 |
| 74 | +min_gtest_port=$((tcp_port_min + gtest_offset)) |
| 75 | +# shellcheck disable=SC2034 |
| 76 | +max_gtest_port=$((tcp_port_max + gtest_offset)) |
0 commit comments