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.

Location:
branches/sql-cleanup/cgi-bin
Files:
2 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'");
  • branches/sql-cleanup/cgi-bin/main.cgi

    r142 r149  
    354354        $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
    355355    # Allow listing of pool if desired/required.
    356     if ($data[2] =~ /^[cdsmw]p$/) {
     356    if ($data[2] =~ /^.[pd]$/) {
    357357      $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
    358358        "&pool=$data[0]\">List IPs</a>";
     
    620620        $data[3], $disp_alloctypes{$data[2]}, $data[1], $data[4]);
    621621    # If the allocation is a pool, allow listing of the IPs in the pool.
    622     if ($data[2] =~ /^[cdsmw]p$/) {
     622    if ($data[2] =~ /^.[pd]$/) {
    623623      $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
    624624        "&pool=$data[0]\">List IPs</a>";
     
    675675  my $cidr = new NetAddr::IP $webvar{pool};
    676676
     677  my ($pooltype,$poolcity);
     678
    677679  # Snag pool info for heading
    678   $sth = $ip_dbh->prepare("select * from allocations where cidr='$cidr'");
     680  $sth = $ip_dbh->prepare("select type,city from allocations where cidr='$cidr'");
    679681  $sth->execute;
    680   my @data = $sth->fetchrow_array;
    681   my $type = $data[2];  # We'll need this later.
     682  $sth->bind_columns(\$pooltype, \$poolcity);
     683  $sth->fetch() || carp $sth->errstr;
    682684
    683685  print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
    684         qq(($disp_alloctypes{$type} in $data[3])</div></center><br>\n);
    685   print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
    686   print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
     686        qq(($disp_alloctypes{$pooltype} in $poolcity)</div></center><br>\n);
     687  # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy
     688  if ($pooltype =~ /^.d$/) {
     689    print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
     690    print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
    687691        $cidr->addr."</td></tr>\n";
    688   $cidr++;
    689   print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
    690   $cidr--;  $cidr--;
    691   print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
     692    $cidr++;
     693    print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
     694    $cidr--;  $cidr--;
     695    print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
    692696        "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
    693697        "</table></div></div>\n";
     698  }
    694699
    695700# probably have to add an "edit IP allocation" link here somewhere.
     
    973978        "'$webvar{alloctype}' by $authuser failed: '$msg'";
    974979    printError("Allocation of $webvar{fullcidr} as $disp_alloctypes{$webvar{alloctype}}".
    975         " failed: $msg\n");
     980        " failed:<br>\n$msg\n");
    976981  }
    977982
Note: See TracChangeset for help on using the changeset viewer.