iterating on new nodes index
[plewww.git] / planetlab / nodes / index.php
1 <?php
2
3 // $Id$
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api, $adm;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 drupal_set_title('Nodes');
15 include 'plc_header.php';
16
17 // Common functions
18 require_once 'plc_functions.php';
19 require_once 'plc_sorts.php';
20
21 // find person roles
22 $_person= $plc->person;
23 $_roles= $_person['role_ids'];
24
25 ////////////////////
26 // The set of columns to fetch
27 // and the filter applied for fetching sites
28 $columns = array( "node_id", "hostname", "boot_state", "peer_id" ) ;
29 $filter = array("node_type"=>"regular");
30 if ( in_array( '10', $_roles ) || in_array('20', $_roles) || in_array('40',$_roles)) {
31   // admins, PIs and techs can see interface details
32   $columns [] = "interface_ids";
33  }
34
35 //////////////////
36 // perform post-processing on objects as returned by GetNodes
37 // performs sanity check and summarize the result in a single column
38 // performs in-place replacement, so passes a reference
39 function layout_node ($node) {
40
41   // we need the 'interface_ids' field to do this
42   // so regular users wont run this
43   if ( ! array_key_exists ('interface_ids', $node))
44     return $node;
45     
46   $messages=array();
47   
48   // do all this stuff on local nodes only
49   if ( ! $node['peer_id'] ) {
50     // check that the node has keys
51     if (count($node['interface_ids']) == 0)
52       $messages [] = "No interface";
53     
54   }
55   // but always cleanup $node columns
56   unset ($node['interface_ids']);
57   $node['status'] = plc_make_table('plc-warning',$messages);
58   $node['comon'] = plc_comon_button("node_id",$node['node_id']);
59   return $node;
60 }
61
62 // if nodepattern is set then set id to that node's id.
63 // we use GET rather than POST so paginate can display the right contents on subsequent pages
64 // can be useful for writing bookmarkable URL's as well
65 if( $_GET['nodepattern'] || $_GET['peerscope']) {
66
67   // nodepattern
68   $nodepattern= $_GET['nodepattern'];
69   // need to use a hash filter for patterns to be properly handled
70   if (empty($nodepattern)) { 
71     $nodepattern="*";
72   }
73   $filter = array_merge (array( "hostname"=>$nodepattern ), $filter);
74
75   // peerscope
76   list ( $peer_filter, $peer_label) = plc_peer_info($api,$_GET['peerscope']);
77   $filter=array_merge($filter,$peer_filter);
78
79   $nodes= $api->GetNodes($filter, $columns);
80   $nodes_count = count ($nodes);
81   if ( $nodes_count == 1) {
82     header( "location: index.php?id=". $nodes[0]['node_id'] );
83     exit();
84   } else if ( $nodes_count == 0) {
85     echo "<span class='plc-warning'> No node matching $nodepattern </span>";
86   } else {
87     drupal_set_title ("Nodes matching $nodepattern on". $peer_label);
88     $nodes = array_map(layout_node,$nodes);
89     sort_nodes ($nodes);
90     echo paginate( $nodes, "node_id", "Nodes", 25, "hostname");
91   }
92 }
93 // if a site_id is given, display the site nodes only
94 else if( $_GET['site_id'] ) {
95
96   $site_id= $_GET['site_id'];
97
98   // Get site info
99   $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "node_ids" ) );
100   drupal_set_title("Nodes on site " . $site_info[0]['name']);
101
102   // Get site nodes
103   $nodes= $api->GetNodes( array_merge(array('node_id'=>$site_info[0]['node_ids']),$filter), $columns);
104
105   if ( empty ($nodes) ) {
106     echo "No node to display";
107   } else {
108
109     $nodes = array_map(layout_node,$nodes);
110     sort_nodes( $nodes );       
111
112     echo paginate( $nodes, "node_id", "Nodes", 25, "hostname");
113   }
114
115 }
116 // if a slice_id is given, display only the nodes related to this slice
117 else if( $_GET['slice_id'] ) {
118
119   $slice_id= $_GET['slice_id'];
120
121   // Get slice infos
122   $slice_info= $api->GetSlices( array( intval( $slice_id ) ), array( "name", "node_ids" ) );
123   drupal_set_title($slice_info[0]['name']."run on");
124
125   // Get slice nodes
126   $nodes= $api->GetNodes( array_merge(array('node_id'=>$slice_info[0]['node_ids']),$filter), $columns);
127
128   if ( empty ($nodes) ) {
129     echo "No node to display";
130   } else {
131
132     $nodes = array_map(layout_node,$nodes);
133     sort_nodes( $nodes );       
134
135     echo paginate( $nodes, "node_id", "Nodes", 25, "hostname");
136     echo "<br /><p><a href='/db/slices/index.php?id=".$slice_id.">Back to slice page</a></div>";
137   }
138   
139  }
140 // if no node id, display list of nodes to choose
141 elseif( !$_GET['id'] ) {
142
143   // GetNodes API call
144   $nodes= $api->GetNodes( empty($filter) ? NULL : $filter, $columns );
145
146   if ( empty ($nodes) ) {
147     echo "No node to display";
148   } else {
149     
150     $nodes = array_map(layout_node,$nodes);
151     sort_nodes( $nodes );
152
153     drupal_set_html_head('<script type="text/javascript" src="/planetlab/bsn/bsn.Ajax.js"></script>
154     <script type="text/javascript" src="/planetlab/bsn/bsn.DOM.js"></script>
155     <script type="text/javascript" src="/planetlab/bsn/bsn.AutoSuggest.js"></script>');
156
157     echo "<div>\n
158         <form method=get action='index.php'>\n";
159     echo "<table><tr>\n
160 <th><label for='testinput'>Enter hostname or pattern: : </label></th>\n
161 <td><input type='text' id='testinput' name='nodepattern' size=40 value='' /></td>\n
162 <td rowspan=2><input type=submit value='Select Node' /></td>\n
163 </tr> <tr>
164 <th><label for='peerscope'>Federation scope: </label></th>\n
165 <td><select id='peerscope' name='peerscope' onChange='submit()'>\n
166 ";
167     echo plc_peers_option_list($api);
168     echo "</select></td>\n
169          </tr></table></form></div>\n
170           <br />\n";
171
172     echo paginate( $nodes, "node_id", "Nodes", 25, "hostname" );
173
174     echo "<script type=\"text/javascript\">\n
175 var options = {\n
176         script:\"/planetlab/nodes/test.php?\",\n
177         varname:\"input\",\n
178         minchars:1\n
179 };\n
180 var as = new AutoSuggest('testinput', options);\n
181 </script>\n";
182   }
183 }
184
185 if ( $_GET['id'] ) {
186   // get the node id from the URL
187   $node_id= intval( $_GET['id'] );
188
189   // make the api call to pull that nodes DATA
190   $node_info= $api->GetNodes( array( $node_id ) );
191
192   if (empty ($node_info)) {
193     echo "No such node.";
194   } else {
195
196     // node info
197     $hostname= $node_info[0]['hostname'];
198     $boot_state= $node_info[0]['boot_state'];
199     $site_id= $node_info[0]['site_id'];
200     $model= $node_info[0]['model'];
201     $version= $node_info[0]['version'];
202
203     // arrays of ids of node info
204     $slice_ids= $node_info[0]['slice_ids'];
205     $conf_file_ids= $node_info[0]['conf_file_ids'];
206     $interface_ids= $node_info[0]['interface_ids'];
207     $nodegroup_ids= $node_info[0]['nodegroup_ids'];
208     $pcu_ids= $node_info[0]['pcu_ids'];
209     $ports= $node_info[0]['ports'];
210
211     // get peer
212     $peer_id= $node_info[0]['peer_id'];
213
214     // gets site info
215     $site_info= $api->GetSites( array( $site_id ) );
216     $site_name= $site_info[0]['name'];
217     $site_nodes= $site_info[0]['node_ids'];
218
219     if( !empty( $site_nodes ) ) {
220       // get site node info basics
221       $site_node_list= $api->GetNodes( $site_nodes );
222
223       foreach( $site_node_list as $s_node ) {
224         $site_node[$s_node['node_id']]= $s_node['hostname'];
225       }
226     }
227
228     // gets slice info for each slice
229     if( !empty( $slice_ids ) )
230       $slice_info= $api->GetSlices( $slice_ids, array( "slice_id", "name" , "peer_id" ) );
231
232     // gets conf file info
233     if( !empty( $conf_file_ids ) )
234       $conf_files= $api->GetConfFiles( $conf_file_ids );
235
236     // get interface info
237     if( !empty( $interface_ids ) )
238       $interfaces= $api->GetInterfaces( $interface_ids );
239
240     // gets nodegroup info
241     if( !empty( $nodegroup_ids ) )
242       $node_groups= $api->GetNodeGroups( $nodegroup_ids );
243
244     // xxx Thierry : disabling call to GetEvents, that gets the session deleted in the DB
245     // gets events 
246     //  $filter= array( "object_type"=>"Node", "object_id"=>$node_id );
247     //  $fields= array( "event_id", "person_id", "fault_code", "call_name", "call", "message", "time" );
248     //
249     //  $event_info= $api->GetEvents( $filter, $fields );
250
251     // gets pcu and port info key to both is $pcu_id
252     if( !empty( $pcu_ids ) )
253       $PCUs= $api->GetPCUs( $pcu_ids );
254
255
256     // display node info
257     if ( $peer_id) {
258       echo "<div class=plc-foreign>";
259     }
260   
261     drupal_set_title("Node " . $hostname . " details");
262
263     $is_admin = in_array( 10, $_roles );
264     $is_pi = in_array( 20, $_roles );
265     $is_tech = in_array( 40, $_roles );
266     $in_site = in_array( $site_id, $_person['site_ids'] );
267
268     // available actions
269     if ( ! $peer_id  && ( $is_admin  || ( ($is_pi||$is_tech) && $in_site ) ) )  {
270       
271       // the javascript callback we set on the form; this
272       // (*) checks whether we clicked on 'delete'
273       // (*) in this case performs a javascript 'confirm'
274       // (*) then, notice that if we select delete, then cancel, we can select back 'Choose action' 
275       //     so submit only when value is not empty
276       $change='if (document.basic.action.value=="delete") if (! confirm("Are you sure you want to delete ' . $hostname . ' ? ") ) return false; if (document.basic.action.value!="") submit();';
277     
278       echo "<table><tr><td>";
279       if ($is_admin) {
280         echo plc_event_button("Node","node",$node_id);
281         echo "</td><td>";
282       }
283       echo plc_comon_button("node_id",$node_id);
284       echo "</td><td>";
285       echo "<form name='basic' action='/db/nodes/node_actions.php' method='post'>\n";
286       echo "<input type=hidden name='node_id' value='$node_id'></input>\n";
287       echo "<select name='action' onChange='$change'>\n";
288       echo "<option value='' selected='selected'> Choose Action </option>\n";
289       echo "<option value='prompt-update'> Update $hostname </option>\n";
290       echo "<option value='delete'> Delete $hostname </option>\n";
291       echo "</select></form>\n";
292
293       echo "</td></tr></table>";
294     }    
295
296     echo "<hr />";
297     echo "<table><tbody>\n";
298
299     echo "<tr><th>Hostname: </th><td> $hostname </td></tr>\n";
300     echo "<tr><th>Model: </th><td> $model</td></tr>\n";
301     echo "<tr><th>Version: </th><td> $version</td></tr>\n";
302     
303     echo "<tr><th>Boot State: </th><td>";
304     if ($peer_id) {
305       echo $boot_state;
306     } else {
307       echo "<form name='bootstate' action='/db/nodes/node_actions.php' method=post>\n";
308       echo "<input type=hidden name='node_id' value='$node_id'>\n";
309       echo "<input type=hidden name='action' value='boot-state'>\n";
310       echo "<select name='boot_state' onChange=\"submit();\">\n";
311
312       $states= array( 'boot'=>'Boot', 'dbg'=>'Debug', 'inst'=>'Install', 'rins'=>'Reinstall', 'rcnf'=>'Reconfigure', 'new'=>'New' );
313
314       foreach( $states as $key => $val ) {
315         echo "<option value='$key'";
316       
317         if( $key == $boot_state )
318           echo " selected";
319       
320         echo ">$val</option>\n";
321       
322       }
323   
324       echo "</select></input></form>";
325     }
326     echo "</td></tr>\n";
327
328     if ( ! $peer_id  && ( $is_admin  || ( ($is_pi||$is_tech) && $in_site ) ) )  {
329       // handle legacy API
330       if ( ! method_exists ($api,"GetBootMedium")) {
331         $new_api_only=" disabled='disabled' ";
332       }
333
334       echo "<tr><th>Download </th><td>";
335       echo "<form name='download' action='/db/nodes/node_actions.php' method='post'>\n";
336       echo "<input type=hidden name='node_id' value='$node_id'></input>\n";
337       echo "<select name='action' onChange='submit();'>\n";
338       echo "<option value='' selected='selected'> Download Mode </option>\n";
339       echo "<option value='' disabled='disabled'> -- All in one images -- </option>"; 
340       echo "<option value='download-node-iso' $new_api_only> Download ISO image for $hostname</option>\n";
341       echo "<option value='download-node-usb' $new_api_only> Download USB image for $hostname</option>\n";
342       echo "<option value='' disabled='disabled'> -- Floppy + generic image -- </option>"; 
343       echo "<option value='download-node-floppy'> Download Floppy file for $hostname</option>\n";
344       echo "<option value='download-generic-iso' $new_api_only> Download generic ISO image (requires floppy) </option>\n";
345       echo "<option value='download-generic-usb' $new_api_only> Download generic USB image (requires floppy) </option>\n";
346       echo "</select></form>";
347       echo "</td></tr>\n";
348
349     }
350
351     // site info and all site nodes
352     echo "<tr><td colspan=2>&nbsp;</td></tr>\n";
353     echo "<tr><th>Site: </th><td> <a href='/db/sites/index.php?id=$site_id'>$site_name</a></td></tr>\n";
354     echo "<tr><th>All site nodes: </th><td>";
355     if (empty($site_node)) {
356       echo "<span class='plc-warning'>Site has no node</span>";
357     } else {
358       foreach( $site_node as $key => $val ) {
359         echo "<a href=index.php?id=$key>$val</a><br />";
360       }
361     }
362     echo "</td></tr>\n";
363
364     echo "</tbody></table><br />\n";
365
366     if ( ! $peer_id ) {
367
368       echo "<hr />\n";
369
370       // display interfaces
371       if( $interfaces ) {
372         echo "<p><table class='list_set' border=0 cellpadding=2>\n";
373         echo "<caption class='list_set'>Interfaces</caption>\n";
374         echo "<thead><tr class='list_set'>";
375         // placeholder for the delete buttons
376         if ( $is_admin || ($is_pi && $in_site)) {
377           echo "<th class='list_set'></th>";
378         }
379         echo "<th class='list_set'>IP Address</th><th class='list_set'>Method</th><th class='list_set'>Type</th><th class='list_set'>MAC</th><th class='list_set'>Bandwidth Limit</th></tr></thead><tbody>\n";
380
381         foreach( $interfaces as $interface ) {
382           $nn_id= $interface['interface_id'];
383           $nn_ip= $interface['ip'];
384           $nn_broad= $interface['broadcast'];
385           $nn_primary= $interface['is_primary'];
386           $nn_network= $interface['network'];
387           $nn_dns1= $interface['dns1'];
388           $nn_dns2= $interface['dns2'];
389           $nn_hostname= $interface['hostname'];
390           $nn_netmaks= $interface['netmask'];
391           $nn_gatewary= $interface['gateway'];
392           $nn_mac= $interface['mac'];
393           $nn_bwlimit= $interface['bwlimit'];
394           $nn_type= $interface['type'];
395           $nn_method= $interface['method'];
396
397           echo "<tr class='list_set'>";
398           if ( $is_admin || ($is_pi && $in_site)) {
399             echo "<td class='list_set'>";
400             if (!$nn_primary) {
401               echo plc_delete_link_button('interfaces.php?id=' . $nn_id . '&delete=1&submitted=1', '\\nInterface ' . $nn_ip);
402             } else {
403               echo '<span title="This interface is primary"> P </span>';
404             }
405             echo "</td>";
406           }
407           echo "<td class='list_set'>";
408           if( $is_admin || $is_pi || $is_tech ) {
409             echo "<a href='interfaces.php?id=$nn_id'>$nn_ip</a>";
410           } else {
411             echo "</td><td class='list_set'>$nn_ip</td>";
412           }
413           echo "<td class='list_set'>$nn_method</td><td class='list_set'>$nn_type</td><td class='list_set'>$nn_mac</td><td class='list_set'>$nn_bwlimit</td></tr>\n";
414         }
415
416         echo "</tbody></table>\n";
417
418       } else {
419         echo "<p><span class='plc-warning'>No interface</span>.  Please add an interface to make this a usable PLC node</p>.\n";
420       }
421
422       echo "<br /><a href='interfaces.php?node_id=$node_id'>Add an interface</a>.\n";
423       echo "<br /><hr />\n";
424     }
425
426     // display node group info
427     if( !empty( $node_groups ) ) {
428
429       echo "<p><table border=0 cellpadding=3>\n<caption>Node Groups</caption>\n<thead><tr><th>Name</th><th>Description</th>";
430       if( in_array( 10, $_roles ) ) echo "<th></th><th></th>";
431       echo "</tr></thead><tbody>\n";
432
433       foreach( $node_groups as $node_group ) {
434         echo "<tr><td><a href='/db/nodes/node_groups.php?id=". $node_group['nodegroup_id'] ."'>". $node_group['name'] ."</a></td><td>". $node_group['description'] ."</td>";
435
436         if( in_array( '10', $_roles ) || ( in_array( 20, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) || ( in_array( 40, $_roles ) && in_array( $site_id, $_person['site_ids'] ) ) ) 
437           echo "<td><a href='node_groups.php?nodegroup_id=". $node_group['nodegroup_id'] ."'>Update</a></td><td><a href='node_groups.php?remove=$node_id&nodegrop_id=". $node_group['nodegroup_id'] ."' onclick=\"javascript:return confirm('Are you sure you want to remove ". $hostname ." from ". $node_group['name'] ."?')\">remove</a></td>";
438
439         echo "</tr>\n";
440
441       }
442       echo "</tbody></table><br />\n";
443
444     } else {
445       echo "<p><span class='plc-warning'>This node is not in any nodegroup.</span></p>\n";
446     }    
447
448     // select list for adding to node group
449     // get nodegroup info
450     $full_ng_info= $api->GetNodeGroups( NULL );
451     if( empty( $node_groups ) ) {
452       $person_ng= $full_ng_info;
453     } else {
454       $person_ng= arr_diff( $full_ng_info, $node_groups );
455     }
456
457     sort_nodegroups( $person_ng );
458
459     if( !empty( $person_ng ) ) {
460       echo "<p>Select nodegroup to add this node to.<br />\n";
461       echo "<select name='ng_add' onChange='submit()'>\n<option value=''>Choose node group...</option>\n";
462     
463       foreach( $person_ng as $ngs ) {
464         echo "<option value=". $ngs['nodegroup_id'] .">". $ngs['name'] ."</option>\n";
465       }
466       echo "</select>\n";
467     }
468
469
470     // display slices
471     echo "<br /><hr />\n";
472     if( !empty( $slice_info ) ) {
473       sort_slices( $slice_info );
474       echo paginate( $slice_info, "slice_id", "Slices", 15, "name", "slivers", $node_id );
475     } else {
476       echo "<p><span class='plc-warning'>This node is not associated to any slice.</span></p>\n";
477     }
478
479
480     // display events - disabled, see GetEvents above
481     if( !empty( $event_info ) ) {
482       echo "<br /><hr />\n";
483       echo "<p><table class='list_set' border=0 cellpadding=2>\n<caption class='list_set'>Node Events</caption>\n<thead><tr class='list_set'><th class='list_set'></th><th class='list_set'>Call Name</th><th class='list_set'>Call</th><th class='list_set'>Message</th><th class='list_set'>Time</th></tr></thead><tbody>\n";
484
485       // display event on rows of table
486       foreach( $event_info as $event ) {
487         echo "<tr><td>". $event['call'] ."</td><td>". $event['message'] ."</td><td>". $event['time'] ."</td></tr>\n";
488       }
489       echo "</tbody></table>\n";
490     }
491
492     if ( $peer_id ) {
493       echo "</div>";
494     }
495   }
496   if( $peer_id )
497     echo "<br /></div>";
498   
499   echo "<br /><hr /><p><a href='/db/nodes/index.php'>Back to nodes list</a></div>";
500   
501  }
502
503 // Print footer
504 include 'plc_footer.php';
505
506 ?>