Changeset 628 for trunk/cgi-bin
- Timestamp:
- 10/08/14 17:26:13 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cgi-bin/IPDB.pm
r627 r628 271 271 # Wrap all the SQL in a transaction 272 272 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) ); 274 282 275 283 if (!$mexist) { … … 277 285 ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things 278 286 ## 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')"); 280 290 281 291 # Unrouted blocks aren't associated with a city (yet). We don't rely on this 282 292 # elsewhere though; legacy data may have traps and pitfalls in it to break this. 283 293 # 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) ); 286 299 287 300 # If we get here, everything is happy. Commit changes. … … 293 306 # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it. 294 307 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"); 296 309 $sth->execute($cidr); 297 310 my @cmasters; 311 my @oldmids; 298 312 while (my @data = $sth->fetchrow_array) { 299 313 my $master = new NetAddr::IP $data[0]; 300 314 push @cmasters, $master; 315 push @oldmids, $data[1]; 301 316 $smallmask = $master->masklen if $master->masklen > $smallmask; 302 317 } … … 312 327 } 313 328 329 ##fixme: master_id 314 330 # collect the unrouted free blocks within the new master 315 331 $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'"); … … 323 339 @blocklist = Compact(@blocklist); 324 340 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 325 349 # and now insert the new data. Make sure to delete old masters too. 326 350 327 351 # 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',?,?,?)"); 331 355 foreach my $newblock (@blocklist) { 332 356 $sth->execute($newblock); 333 $sth2->execute($newblock, $ cidr, $args{vrf});334 } 335 336 # update parent relations at rdepth=1337 $ 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 # master341 $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 } 343 367 344 368 # *whew* If we got here, we likely suceeded. 345 369 $dbh->commit; 370 346 371 } # new master contained existing master(s) 347 372 }; # end eval … … 386 411 } 387 412 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); 392 418 } 393 419 } # end addMaster
Note:
See TracChangeset
for help on using the changeset viewer.