moving the onelab www to a fresh location
[plewww.git] / planetlab / nodes / add_node.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 // Common functions
11 require_once 'plc_functions.php';
12 require_once 'plc_sorts.php';
13   
14 // find person roles
15 $_person= $plc->person;
16 $_roles= $_person['role_ids'];
17
18 // if not a admin, pi, or tech then redirect to node index
19 if( !(in_array( 10, $plc->person['role_ids'] ) || in_array( 20, $plc->person['role_ids'] ) || in_array( 40, $plc->person['role_ids'] )) ) {
20   header( "index.php" );
21  }
22
23
24 // this sets up which box is to be checked the first time the page is loaded
25 $method= $_POST['method'];
26 if( $method == "" )
27   $method= "static";
28
29 $model= $_POST['model'];
30 if( $model == "" )
31   $model= "Custom";
32
33
34 $submitted = false;
35
36 // if submitted validate and add
37 if( $_POST['submitted'] )
38 {
39   $submitted = true;
40
41   $errors= array();
42
43   $method = trim($_POST['method']);
44   $ip = trim($_POST['ip']);
45   $netmask = trim($_POST['netmask']);
46   $network = trim($_POST['network']);
47   $gateway = trim($_POST['gateway']);
48   $broadcast = trim($_POST['broadcast']);
49   $dns1 = trim($_POST['dns1']);
50   $dns2 = trim($_POST['dns2']);
51   $hostname = trim($_POST['hostname']);
52   $model= trim($_POST['model']);
53
54   // used to generate error strings for static fields only
55   $static_fields= array();
56   $static_fields['netmask']= "Netmask address";
57   $static_fields['network']= "Network address";
58   $static_fields['gateway']= "Gateway address";
59   $static_fields['broadcast']= "Broadcast address";
60   $static_fields['dns1']= "Primary DNS address";
61   
62   if( $method == 'static' )
63   {
64     foreach( $static_fields as $field => $desc )
65     {
66       if( trim($_POST[$field]) == "" )
67       {
68         $errors[] = "$desc is required";
69       }
70       elseif( !is_valid_ip(trim($_POST[$field])) )
71       {
72         $errors[] = "$desc is not a valid address";
73       }
74     }
75
76     if( !is_valid_network_addr($network,$netmask) )
77     {
78       $errors[] = "The network address does not coorespond to the netmask";
79     }
80   }
81
82   if( $hostname == "" )
83   {
84     $errors[] = "Hostname is required";
85   }
86
87   if( $ip == "" )
88   {
89     $errors[] = "IP is required";
90   }
91
92   if( count($errors) == 0 )
93   {
94     $success= 1;
95
96     // add new node and its interface
97     $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
98
99     $site_id= $_person['site_ids'][0];
100
101     $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
102
103     if ( $api->error() ) {
104        $errors[] = "Hostname already present or not valid";
105        $success= 0;
106     }
107     else
108     {
109       // now, try to add the network.
110       $optional_vals= array();
111       $optional_vals['is_primary']= true;
112       $optional_vals['ip']= $ip;
113       $optional_vals['type']= 'ipv4';
114       $optional_vals['method']= $method;
115     
116       if( $method == 'static' )
117       {
118       
119         $optional_vals['gateway']= $gateway;
120         $optional_vals['network']= $network;
121         $optional_vals['broadcast']= $broadcast;
122         $optional_vals['netmask']= $netmask;
123         $optional_vals['dns1']= $dns1;
124         if (!empty($dns2)) {
125           $optional_vals['dns2']= $dns2;
126         }
127       }
128
129       $interface_id= $api->AddInterface( $node_id, $optional_vals);
130       // if AddInterface fails, we have the node created,
131       // but no primary interface is present.
132       // The primary interface can be added later,
133       // but take a look at the possible Methods,
134       // if we specify TUN/TAP Method we will have
135       // an error on download of the configuration file
136     }
137   }
138
139 }
140
141
142 // Print header
143 require_once 'plc_drupal.php';
144 drupal_set_title('Nodes');
145 include 'plc_header.php';
146
147
148 ?>
149
150 <script language="javascript">
151 function updateStaticFields()
152 {
153   var is_dhcp= document.fm.method[0].checked;
154
155   document.fm.netmask.disabled= is_dhcp;
156   document.fm.network.disabled= is_dhcp;
157   document.fm.gateway.disabled= is_dhcp;
158   document.fm.broadcast.disabled= is_dhcp;
159   document.fm.dns1.disabled= is_dhcp;
160   document.fm.dns2.disabled= is_dhcp;
161 }
162 </script>
163
164 <?php
165
166 if( $success )
167 {
168 ?>
169 <h2>Node Created</h2>
170
171 <p>The node has been successfully added.
172
173 <p>View node details and download a configuration 
174 file <a href="/db/nodes/index.php?id=<?php echo $node_id ?>">here</a>.
175
176 <?
177 }
178 else
179 {
180 ?>
181
182 <h2>Add A New Node</h2>
183
184 <p>This page will allow you to add a new machine to your site. This must
185 be done before the machine is turned on, as it will allow you to download 
186 a configuration file when complete for this node.
187
188 <p>Even for DHCP, you must enter the IP address of the node.
189
190 <?php
191 if( count($errors) > 0 )
192 {
193   print( "<p><strong>The following errors occured:</strong>" );
194   print( "<font color='red' size='-1'><ul>\n" );
195   foreach( $errors as $err )
196     {
197       print( "<li>$err\n" );
198     }
199   print( "</ul></font>\n" );
200 }
201
202 $self = $_SERVER['PHP_SELF'];
203 if (!empty($_SERVER['QUERY_STRING'])) {
204   $self .= "?" . $_SERVER['QUERY_STRING'];
205 }
206
207 ?>
208
209 <form name="fm" method="post" action="<?php echo $self; ?>">
210 <input type="hidden" name="submitted" value="1">
211
212 <h3>Node Details</h3>
213
214
215 <input type="hidden" name="submitted" value="1">
216
217 <table width="100%" cellspacing="0" cellpadding="4" border="0">
218
219 <tr>
220 <td width=250>Hostname:</td>
221 <td><input type="text" name="hostname"
222 value="<?php print($hostname); ?>" size="40" maxlength="256"></td>
223 </tr>
224
225 <tr>
226 <td>Model:</td>
227 <td><input type="text" name="model"
228 value="<?php print($model); ?>" size="40" maxlength="256"></td>
229 </tr>
230
231 </table>
232
233
234 <h3>Network Settings</h3>
235
236 <table width="100%" cellspacing="0" cellpadding="4" border="0">
237
238 <tr>
239 <td valign='top' width="250">Addressing Method</td>
240 <td>
241 <input type="radio" name="method" value="dhcp" onChange='updateStaticFields()'
242 <?php if($method == 'dhcp') { echo "checked"; } ?>>DHCP 
243 <input type="radio" name="method" value="static" onChange='updateStaticFields()'
244 <?php if($method == 'static') { echo "checked"; } ?>>Static
245 </td>
246 </tr>
247
248 <tr> 
249 <td valign='top'>IP Address</td>
250 <td><input type="text" name="ip" value="<?php print($ip); ?>"></td>
251 </tr>
252
253 <tr> 
254 <td valign='top'>Netmask</font></td>
255 <td><input type="text" name="netmask" value="<?php print($netmask); ?>"></td>
256 </tr>
257
258 <tr> 
259 <td valign='top'>Network address</td>
260 <td><input type="text" name="network" value="<?php print($network); ?>">
261 </td>
262 </tr>
263
264 <tr> 
265 <td valign='top'>Gateway Address</td>
266 <td><input type="text" name="gateway" value="<?php print($gateway); ?>"></td>
267 </tr>
268
269 <tr> 
270 <td valign='top'>Broadcast address</td>
271 <td><input type="text" name="broadcast" value="<?php print($broadcast); ?>">
272 </td>
273 </tr>
274
275 <tr> 
276 <td valign='top'>Primary DNS</td>
277 <td><input type="text" name="dns1" value="<?php print($dns1); ?>">
278 </td>
279 </tr>
280
281 <tr>
282 <td valign='top'>Secondary DNS (optional)</td>
283 <td><input type="text" name="dns2" value="<?php print($dns2); ?>">
284 </td>
285 </tr>
286
287 <tr>
288 <td></td>
289 <td><input type="submit" name="Submit" value="Add"></td>
290 </tr>
291
292 </table>
293 </form>
294
295 <?php
296
297 }
298
299
300 // Print footer
301 include 'plc_footer.php';
302
303 ?>