dhcpd_makeconf.in 2.38 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
# All rights reserved.
#
8
use Getopt::Std;
Robert Ricci's avatar
Robert Ricci committed
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# dhcpd_makeconf - helper script to create dhcpd.conf files from the database.
# The template file should look like an ordinary dhcpd.conf file, but have
# the string %%nodetype=<type> where you want entries for a set of nodes
# filled out. For example:
#
# subnet 155.101.132.0 netmask 255.255.252.0 {
#	option  routers                 155.101.132.1;
#	option subnet-mask		255.255.252.0;
#
#	option dhcp-class-identifier "PXEClient";
#
#	# testbed PCs
#	group {
#		option dhcp-class-identifier "PXEClient";
#
#		%%nodetype=pc600
#
#		%%nodetype=pc850
#
#	}
# }
32
33
34
35
#
# There is an example template file in the CVS repository, in the dhcpd/
# directory.
#
36
37


Robert Ricci's avatar
Robert Ricci committed
38
39
40
#
# Configure variables
#
41
42
use lib "@prefix@/lib";
use libdb;
Robert Ricci's avatar
Robert Ricci committed
43
44

my %opt = ();
45
getopts("hv",\%opt);
Robert Ricci's avatar
Robert Ricci committed
46
47
if ($opt{h}) { exit &usage; }

48
$infile = shift @ARGV || exit &usage;
Robert Ricci's avatar
Robert Ricci committed
49
50
51
52
53
54
55
56
57
58

open(IF,"<$infile") or die "Unable to open $infile for reading\n";
while (<IF>) {
	if (/^(\s*)\%\%nodetype=(\w+)/) {
		my $spaces = $1;
		my $nodetype = $2;
		my $query = "SELECT n.node_id, i.IP, i.MAC ";
		if ($opt{v}) {
			$query .= ", r.vname ";
		}
59
60
61
		$query .= "FROM nodes AS n LEFT JOIN interfaces AS i ON ".
		    " n.node_id = i.node_id LEFT JOIN node_types AS t " .
		    " ON n.type = t.type ";
Robert Ricci's avatar
Robert Ricci committed
62
63
64
		if ($opt{v}) {
			$query .= "LEFT JOIN reserved AS r ON n.node_id = r.node_id ";
		}
65
		$query .= "WHERE (n.type='$nodetype' OR t.class='$nodetype') ";
66
		$query .= "AND i.iface = t.control_iface ";
Robert Ricci's avatar
Robert Ricci committed
67
		$query .= "ORDER BY n.priority";
68
69
70

		my $result = DBQueryFatal($query);
		while (@row = $result->fetchrow) {
Robert Ricci's avatar
Robert Ricci committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
			my $ip = $row[1];
			my $mac = $row[2];
			my $node_id;
			if ($opt{v} && $row[3]) {
				$node_id = $row[3];
			} else {
				$node_id = $row[0];
			}
				
			# Need to make MAC look right..
			$mac =~ s/(..)\B/$1:/g;

			print "${spaces}host $ip {\n";
			print "${spaces}\thardware ethernet $mac;\n";
			print "${spaces}\toption host-name \"$node_id\";\n";
			print "${spaces}\tfixed-address $ip;\n";
			print "${spaces}}\n\n";
		}
	} else {
		# It's a regular line
		print;
	}
}

sub usage {
	print "Usage: $0 [-h] [-v] <templatefile>\n";
	print "-h		Show this message\n";
	print "-v		Use virtual names, when possible, for hostnames\n";
99
100
	print "Spits out a dhcpd config file on stdout, as in:\n";
	print "$0 dhcpd.conf.template > dhcpd.conf\n";
Robert Ricci's avatar
Robert Ricci committed
101
}