Changeset 465


Ignore:
Timestamp:
03/12/13 11:44:32 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Object conversion of DNSDB.pm, 1 of <mumble>. See #11.

  • object initialization and shutdown
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r464 r465  
    218218
    219219##
     220## Constructor and destructor
     221##
     222
     223sub new {
     224  my $this = shift;
     225  my $class = ref($this) || $this;
     226  my %args = @_;
     227##fixme?  to ponder:  do we do some magic if the caller sets eg dbname to prevent parsing of the config file?
     228  if (!loadConfig(basename => $args{configfile})) {
     229    warn "Using default configuration;  unable to load custom settings: $errstr\n";
     230  }
     231  my $self = \%config;
     232  $self->{configfile} = $args{configfile};
     233  bless $self, $class;
     234  $self->{dbh} = connectDB($self->{dbname}, $self->{dbuser}, $self->{dbpass}, $self->{dbhost}) or return;
     235  $self->initGlobals();
     236
     237  return $self;
     238}
     239
     240sub DESTROY {
     241  my $self = shift;
     242  $self->{dbh}->disconnect;
     243}
     244
     245##
    220246## utility functions
    221247##
     
    11951221##
    11961222
    1197 
    11981223## DNSDB::loadConfig()
    11991224# Load the minimum required initial state (DB connect info) from a config file
     
    12011226# Takes an optional hash that may contain:
    12021227#  - basename and config path to look for
    1203 #  - RPC flag (saves parsing the more complex RPC bits if not needed)
    12041228# Populates the %config and %def hashes
    12051229sub loadConfig {
    12061230  my %args = @_;
    1207   $args{basename} = '' if !$args{basename};
    1208   $args{rpcflag} = '' if !$args{rpcflag};
    1209 ##fixme  $args{basename} isn't doing what I think I thought I was trying to do.
     1231  $args{configfile} = '' if !$args{configfile};
     1232
     1233##fixme  this is *intended* to load a system-default config template, and allow
     1234# overriding on a per-tool or per-web-UI-instance basis with a secondary config
     1235# file.  The "default" config file can't be deleted in the current form.
    12101236
    12111237  my $deferr = '';      # place to put error from default config file in case we can't find either one
    12121238
    12131239  my $configroot = "/etc/dnsdb";        ##CFG_LEAF##
    1214   $configroot = '' if $args{basename} =~ m|^/|;
    1215   $args{basename} .= ".conf" if $args{basename} !~ /\.conf$/;
     1240  $configroot = '' if $args{configfile} =~ m|^/|;  # allow passed siteconfig to specify an arbitrary absolute path
     1241  $args{configfile} .= ".conf" if $args{configfile} !~ /\.conf$/;
    12161242  my $defconfig = "$configroot/dnsdb.conf";
    1217   my $siteconfig = "$configroot/$args{basename}";
     1243  my $siteconfig = "$configroot/$args{configfile}";
    12181244
    12191245  # System defaults
    1220   __cfgload("$defconfig", $args{rpcflag}) or $deferr = $errstr;
     1246  __cfgload("$defconfig") or $deferr = $errstr;
    12211247
    12221248  # Per-site-ish settings.
    1223   if ($args{basename} ne '.conf') {
    1224     unless (__cfgload("$siteconfig"), $args{rpcflag}) {
     1249  if ($args{configfile} ne '.conf') {
     1250    unless (__cfgload("$siteconfig")) {
    12251251      $errstr = ($deferr ? "Error opening default config file $defconfig: $deferr\n" : '').
    12261252        "Error opening site config file $siteconfig";
     
    12541280## DNSDB::__cfgload()
    12551281# Private sub to parse a config file and load it into %config
    1256 # Takes a file handle on an open config file
     1282# Takes a filename
    12571283sub __cfgload {
    12581284  $errstr = '';
    12591285  my $cfgfile = shift;
    1260   my $rpcflag = shift;
    12611286
    12621287  if (open CFG, "<$cfgfile") {
     
    12971322      $config{exportcache}      = $1 if m{^exportcache\s*=\s*([a-z0-9/_.-]+)}i;
    12981323      # RPC options
    1299       if ($rpcflag && /^rpc/) {
    1300         if (my ($tmp) = /^rpc_iplist\s*=\s*(.+)/i) {
    1301           my @ips = split /[,\s]+/, $tmp;
    1302           my $rpcsys = shift @ips;
    1303           push @{$config{rpcacl}{$rpcsys}}, @ips;
    1304         }
    1305         $config{rpcmode} = $1 if /^rpc_mode\s*=\s*(socket|HTTP|XMLRPC)\s*$/i;
     1324      $config{rpcmode}          = $1 if /^rpc_mode\s*=\s*(socket|HTTP|XMLRPC)\s*$/i;
     1325      if (my ($tmp) = /^rpc_iplist\s*=\s*(.+)/i) {
     1326        my @ips = split /[,\s]+/, $tmp;
     1327        my $rpcsys = shift @ips;
     1328        push @{$config{rpcacl}{$rpcsys}}, @ips;
    13061329      }
    13071330    }
     
    13181341# Creates connection to DNS database.
    13191342# Requires the database name, username, and password.
    1320 # Returns a handle to the db.
     1343# Returns a handle to the db or undef on failure.
    13211344# Set up for a PostgreSQL db;  could be any transactional DBMS with the
    13221345# right changes.
     1346# Called by new();  not intended to be called publicly.
    13231347sub connectDB {
    13241348  $errstr = '';
     
    13371361        AutoCommit => 1,
    13381362        PrintError => 0
    1339         })
    1340     or return (undef, $DBI::errstr) if(!$dbh);
    1341 
     1363        });
     1364  if (!$dbh) {
     1365    $errstr = $DBI::errstr;
     1366    return;
     1367  }
     1368#) if(!$dbh);
     1369
     1370  local $dbh->{RaiseError} = 1;
     1371
     1372  eval {
    13421373##fixme:  initialize the DB if we can't find the table (since, by definition, there's
    13431374# nothing there if we can't select from it...)
    1344   my $tblsth = $dbh->prepare("SELECT count(*) FROM pg_catalog.pg_class WHERE relkind='r' AND relname=?");
    1345   my ($tblcount) = $dbh->selectrow_array($tblsth, undef, ('misc'));
    1346   return (undef,$DBI::errstr) if $dbh->err;
     1375    my $tblsth = $dbh->prepare("SELECT count(*) FROM pg_catalog.pg_class WHERE relkind='r' AND relname=?");
     1376    my ($tblcount) = $dbh->selectrow_array($tblsth, undef, ('misc'));
     1377#  return (undef,$DBI::errstr) if $dbh->err;
    13471378
    13481379#if ($tblcount == 0) {
     
    13511382#}
    13521383
    1353 
    13541384# Return here if we can't select.
    13551385# This should retrieve the dbversion key.
    1356   my $sth = $dbh->prepare("SELECT key,value FROM misc WHERE misc_id=1");
    1357   $sth->execute();
    1358   return (undef,$DBI::errstr) if ($sth->err);
     1386    my $sth = $dbh->prepare("SELECT key,value FROM misc WHERE misc_id=1");
     1387    $sth->execute();
     1388#  return (undef,$DBI::errstr) if ($sth->err);
    13591389
    13601390##fixme:  do stuff to the DB on version mismatch
     
    13651395# See if the select returned anything (or null data).  This should
    13661396# succeed if the select executed, but...
    1367   $sth->fetchrow();
    1368   return (undef,$DBI::errstr)  if ($sth->err);
    1369 
    1370   $sth->finish;
     1397    $sth->fetchrow();
     1398#  return (undef,$DBI::errstr)  if ($sth->err);
     1399
     1400    $sth->finish;
     1401
     1402  }; # wrapped DB checks
     1403  if ($@) {
     1404    $errstr = $@;
     1405    return;
     1406  }
    13711407
    13721408# If we get here, we should be OK.
    1373   return ($dbh,"DB connection OK");
     1409  return $dbh;
    13741410} # end connectDB
    13751411
     
    13791415# Requires a database handle
    13801416sub finish {
    1381   my $dbh = $_[0];
    1382   $dbh->disconnect;
     1417  my $self = shift;
     1418  $self->{dbh}->disconnect;
    13831419} # end finish
    13841420
     
    13871423# Initialize global variables
    13881424# NB: this does NOT include web-specific session variables!
    1389 # Requires a database handle
    13901425sub initGlobals {
    1391   my $dbh = shift;
     1426  my $self = shift;
     1427  my $dbh = $self->{dbh};
    13921428
    13931429# load record types from database
Note: See TracChangeset for help on using the changeset viewer.