named_setup.in 3.77 KB
Newer Older
1
2
3
4
5
6
7
#!/usr/bin/perl -wT
use English;
use Fcntl ':flock';

#
# Suck out virtual names and create CNAME map entries.
#
8
9
# This script always does the right thing, so it does not matter who calls it. 
#
10
11
12
13
14
15
16
# usage: named_setup
#

#
# Configure variables
#
my $TB		= "@prefix@";
17
my $TBOPS       = "@TBOPSEMAIL@";
18
19
20
21
22
23
24
25
26
27

my $mapdir      = "/etc/namedb";
my $mapfile	= "$mapdir/emulab.db";
my $mapfileback	= "$mapdir/emulab.db.backup";
my $mapfilehead = "$mapdir/emulab.db.head";
my $mapfiletail = "$mapdir/emulab.db.tail";
my $lockfile    = "/var/tmp/testbed_named_lockfile";
my $dbg		= 0;
my @row;

28
29
# We don't want to run this script unless its the real version.
if ($EUID != 0) {
30
    die("Must be root! Maybe its a development version?\n");
31
32
33
}
# XXX Hacky!
if ($TB ne "/usr/testbed") {
34
    die("Wrong version. Maybe its a development version?\n");
35
36
}

37
38
39
40
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

41
42
# Turn off line buffering on output
$| = 1;
43

44
# Testbed Support libraries
45
46
47
use lib "@prefix@/lib";
use libtestbed;
use libdb;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

#
# We need to serialize this script to avoid a trashed map file. Use
# a dummy file in /var/tmp, opened for writing and flock'ed. 
#
open(LOCK, ">>$lockfile") || fatal("Couldn't open $lockfile\n");
$count = 0;
while (flock(LOCK, LOCK_EX|LOCK_NB) == 0) {
    print "Another named map update in progress. Waiting a moment ...\n";
    if ($count++ > 20) {
	fatal("Could not get the lock after a long time!\n");
    }
    sleep(1);
}

#
# We stick the new map entries into the tail file. First zero it out.
#
open(MAP, ">$mapfiletail") || fatal("Couldn't open $mapfiletail\n");
print MAP "\n";
print MAP ";\n";
print MAP "; DO NOT EDIT below this point. Auto generated map entries!\n";
print MAP ";\n";
print MAP "\n";
print MAP "\$TTL\t1\n";

#
# Figure out the set of nodes we are working on. These are the nodes
# that have a vname in the reserved table. 
#
78
79
$db_result =
    DBQueryFatal("select node_id,pid,eid,vname from reserved where vname!=''");
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

if ($db_result->numrows > 0) {
    #
    # Create a CNAME for each reserved node.
    #
    while (@row = $db_result->fetchrow_array) {
	my $node_id = $row[0];
	my $pid     = $row[1];
	my $eid     = $row[2];
	my $vname   = $row[3];

	my $cname   = sprintf("%-40s", "$vname.$eid.$pid");
	printf MAP "$cname IN\tCNAME\t$node_id\n";
    }
}

print MAP "\n";
close(MAP);

#
# Concat the head and tail files to create the new map.
#
if (-e $mapfile) {
103
    system("mv -f $mapfile $mapfileback") == 0 or
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	fatal("Could not back up $mapfile to $mapfileback\n");
}

#
# Generate a warning so that no one tries to edit the file by hand
#
open(MAP, ">$mapfile") || fatal("Couldn't open $mapfile\n");
print MAP
    ";\n".
    "; ******************************************************************\n".
    "; DO NOT EDIT THIS FILE. IT IS A CREATION, A FIGMENT, A CONTRIVANCE!\n".
    ";\n".
    "; Edit the \"head\" file, then run ${TB}bin/named_setup.\n".
    "; ******************************************************************\n".
    ";\n";

#
# Now copy in the head part of the map, looking for the serial
# number so it can be bumped up.
#
open(MAPHEAD, "<$mapfilehead") || fatal("Couldn't open $mapfilehead\n");
while (<MAPHEAD>) {
    if ( /Serial Number/i ) {
	my $serial = `date +%s`;
	chop $serial;

	print MAP "\t\t\t$serial\t; Serial Number -- DO NOT EDIT\n";
    }
    else {
	print MAP "$_";
    }
}
close(MAPHEAD);
close(MAP);

#
# Now the tail of the map.
# 
system("cat $mapfiletail >> $mapfile") == 0 or
    fatal("Failed to concat $mapfiletail to $mapfile\n");

#
# This is better than HUPing the nameserver.
# 
system("named.reload > /dev/null") == 0 or
    fatal("named.reload failed!\n");

exit(0);

sub fatal {
    local($msg) = $_[0];

156
    SENDMAIL($TBOPS, "TESTBED: Named Setup Failed", $msg);
157
158
    die($msg);
}