#! /usr/bin/perl -w use strict ; use Repocafe ; use Net::LDAP ; my $prog = substr($0,rindex($0,'/')+1) ; my $Usage = < option V : show repocafe version ; exit option R : show repocafe revision ; exit ---------------------------------------- - $prog - test the repocafe configuration. - Program $prog just loads the config ; any detected errors will be shown. - Optionally, $prog connects to the database as the configured database user db_user or db_owner. - The program can also test the ldap connection(s). ---------------------------------------- USAGE sub Usage { die "$_[0]$Usage" ; } sub Error { die "$prog: $_[0]\n" ; } sub Warn { warn "$prog: $_[0]\n" ; } # usage: &GetOptions(ARG,ARG,..) defines $opt_ID as 1 or user spec'ed value # usage: &GetOptions(\%opt,ARG,ARG,..) defines $opt{ID} as 1 or user value # ARG = 'ID' | 'ID=SPC' | 'ID:SPC' for no-arg, required-arg or optional-arg # ID = perl identifier # SPC = i|f|s for integer, fixedpoint real or string argument use Getopt::Long ; Getopt::Long::config('no_ignore_case') ; our %opt = () ; Usage('') unless GetOptions ( \%opt, qw(V R v q d u o t c=s l a help B=s) ) ; Usage("Arg count\n") unless @ARGV == 0 ; if ( $opt{help} ) { print $Usage ; exit 0 ; } if ( $opt{V} ) { printf "%s\n", version ; exit ; } if ( $opt{a} ) { $opt{u} ++ ; $opt{o} ++ ; $opt{l} ++ ; $opt{t} ++ ; } $opt{v} ||= $opt{d} ; if ( $opt{B} ) { Blib -> verbose ( 1 ) if $opt{B} eq 'v' ; if ( $opt{B} =~ /^d([12]?)$/ ) { Blib::lvl ( $1 || 0 ) ; Blib -> debug ( 1 ) ; } else { Error "bad opt B ($opt{B}) ; should by [d|d1|d2]\n" ; } } my $Cafe = make_cafe ( %opt, -conn => 0 ) ; my $conf = $Cafe -> conf ; if ( $opt{R} ) { printf "%s\n", $Cafe -> revision ; exit ; } print "$prog : no config errors detected\n" ; sub try_connect { my $ownr = shift ; my $pswd = shift ; $Cafe -> db_connect_as ( $ownr, $pswd ) ; if ( $Cafe -> _dbh ) { print "$prog : connected as database user '$ownr'\n" ; $Cafe -> _dbh -> disconnect () ; } } sub check_tabs { my $ownr = shift ; my $pswd = shift ; $Cafe -> db_connect_as ( $ownr, $pswd ) ; unless ( $Cafe -> _dbh ) { print "$prog : can't connected as database user '$ownr'\n" ; print "* can't check tables\n" ; return ; } my $rep = $Cafe -> check_tabs ; my $have ; for my $tab ( @{ $Cafe -> _tabs } ) { $have = $rep -> { $tab } { have } ? 'found' : 'not found' ; if ( $opt{v} or $have ne 'found' ) { printf "table %s %s\n", $tab -> _name, $have ; printf " in db %s -> %s\n", @$_ for @{ $rep -> { $tab } { refs } } ; } next unless $have eq 'found' ; for my $col ( @{ $tab -> _cols } ) { $have = $rep -> { $col } { have } ? 'found' : 'not found' ; if ( $opt{v} or $have ne 'found' ) { printf " column %s %s\n", $col -> _name, $have ; } if ( $opt{v} ) { printf " fkey %s -> %s\n", @$_ for @{ $rep -> { $col } { fkey } { have } } ; } printf "* missing in db : %s -> %s\n", @$_ for @{ $rep -> { $col } { fkey } { miss } } ; printf "* in db fkey : %s -> %s ;\n" . " SHOULD BE : %s -> %s\n", @$_ [ 0, 2, 0, 1 ] for @{ $rep -> { $col } { fkey } { bads } } ; } } if ( $rep -> { res } ) { printf "$prog : all tables, columns and contraints are ok\n" ; } else { printf "$prog : some tables, columns or contraints are not ok\n" ; } $Cafe -> _dbh -> disconnect () ; } sub try_ldaps { my $ldaps = $conf -> ldaps ; for my $tag ( sort keys %$ldaps ) { printf "$prog : %s\n", $ldaps -> { $tag } -> test_bind ; } } if ( $opt{u} ) { print "$prog : database user 'db_user' empty or not configured\n" unless $conf -> db_user ; try_connect $conf -> db_user, $conf -> db_pswd ; } if ( $opt{o} ) { print "$prog : database user 'db_owner' empty or not configured\n" unless $conf -> db_owner ; try_connect $conf -> db_owner, $conf -> db_opswd ; } if ( $opt{t} ) { check_tabs $conf -> db_user, $conf -> db_pswd ; } if ( $opt{l} ) { try_ldaps ; } # printf "queries %s, records %s\n", Blib -> _que_cnt, Blib -> _rec_cnt ; for my $mod ( qw(DBI DBD::Pg Net::LDAP SVN::Notify) ) { eval "use $mod ; 1" or print "$prog : error: missing module '$mod'\n" ; } for my $hook ( qw(lib/pre-commit lib/post-commit) ) { printf "$prog : MISSING : %-15s ; run setup-data\n", $hook unless -f $hook ; }