Changeset 628 for trunk/cgi-bin


Ignore:
Timestamp:
10/08/14 17:26:13 (10 years ago)
Author:
Kris Deugau
Message:

/trunk

Commit 4/mumble for work done intermittently over the past ~year.
See #5, comment 25:

  • Update IPDB::addMaster() and success/error page template
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r627 r628  
    271271  # Wrap all the SQL in a transaction
    272272  eval {
    273     my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) );
     273    # First check - does the master exist?  Ignore VRFs until we can see a sane UI
     274    my ($mcontained) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr >>= ? AND type = 'mm'",
     275        undef, ($cidr) );
     276    die "Master block $mcontained already exists and entirely contains $cidr\n"
     277        if $mcontained;
     278
     279    # Second check - does the new master contain an existing one or ones?
     280    my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr <<= ? AND type = 'mm'",
     281        undef, ($cidr) );
    274282
    275283    if (!$mexist) {
     
    277285##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
    278286## maybe a db table called "config"?
    279       $dbh->do("INSERT INTO masterblocks (cidr,rwhois,vrf,rdns) VALUES (?,?,?,?)", undef, ($cidr, 'y', $args{vrf}, $args{rdns}) );
     287      $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
     288        ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
     289      ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
    280290
    281291# Unrouted blocks aren't associated with a city (yet).  We don't rely on this
    282292# elsewhere though;  legacy data may have traps and pitfalls in it to break this.
    283293# Thus the "routed" flag.
    284       $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent,rdepth,vrf) VALUES (?,?,?,?,?,?)", undef,
    285         ($cidr, '<NULL>', 'm', $cidr, 1, $args{vrf}) );
     294      $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
     295        ($cidr, '<NULL>', 'm', $mid, $args{vrf}, $mid) );
     296
     297      # master should be its own master, so deletes directly at the master level work
     298      $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
    286299
    287300      # If we get here, everything is happy.  Commit changes.
     
    293306      # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
    294307      my $smallmask = $cidr->masklen;
    295       my $sth = $dbh->prepare("SELECT cidr FROM masterblocks WHERE cidr <<= ?");
     308      my $sth = $dbh->prepare("SELECT cidr,id FROM allocations WHERE cidr <<= ? AND type='mm' AND parent_id=0");
    296309      $sth->execute($cidr);
    297310      my @cmasters;
     311      my @oldmids;
    298312      while (my @data = $sth->fetchrow_array) {
    299313        my $master = new NetAddr::IP $data[0];
    300314        push @cmasters, $master;
     315        push @oldmids, $data[1];
    301316        $smallmask = $master->masklen if $master->masklen > $smallmask;
    302317      }
     
    312327      }
    313328
     329##fixme:  master_id
    314330      # collect the unrouted free blocks within the new master
    315331      $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'");
     
    323339      @blocklist = Compact(@blocklist);
    324340
     341      # master
     342      $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
     343        ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
     344      ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
     345
     346      # master should be its own master, so deletes directly at the master level work
     347      $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
     348
    325349      # and now insert the new data.  Make sure to delete old masters too.
    326350
    327351      # freeblocks
    328       $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ?");
    329       my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent,rdepth,vrf)".
    330         " VALUES (?,'<NULL>','m',?,1,?)");
     352      $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ? AND parent_id IN (".join(',', @oldmids).")");
     353      my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id)".
     354        " VALUES (?,'<NULL>','m',?,?,?)");
    331355      foreach my $newblock (@blocklist) {
    332356        $sth->execute($newblock);
    333         $sth2->execute($newblock, $cidr, $args{vrf});
    334       }
    335 
    336       # update parent relations at rdepth=1
    337       $dbh->do("UPDATE allocations SET parent = ? WHERE parent << ? AND rdepth=1", undef, ($cidr, $cidr) );
    338       $dbh->do("UPDATE freeblocks SET parent = ? WHERE parent << ? AND rdepth=1", undef, ($cidr, $cidr) );
    339 
    340       # master
    341       $dbh->do("DELETE FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) );
    342       $dbh->do("INSERT INTO masterblocks (cidr,rwhois,vrf,rdns) VALUES (?,?,?,?)", undef, ($cidr, 'y', $args{vrf}, $args{rdns}) );
     357        $sth2->execute($newblock, $mid, $args{vrf}, $mid);
     358      }
     359
     360      # Update immediate allocations, and remove the old parents
     361      $sth = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ?");
     362      $sth2 = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
     363      foreach my $old (@oldmids) {
     364        $sth->execute($mid, $old);
     365        $sth2->execute($old);
     366      }
    343367
    344368      # *whew*  If we got here, we likely suceeded.
    345369      $dbh->commit;
     370
    346371    } # new master contained existing master(s)
    347372  }; # end eval
     
    386411      }
    387412      if (@fails) {
    388         return ('WARN',"Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails));
    389       }
    390     }
    391     return ('OK','OK');
     413        $errstr = "Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails);
     414        return ('WARN',$mid);
     415      }
     416    }
     417    return ('OK',$mid);
    392418  }
    393419} # end addMaster
Note: See TracChangeset for help on using the changeset viewer.