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