Skip to content

Commit

Permalink
MT#58352 helper/sync-db add lb nodes outbound_socket support
Browse files Browse the repository at this point in the history
* sync_extra_sockets() renamed into sync_lb_and_extra_sockets()
* extra_sockets_sync() renamed into lb_and_extra_sockets_sync()
* $config now also contains merged $NETWORK_CONFIG (network.yml)
* lb_and_extra_sockets_sync does the following and then calls
  generic_enum_sync() to provide with the gathered data:
  * all shared ips from interfaces that
   - host have 'lb' role
   - host status 'online'
   - interface has type '^sip_'
   are added into the outbound sockets list of protos (udp, tcp, tls)
   as: 'host:interface:proto' = 'proto:shared_ip:port'
   (port is taken from config.kamailio.lb.port in case of ('udp', 'tcp')
    and config.kamailio.tls.port in case of 'tls'
   )
   - if an interface has multiple shared IPs,
     the key becomes 'host:interface:shared_ip:proto'
   - $config->{kamailio}{lb}{extra_sockets} keys are merged
     with the gathered config
* generic_enum_sync() - processes hash keys are now sorted
  to have consistent output/processing

Change-Id: I218f1ae584e2dc9dfeb3a51c43d9f3fc950e829b
(cherry picked from commit 0f8ad35)
  • Loading branch information
sipwiser authored and marcocapetta committed Oct 17, 2023
1 parent ea3fa57 commit c041397
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
70 changes: 64 additions & 6 deletions helper/sync-db
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use DBI;
use Capture::Tiny qw(capture);
use DateTime::TimeZone;
use DateTime::TimeZone::Catalog;
use List::Util qw(any);

sub sync_extra_sockets;
sub db_connect;
Expand Down Expand Up @@ -41,7 +42,7 @@ unless(defined $dbname) {

$dbh = db_connect();

exit 1 unless(sync_extra_sockets($dbh,
exit 1 unless(sync_lb_and_extra_sockets($dbh,
$config->{kamailio}->{lb}->{extra_sockets},
1, 1, 1));

Expand Down Expand Up @@ -75,6 +76,63 @@ sub db_connect
return $dbh;
}

sub lb_and_extra_sockets_sync {
my $dbh = shift;
my $extra_sockets_config = shift;
my $usr_pref = shift;
my $dom_pref = shift;
my $peer_pref = shift;
my $pref_name = shift;

my $hosts = $config->{hosts}
// die 'Error: cannot fetch hosts from $config->{hosts}';

my $lb_config = $config->{kamailio}{lb}
// die 'Error: cannot fetch kamailio lb config from $config->{kamailio}{lb}';

my %lb_pairs = ();
my %ports = (
udp => $lb_config->{port},
tcp => $lb_config->{port},
tls => $lb_config->{tls}{port},
);

for my $host (keys %{$hosts}) {
my $host_ref = $hosts->{$host};
my $ifaces = $host_ref->{interfaces};

next unless $host_ref->{status} eq 'online';
next unless any { $_ eq 'lb' } @{$host_ref->{role}};

foreach my $iface (@{$ifaces}) {
my $iface_ref = $host_ref->{$iface};
next unless any { $_ =~ /^sip_/ } @{$iface_ref->{type}};

my $shared_ips = $iface_ref->{shared_ip} // next;
my $shared_ips_cnt = scalar @{$shared_ips};
my $pair = substr $host, 0, -1;

foreach my $ip (@{$shared_ips}) {
for my $proto (qw(udp tcp tls)) {
my $port = $ports{$proto};
$shared_ips_cnt > 1
? $lb_pairs{"$pair:$iface:$ip:$proto"} = "$proto:$ip:$port"
: $lb_pairs{"$pair:$iface:$proto"} = "$proto:$ip:$port";
}
}
}
}

generic_enum_sync(
$dbh,
{ %lb_pairs, %{$extra_sockets_config} },
$usr_pref, $dom_pref, $peer_pref,
$pref_name
);

return 1;
}

sub generic_enum_sync {
my $dbh = shift;
my $config_hash = shift;
Expand Down Expand Up @@ -102,7 +160,7 @@ sub generic_enum_sync {
my $db_hash = $sth->fetchall_hashref('label');
$sth->finish;

foreach my $row (keys %{$db_hash}) {
foreach my $row (sort keys %{$db_hash}) {
$row = $db_hash->{$row};
next if $row->{label} eq 'default';
if (!exists $config_hash->{$row->{label}}) {
Expand All @@ -117,7 +175,7 @@ sub generic_enum_sync {
delete $config_hash->{$row->{label}};
}
}
foreach my $label (keys %{$config_hash}) {
foreach my $label (sort keys %{$config_hash}) {
print "insert new $pref_name $label=$config_hash->{$label} from config into db\n";
$enum_insert_sth->execute($pref_id, $label, $config_hash->{$label},
$usr_pref ? 1 : 0, $dom_pref ? 1 : 0,
Expand All @@ -131,9 +189,9 @@ sub generic_enum_sync {
return 1;
}

## kamailio.lb.extra_sockets handling: ##############################
sub sync_extra_sockets {
return generic_enum_sync(@_, 'outbound_socket');
## lb nodes and kamailio.lb.extra_sockets handling: ##############################
sub sync_lb_and_extra_sockets {
return lb_and_extra_sockets_sync(@_, 'outbound_socket');
}

## rtp_* interfaces handling: ##############################
Expand Down
4 changes: 2 additions & 2 deletions scripts/commit
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ cd "$NGCPCTL_MAIN"
if [ -z "${NO_DB_SYNC:-}" ] ; then
if [ -f "${RTP_INTERFACES_CONFIG}" ] ; then
log_info "Executing sync-db: "
"${HELPER}/sync-db" "$NGCPCTL_CONFIG" "$CONSTANTS_CONFIG" "$RTP_INTERFACES_CONFIG" 2>&1 | \
"${HELPER}/sync-db" "$NGCPCTL_CONFIG" "$CONSTANTS_CONFIG" "$RTP_INTERFACES_CONFIG" "$NETWORK_CONFIG" 2>&1 | \
sed "s/^/$timestamp_replacementchars/" 2>&1 | \
tee >(logger -t ngcpcfg --id="${NGCPCFG_PID}") || true
else
log_info "Executing sync-db: "
"${HELPER}/sync-db" "$NGCPCTL_CONFIG" "$CONSTANTS_CONFIG" 2>&1 | \
"${HELPER}/sync-db" "$NGCPCTL_CONFIG" "$CONSTANTS_CONFIG" "$NETWORK_CONFIG" 2>&1 | \
sed "s/^/$timestamp_replacementchars/" 2>&1 | \
tee >(logger -t ngcpcfg --id="${NGCPCFG_PID}") || true
fi
Expand Down

0 comments on commit c041397

Please sign in to comment.