[188] | 1 | #!/usr/bin/perl
|
---|
| 2 | # Shell-based script to allocate arbitrary block
|
---|
| 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2016-11-18 19:00:54 +0000 (Fri, 18 Nov 2016) $
|
---|
| 6 | # SVN revision $Rev: 895 $
|
---|
| 7 | # Last update by $Author: kdeugau $
|
---|
| 8 | ###
|
---|
| 9 |
|
---|
| 10 | use strict;
|
---|
| 11 | use warnings;
|
---|
| 12 | use DBI;
|
---|
| 13 | use NetAddr::IP;
|
---|
| 14 |
|
---|
| 15 | use Sys::Syslog;
|
---|
| 16 |
|
---|
[417] | 17 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 18 | ##uselib##
|
---|
| 19 |
|
---|
| 20 | use MyIPDB;
|
---|
| 21 |
|
---|
[431] | 22 | openlog "IPDBshell","pid","$IPDB::syslog_facility";
|
---|
[188] | 23 |
|
---|
| 24 | # Collect the username from the environment. If undefined, something
|
---|
| 25 | # is Officially Hosed.
|
---|
| 26 | my $authuser;
|
---|
| 27 | if (!defined($ENV{'USER'})) {
|
---|
| 28 | die "Bad environment! USER not defined.\n";
|
---|
| 29 | } else {
|
---|
| 30 | $authuser = $ENV{'USER'};
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
| 34 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
| 35 | my $ip_dbh;
|
---|
| 36 | my $sth;
|
---|
| 37 | my $errstr;
|
---|
| 38 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
[517] | 39 | die "Failed to connect to database: $errstr\n"
|
---|
| 40 | if !$ip_dbh;
|
---|
| 41 |
|
---|
[188] | 42 | checkDBSanity($ip_dbh);
|
---|
| 43 | initIPDBGlobals($ip_dbh);
|
---|
| 44 |
|
---|
| 45 | # Hokay, now we can start to handle the allocation.
|
---|
| 46 |
|
---|
[895] | 47 | my ($cidr, $type, $vrf, $custid, $city, $desc) = @ARGV;
|
---|
| 48 | my ($fbid, $fbparent);
|
---|
[188] | 49 | # Check ARGV. We need some information to determine what to allocate.
|
---|
[895] | 50 | if (!defined($vrf)) {
|
---|
[188] | 51 | # Usage message
|
---|
[895] | 52 | print "Usage: allocate.pl <IP/subnet> <Type> <VRF> [CustID] [City] [\"Description\"]\n".
|
---|
| 53 | " IP/subnet, Type, and VRF are required\n".
|
---|
[188] | 54 | " Further information can be entered via the web interface\n";
|
---|
| 55 | exit;
|
---|
| 56 | } else {
|
---|
[895] | 57 |
|
---|
| 58 | $cidr = new NetAddr::IP $cidr;
|
---|
| 59 | ($fbid,$fbparent) = $ip_dbh->selectrow_array(
|
---|
| 60 | "SELECT id,parent_id FROM freeblocks WHERE cidr >>= ? AND vrf = ?", undef, "$cidr", $vrf)
|
---|
| 61 | or die "Couldn't find a free block to match '$cidr' in $vrf\n";
|
---|
| 62 | if (!$desc) {
|
---|
[188] | 63 | # Default desc
|
---|
| 64 | $desc = "DEFAULT: $disp_alloctypes{$type}";
|
---|
| 65 | }
|
---|
[895] | 66 | if (!$city) {
|
---|
| 67 | # Default city taken from parent allocation
|
---|
| 68 | ($city) = $ip_dbh->selectrow_array("SELECT city FROM allocations WHERE id = ?", undef, $fbparent);
|
---|
[188] | 69 | }
|
---|
[895] | 70 | if (!$custid) {
|
---|
| 71 | # See if the type has a default custID....
|
---|
| 72 | $custid = $ip_dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type = ?", undef, $type);
|
---|
| 73 | # ... and if not, make it REAL obvious this needs to be fixed.
|
---|
| 74 | $custid = "FIXME" if !$custid;
|
---|
[188] | 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
|
---|
| 79 |
|
---|
[895] | 80 | my %insert_args = (
|
---|
| 81 | cidr => "$cidr",
|
---|
| 82 | fbid => $fbid,
|
---|
| 83 | parent => $fbparent,
|
---|
| 84 | custid => $custid,
|
---|
| 85 | type => $type,
|
---|
| 86 | city => $city,
|
---|
| 87 | desc => $desc,
|
---|
| 88 | vrf => $vrf,
|
---|
| 89 | user => $authuser,
|
---|
| 90 | );
|
---|
[188] | 91 |
|
---|
[895] | 92 | my ($code,$msg) = allocateBlock($ip_dbh, %insert_args);
|
---|
| 93 |
|
---|
[188] | 94 | if ($code eq 'OK') {
|
---|
| 95 | print "Allocation OK!\n";
|
---|
| 96 | syslog "notice", "($authuser) Allocated '$cidr' to '$custid' as '$type'";
|
---|
| 97 | } else {
|
---|
| 98 | print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
|
---|
| 99 | syslog "err", "($authuser) Allocation of '$cidr' to '$custid' as '$type' failed: '$msg'";
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | # Close it down.
|
---|
| 103 | finish($ip_dbh);
|
---|