From d17f8e04b8fb56ce3dcbd499e550175d88b6478a Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Mon, 29 Jun 2020 18:13:55 +0200 Subject: [PATCH] TT#68855 clean_registrations.pl: clean bannedusers too to be sure no other test is messing with actual test run Change-Id: I2413e97f3e100146e63e5a3e7e8c28359107a735 --- bin/clean_registrations.pl | 36 ++++++++++++++++++++++-------------- lib/Sipwise/API.pm | 15 +++++++++++++-- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/bin/clean_registrations.pl b/bin/clean_registrations.pl index 3b46fab0..24acf5a1 100755 --- a/bin/clean_registrations.pl +++ b/bin/clean_registrations.pl @@ -24,12 +24,24 @@ use warnings; use English; use Getopt::Long; use Cwd 'abs_path'; +use Config::Tiny; use Capture::Tiny qw(capture); use YAML::XS; use List::MoreUtils qw(uniq); use DBI qw(:sql_types); +use Sipwise::API qw(all); my $CONSTANTS = YAML::XS::LoadFile('/etc/ngcp-config/constants.yml'); +my $config = Config::Tiny->read('/etc/default/ngcp-api'); +my $opts; +if ($config) { + $opts = {}; + $opts->{host} = $config->{_}->{NGCP_API_IP}; + $opts->{port} = $config->{_}->{NGCP_API_PORT}; + $opts->{sslverify} = $config->{_}->{NGCP_API_SSLVERIFY}; +} +my $api = Sipwise::API->new($opts); +$opts = $api->opts; sub usage { @@ -40,9 +52,10 @@ sub usage } my $help = 0; -my $del = 0; -GetOptions ("h|help" => \$help) - or die("Error in command line arguments\n".usage()); +GetOptions ( + "h|help" => \$help, + "d|debug" => \$opts->{verbose}, +) or die("Error in command line arguments\n".usage()); die(usage()) unless (!$help); die("Wrong number of arguments\n".usage()) unless ($#ARGV == 0); @@ -51,21 +64,10 @@ my $filename = abs_path($ARGV[0]); my $cf = YAML::XS::LoadFile($filename); my $MYSQL_CREDENTIALS = "/etc/mysql/sipwise_extra.cnf"; -if (! -e ${MYSQL_CREDENTIALS}) { - die("Error: missing DB credentials file '${MYSQL_CREDENTIALS}'.\n") -} - sub get_mysql_credentials { return $CONSTANTS->{credentials}->{mysql}->{system}->{u}; } -sub get_nodename { - my @lines = capturex( [ 0 ], "/usr/sbin/ngcp-nodename"); - my $l = shift @lines; - chomp $l; - return $l; -} - sub connect_db { my ($dbhost, $dbport, $mysql_user, $mysql_pass) = @_; @@ -138,6 +140,9 @@ sub clean_kamailio } foreach (uniq @values) { clean_kamailio_ul($_); + if($api->delete_banneduser($_)) { + print("$_ removed from banned\n"); + } } return; } @@ -188,5 +193,8 @@ SQL clean_kamailio($cf); if(usrloc_in_redis() eq 0) { + if (! -e ${MYSQL_CREDENTIALS}) { + die("Error: missing DB credentials file '${MYSQL_CREDENTIALS}'.\n") + } clean_locations(); } diff --git a/lib/Sipwise/API.pm b/lib/Sipwise/API.pm index c23ae1d2..03d14f4f 100644 --- a/lib/Sipwise/API.pm +++ b/lib/Sipwise/API.pm @@ -93,7 +93,9 @@ sub do_request { my $res = $ua->request($req); if(!$res->is_success) { print "$url\n"; - print Dumper $data unless $self->{opts}->{verbose}; + if($data) { + print Dumper $data unless $self->{opts}->{verbose}; + } } return $res; } @@ -131,7 +133,9 @@ sub do_query { my $res = $ua->request($req); if(!$res->is_success) { print "$url\n"; - print Dumper $data unless $self->{opts}->{verbose}; + if($data) { + print Dumper $data unless $self->{opts}->{verbose}; + } } return $res; } @@ -1012,4 +1016,11 @@ sub upload_soundfile { return; } +sub delete_banneduser { + my ($self, $id) = @_; + my $urldata = "/api/bannedusers/${id}"; + + return $self->_delete($urldata); +} + 1;