Ignore:
Timestamp:
02/03/05 14:23:34 (20 years ago)
Author:
Kris Deugau
Message:

/branches/sql-cleanup

Fixed a bunch of general problems with pool allocations, and
rearranged some code, alloctypes, and the poolips table (ugh)
to more cleanly support different types of IP pool.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sql-cleanup/cgi-bin/IPDB.pm

    r148 r149  
    251251
    252252          # And initialize the pool, if necessary
     253          # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
     254          # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
    253255          if ($type =~ /^.p$/) {
    254             $msg = "Could not initialize IPs in new $disp_alloctypes{$type} pool $cidr";
    255             initPool($dbh,$cidr,$type,$city,($type eq 'dp' ? "all" : "normal"));
     256            $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
     257            my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all");
     258            die $rmsg if $code eq 'FAIL';
     259          } elsif ($type =~ /^.d$/) {
     260            $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr";
     261            my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal");
     262            die $rmsg if $code eq 'FAIL';
    256263          }
    257264
     
    261268      }; # end of eval
    262269      if ($@) {
    263         $msg = $@;
     270        $msg .= ": ".$@;
    264271        eval { $dbh->rollback; };
    265         return ('FAIL',$@);
     272        return ('FAIL',$msg);
    266273      } else {
    267274        return ('OK',"OK");
     
    367374  my $pool = new NetAddr::IP $_[1];
    368375
    369   my ($pooltype) = ($type =~ /^(.)p$/);
     376##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type
     377  $type =~ s/[pd]$/i/;
    370378  my $sth;
    371 
    372   # have to insert all pool IPs into poolips table as "unallocated".
    373   $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,ptype)".
    374         " values ('$pool', ?, '6750400', '$city', '$pooltype')");
    375   my @poolip_list = $pool->hostenum;
    376   if ($class eq 'all') { # (DSL-ish block - *all* IPs available
    377     $sth->execute($pool->addr);
    378     for (my $i=0; $i<=$#poolip_list; $i++) {
    379       $sth->execute($poolip_list[$i]->addr);
    380     }
    381     $pool--;
    382     $sth->execute($pool->addr);
    383   } else { # (real netblock)
    384     for (my $i=1; $i<=$#poolip_list; $i++) {
    385       $sth->execute($poolip_list[$i]->addr);
    386     }
     379  my $msg;
     380
     381  # Trap errors so we can pass them back to the caller.  Even if the
     382  # caller is only ever supposed to be local, and therefore already
     383  # trapping errors.  >:(
     384  local $dbh->{AutoCommit} = 0; # These need to be local so we don't
     385  local $dbh->{RaiseError} = 1; # step on our toes by accident.
     386
     387  eval {
     388    # have to insert all pool IPs into poolips table as "unallocated".
     389    $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)".
     390        " values ('$pool', ?, '6750400', '$city', '$type')");
     391    my @poolip_list = $pool->hostenum;
     392    if ($class eq 'all') { # (DSL-ish block - *all* IPs available
     393      $sth->execute($pool->addr);
     394      for (my $i=0; $i<=$#poolip_list; $i++) {
     395        $sth->execute($poolip_list[$i]->addr);
     396      }
     397      $pool--;
     398      $sth->execute($pool->addr);
     399    } else { # (real netblock)
     400      for (my $i=1; $i<=$#poolip_list; $i++) {
     401        $sth->execute($poolip_list[$i]->addr);
     402      }
     403    }
     404  };
     405  if ($@) {
     406    $msg = "'".$sth->errstr."'";
     407    eval { $dbh->rollback; };
     408    return ('FAIL',$msg);
     409  } else {
     410    return ('OK',"OK");
    387411  }
    388412} # end initPool()
     
    412436
    413437    eval {
    414       $msg = "Unable to deallocate $type $cidr";
     438      $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr";
    415439      $sth = $dbh->prepare("update poolips set custid='6750400',available='y',".
    416440        "city=(select city from allocations where cidr >>= '$cidr'),".
     
    469493        $sth->execute;
    470494        # Special case - delete pool IPs
    471         if ($type =~ /^.p$/) {
     495        if ($type =~ /^.[pd]$/) {
    472496          # We have to delete the IPs from the pool listing.
    473497          $sth = $dbh->prepare("delete from poolips where pool='$cidr'");
Note: See TracChangeset for help on using the changeset viewer.