more renamings
[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 $site_id= $node['site_id'];
45 $model= $node['model'];
46 $version= $node['version'];
47 $node_type = $node['node_type'];
48
49 // arrays of ids of node info
50 $slice_ids= $node['slice_ids'];
51 $conf_file_ids= $node['conf_file_ids'];
52 $interface_ids= $node['interface_ids'];
53 $nodegroup_ids= $node['nodegroup_ids'];
54 $pcu_ids= $node['pcu_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 // xxx Thierry : remaining stuff
90 // (*) events: should display the latest events relating to that node.
91 // disabling call to GetEvents, that gets the session deleted in the DB
92 // (*) conf_files: is fetched but not displayed
93 if( !empty( $conf_file_ids ) )
94   $conf_files= $api->GetConfFiles( $conf_file_ids );
95 // (*) idem for PCUs
96 // gets pcu and port info key to both is $pcu_id
97 // turning this off: GetPCUs is not allowed to users, and we don't show PCUs yet anyway
98 //if( !empty( $pcu_ids ) )
99 //  $PCUs= $api->GetPCUs( $pcu_ids );
100
101 //////////////////// display node info
102
103 drupal_set_title("Details for node " . $hostname);
104 $local_peer= ! $peer_id;
105
106   
107 // extra privileges to admins, and (pi||tech) on this site
108 $privileges = (plc_is_admin () && $local_peer) || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
109   
110 $tabs=array();
111 // available actions
112 $tabs [] = tab_nodes_site($site_id);
113 $tabs [] = tab_site($site_id);
114 $tabs [] = tab_nodes();
115
116 if ( $local_peer  && $privileges ) {
117     
118   $tabs['Delete'] = array ('url'=>l_actions(),
119                            'method'=>'POST',
120                            'values'=>array('action'=>'delete-node','node_id'=>$node_id),
121                            'bubble'=>"Delete node $hostname",
122                            'confirm'=>'Are you sure to delete ' . $hostname. ' ?');
123   // xxx subject to roles
124   $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
125                                'bubble'=>"Define new network interface on $hostname");
126   $tabs["Events"]=array_merge(tablook_event(),
127                               array('url'=>l_event("Node","node",$node_id),
128                                     'bubble'=>"Events for node $hostname"));
129   $tabs["Comon"]=array_merge(tablook_comon(),
130                              array('url'=>l_comon("node_id",$node_id),
131                                    'bubble'=>"Comon page about node $hostname"));
132  }
133
134 plekit_linetabs($tabs);
135
136 // show gray background on foreign objects : start a <div> with proper class
137 $peers->block_start ($peer_id);
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 $details->th_td("Hostname",$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 $details->th_td("Type",$node_type);
154 $details->th_td("Version",$version);
155 // let's use plc_objects
156 $Node = new Node($node);
157 $details->th_td("Date created",$Node->dateCreated());
158 $details->th_td("Last contact",$Node->lastContact());
159 $details->th_td("Last update",$Node->lastUpdated());
160
161 // boot area
162 $details->space ();
163 if ( ! ($local_peer && $privileges)) {
164   // just display it
165   $boot_value=$boot_state;
166  } else {
167   $boot_value="";
168   $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
169                                                "action"=>"node-boot-state"));
170   $boot_value .= $boot_form->start_html();
171   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 'failboot'=>'FailBoot', 
172                    'disabled' => 'Disabled', 'install'=>'Install', 'reinstall'=>'Reinstall');
173   $selectors=array();
174   foreach ($states as $dbname=>$displayname) { 
175     $selector=array("display"=>$displayname, "value"=>$dbname);
176     if ($dbname == $boot_state) $selector['selected']=true;
177     $selectors []= $selector;
178   }
179   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
180   $boot_value .= $boot_form->end_html();
181  }
182 $details->th_td ("Boot state",$boot_value);
183
184 // same here for the download area
185 if ( $local_peer  && $privileges) {
186
187   $download_value="";
188   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
189   $download_value .= $download_form->start_html();
190   $selectors = array( 
191                      array("display"=>"-- All in one images --","disabled"=>true),
192                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
193                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname<"),
194                      array("display"=>"-- Floppy + generic image --","disabled"=>true),
195                      array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
196                      array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
197                      array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
198   $download_value .= $download_form->select_html("action",$selectors,
199                                                  array('label'=>"Download mode",'autosubmit'=>true));
200   $download_value .= $download_form->end_html();
201   $details->th_td ("Download",$download_value);
202  }
203
204 // site info and all site nodes
205 $details->space ();
206 $details->th_td("Site",l_site_t($site_id,$site_name));
207                    
208 // build list of node links
209 $nodes_area=array();
210 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
211   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
212 }
213 $details->th_tds ("All site nodes",$nodes_area);
214
215 $details->end ();
216
217 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
218 $form->start();
219
220 //////////////////////////////////////////////////////////// Tags
221 // tags section
222 $show_tags = (plc_is_admin());
223 if ( $local_peer ) {
224   
225   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
226   function get_tagname ($tag) { return $tag['tagname'];}
227   $tagnames = array_map ("get_tagname",$tags);
228   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
229   
230   $toggle = new PlekitToggle ('tags',"Tags",array('trigger-tagname'=>'h2',
231                                                'trigger-bubble'=>'Inspect and set tags on that node',
232                                                'start-visible'=>$show_tags));
233   $toggle->start();
234
235   $headers=array("Name"=>"string",
236                  "Value"=>"string",
237                  "Nodegroup"=>"string",
238                  );
239   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
240   
241   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
242   $table=new PlekitTable("node_tags",$headers,0,$table_options);
243   $table->start();
244   if ($tags) foreach ($tags as $tag) {
245       // does this match a nodegroup ?
246       $nodegroup_name="n/a";
247       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
248       $nodegroup=$nodegroups_hash[$nodegroup_key];
249       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
250       $table->row_start();
251       $table->cell(l_tag_obj($tag));
252       $table->cell($tag['value']);
253       $table->cell($nodegroup_name);
254       // the remove checkbox
255       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
256       $table->row_end();
257     }
258   
259   if ($privileges) {
260     $table->tfoot_start();
261
262     // remove tag 
263     $table->row_start();
264     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
265                  // use the whole columns and right adjust
266                  $table->columns(), "right");
267     $table->row_end();
268
269     // set tag area
270     $table->row_start();
271     // get list of tag names in the node/* category    
272     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
273     // xxx cannot use onchange=submit() - would need to somehow pass action name 
274     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
275     $selector=array_map("tag_selector",$all_tags);
276     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
277     $table->cell($form->text_html("value","",array('width'=>8)));
278     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),2,"left");
279     $table->row_end();
280   }
281   
282   $table->end();
283   $toggle->end();
284 }
285
286 //////////////////////////////////////////////////////////// interfaces
287 if ( $local_peer ) {
288   $toggle=new PlekitToggle ('interfaces',"Interfaces",array('trigger-tagname'=>'h2',
289                                                          'trigger-bubble'=>'Inspect and tune interfaces on that node',
290                                                          'start-hidden'=>true));
291   $toggle->start();
292   // display interfaces
293   if( ! $interfaces ) {
294     echo '<p>';
295     plc_warning_html("This node has no interface");
296     echo "Please add an interface to make this a usable PLC node.</p>\n";
297   } else {
298     $headers=array();
299
300     $headers["IP"]="IPAddress";
301     $headers["Method"]="string";
302     $headers["Type"]="string";
303     $headers["MAC"]="string";
304     $headers["bw limit"]="FileSize";
305     // a single symbol, marking 'p' for primary and a delete button for non-primary
306     if ( $privileges ) $headers[plc_delete_icon()]='string';
307
308     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
309     $table=new PlekitTable("node_interfaces",$headers,2,$table_options);
310     $table->start();
311         
312     foreach ( $interfaces as $interface ) {
313       $interface_id= $interface['interface_id'];
314       $interface_ip= $interface['ip'];
315       $interface_broad= $interface['broadcast'];
316       $interface_primary= $interface['is_primary'];
317       $interface_network= $interface['network'];
318       $interface_dns1= $interface['dns1'];
319       $interface_dns2= $interface['dns2'];
320       $interface_hostname= $interface['hostname'];
321       $interface_netmaks= $interface['netmask'];
322       $interface_gatewary= $interface['gateway'];
323       $interface_mac= $interface['mac'];
324       $interface_bwlimit= $interface['bwlimit'];
325       $interface_type= $interface['type'];
326       $interface_method= $interface['method'];
327
328       $table->row_start();
329       $table->cell(l_interface_t($interface_id,$interface_ip));
330       $table->cell($interface_method);
331       $table->cell($interface_type);
332       $table->cell($interface_mac);
333       $table->cell($interface_bwlimit);
334       if ( $privileges ) {
335         if ($interface_primary) {
336           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
337         } else {
338           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
339         }
340       }
341       $table->row_end();
342     }
343     if ($privileges) {
344       $table->tfoot_start();
345       $table->row_start();
346       $add_button=new PlekitFormButton (l_interface_add($node_id),"add_interface","Add interface","GET");
347       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
348       $table->cell($add_button->html(), 3,"left");
349       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), $table->columns()-3,"right");
350       $table->row_end();
351     }
352     $table->end();
353   }
354   $toggle->end();
355  }
356
357 //////////////////////////////////////////////////////////// slices
358 // display slices
359
360 {
361   $toggle=new PlekitToggle ('slices',"Slices",array('trigger-tagname'=>'h2',
362                                                  'trigger-bubble'=>'Review slices running on that node',
363                                                  'start-hidden'=>true));
364   $toggle->start();
365   if ( ! $slices  ) {
366     plc_warning ("This node is not associated to any slice");
367   } else {
368     $headers=array();
369     $headers['Peer']="string";
370     $headers['Name']="string";
371     $headers['Slivers']="string";
372     $reasonable_page=10;
373     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
374     if (count ($slices) <= $reasonable_page) {
375       $table_options['search_area']=false;
376       $table_options['pagesize_area']=false;
377     }
378     $table=new PlekitTable("node_slices",$headers,1,$table_options);
379     $table->start();
380
381     foreach ($slices as $slice) {
382       $table->row_start();
383       $table->cell ($peers->shortname($peer_id));
384       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
385       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'view'));
386       $table->row_end();
387     }
388     $table->end();
389   }
390   $toggle->end();
391 }
392
393 $form->end();
394
395 ////////////////////////////////////////////////////////////
396 $peers->block_end($peer_id);
397
398 //plekit_linetabs ($tabs,"bottom");
399
400 // Print footer
401 include 'plc_footer.php';
402
403 ?>