TT#154852 build: inject instance_info to templates

will provide a new instance_info variable with the all the content
defined at network.yml for the instance

removed intance_name since instance_info.name has the same value

Change-Id: I349d0b430b59280bc383d46d5ba6a61f5d1f9046
mr10.3.1
ngcp-config 5 years ago
parent e736773711
commit 664ad7ddfe

@ -4,6 +4,7 @@ functions/logs usr/share/ngcp-ngcpcfg/functions/
functions/main usr/share/ngcp-ngcpcfg/functions/
helper/check-for-mysql usr/share/ngcp-ngcpcfg/helper/
helper/fileformat_version usr/share/ngcp-ngcpcfg/helper/
helper/instance-info usr/share/ngcp-ngcpcfg/helper/
helper/instances-info usr/share/ngcp-ngcpcfg/helper/
helper/restore-permissions usr/share/ngcp-ngcpcfg/helper/
helper/sort-yml usr/share/ngcp-ngcpcfg/helper/

@ -0,0 +1,65 @@
#!/usr/bin/perl -CSD
# Purpose: generate yaml output with instance_info for bash script
################################################################################
use strict;
use warnings;
use YAML::XS;
use Time::Piece;
use Getopt::Long qw(:config posix_default bundling_values no_ignorecase);
my $DEBUG = $ENV{DEBUG} || 0;
my $HNAME = $ENV{HNAME} // '';
my $TIME_FORMAT = $ENV{TIME_FORMAT} // '%F %T';
$TIME_FORMAT =~ s/^\+//;
my $NETWORK_CONFIG = $ENV{NETWORK_CONFIG};
sub usage {
print <<HELP;
Usage: $0 [<option>...] instance_name
Options:
-h, --help This help message.
HELP
}
if (@ARGV != 1) {
error("wrong number of arguments");
usage();
exit 1;
}
my %options = ( help => sub { usage(); exit 0; }, );
error("NETWORK_CONFIG is not defined") unless $NETWORK_CONFIG;
GetOptions( \%options, 'help|?', );
my $yaml = YAML::XS::LoadFile($NETWORK_CONFIG);
sub output_prefix {
my $t = Time::Piece->new;
my $timestamp = $t->strftime($TIME_FORMAT);
return "$timestamp $HNAME";
}
sub error {
my $prefix = output_prefix();
die "$prefix: Error: @_\n";
}
exit 1 unless defined $yaml->{instances};
my $instance_name = $ARGV[0];
foreach my $instance ( sort @{ $yaml->{instances} } ) {
if ($instance->{name} eq $instance_name) {
my $out = {
instance_info => $instance,
};
print Dump($out);
exit 0;
}
}
exit 1;

@ -68,13 +68,12 @@ build_config_files_instances() {
target=${info[1]}
instance=${info[2]}
log_debug "source:${source} target:${target} instance:${instance}"
cat <<EOF > "${inst_cfg}"
---
instance_name: ${instance}
EOF
log_debug "${RUNNER} ${HELPER}/instance-info ${instance}"
${RUNNER} "${HELPER}/instance-info" "${instance}" > "${inst_cfg}"
log_debug "INSTANCE_NAME=${instance} ${RUNNER} ${HELPER}/tt2-process ${ARGS[*]} -c ${inst_cfg} -r ${source}:${target} ${source}"
INSTANCE_NAME="${instance}" ${RUNNER} "${HELPER}/tt2-process" "${ARGS[@]}" -c "${inst_cfg}" -r "${source}:${target}" "${source}"
done< <(${RUNNER} "${HELPER}/instances-info" "$@")
rm -f "${inst_cfg}"
}
build_config_files() {

@ -10,11 +10,11 @@
ELSE;
pairname = ngcp.get_pairname(hostname);
END;
# instances
host_instances = ngcp.get_instances(hostname);
- %]
# [% hostname %]
# [% instance_name %]
[% IF instance_info -%]
#!define INSTANCE [% instance_info.name %]
[% END -%]
#!ifndef INSTANCE
# external interfaces
[% FOREACH iface IN hosts.$hostname.interfaces.sort -%]
@ -37,18 +37,14 @@ listen=tls:[% ip_address %]:[% kamailio.lb.tls.port %][% IF hosts.$hostname.$ifa
[% END -%]
#!else
[%
FOREACH instance IN host_instances;
NEXT UNLESS instance.name == instance_name;
argv.instance = instance;
argv.type = 'sip_ext';
PROCESS '../lib/get_all_ips_for_instance';
sip_ext_ips = out;
argv.instance = instance_info;
argv.type = 'sip_ext';
PROCESS '../lib/get_all_ips_for_instance';
sip_ext_ips = out;
argv.type = 'sip_int';
PROCESS '../lib/get_all_ips_for_instance';
sip_int_ips = out;
END;
argv.type = 'sip_int';
PROCESS '../lib/get_all_ips_for_instance';
sip_int_ips = out;
-%]
# external interfaces
[% FOREACH ip IN sip_ext_ips -%]

@ -0,0 +1,14 @@
---
instance_info:
host: lb01b
interfaces:
- ip: 172.30.52.204
name: vlan1719
type: sip_ext
- ip: 172.30.52.146
name: vlan1720
type: sip_int
label: lb
name: A
service: kamailio-lb
status: online

@ -1,6 +1,5 @@
#!KAMAILIO
# lb01a
#
#!ifndef INSTANCE
# external interfaces
listen=udp:172.30.52.103:5060

@ -1,6 +1,6 @@
#!KAMAILIO
# lb01a
# A
#!define INSTANCE A
#!ifndef INSTANCE
# external interfaces
listen=udp:172.30.52.103:5060

@ -1,6 +1,6 @@
#!KAMAILIO
# lb01a
# B
#!define INSTANCE B
#!ifndef INSTANCE
# external interfaces
listen=udp:172.30.52.103:5060

@ -0,0 +1,42 @@
#!/usr/bin/env py.test-3
from pathlib import Path
import pytest
import re
from fixtures.fs import check_stdoutput
def test_instance_info_noargs(helpercli):
out = helpercli(
"instance-info",
env={
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
},
)
assert out.returncode != 0
assert "Error: wrong number of arguments" in out.stderr
def test_instances_info_no_instance(helpercli):
out = helpercli(
"instance-info",
"C",
env={
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
},
)
assert out.returncode != 0
def test_instance_info(helpercli, tmpdir):
out = helpercli(
"instance-info",
"A",
env={
"NETWORK_CONFIG": "fixtures/repos/network_carrier_instances.yml",
},
)
test_file = "fixtures/output/instance_info_A"
assert out.returncode == 0
check_stdoutput(out.stdout, test_file, tmpdir)
Loading…
Cancel
Save