source: trunk/cgi-bin/allocate.pl@ 898

Last change on this file since 898 was 895, checked in by Kris Deugau, 8 years ago

/trunk

Commit update to allocate.pl utility for allocateBlock() API change
waaaay back in r554

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.9 KB
RevLine 
[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
10use strict;
11use warnings;
12use DBI;
13use NetAddr::IP;
14
15use Sys::Syslog;
16
[417]17# don't remove! required for GNU/FHS-ish install from tarball
18##uselib##
19
20use MyIPDB;
21
[431]22openlog "IPDBshell","pid","$IPDB::syslog_facility";
[188]23
24# Collect the username from the environment. If undefined, something
25# is Officially Hosed.
26my $authuser;
27if (!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
35my $ip_dbh;
36my $sth;
37my $errstr;
38($ip_dbh,$errstr) = connectDB_My;
[517]39die "Failed to connect to database: $errstr\n"
40 if !$ip_dbh;
41
[188]42checkDBSanity($ip_dbh);
43initIPDBGlobals($ip_dbh);
44
45# Hokay, now we can start to handle the allocation.
46
[895]47my ($cidr, $type, $vrf, $custid, $city, $desc) = @ARGV;
48my ($fbid, $fbparent);
[188]49# Check ARGV. We need some information to determine what to allocate.
[895]50if (!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
78print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
79
[895]80my %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]92my ($code,$msg) = allocateBlock($ip_dbh, %insert_args);
93
[188]94if ($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.
103finish($ip_dbh);
Note: See TracBrowser for help on using the repository browser.