display node's hrn as well
[plewww.git] / planetlab / nodes / 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 // Print header
11 require_once 'plc_drupal.php';
12 include 'plc_header.php';
13
14 // Common functions
15 require_once 'plc_functions.php';
16 require_once 'plc_peers.php';
17 require_once 'linetabs.php';
18 require_once 'table.php';
19 require_once 'details.php';
20 require_once 'form.php';
21 require_once 'toggle.php';
22 require_once 'plc_objects.php';
23
24 // -------------------- 
25 // recognized URL arguments
26 $node_id=intval($_GET['id']);
27 if ( ! $node_id ) { plc_error('Malformed URL - id not set'); return; }
28
29 ////////////////////
30 // Need to mention columns explicitly as we want hrn which is a tag
31 $columns=array ('hostname','boot_state','run_level','site_id','model','node_type','version',
32                 'slice_ids','conf_file_ids','interface_ids','nodegroup_ids','peer_id',
33                 'pcu_ids','ports','hrn');
34 $nodes= $api->GetNodes( array($node_id),$columns);
35
36 if (empty($nodes)) {
37   drupal_set_message ("Node " . $node_id . " not found");
38   return;
39  }
40
41 $node=$nodes[0];
42 // node info
43 $hostname= $node['hostname'];
44 $hrn=$node['hrn'];
45 $boot_state= $node['boot_state'];
46 $run_level = $node['run_level'];
47 $site_id= $node['site_id'];
48 $model= $node['model'];
49 $node_type= $node['node_type'];
50 $version= $node['version'];
51
52 // arrays of ids of node info
53 $slice_ids= $node['slice_ids'];
54 $conf_file_ids= $node['conf_file_ids'];
55 $interface_ids= $node['interface_ids'];
56 $nodegroup_ids= $node['nodegroup_ids'];
57
58 // get peers
59 $peer_id = $node['peer_id'];
60 $peers=new Peers ($api);
61
62 // gets site info
63 $sites= $api->GetSites( array( $site_id ) );
64 $site=$sites[0];
65 $site_name= $site['name'];
66 $site_node_ids= $site['node_ids'];
67
68 // hash node_id=>hostname for this site's nodes
69 $site_node_hash=array();
70 if( !empty( $site_node_ids ) ) {
71   // get site node info basics
72   $site_nodes= $api->GetNodes( $site_node_ids );
73     
74   foreach( $site_nodes as $site_node ) {
75     $site_node_hash[$site_node['node_id']]= $site_node['hostname'];
76   }
77  }
78   
79 // gets slice info for each slice
80 if( !empty( $slice_ids ) )
81   $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" , "peer_id" ) );
82
83 // get interface info
84 if( !empty( $interface_ids ) )
85   $interfaces= $api->GetInterfaces( $interface_ids );
86
87 // gets nodegroup info
88 if( !empty( $nodegroup_ids ) )
89   $nodegroups= $api->GetNodeGroups( $nodegroup_ids, array("groupname","tag_type_id","value"));
90
91 // Thierry : remaining stuff
92 // (*) events: xxx todo xxx for admins xxx 
93 // should display the latest events relating to that node.
94 // historically we had to turn this off at some point as GetEvents was getting the session deleted in the DB
95 // (*) conf_files: xxx todo xxx for admins xxx
96 //if( !empty( $conf_file_ids ) )
97 //  $conf_files= $api->GetConfFiles( $conf_file_ids );
98
99 //////////////////// display node info
100
101 drupal_set_title("Details for node " . $hostname);
102 $local_peer= ! $peer_id;
103
104   
105 // extra privileges to admins, and (pi||tech) on this site
106 $admin_privileges=(plc_is_admin () && $local_peer);
107 $privileges =  $admin_privileges || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
108   
109 $tabs=array();
110 // available actions
111 $tabs [] = tab_nodes_site($site_id);
112 $tabs [] = tab_site($site_id);
113 //$tabs [] = tab_nodes();
114
115 if ( $local_peer  && $privileges ) {
116     
117   $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
118                                'bubble'=>"Define new network interface on $hostname");
119   $tabs['Delete'] = array ('url'=>l_actions(),
120                            'method'=>'POST',
121                            'values'=>array('action'=>'delete-node','node_id'=>$node_id),
122                            'bubble'=>"Delete node $hostname",
123                            'confirm'=>'Are you sure to delete ' . $hostname);
124   $tabs["Events"]=array_merge(tablook_event(),
125                               array('url'=>l_event("Node","node",$node_id),
126                                     'bubble'=>"Events for node $hostname"));
127   $tabs["Comon"]=array_merge(tablook_comon(),
128                              array('url'=>l_comon("node_id",$node_id),
129                                    'bubble'=>"Comon page about node $hostname"));
130  }
131
132 plekit_linetabs($tabs);
133
134 // show gray background on foreign objects : start a <div> with proper class
135 $peers->block_start ($peer_id);
136   
137 $toggle = new PlekitToggle ('node',"Details",
138                             array('bubble'=>'Display and modify details for that node',
139                                   'visible'=>get_arg('show_details')));
140 $toggle->start();
141
142 $details=new PlekitDetails($privileges);
143 $details->start();
144 if ( ! $local_peer) {
145   $details->th_td("Peer",$peers->peer_link($peer_id));
146   $details->space();
147  }
148
149 $details->form_start(l_actions(),array("action"=>"update-node", "node_id"=>$node_id, "hostname"=>$hostname));
150 // xxx can hostname really be changed like this without breaking the rest, bootcd .. ?
151 //$details->th_td("Hostname",$hostname,"hostname"); 
152 $details->th_td("Hostname",$hostname); 
153 if ($hrn) $details->th_td("SFA hrn",$hrn);
154 else $details->tr("SFA hrn not set","center");
155 $details->th_td("Model",$model,"model");
156 // reservation ?
157 if ( $admin_privileges) {
158   $reservation_value = $details->form->select_html("node_type",
159                                                    node_type_selectors ($api, $node_type));
160 } else {
161   $reservation_value = node_type_display ($api,$node_type);
162 }
163 $details->th_td("Reservation",$reservation_value);
164
165 $details->tr_submit("submit","Update Node");
166 $details->form_end();
167 if ($privileges) $details->space();
168
169 $display_reboot_button = FALSE;
170 ////////////////////
171 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
172 $display_pcus = ( $local_peer && (plc_is_admin() || plc_is_pi() || plc_is_tech()));
173 if ($display_pcus) {
174   $pcu_ids= $node['pcu_ids'];
175   $ports= $node['ports'];
176   // avoid 2 API calls : get all site PCUs and then search from there
177   function search_pcu ($site_pcus,$pcu_id) {
178     if ($site_pcus) foreach ($site_pcus as $site_pcu) if ($site_pcu['pcu_id']==$pcu_id) return $site_pcu;
179     return FALSE;
180   }
181   $site_pcus = $api->GetPCUs(array('site_id'=>$site_id));
182   // not sure what the exact semantics is, but I expect pcu_ids and ports should have same cardinality
183   if (count ($pcu_ids) != count ($ports)) 
184     $pcu_string = plc_error_html("Unexpected condition: " . count($pcu_ids) . " pcu_ids and " . count($ports) . " ports");
185   else if (count($pcu_ids) == 0) 
186     $pcu_string = plc_warning_html("No PCU !");
187   else if (count($pcu_ids) == 1) {
188     $pcu_id=$pcu_ids[0];
189     $port=$ports[0];
190     $pcu_columns = array('hostname');
191     $pcu=search_pcu($site_pcus,$pcu_id);
192     if ( ! $pcu ) {
193       $pcu_string = plc_error_html("Cannot find PCU " . $pcu_id);
194     } else {
195       // else : regular case - don't set pcu_string
196       // NOTE: temporarily only offer the reboot_button for DC7x00, DRAC, and HPiLO PCU models
197       if ( $pcu['model'] == "IntelAMT" || $pcu['model'] == "DRAC" || $pcu['model'] == "HPiLO" ){
198         $display_reboot_button = TRUE;
199       }
200     }
201   } else 
202     $pcu_string = plc_warning_html("More than one PCU attached ? ");
203
204   // in the regular case, pcu_string is not set here
205   
206   $details->form_start(l_actions(),array("action"=>"attach-pcu","node_id"=>$node_id));
207   // prepare selectors
208   if (! $site_pcus) {
209     $pcu_update_area = "This site has no PCU - " . href ( l_pcu_add(), "add one here");
210   } else {
211     $pcu_add_link = href (l_pcu_add(),plc_add_icon() . "Add new");
212
213     // first option in pcus
214     if ($pcu_ids) 
215       $none_detach = 'Detach';
216     else 
217       $none_detach='None';
218     $pcu_selectors = array(array('display'=>$none_detach,'value'=>-1));
219     // one option per site pcu
220     foreach ($site_pcus as $site_pcu) {
221       $selector=array('display'=>$site_pcu['hostname'],'value'=>$site_pcu['pcu_id']);
222       if ($pcu_id == $site_pcu['pcu_id']) $selector['selected']=true;
223       $pcu_selectors []= $selector;
224     }
225     $pcu_chooser = $details->form()->select_html('pcu_id',$pcu_selectors);
226
227     function port_selector ($i,$port) { 
228       $selector = array ('display'=>'port ' . $i, 'value'=>$i); 
229       if ($i == $port) $selector['selected'] = true;
230       return $selector;
231     }
232     $port_selectors = array () ;
233     $available_ports =range(1,8);
234     foreach ($available_ports as $available_port) 
235       $port_selectors []= port_selector ($available_port,$port);
236     $port_chooser = $details->form()->select_html('port',$port_selectors);
237
238     $pcu_attach_button = 
239       $details->form()->submit_html('attach_pcu',"Attach PCU");
240
241     $pcu_update_area = $pcu_add_link . "<br>Or, select existing  " . $pcu_chooser . " " . $port_chooser . " " . $pcu_attach_button;
242   }
243
244   if ($pcu_string) 
245     $pcu_value_area=plc_vertical_table(array($pcu_string,$pcu_update_area));
246   else 
247     $pcu_value_area=$pcu_update_area;
248     
249   $details->th_td("PCU",$pcu_value_area);
250   $details->form_end();
251  }
252
253 //////////////////// Reboot Node
254 if ( $display_reboot_button ) 
255 {
256     if ( ! empty($_SESSION['messages']) ) {
257         $msg = $_SESSION['messages']['status'][0];
258     } else {
259         $msg = "";
260     }
261     $body="Hello,
262
263 This message is a template from the 'Report a problem' link on the node details page.
264
265 I've experienced a problem rebooting $hostname with the pcu_id $pcu_id; 
266
267     http://".PLC_WWW_HOST."/db/sites/pcu.php?id=$pcu_id
268     http://".PLC_WWW_HOST."/db/nodes/node.php?id=$node_id\n\n";
269
270     if ( $msg != "" ) {
271         $body .= "The last time I tried, it returned:\n    $msg\n\n";
272     }
273     $body .= "And, this is what I've tried, which leads me to believe that there is a bug on your side:";
274
275     $url=rawurlencode($body);
276     $email = "<font style='font-size: smaller'>><a href=\"mailto:".PLC_MAIL_SUPPORT_ADDRESS."?Subject=Reporting a problem rebooting $hostname&Body=$url\">Report a problem</a></font>";
277
278     // NOTE: not sure how to make the buttons display side-by-side...
279     $reboot = $details->form_start_html(l_actions(),array("action"=>"reboot-node-with-pcu",
280                                 "node_id"=>$node_id, "hostname"=>$hostname, "test"=>FALSE));
281     $reboot .= $email . $details->form->submit_html("submit","Reboot Node");
282     $reboot .= $details->form_end_html();
283
284     $reboot .= $details->form_start_html(l_actions(),array("action"=>"reboot-node-with-pcu",
285                                 "node_id"=>$node_id, "hostname"=>$hostname, "test"=>TRUE));
286     $reboot .= $details->form->submit_html("submit","Test PCU");
287     $reboot .= $details->form_end_html();
288
289     $details->tr($reboot, "right");
290
291 }
292 $details->space();
293
294 //////////////////// type & version
295 $details->th_td("CD Version",$version);
296 // let's use plc_objects
297 $Node = new Node($node);
298 $details->th_td("Date created",$Node->dateCreated());
299 $details->th_td("Last update",$Node->lastUpdated());
300 $details->th_td("Last contact",$Node->lastContact());
301
302 // boot area
303 $details->space ();
304 $stale_text =  $Node->stale() ? ("... (more than " . Node::stale_text() . " ago)") : "" ;
305 $details->th_td ("Observed Boot state", $run_level . $stale_text);
306 if ( ! ($local_peer && $privileges)) {
307   // just display it
308   $boot_value=$boot_state;
309  } else {
310   $boot_value="";
311   $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
312                                                "action"=>"node-boot-state"));
313   $boot_value .= $boot_form->start_html();
314   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 
315                    'disabled' => 'Disabled', 'reinstall'=>'Reinstall');
316   $selectors=array();
317   foreach ($states as $dbname=>$displayname) { 
318     $selector=array("display"=>$displayname, "value"=>$dbname);
319     if ($dbname == $boot_state) $selector['selected']=true;
320     $selectors []= $selector;
321   }
322   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
323   $boot_value .= $boot_form->end_html();
324  }
325 $details->th_td ("Preferred Boot state",$boot_value);
326
327 // same here for the download area
328 if ( $local_peer  && $privileges) {
329
330   $download_value="";
331   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
332   $download_value .= $download_form->start_html();
333   $selectors = array( 
334                      array("display"=>"-- All in one images --","disabled"=>true),
335                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
336                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
337                      array("value"=>"download-node-usb-partition", "display"=>"Download partitioned, USB image for $hostname"),
338                      //              array("display"=>"-- Floppy + generic image --","disabled"=>true),
339                      //              array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
340                      //              array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
341                      //              array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"),
342                       );
343   $download_value .= $download_form->select_html("action",$selectors,
344                                                  array('label'=>"Download mode",'autosubmit'=>true));
345   $download_value .= $download_form->end_html();
346   $details->th_td ("Download",$download_value);
347
348  }
349
350 // site info and all site nodes
351 $details->space ();
352 $details->th_td("Site",l_site_t($site_id,$site_name));
353                    
354 // build list of node links
355 $nodes_area=array();
356 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
357   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
358 }
359 $details->th_tds ("All site nodes",$nodes_area);
360
361 $details->end ();
362 $toggle->end();
363
364 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
365 $form->start();
366
367 //////////////////////////////////////////////////////////// slivers
368 {
369   $toggle=new PlekitToggle ('slices',count_english_warning($slices,'sliver'),
370                             array('bubble'=>'Review slices running on that node',
371                                   'visible'=>get_arg('show_slices')));
372   $toggle->start();
373   if ( ! $slices  ) {
374     plc_warning ("This node is not associated to any slice");
375   } else {
376     $headers=array();
377     $headers['Peer']="string";
378     $headers['Slice Name']="string";
379     $headers['Sliver']="string";
380     $reasonable_page=10;
381     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
382     if (count ($slices) <= $reasonable_page) {
383       $table_options['search_area']=false;
384       $table_options['pagesize_area']=false;
385     }
386     $table=new PlekitTable("node_slices",$headers,1,$table_options);
387     $table->start();
388
389     foreach ($slices as $slice) {
390       $table->row_start();
391       $peers->cell ($table,$slice['peer_id']);
392       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
393       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'sliver tags'));
394       $table->row_end();
395     }
396     $table->end();
397   }
398   $toggle->end();
399 }
400
401 //////////////////////////////////////////////////////////// Tags
402 // tags section
403 if ( $local_peer ) {
404   
405   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
406   function get_tagname ($tag) { return $tag['tagname'];}
407   // xxx looks like tech-only see an error here, 
408   // might be that GetNodeTags is not accessible or something
409   $tagnames = array_map ("get_tagname",$tags);
410   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
411   
412   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
413                               array('bubble'=>'Inspect and set tags on that node',
414                                     'visible'=>get_arg('show_tags')));
415   $toggle->start();
416
417   $headers=array("Name"=>"string",
418                  "Value"=>"string",
419                  "Nodegroup"=>"string",
420                  );
421   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
422   
423   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
424   $table=new PlekitTable("node_tags",$headers,0,$table_options);
425   $table->start();
426   if ($tags) foreach ($tags as $tag) {
427       // does this match a nodegroup ?
428       $nodegroup_name="n/a";
429       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
430       $nodegroup=$nodegroups_hash[$nodegroup_key];
431       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
432       $table->row_start();
433       $table->cell(l_tag_obj($tag));
434       $table->cell($tag['value']);
435       $table->cell($nodegroup_name);
436       // the remove checkbox
437       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
438       $table->row_end();
439     }
440   
441   if ($privileges) {
442     $table->tfoot_start();
443
444     // remove tag 
445     $table->row_start();
446     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
447                  // use the whole columns and right adjust
448                  array('hfill'=>true,'align'=>'right'));
449     $table->row_end();
450
451     // set tag area
452     $table->row_start();
453     // get list of tag names in the node/* category    
454     $all_tags= $api->GetTagTypes( array ("category"=>"node*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
455     // xxx cannot use onchange=submit() - would need to somehow pass action name 
456     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
457     $selector=array_map("tag_selector",$all_tags);
458     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
459     $table->cell($form->text_html("value","",array('width'=>8)));
460     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),array('columns'=>2,'align'=>'left'));
461     $table->row_end();
462   }
463   
464   $table->end();
465   $toggle->end();
466 }
467
468 //////////////////////////////////////////////////////////// interfaces
469 if ( $local_peer ) {
470   $toggle=new PlekitToggle ('interfaces',count_english_warning($interfaces,'interface'),
471                             array('bubble'=>'Inspect and tune interfaces on that node',
472                                   'visible'=>get_arg('show_interfaces')));
473   $toggle->start();
474   // display interfaces
475   if( ! $interfaces ) {
476     echo '<p>';
477     plc_warning_html("This node has no interface");
478     echo "Please add an interface to make this a usable PLC node.</p>\n";
479   } // else { // show this unconditionnally as otherwise there's no mean to create one..
480
481     // display a hostname column iff at least one interface has a hostname
482     $need_hostname=false;
483     if ($interfaces) foreach ($interfaces as $interface) if ($interface['hostname']) $need_hostname=true;
484
485     $headers=array();
486
487     $sort_column=0;
488     if (plc_is_admin()) { $headers['I']='int'; $sort_column +=1;}
489     $headers["IP"]="sortIPAddress";
490     if ($need_hostname) $headers['hostname']='string';
491     $headers["Method"]="string";
492     $headers["Type"]="string";
493     $headers["MAC"]="string";
494     $headers["bw limit"]="sortBandwidth";
495     $headers["tags"]=array('type'=>'int',
496                            'title'=>"number of tags set on interface");
497     // a single symbol, marking 'p' for primary and a delete button for non-primary
498     if ( $privileges ) $headers[plc_delete_icon()]='string';
499
500     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
501     $table=new PlekitTable("node_interfaces",$headers,$sort_column,$table_options);
502     $table->start();
503         
504     if ($interfaces) foreach ( $interfaces as $interface ) {
505       $interface_id= $interface['interface_id'];
506       $interface_ip= $interface['ip'];
507
508       $table->row_start();
509       if (plc_is_admin()) $table->cell(l_interface_t($interface_id,$interface_id));
510       $table->cell(l_interface_t($interface_id,$interface_ip));
511       if ($need_hostname) $table->cell($interface['hostname']);
512       $table->cell($interface['method']);
513       $table->cell($interface['type']);
514       $table->cell($interface['mac']);
515       $table->cell(pretty_bandwidth($interface['bwlimit']));
516       $table->cell(href(l_interface_tags($interface_id),
517                         count($interface['interface_tag_ids'])));
518       if ( $privileges ) {
519         if ($interface['is_primary']) {
520           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
521         } else {
522           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
523         }
524       }
525       $table->row_end();
526     }
527     if ($privileges) {
528       $table->tfoot_start();
529       $table->row_start();
530       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
531       //$add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
532       //$table->cell($add_button->html(),array('columns'=> 3,'align'=>'left'));
533       $table->cell($form->submit_html("new-interface","Add Interface"), 
534                 array('columns'=> 3,'align'=>'left'));
535       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), 
536                    array('columns'=>$table->columns()-3,'align'=>'right'));
537       $table->row_end();
538     }
539     $table->end();
540     //  }
541   $toggle->end();
542  }
543
544 $form->end();
545
546 ////////////////////////////////////////////////////////////
547 $peers->block_end($peer_id);
548
549 //plekit_linetabs ($tabs,"bottom");
550
551 // Print footer
552 include 'plc_footer.php';
553
554 ?>