a little better
[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));
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 ////////////////////
156 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
157 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
158 if ($display_pcus) {
159   $pcu_ids= $node['pcu_ids'];
160   $ports= $node['ports'];
161   // avoid 2 API calls : get all site PCUs and then search from there
162   function search_pcu ($site_pcus,$pcu_id) {
163     if ($site_pcus) foreach ($site_pcus as $site_pcu) if ($site_pcu['pcu_id']==$pcu_id) return $site_pcu;
164     return FALSE;
165   }
166   $site_pcus = $api->GetPCUs(array('site_id'=>$site_id));
167   // not sure what the exact semantics is, but I expect pcu_ids and ports should have same cardinality
168   if (count ($pcu_ids) != count ($ports)) 
169     $pcu_string = plc_error_html("Unexpected condition: " . count($pcu_ids) . " pcu_ids and " . count($ports) . " ports");
170   else if (count($pcu_ids) == 0) 
171     $pcu_string = plc_warning_html("No PCU !");
172   else if (count($pcu_ids) == 1) {
173     $pcu_id=$pcu_ids[0];
174     $port=$ports[0];
175     $pcu_columns = array('hostname');
176     $pcu=search_pcu($site_pcus,$pcu_id);
177     if ( ! $pcu ) 
178       $pcu_string = plc_error_html("Cannot find PCU " . $pcu_id);
179     // else : regular case - don't set pcu_string
180   } else 
181     $pcu_string = plc_warning_html("More than one PCU attached ? ");
182
183   // in the regular case, pcu_string is not set here
184   
185   $details->form_start(l_actions(),array("action"=>"attach-pcu","node_id"=>$node_id));
186   // prepare selectors
187   if (! $site_pcus) {
188     $pcu_update_area = "This site has no PCU - " . href ( l_pcu_add(), "add one here");
189   } else {
190     $pcu_add_link = href (l_pcu_add(),plc_add_icon());
191
192     // first option in pcus
193     if ($pcu_ids) 
194       $none_detach = 'Detach';
195     else 
196       $none_detach='None';
197     $pcu_selectors = array(array('display'=>$none_detach,'value'=>-1));
198     // one option per site pcu
199     foreach ($site_pcus as $site_pcu) {
200       $selector=array('display'=>$site_pcu['hostname'],'value'=>$site_pcu['pcu_id']);
201       if ($pcu_id == $site_pcu['pcu_id']) $selector['selected']=true;
202       $pcu_selectors []= $selector;
203     }
204     $pcu_chooser = $details->form()->select_html('pcu_id',$pcu_selectors);
205
206     function port_selector ($i,$port) { 
207       $selector = array ('display'=>'port ' . $i, 'value'=>$i); 
208       if ($i == $port) $selector['selected'] = true;
209       return $selector;
210     }
211     $port_selectors = array () ;
212     $available_ports =range(1,8);
213     foreach ($available_ports as $available_port) 
214       $port_selectors []= port_selector ($available_port,$port);
215     $port_chooser = $details->form()->select_html('port',$port_selectors);
216
217     $pcu_attach_button = 
218       $details->form()->submit_html('attach_pcu',"Attach PCU");
219
220     $pcu_update_area = $pcu_add_link . " " . $pcu_chooser . " " . $port_chooser . " " . $pcu_attach_button;
221   }
222
223   if ($pcu_string) 
224     $pcu_value_area=plc_vertical_table(array($pcu_string,$pcu_update_area));
225   else 
226     $pcu_value_area=$pcu_update_area;
227     
228   $details->th_td("PCU",$pcu_value_area);
229   $details->form_end();
230   $details->space();
231  }
232
233 //////////////////// type & version
234 $details->th_td("Type",$node_type);
235 $details->th_td("Version",$version);
236 // let's use plc_objects
237 $Node = new Node($node);
238 $details->th_td("Date created",$Node->dateCreated());
239 $details->th_td("Last contact",$Node->lastContact());
240 $details->th_td("Last update",$Node->lastUpdated());
241
242 // boot area
243 $details->space ();
244 $details->th_td ("Observed Boot state",$run_level . ( $Node->stale() ? " -- stale value" : "" ));
245 if ( ! ($local_peer && $privileges)) {
246   // just display it
247   $boot_value=$boot_state;
248  } else {
249   $boot_value="";
250   $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
251                                                "action"=>"node-boot-state"));
252   $boot_value .= $boot_form->start_html();
253   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 
254                    'disabled' => 'Disabled', 'reinstall'=>'Reinstall');
255   $selectors=array();
256   foreach ($states as $dbname=>$displayname) { 
257     $selector=array("display"=>$displayname, "value"=>$dbname);
258     if ($dbname == $boot_state) $selector['selected']=true;
259     $selectors []= $selector;
260   }
261   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
262   $boot_value .= $boot_form->end_html();
263  }
264 $details->th_td ("Preferred Boot state",$boot_value);
265
266 // same here for the download area
267 if ( $local_peer  && $privileges) {
268
269   $download_value="";
270   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
271   $download_value .= $download_form->start_html();
272   $selectors = array( 
273                      array("display"=>"-- All in one images --","disabled"=>true),
274                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
275                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
276                      array("value"=>"download-node-usb-partition", "display"=>"Download partitioned, USB image for $hostname"),
277                      array("display"=>"-- Floppy + generic image --","disabled"=>true),
278                      array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
279                      array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
280                      array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
281   $download_value .= $download_form->select_html("action",$selectors,
282                                                  array('label'=>"Download mode",'autosubmit'=>true));
283   $download_value .= $download_form->end_html();
284   $details->th_td ("Download",$download_value);
285
286  }
287
288 // site info and all site nodes
289 $details->space ();
290 $details->th_td("Site",l_site_t($site_id,$site_name));
291                    
292 // build list of node links
293 $nodes_area=array();
294 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
295   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
296 }
297 $details->th_tds ("All site nodes",$nodes_area);
298
299 $details->end ();
300 $toggle->end();
301
302 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
303 $form->start();
304
305 //////////////////////////////////////////////////////////// slivers
306 {
307   $toggle=new PlekitToggle ('slices',count_english_warning($slices,'sliver'),
308                             array('bubble'=>'Review slices running on that node',
309                                   'visible'=>get_arg('show_slices',false)));
310   $toggle->start();
311   if ( ! $slices  ) {
312     plc_warning ("This node is not associated to any slice");
313   } else {
314     $headers=array();
315     $headers['Peer']="string";
316     $headers['Slice Name']="string";
317     $headers['Sliver']="string";
318     $reasonable_page=10;
319     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
320     if (count ($slices) <= $reasonable_page) {
321       $table_options['search_area']=false;
322       $table_options['pagesize_area']=false;
323     }
324     $table=new PlekitTable("node_slices",$headers,1,$table_options);
325     $table->start();
326
327     foreach ($slices as $slice) {
328       $table->row_start();
329       $peers->cell ($table,$slice['peer_id']);
330       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
331       # xxx l_sliver not implemented yet - what should we show exactly ?
332       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'sliver tags'));
333       $table->row_end();
334     }
335     $table->end();
336   }
337   $toggle->end();
338 }
339
340 //////////////////////////////////////////////////////////// Tags
341 // tags section
342 if ( $local_peer ) {
343   
344   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
345   function get_tagname ($tag) { return $tag['tagname'];}
346   // xxx looks like tech-only see an error here, 
347   // might be that GetNodeTags is not accessible or something
348   $tagnames = array_map ("get_tagname",$tags);
349   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
350   
351   $toggle = new PlekitToggle ('tags',count_english_warning($tags,'tag'),
352                               array('bubble'=>'Inspect and set tags on that node',
353                                     'visible'=>get_arg('show_tags',false)));
354   $toggle->start();
355
356   $headers=array("Name"=>"string",
357                  "Value"=>"string",
358                  "Nodegroup"=>"string",
359                  );
360   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
361   
362   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
363   $table=new PlekitTable("node_tags",$headers,0,$table_options);
364   $table->start();
365   if ($tags) foreach ($tags as $tag) {
366       // does this match a nodegroup ?
367       $nodegroup_name="n/a";
368       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
369       $nodegroup=$nodegroups_hash[$nodegroup_key];
370       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
371       $table->row_start();
372       $table->cell(l_tag_obj($tag));
373       $table->cell($tag['value']);
374       $table->cell($nodegroup_name);
375       // the remove checkbox
376       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
377       $table->row_end();
378     }
379   
380   if ($privileges) {
381     $table->tfoot_start();
382
383     // remove tag 
384     $table->row_start();
385     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
386                  // use the whole columns and right adjust
387                  array('hfill'=>true,'align'=>'right'));
388     $table->row_end();
389
390     // set tag area
391     $table->row_start();
392     // get list of tag names in the node/* category    
393     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
394     // xxx cannot use onchange=submit() - would need to somehow pass action name 
395     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
396     $selector=array_map("tag_selector",$all_tags);
397     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
398     $table->cell($form->text_html("value","",array('width'=>8)));
399     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),array('columns'=>2,'align'=>'left'));
400     $table->row_end();
401   }
402   
403   $table->end();
404   $toggle->end();
405 }
406
407 //////////////////////////////////////////////////////////// interfaces
408 if ( $local_peer ) {
409   $toggle=new PlekitToggle ('interfaces',count_english_warning($interfaces,'interface'),
410                             array('bubble'=>'Inspect and tune interfaces on that node',
411                                   'visible'=>get_arg('show_interfaces',false)));
412   $toggle->start();
413   // display interfaces
414   if( ! $interfaces ) {
415     echo '<p>';
416     plc_warning_html("This node has no interface");
417     echo "Please add an interface to make this a usable PLC node.</p>\n";
418   } else {
419     $headers=array();
420
421     $headers["IP"]="IPAddress";
422     $headers["Method"]="string";
423     $headers["Type"]="string";
424     $headers["MAC"]="string";
425     $headers["bw limit"]="FileSize";
426     // a single symbol, marking 'p' for primary and a delete button for non-primary
427     if ( $privileges ) $headers[plc_delete_icon()]='string';
428
429     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
430     $table=new PlekitTable("node_interfaces",$headers,2,$table_options);
431     $table->start();
432         
433     foreach ( $interfaces as $interface ) {
434       $interface_id= $interface['interface_id'];
435       $interface_ip= $interface['ip'];
436       $interface_broad= $interface['broadcast'];
437       $interface_primary= $interface['is_primary'];
438       $interface_network= $interface['network'];
439       $interface_dns1= $interface['dns1'];
440       $interface_dns2= $interface['dns2'];
441       $interface_hostname= $interface['hostname'];
442       $interface_netmaks= $interface['netmask'];
443       $interface_gatewary= $interface['gateway'];
444       $interface_mac= $interface['mac'];
445       $interface_bwlimit= $interface['bwlimit'];
446       $interface_type= $interface['type'];
447       $interface_method= $interface['method'];
448
449       $table->row_start();
450       $table->cell(l_interface_t($interface_id,$interface_ip));
451       $table->cell($interface_method);
452       $table->cell($interface_type);
453       $table->cell($interface_mac);
454       $table->cell($interface_bwlimit);
455       if ( $privileges ) {
456         if ($interface_primary) {
457           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
458         } else {
459           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
460         }
461       }
462       $table->row_end();
463     }
464     if ($privileges) {
465       $table->tfoot_start();
466       $table->row_start();
467       $add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
468       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
469       $table->cell($add_button->html(),array('columns'=> 3,'align'=>'left'));
470       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), 
471                    array('columns'=>$table->columns()-3,'align'=>'right'));
472       $table->row_end();
473     }
474     $table->end();
475   }
476   $toggle->end();
477  }
478
479 $form->end();
480
481 ////////////////////////////////////////////////////////////
482 $peers->block_end($peer_id);
483
484 //plekit_linetabs ($tabs,"bottom");
485
486 // Print footer
487 include 'plc_footer.php';
488
489 ?>