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

Last change on this file since 913 was 906, checked in by Kris Deugau, 7 years ago

/trunk

Bulk addition of "add 'the directory the script is in' to @INC" for Perls
that have dropped '.' from @INC

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