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