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