From 53ba3ec01702d01c4c37bdeff5311ad996a7c50f Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 30 Mar 2021 11:40:12 +0200 Subject: [PATCH] TT#116100 provide_scenarios.sh * provide_next_network.pl helper to get sipp port and mport used on that scenario Entry point to provide the whole group of scenarios with different port/mport assigned by scenario Change-Id: Ie3cc83388afe089242830373643e32421db83e51 --- bin/provide_next_network.pl | 71 +++++++++++++++++ bin/provide_scenarios.sh | 149 ++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100755 bin/provide_next_network.pl create mode 100755 bin/provide_scenarios.sh diff --git a/bin/provide_next_network.pl b/bin/provide_next_network.pl new file mode 100755 index 00000000..2f3e94c6 --- /dev/null +++ b/bin/provide_next_network.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# +# Copyright: 2021 Sipwise Development Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This package is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# On Debian systems, the complete text of the GNU General +# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +# +use strict; +use warnings; +use English; +use Getopt::Long; +use Cwd 'abs_path'; +use YAML::XS qw(DumpFile LoadFile); + +sub usage +{ + my $output = "usage: $PROGRAM_NAME [-h] [-p] scenario_ids.yml\n"; + $output .= "Options:\n"; + $output .= "\t-h: this help\n"; + $output .= "\t-p: peer info\n"; + return $output +} + +my $help = 0; +my $peer = ''; +my $port = 0; +my $mport = 0; +GetOptions ( + "h|help" => \$help, + "p|peer" => \$peer, +) or die("Error in command line arguments\n".usage()); + +die(usage()) unless (!$help); +die("Wrong number of arguments[$#ARGV]\n".usage()) unless ($#ARGV == 0); + +my $ids = LoadFile(abs_path($ARGV[0])); + +foreach my $scen (@{$ids->{scenarios}}) +{ + if($peer && !defined($scen->{peer})){ + # nothing to do here + } else { + $port = $scen->{port}; + $mport = $scen->{mport}; + } + foreach my $resp (@{$scen->{responders}}) + { + if($peer && !defined($resp->{peer})){ + # nothing to do here + } else { + $port = $resp->{port}; + $mport = $resp->{mport}; + } + } +} +$port = $port +1; +$mport = $mport + 2; +print "${port}:${mport}\n"; diff --git a/bin/provide_scenarios.sh b/bin/provide_scenarios.sh new file mode 100755 index 00000000..31c8b252 --- /dev/null +++ b/bin/provide_scenarios.sh @@ -0,0 +1,149 @@ +#!/bin/bash +# +# Copyright: 2013-2021 Sipwise Development Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This package is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# On Debian systems, the complete text of the GNU General +# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +# +set -e +# shellcheck disable=SC2155 +declare -r ME="$(basename "$0")" +BASE_DIR="${BASE_DIR:-/usr/share/kamailio-config-tests}" +BIN_DIR="${BASE_DIR}/bin" +if [ -z "${PERL5LIB}" ]; then + # Set up the environment, to use local perl modules + export PERL5LIB="${BASE_DIR}/lib" +fi + +usage() { + cat <&2 + exit 1 + fi +} + +while getopts 'hi:p:m:I:P:M:x:t:' opt; do + case $opt in + i) IP=${OPTARG};; + p) PORT=${OPTARG};; + m) MPORT=${OPTARG};; + I) PEER_IP=${OPTARG};; + P) PEER_PORT=${OPTARG};; + M) PEER_MPORT=${OPTARG};; + x) GROUP=${OPTARG};; + t) PROFILE=${OPTARG};; + h) usage; exit 0;; + *) usage; exit 1;; + esac +done +shift $((OPTIND - 1)) + +if [[ $# != 1 ]]; then + echo "Wrong number of arguments" >&2 + usage + exit 1 +fi +ACTION="$1" + +if [ -z "${PROFILE}" ]; then + ngcp_type=$(command -v ngcp-type) + if [ -n "${ngcp_type}" ]; then + case $(${ngcp_type}) in + sppro|carrier) PROFILE=PRO;; + spce) PROFILE=CE;; + *) ;; + esac + echo "ngcp-type: profile ${PROFILE}" + fi +fi +if [ "${PROFILE}" != "CE" ] && [ "${PROFILE}" != "PRO" ]; then + echo "PROFILE ${PROFILE} unknown" >&2 + usage + exit 2 +fi + +update_network() { + local scen=$1 + local ids=${scen}/scenario_ids.yml + + if [ ! -f "${ids}" ]; then + echo "can't read ${ids}" >&2 + exit 3 + fi + mapfile -d: -t data < <("${BIN_DIR}/provide_next_network.pl" "${ids}") + PORT=${data[0]} + MPORT=${data[1]} + mapfile -d: -t data < <("${BIN_DIR}/provide_next_network.pl" -peer "${ids}") + if [[ ${data[0]} -ne 1 ]]; then + PEER_PORT=${data[0]} + fi + if [[ ${data[1]} -ne 2 ]]; then + PEER_MPORT=${data[1]} + fi +} + +create() { + local scen=${BASE_DIR}/${GROUP}/$1 + echo "*** $1 IP:${IP} PORT:${PORT} MPORT:${MPORT} ***" + "${BIN_DIR}/provide_scenario.sh" \ + -i "${IP}" -p "${PORT}" -m "${MPORT}" \ + -I "${PEER_IP}" -P "${PEER_PORT}" -M "${PEER_MPORT}" \ + "${scen}" "${ACTION}" + echo "*** done ***" + update_network "${scen}" +} + +delete() { + local scen=${BASE_DIR}/${GROUP}/$1 + "${BIN_DIR}/provide_scenario.sh" "${scen}" "${ACTION}" +} + +get_scenarios + +case ${ACTION} in + create) for t in "${SCEN[@]}"; do create "${t}"; done;; + delete) for t in "${SCEN[@]}"; do delete "${t}"; done;; + *) echo "action ${ACTION} unknown" >&2; exit 2 ;; +esac +echo "$(date) - Done"