newnodecheckin.php 5.77 KB
Newer Older
1
2
3
4
5
6
7
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003 University of Utah and the Flux Group.
# All rights reserved.
#
require("defs.php3");
8
require("newnode-defs.php3");
9
10
11
12
13
14
15
16
17
18

#
# Note - this script is not meant to be called by humans! It returns no useful
# information whatsoever, and expects the client to fill in all fields
# properly.
# Since this script does not cause any action to actually happen, so it's save
# to leave 'in the open' - the worst someone can do is annoy the testbed admins
# with it!
#

19
20
21
22
23
24
#
# Grab the IP address that this node has right now, so that we can contact it
# later if we need to, say, reboot it.
#
$tmpIP = getenv("REMOTE_ADDR");

25
26
27
#
# Find all interfaces
#
28
$interfaces = array();
29
30
31
32
33
34
35
foreach ($HTTP_GET_VARS as $key => $value) {
    if (preg_match("/iface(name|mac)(\d+)/",$key,$matches)) {
        $vartype = $matches[1];
    	$ifacenum = $matches[2];
    	if ($vartype == "name") {
	    if (preg_match("/^([a-z]+)(\d+)$/i",$value,$matches)) {
		$interfaces[$ifacenum]["type"] = $matches[1];
36
	        $interfaces[$ifacenum]["card"] = $ifacenum;
37
38
39
40
41
42
43
44
45
46
	    } else {
		echo "Bad interface name $value!";
		continue;
	    }
	} else {
	    $interfaces[$ifacenum]["mac"] = $value;
	}
    }
}

47
48
49
50
#
# Use one of the interfaces to see if this node seems to have already checked
# in once
#
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
if (count($interfaces)) {
    $testmac = $interfaces[0]["mac"];

    #
    # First, make sure it isn't a 'real boy' - we should let the operators know
    # about this, because there may be some problem.
    #
    $query_result = DBQueryFatal("select n.node_id from " .
	"nodes as n left join interfaces as i " .
	"on n.node_id=i.node_id " .
	"where i.mac='$testmac'");
    if  (mysql_num_rows($query_result)) {
        $row = mysql_fetch_array($query_result);
	$node_id = $row["node_id"];
        echo "Node is already a real node, named $node_id\n";
	TBMAIL($TBMAIL_OPS,"Node Checkin Error","A node attempted to check " .
	    "in as a new node, but it is already\n in the database as " .
	    "$node_id!");
	exit;
    }


    #
    # Next, try the new nodes
    #
    $query_result = DBQueryFatal("select n.new_node_id, n.node_id from " .
	"new_nodes as n left join new_interfaces as i " .
	"on n.new_node_id=i.new_node_id " .
	"where i.mac='$testmac'");

    if  (mysql_num_rows($query_result)) {
        $row = mysql_fetch_array($query_result);
	$id = $row["new_node_id"];
	$node_id = $row["node_id"];
        echo "Node has already checked in as ID $id, name $node_id\n";

	#
	# Keep the temp. IP address around in case it's gotten a new one
	#
	DBQueryFatal("update new_nodes set temporary_IP='$tmpIP' " .
	    "where new_node_id=$id");

	exit;
    }
}
96

97
98
99
100

#
# Attempt to come up with a node_id and an IP address for it
#
101
102
103
104
105
$name_info = find_free_id("pc");
$node_prefix = $name_info[0];
$node_num = $name_info[1];
$hostname = $node_prefix . $node_num;
$IP = guess_IP($node_prefix,$node_num);
106
107
108
109
110
111
112
113
114

#
# Make an educated guess as to what type it belongs to
#
$type = guess_node_type($cpuspeed,$disksize);

#
# Stash this information in the database
#
115
116
117
118
119
if ($identifier) {
    $identifier = "'$identifier'";
} else {
    $identifier = "NULL";
}
120
DBQueryFatal("insert into new_nodes set node_id='$hostname', type='$type', " .
121
122
	"IP='$IP', temporary_IP='$tmpIP', dmesg='$messages', created=now(), " .
	"identifier=$identifier");
123
124
125
126
127
128

$query_result = DBQueryFatal("select last_insert_id()");
$row = mysql_fetch_array($query_result);
$new_node_id = $row[0];

foreach ($interfaces as $interface) {
129
	$card = $interface["card"];
130
131
	$mac = $interface["mac"];
	$type = $interface["type"];
132
133
134
	DBQueryFatal("insert into new_interfaces set " .
	    "new_node_id=$new_node_id, card=$card, mac='$mac', " .
	    "interface_type='$type'");
135
136
137
138
139
140
141
}

#
# Send mail to testbed-ops about the new node
#
TBMAIL($TBMAIL_OPS,"New Node","A new node, $hostname, has checked in");

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function check_node_exists($node_id) {
    #
    # Just check to see if this node already exists in one of the
    # two tables - return 1 if it does, 0 if not
    #
    $query_result = DBQueryFatal("select node_id from nodes " .
	    "where node_id='$node_id'");
    if (mysql_num_rows($query_result)) {
	return 1;
    }
    $query_result = DBQueryFatal("select node_id from new_nodes " .
	    "where node_id='$node_id'");
    if (mysql_num_rows($query_result)) {
	return 1;
    }

    return 0;
}

161
162
163
function find_free_id($prefix) {

    #
164
165
    # First, check to see if there's a recent entry in new_nodes we can name
    # this node after
166
    #
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    $query_result = DBQueryFatal("select node_id from new_nodes " .
        "order by created desc limit 1");
    if (mysql_num_rows($query_result)) {
        $row = mysql_fetch_array($query_result);
	$old_node_id = $row[0];
	#
	# Try to figure out if this is in some format we can increment
	#
	if (preg_match("/^(.*[^\d])(\d+)$/",$old_node_id,$matches)) {
	    echo "Matches pcXXX format";
	    # pcXXX format
	    $base = $matches[1];
	    $number = $matches[2];
	    $potential_name = $base . ($number + 1);
	    if (!check_node_exists($potential_name)) {
		return array($base,($number +1));
	    }
	} elseif (preg_match("/^(.*)-([a-zA-Z])$/",$old_node_id,$matches)) {
	    # Something like WAIL's (type-rack-A) format
	    $base = $matches[1];
	    $lastchar = $matches[2];
	    $newchar = chr(ord($lastchar) + 1);
	    $potential_name = $base . '-' . $newchar;
	    if (!check_node_exists($potential_name)) {
		return array($base . '-', $newchar);
	    }
	}
    }
195
196

    #
197
    # Okay, that didn't work.
198
199
200
201
    # Just go through the nodes and new_nodes tables looking for one that
    # hasn't been used yet - put in a silly little guard to prevent an
    # infinite loop in case of bugs.
    #
202
203
    $node_number = 0;
    while ($node_number < 10000) {
204
205
	$node_number++;
    	$potential_name = $prefix . $node_number;
206
207
	if (!check_node_exists($potential_name)) {
	    break;
208
209
210
	}
    }

211
    return array($prefix, $node_number);
212
213
214
215

}

?>