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