MT#10699 move ngcp-check-rev to ngcp-system-tools

Change-Id: I15c447a8d1eb3786b919dea64f04739f647aad25
changes/67/667/2
Victor Seva 11 years ago
parent dc1c7e6eca
commit df6902d610

4
debian/control vendored

@ -9,9 +9,7 @@ Homepage: http://sipwise.com/
Package: ngcp-db-schema
Architecture: all
Pre-Depends: ngcp-system-tools
Depends: libconfig-tiny-perl,
liblist-moreutils-perl,
${misc:Depends}
Depends: ${misc:Depends}
Description: database schema for Sipwise's NGCP platform
This package provides the database schema for Sipwise's NGCP
platform.

@ -1,3 +1,2 @@
db_scripts usr/share/ngcp-db-schema/
ngcp-check-rev-applied usr/sbin/
ngcp-update-db-schema usr/sbin/

@ -1 +0,0 @@
ngcp-check-rev-applied.1

3
debian/rules vendored

@ -6,6 +6,3 @@
%:
dh $@
override_dh_auto_build:
pod2man ./ngcp-check-rev-applied > ./ngcp-check-rev-applied.1

@ -1,235 +0,0 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use Carp;
use Config::Tiny;
use DBI;
use English qw( -no_match_vars );
use Getopt::Long;
use List::MoreUtils qw{ any };
use Pod::Usage;
my $dbhost = 'localhost';
my $dbname = 'ngcp';
my $debug = 0;
my $exit_code = 0;
my $help = 0;
my $man = 0;
my $node = undef;
my $result_date = undef;
my $result_host = undef;
my $result_id = undef;
my $result_rev = undef;
my @revision;
my @result_list;
my $schema = undef;
GetOptions(
'dbhost=s' => \$dbhost,
'dbname=s' => \$dbname,
'debug|d' => \$debug,
'help|?' => \$help,
'man' => \$man,
'node=s' => \$node,
'revision=s{1,}' => \@revision,
'schema=s' => \$schema,
) or pod2usage(2);
pod2usage( -exitval => 2, -verbose => 0 ) if $help;
pod2usage( -exitval => 2, -verbose => 2 ) if $man;
pod2usage(2) if !@revision;
pod2usage(2) if !defined $schema;
# connect as user sipwise to DB
my $Config = Config::Tiny->read('/etc/mysql/sipwise.cnf')
or croak "Could not read /etc/mysql/sipwise.cnf: $ERRNO";
my $dbuser = 'sipwise';
my $dbpwd = $Config->{_}->{SIPWISE_DB_PASSWORD};
if ( !defined $dbpwd ) {
croak "Couldn't identify password for user sipwise to connect to database.";
}
$dbpwd =~ s/^['"](.*)['"]$/$1/; # get rid of possibly surrounding quotes
my $dbh =
DBI->connect( "dbi:mysql:dbname=$dbname;host=$dbhost", $dbuser, $dbpwd )
or croak "Couldn't connect to database: " . DBI->errstr;
$schema = $dbh->quote_identifier($schema);
my $sql_clause = ""; # if no node is specified then use empty SQL statement
if ( defined $node ) {
$sql_clause = "where node = ?";
}
my $sth = $dbh->prepare("SELECT * FROM $schema $sql_clause")
or croak "Couldn't prepare statement: " . $dbh->errstr;
if ( defined $node ) {
$sth->bind_param( 1, $node );
}
$sth->execute()
or croak "Couldn't execute statement: " . $sth->errstr;
$sth->bind_columns( \$result_id, \$result_rev, \$result_host, \$result_date );
while ( $sth->fetch ) {
foreach my $rev (@revision) {
# adjust for MySQL handling (we're interested only in the ID in MySQL speak)
# the regex handling is ugly, some love to it would be nice...
my $short_rev = $rev;
$short_rev =~ s/.*\///; # drop anything until and incl. the first slash
$short_rev =~ s/\..*//; # drop anything starting at the first '.'
$short_rev =~ s/_.*//; # drop anything starting at the first '_'
$short_rev =~ s/^0+//; # strip any trailing zeros from the id
if ( $result_rev =~ /^${short_rev}$/msx ) {
if ( defined $node ) {
if ( $result_host =~ /$node/msx ) {
push @result_list, $rev;
}
}
else {
push @result_list, $rev;
}
}
}
}
foreach my $rev (@revision) {
if ( any { /^${rev}$/msx } @result_list ) {
if ( defined $node ) {
print "Revision $rev already executed on host $node\n";
}
else {
print "Revision $rev already executed\n";
}
}
else {
if ( defined $node ) {
print "No match for revision $rev on host $node\n";
$exit_code = 1;
}
else {
print "No match for revision $rev\n";
$exit_code = 1;
}
}
}
$sth->finish;
$dbh->disconnect;
exit($exit_code);
__END__
=head1 NAME
ngcp-check-rev-applied - check which db/config revisions have been executed
=head1 SYNOPSIS
ngcp-check-rev-applied --schema <schema> --revision <id> [<2nd_id> [<3rd_id> [...]]
[--node <name>] [--dbname <db>] [--dbhost <host>]
=head1 DESCRIPTION
This program queries the ngcp database to check for db/config revisions that
have been applied. If the specified ID is present (or when multiple IDs are
specified: all of them are present) the exit code is 0, otherwise (the specified
ID is not present, or when multiple IDs are specified:: one of them is not
present) the exit code is 1.
=head1 USAGE EXAMPLES
=over 8
=item Check db_schema table for revision 23:
% ngcp-check-rev-applied --schema db_schema --revision 23
=item Check cfg_schema table for revision 42 and use output of `hostname`
to limit the specified ID to the current host:
% ngcp-check-rev-applied --schema cfg_schema --revision 42 --node $(hostname)
=item Check db_schema table for revisions 11, 23 and 42:
% ngcp-check-rev-applied --schema db_schema --revision 11 23 42 --debug
No match for revision 11.
No match for revision 23.
Revision 42 already executed.
=back
=head1 EXIT_CODES
=over 8
=item B<0>
The requested ID is present. If multiple IDs have been specified all of them are present.
=item B<1>
The requested ID is *NOT* present. If multiple IDs have been specified at least
one of the specified IDs it *NOT* present.
=item B<2>
Error or invalid usage of command line options.
=item B<255>
Error while running database query.
=back
=head1 OPTIONS
=over 8
=item B<--dbhost> <host>
Query specified host instead of the default (being "localhost").
=item B<--dbname> <db>
Use specified database instead of the default (being "ngcp").
=item B<--debug>
Be more verbose during execution.
=item B<--help>
Print help message and exit.
=item B<--man>
Display manpage using pod2man.
=item B<--node> <name>
Restrict querying ID to specified node name.
This is relevant only in high availability environments
to check for (non-)replicated revisions.
=item B<--revision> <id>
Query database for specified ID. Multiple IDs can be specified white-space
separated (e.g.: '--revision 11 23 42').
=item B<--schema> <name>
Use specified schema as table name to look for specified ID.
Supported table names: cfg_schema, db_schema
=back
=cut

@ -1,197 +0,0 @@
.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "NGCP-CHECK-REV-APPLIED 1"
.TH NGCP-CHECK-REV-APPLIED 1 "2012-03-16" "perl v5.14.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
ngcp\-check\-rev\-applied \- check which db/config revisions have been executed
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
ngcp-check-rev-applied \-\-schema <schema> \-\-revision <id>
[\-\-node <name>] [\-\-dbname <db>] [\-\-dbhost <host>]
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This program queries the ngcp database to check for db/config revisions that
have been applied. If the specified \s-1ID\s0 is present the exit code is 0, otherwise
(the specified \s-1ID\s0 is not present) the exit code is 1.
.SH "USAGE EXAMPLES"
.IX Header "USAGE EXAMPLES"
.IP "Check db_schema table for revision 23:" 8
.IX Item "Check db_schema table for revision 23:"
.Vb 1
\& ngcp\-check\-rev\-applied \-\-schema db_schema \-\-revision 23
.Ve
.IP "Check cfg_schema table for revision 42 and use output of `hostname` to limit the specified \s-1ID\s0 to the current host:" 8
.IX Item "Check cfg_schema table for revision 42 and use output of `hostname` to limit the specified ID to the current host:"
.Vb 1
\& ngcp\-check\-rev\-applied \-\-schema cfg_schema \-\-revision 42 \-\-node $(hostname)
.Ve
.SH "EXIT_CODES"
.IX Header "EXIT_CODES"
.IP "\fB0\fR" 8
.IX Item "0"
The requested \s-1ID\s0 is present.
.IP "\fB1\fR" 8
.IX Item "1"
The requested \s-1ID\s0 is *NOT* present.
.IP "\fB2\fR" 8
.IX Item "2"
Error or invalid usage of command line options.
.IP "\fB255\fR" 8
.IX Item "255"
Error while running database query.
.SH "OPTIONS"
.IX Header "OPTIONS"
.IP "\fB\-\-dbhost\fR <host>" 8
.IX Item "--dbhost <host>"
Query specified host instead of the default (being \*(L"localhost\*(R").
.IP "\fB\-\-dbname\fR <db>" 8
.IX Item "--dbname <db>"
Use specified database instead of the default (being \*(L"ngcp\*(R").
.IP "\fB\-\-debug\fR" 8
.IX Item "--debug"
Be more verbose during execution.
.IP "\fB\-\-help\fR" 8
.IX Item "--help"
Print help message and exit.
.IP "\fB\-\-man\fR" 8
.IX Item "--man"
Display manpage using pod2man.
.IP "\fB\-\-node\fR <name>" 8
.IX Item "--node <name>"
Restrict querying \s-1ID\s0 to specified node name.
This is relevant only in high availability environments
to check for (non\-)replicated revisions.
.IP "\fB\-\-revision\fR <id>" 8
.IX Item "--revision <id>"
Query database for specified \s-1ID\s0.
.IP "\fB\-\-schema\fR <name>" 8
.IX Item "--schema <name>"
Use specified schema as table name to look for specified \s-1ID\s0.
Supported table names: cfg_schema, db_schema
Loading…
Cancel
Save