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

Last change on this file since 417 was 417, checked in by Kris Deugau, 14 years ago

/trunk

Rearrangements and tweaks toward releaseability:

  • Add Makefile to install halfway-sanely
  • Add .spec file
  • Shuffle "use IPDB;" and "use MyIPDB;" lines so that we can automagically insert a suitable "use lib..." line during 'make install'
  • Check copyright statements
  • Clear up some defaults, and place a number of "constants" in IPDB/MyIPDB rather than having them hardcoded all over the place (Still need to think of a sane way to do this for ipdb.psql's alloctype preseeding)
  • Tweak $VERSION identifier in IPDB.pm so it can be defined in the Makefile
  • Clean up some dangling bits from repository history conversion, remove unneeded "use ..." statements
  • Tweak rWHOIS export script to use more globals and "constants" from (My)IPDB.pm, and more Perl internals than system()-equivalents. Add a few fixme comments for longer-term flexibility improvements.
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.6 KB
Line 
1#!/usr/bin/perl
2# Shell-based script to allocate arbitrary block
3###
4# SVN revision info
5# $Date: 2010-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $
6# SVN revision $Rev: 417 $
7# Last update by $Author: kdeugau $
8###
9
10use strict;
11use warnings;
12use DBI;
13use CommonWeb qw(:ALL);
14use NetAddr::IP;
15
16use Sys::Syslog;
17
18# don't remove! required for GNU/FHS-ish install from tarball
19##uselib##
20
21use MyIPDB;
22
23openlog "IPDBshell","pid","local2";
24
25# Collect the username from the environment. If undefined, something
26# is Officially Hosed.
27my $authuser;
28if (!defined($ENV{'USER'})) {
29 die "Bad environment! USER not defined.\n";
30} else {
31 $authuser = $ENV{'USER'};
32}
33
34# Why not a global DB handle? (And a global statement handle, as well...)
35# Use the connectDB function, otherwise we end up confusing ourselves
36my $ip_dbh;
37my $sth;
38my $errstr;
39($ip_dbh,$errstr) = connectDB_My;
40if (!$ip_dbh) {
41 printAndExit("Failed to connect to database: $errstr\n");
42}
43checkDBSanity($ip_dbh);
44initIPDBGlobals($ip_dbh);
45
46# Hokay, now we can start to handle the allocation.
47
48my ($cidr, $type, $custid, $city, $desc, $alloc_from);
49# Check ARGV. We need some information to determine what to allocate.
50if (!$ARGV[1]) {
51 # Usage message
52 print "Usage: allocate.pl [IP/subnet] [Type] [CustID] [City] [\"Description\"]\n".
53 " Further information can be entered via the web interface\n";
54 exit;
55} else {
56 $cidr = new NetAddr::IP "$ARGV[0]";
57 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr'");
58 $sth->execute;
59 my @data = $sth->fetchrow_array;
60# User deserves errors if user can't be bothered to find the free block first.
61 die "Can't allocate from outside a free block!!\n"
62 if !$data[0];
63 $alloc_from = new NetAddr::IP $data[0];
64 $sth->finish;
65 $type = $ARGV[1];
66 if (!$ARGV[4]) {
67 # Default desc
68 $desc = "DEFAULT: $disp_alloctypes{$type}";
69 } else {
70 $desc = $ARGV[4];
71 }
72 if (!$ARGV[3]) {
73 # Default city
74 $sth = $ip_dbh->prepare("select city from routed where cidr >>='$cidr'");
75 $sth->execute;
76 my @data = $sth->fetchrow_array;
77 $city = $data[0];
78 $sth->finish;
79 } else {
80 $city = $ARGV[3];
81 }
82 if (!$ARGV[2]) {
83 # Default custid - make it REAL obvious.
84 $custid = "FIXME";
85 } else {
86 $custid = $ARGV[2];
87 }
88}
89
90print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
91
92my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city,
93 $desc, '', '');
94
95if ($code eq 'OK') {
96 print "Allocation OK!\n";
97 syslog "notice", "($authuser) Allocated '$cidr' to '$custid' as '$type'";
98} else {
99 print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
100 syslog "err", "($authuser) Allocation of '$cidr' to '$custid' as '$type' failed: '$msg'";
101}
102
103# Close it down.
104finish($ip_dbh);
Note: See TracBrowser for help on using the repository browser.