offer the option to set node in 'upgrade' boot_mode
[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                    'upgrade' => 'Upgrade');
317   $selectors=array();
318   foreach ($states as $dbname=>$displayname) { 
319     $selector=array("display"=>$displayname, "value"=>$dbname);
320     if ($dbname == $boot_state) $selector['selected']=true;
321     $selectors []= $selector;
322   }
323   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
324   $boot_value .= $boot_form->end_html();
325  }
326 $details->th_td ("Preferred Boot state",$boot_value);
327
328 // same here for the download area
329 if ( $local_peer  && $privileges) {
330
331   $download_value="";
332   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
333   $download_value .= $download_form->start_html();
334   $selectors = array( 
335                      array("display"=>"-- All in one images --","disabled"=>true),
336                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
337                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
338                      array("value"=>"download-node-usb-partition", "display"=>"Download partitioned, USB image for $hostname"),
339                      //              array("display"=>"-- Floppy + generic image --","disabled"=>true),
340                      //              array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
341                      //              array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
342                      //              array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"),
343                       );
344   $download_value .= $download_form->select_html("action",$selectors,
345                                                  array('label'=>"Download mode",'autosubmit'=>true));
346   $download_value .= $download_form->end_html();
347   $details->th_td ("Download",$download_value);
348
349  }
350
351 // site info and all site nodes
352 $details->space ();
353 $details->th_td("Site",l_site_t($site_id,$site_name));
354                    
355 // build list of node links
356 $nodes_area=array();
357 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
358   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
359 }
360 $details->th_tds ("All site nodes",$nodes_area);
361
362 $details->end ();
363 $toggle->end();
364
365 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
366 $form->start();
367
368 //////////////////////////////////////////////////////////// slivers
369 {
370   $toggle=new PlekitToggle ('slices',count_english_warning($slices,'sliver'),
371                             array('bubble'=>'Review slices running on that node',
372                                   'visible'=>get_arg('show_slices')));
373   $toggle->start();
374   if ( ! $slices  ) {
375     plc_warning ("This node is not associated to any slice");
376   } else {
377     $headers=array();
378     $headers['Peer']="string";
379     $headers['Slice Name']="string";
380     $headers['Sliver']="string";
381     $reasonable_page=10;
382     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
383     if (count ($slices) <= $reasonable_page) {
384       $table_options['search_area']=false;
385       $table_options['pagesize_area']=false;
386     }
387     $table=new PlekitTable("node_slices",$headers,1,$table_options);
388     $table->start();
389
390     foreach ($slices as $slice) {
391       $table->row_start();
392       $peers->cell ($table,$slice['peer_id']);
393       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
394       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'sliver tags'));
395       $table->row_end();
396     }
397     $table->end();
398   }
399   $toggle->end();
400 }
401
402 //////////////////////////////////////////////////////////// Tags
403 // tags section
404 if ( $local_peer ) {
405   
406   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
407   function get_tagname ($tag) { return $tag['tagname'];}
408   // xxx looks like tech-only see an error here, 
409   // might be that GetNodeTags is not accessible or something
410   $tagnames = array_map ("get_tagname",$tags);
411   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
412   
413   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
414                               array('bubble'=>'Inspect and set tags on that node',
415                                     'visible'=>get_arg('show_tags')));
416   $toggle->start();
417
418   $headers=array("Name"=>"string",
419                  "Value"=>"string",
420                  "Nodegroup"=>"string",
421                  );
422   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
423   
424   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
425   $table=new PlekitTable("node_tags",$headers,0,$table_options);
426   $table->start();
427   if ($tags) foreach ($tags as $tag) {
428       // does this match a nodegroup ?
429       $nodegroup_name="n/a";
430       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
431       $nodegroup=$nodegroups_hash[$nodegroup_key];
432       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
433       $table->row_start();
434       $table->cell(l_tag_obj($tag));
435       $table->cell($tag['value']);
436       $table->cell($nodegroup_name);
437       // the remove checkbox
438       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
439       $table->row_end();
440     }
441   
442   if ($privileges) {
443     $table->tfoot_start();
444
445     // remove tag 
446     $table->row_start();
447     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
448                  // use the whole columns and right adjust
449                  array('hfill'=>true,'align'=>'right'));
450     $table->row_end();
451
452     // set tag area
453     $table->row_start();
454     // get list of tag names in the node/* category    
455     $all_tags= $api->GetTagTypes( array ("category"=>"node*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
456     // xxx cannot use onchange=submit() - would need to somehow pass action name 
457     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
458     $selector=array_map("tag_selector",$all_tags);
459     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
460     $table->cell($form->text_html("value","",array('width'=>8)));
461     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),array('columns'=>2,'align'=>'left'));
462     $table->row_end();
463   }
464   
465   $table->end();
466   $toggle->end();
467 }
468
469 //////////////////////////////////////////////////////////// interfaces
470 if ( $local_peer ) {
471   $toggle=new PlekitToggle ('interfaces',count_english_warning($interfaces,'interface'),
472                             array('bubble'=>'Inspect and tune interfaces on that node',
473                                   'visible'=>get_arg('show_interfaces')));
474   $toggle->start();
475   // display interfaces
476   if( ! $interfaces ) {
477     echo '<p>';
478     plc_warning_html("This node has no interface");
479     echo "Please add an interface to make this a usable PLC node.</p>\n";
480   } // else { // show this unconditionnally as otherwise there's no mean to create one..
481
482     // display a hostname column iff at least one interface has a hostname
483     $need_hostname=false;
484     if ($interfaces) foreach ($interfaces as $interface) if ($interface['hostname']) $need_hostname=true;
485
486     $headers=array();
487
488     $sort_column=0;
489     if (plc_is_admin()) { $headers['I']='int'; $sort_column +=1;}
490     $headers["IP"]="sortIPAddress";
491     if ($need_hostname) $headers['hostname']='string';
492     $headers["Method"]="string";
493     $headers["Type"]="string";
494     $headers["MAC"]="string";
495     $headers["bw limit"]="sortBandwidth";
496     $headers["tags"]=array('type'=>'int',
497                            'title'=>"number of tags set on interface");
498     // a single symbol, marking 'p' for primary and a delete button for non-primary
499     if ( $privileges ) $headers[plc_delete_icon()]='string';
500
501     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
502     $table=new PlekitTable("node_interfaces",$headers,$sort_column,$table_options);
503     $table->start();
504         
505     if ($interfaces) foreach ( $interfaces as $interface ) {
506       $interface_id= $interface['interface_id'];
507       $interface_ip= $interface['ip'];
508
509       $table->row_start();
510       if (plc_is_admin()) $table->cell(l_interface_t($interface_id,$interface_id));
511       $table->cell(l_interface_t($interface_id,$interface_ip));
512       if ($need_hostname) $table->cell($interface['hostname']);
513       $table->cell($interface['method']);
514       $table->cell($interface['type']);
515       $table->cell($interface['mac']);
516       $table->cell(pretty_bandwidth($interface['bwlimit']));
517       $table->cell(href(l_interface_tags($interface_id),
518                         count($interface['interface_tag_ids'])));
519       if ( $privileges ) {
520         if ($interface['is_primary']) {
521           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
522         } else {
523           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
524         }
525       }
526       $table->row_end();
527     }
528     if ($privileges) {
529       $table->tfoot_start();
530       $table->row_start();
531       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
532       //$add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
533       //$table->cell($add_button->html(),array('columns'=> 3,'align'=>'left'));
534       $table->cell($form->submit_html("new-interface","Add Interface"), 
535                 array('columns'=> 3,'align'=>'left'));
536       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), 
537                    array('columns'=>$table->columns()-3,'align'=>'right'));
538       $table->row_end();
539     }
540     $table->end();
541     //  }
542   $toggle->end();
543  }
544
545 $form->end();
546
547 ////////////////////////////////////////////////////////////
548 $peers->block_end($peer_id);
549
550 //plekit_linetabs ($tabs,"bottom");
551
552 // Print footer
553 include 'plc_footer.php';
554
555 ?>