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