You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngcpcfg/helper/sync-db

189 lines
5.8 KiB

#!/usr/bin/perl -CSD
# Purpose: template toolkit helper script
################################################################################
use strict;
use warnings;
use YAML qw/LoadFile/;
use Template;
use Hash::Merge qw(merge);
use DBI;
sub sync_extra_sockets;
my $tt = Template->new({ ABSOLUTE => 1, RELATIVE => 1 });
my $config = {};
foreach my $file (@ARGV) {
$config = merge($config, LoadFile($file) || die $!);
}
open my $SWFH, '<', '/etc/mysql/sipwise.cnf';
my $dbpass = join ' ', <$SWFH>;
close $SWFH;
$dbpass =~ s/^.*SIPWISE_DB_PASSWORD=\'([^\']+)\'.*$/$1/;
chomp $dbpass;
my $dbuser = 'sipwise';
my $dbname = $config->{ossbss}->{provisioning}->{database}->{name};
unless(defined $dbname) {
print "Could not determine provisioning db name\n";
exit 1;
}
unless(defined $dbuser) {
print "Could not determine provisioning db user\n";
exit 1;
}
unless(defined $dbpass) {
print "Could not determine provisioning db password\n";
exit 1;
}
my $dbh = DBI->connect('DBI:mysql:'.$dbname, $dbuser, $dbpass);
unless(defined $dbh) {
print "Could not connect to database '$dbname': $DBI::errstr\n";
exit 1;
}
exit 1 unless(sync_extra_sockets());
$dbh->disconnect;
# this part sync with kamailio db
$dbuser = $config->{kamailio}->{proxy}->{dbrwuser};
$dbname = $config->{kamailio}->{proxy}->{dbname};
$dbpass = $config->{kamailio}->{proxy}->{dbrwpw};
unless(defined $dbname) {
print "Could not determine kamailio db name\n";
exit 1;
}
unless(defined $dbuser) {
print "Could not determine kamailio db user\n";
exit 1;
}
unless(defined $dbpass) {
print "Could not determine kamailio db password\n";
exit 1;
}
$dbh = DBI->connect('DBI:mysql:'.$dbname, $dbuser, $dbpass);
unless(defined $dbh) {
print "Could not connect to database '$dbname': $DBI::errstr\n";
exit 1;
}
exit 1 unless(sync_faxserver_uris());
$dbh->disconnect;
exit 0;
## kamailio.lb.extra_sockets handling: ##############################
sub sync_extra_sockets {
my $pref_name = 'outbound_socket';
my $sth = $dbh->prepare("select id from voip_preferences where attribute=?");
$sth->execute($pref_name);
my $res = $sth->fetchrow_hashref();
unless(defined $res) {
print "Could not find preference '$pref_name' in db\n";
return;
}
my $pref_id = $res->{id};
$sth->finish;
my $enum_insert_sth = $dbh->prepare("insert into voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref) values(?, ?, ?, 1, 1, 1)");
my $enum_update_sth = $dbh->prepare("update voip_preferences_enum set value=? where id=?");
my $enum_delete_sth = $dbh->prepare("delete from voip_preferences_enum where id=?");
$sth = $dbh->prepare("select id, label, value from voip_preferences_enum where preference_id=?");
$sth->execute($pref_id);
my $sockets = $config->{kamailio}->{lb}->{extra_sockets};
my $dbsockets = $sth->fetchall_hashref('label');
$sth->finish;
foreach my $row (keys %{$dbsockets}) {
$row = $dbsockets->{$row};
next if($row->{label} eq 'default');
if(!exists $sockets->{$row->{label}}) {
print "socket $row->{label} does not exist anymore in config, delete from db\n";
$enum_delete_sth->execute($row->{id});
} elsif($sockets->{$row->{label}} ne $row->{value}) {
print "update $row->{label}=$row->{value} to $row->{label}=$sockets->{$row->{label}} in db\n";
$enum_update_sth->execute($sockets->{$row->{label}}, $row->{id});
delete $sockets->{$row->{label}};
} else {
print "socket $row->{label}=$row->{value} is sync between config and db\n";
delete $sockets->{$row->{label}};
}
}
foreach my $sock(keys %{$sockets}) {
print "insert new socket $sock=$sockets->{$sock} from config into db\n";
$enum_insert_sth->execute($pref_id, $sock, $sockets->{$sock});
}
$enum_insert_sth->finish;
$enum_update_sth->finish;
$enum_delete_sth->finish;
return 1;
}
## faxserver.hw_fax_gateways handling: ##############################
sub sync_faxserver_uris {
my $fax_set_id = 4;
my $reload_dispatcher = 0;
my $dispatcher_insert_sth = $dbh->prepare("insert into dispatcher (setid, destination, flags, priority, description) values(4, ?, 0, 0, 'Fax2Mail Servers')");
my $dispatcher_delete_sth = $dbh->prepare("delete from dispatcher where id=?");
my $sth = $dbh->prepare("select id,destination from dispatcher where setid=?");
$sth->execute($fax_set_id);
my $db_fax_gateways = $sth->fetchall_hashref('destination');
$sth->finish;
# array reference
my $yaml_fax_gateways = $config->{faxserver}->{fax_gateways};
unless(defined $yaml_fax_gateways) {
return 1;
}
my @tmp_array = @{$yaml_fax_gateways};
# turn the array into hash reference
my $fax_gateways = {};
foreach my $gw (@tmp_array) {
# e.g: sip:127.0.0.1:5070/255.255.255.0, remove /255.255.255.0
my @new_gw_parts = split('/', $gw);
my $gw_sip_parts = $new_gw_parts[0];
$fax_gateways->{$gw_sip_parts} = 1 ;
}
foreach my $row (keys %{$db_fax_gateways}) {
$row = $db_fax_gateways->{$row};
if(!exists $fax_gateways->{$row->{destination}}) {
print "fax gateway $row->{destination} does not exist anymore in config, delete from db\n";
$dispatcher_delete_sth->execute($row->{id});
$reload_dispatcher ||= 1;
} else {
print "fax gateway $row->{destination} sync between config and db\n";
delete $fax_gateways->{$row->{destination}};
}
}
foreach my $fax_gw(keys %{$fax_gateways}) {
print "insert new fax gateway $fax_gw from config into db\n";
$dispatcher_insert_sth->execute($fax_gw);
$reload_dispatcher ||= 1;
}
$dispatcher_insert_sth->finish;
$dispatcher_delete_sth->finish;
if($reload_dispatcher){
print "fax gateway changes, need to reload kamailio dispatcher ... \n";
my $result = `ngcp-kamctl proxy dispatcher reload`;
print "reload kamailio dispatcher done, status was: $? \n";
}
return 1;
}
## END OF FILE #################################################################