rework footers
[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['Update'] = array ('url'=>"/db/nodes/node_actions.php",
112                            'method'=>'POST',
113                            'values'=>array('action'=>'prompt-update','node_id'=>$node_id),
114                            'bubble'=>"Update details of $hostname");
115   $tabs['Delete'] = array ('url'=>"/db/nodes/node_actions.php",
116                            'method'=>'POST',
117                            'values'=>array('action'=>'delete','node_id'=>$node_id),
118                            'bubble'=>"Delete node $hostname",
119                            'confirm'=>'Are you sure to delete ' . $hostname. ' ?');
120   // xxx subject to roles
121   $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
122                                'bubble'=>"Declare new network interface on $hostname");
123   $tabs["Events"]=array_merge(tabs_events(),
124                               array('url'=>l_event("Node","node",$node_id),
125                                     'bubble'=>"Events for node $hostname"));
126   $tabs["Comon"]=array_merge(tabs_comon(),
127                              array('url'=>l_comon("node_id",$node_id),
128                                    'bubble'=>"Comon page about node $hostname"));
129  }
130
131 $tabs["All nodes"]=l_nodes();
132
133 plc_tabs($tabs);
134
135 // show gray background on foreign objects : start a <div> with proper class
136 $peers->block_start ($peer_id);
137   
138 plc_details_start ();
139 if ( ! $local_peer) {
140   plc_details_line("Peer",$peers->peer_link($peer_id));
141   plc_details_space_line();
142  }
143
144 plc_details_line("Hostname",$hostname);
145 plc_details_line("Type",$node_type);
146 plc_details_line("Model",$model);
147 plc_details_line("Version",$version);
148 // no tool to implement this multiple-choice setting yet
149 // xxx would need at least to use the proper class, like plc_details_class() or something
150 plc_details_space_line ();
151 echo "<tr><th>Boot State: </th><td>";
152 if ( ! $local_peer) {
153   echo $boot_state;
154  } else {
155   echo "<form name='bootstate' action='/db/nodes/node_actions.php' method=post>\n";
156   echo "<input type=hidden name='node_id' value='$node_id'>\n";
157   echo "<input type=hidden name='action' value='boot-state'>\n";
158   echo "<select name='boot_state' onChange=\"submit();\">\n";
159
160   $states= array( 'boot'=>'Boot', 'dbg'=>'Debug', 'inst'=>'Install', 'rins'=>'Reinstall', 'rcnf'=>'Reconfigure', 'new'=>'New' );
161
162   foreach( $states as $key => $val ) {
163     echo "<option value='$key'";
164       
165     if( $key == $boot_state )
166       echo " selected";
167       
168     echo ">$val</option>\n";
169       
170   }
171   echo "</select></input></form>";
172  }
173 echo "</td></tr>\n";
174
175 // same here for the download area
176 if ( $local_peer  && $privileges) {
177
178   echo "<tr><th>Download </th><td>";
179   echo "<form name='download' action='/db/nodes/node_actions.php' method='post'>\n";
180   echo "<input type=hidden name='node_id' value='$node_id'></input>\n";
181   echo "<select name='action' onChange='submit();'>\n";
182   echo "<option value='' selected='selected'> Download Mode </option>\n";
183   echo "<option value='' disabled='disabled'> -- All in one images -- </option>"; 
184   echo "<option value='download-node-iso' $new_api_only> Download ISO image for $hostname</option>\n";
185   echo "<option value='download-node-usb' $new_api_only> Download USB image for $hostname</option>\n";
186   echo "<option value='' disabled='disabled'> -- Floppy + generic image -- </option>"; 
187   echo "<option value='download-node-floppy'> Download Floppy file for $hostname</option>\n";
188   echo "<option value='download-generic-iso' $new_api_only> Download generic ISO image (requires floppy) </option>\n";
189   echo "<option value='download-generic-usb' $new_api_only> Download generic USB image (requires floppy) </option>\n";
190   echo "</select></form>";
191   echo "</td></tr>\n";
192
193  }
194
195 // site info and all site nodes
196 plc_details_space_line ();
197 plc_details_line("Site",l_site_t($site_id,$site_name));
198                    
199 // build list of node links
200 $nodes_area=array();
201 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
202   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
203 }
204 plc_details_line_list ("All site nodes",$nodes_area);
205
206 plc_details_end ();
207
208 plc_form_start(l_actions(), array('node_id'=>$node_id));
209
210 //////////////////////////////////////////////////////////// Tags
211 // get tags
212 if ( $local_peer ) {
213   
214   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
215   function get_tagname ($tag) { return $tag['tagname'];}
216   $tagnames = array_map ("get_tagname",$tags);
217   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
218   
219   plc_section("Tags");
220   $headers=array("Name"=>"string",
221                  "Value"=>"string",
222                  "Nodegroup"=>"string",
223                  "Remove"=>"string",
224                  );
225   
226   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
227   $table=new PlcTable("node_tags",$headers,0,$table_options);
228   $table->start();
229   if ($tags) foreach ($tags as $tag) {
230       // does this match a nodegroup ?
231       $nodegroup_name="n/a";
232       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
233       $nodegroup=$nodegroups_hash[$nodegroup_key];
234       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
235       $table->row_start();
236       $table->cell($tag['tagname']);
237       $table->cell($tag['value']);
238       $table->cell($nodegroup_name);
239       $table->cell (plc_form_checkbox_text('node_tag_ids[]',$tag['node_tag_id']));
240       $table->row_end();
241     }
242   
243   if ($privileges) {
244     $table->tfoot_start();
245
246     // remove tag 
247     $table->row_start();
248     $table->cell(plc_form_submit_text("remove-node-tags","Remove Tags"),
249                  // use the whole columns and right adjust
250                  $table->columns(), "right");
251     $table->row_end();
252
253     // set tag area
254     $table->row_start();
255     // get list of tag names in the node/* category    
256     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
257     // xxx cannot use onchange=submit() - would need to somehow pass action name 
258     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
259     $selector=array_map("tag_selector",$all_tags);
260     $table->cell(plc_form_select_text("tag_type_id",$selector,"Choose"));
261     $table->cell(plc_form_text_text("value","",8));
262     $table->cell(plc_form_submit_text("set-tag-on-node","Set Tag"),2,"left");
263     $table->row_end();
264   }
265   
266   $table->end();
267  }
268
269 //////////////////////////////////////////////////////////// slices
270 // display slices
271
272 plc_section ("Slices");
273 if ( ! $slices  ) {
274   plc_warning ("This node is not associated to any slice");
275  } else {
276   $headers=array();
277   $headers['Peer']="string";
278   $headers['Name']="string";
279   $headers['Slivers']="string";
280   $reasonable_page=10;
281   $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
282   if (count ($slices) <= $reasonable_page) {
283     $table_options['search_area']=false;
284     $table_options['pagesize_area']=false;
285   }
286   $table=new PlcTable("node_slices",$headers,1,$table_options);
287   $table->start();
288
289   foreach ($slices as $slice) {
290     $table->row_start();
291     $table->cell ($peers->shortname($peer_id));
292     $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
293     $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'view'));
294     $table->row_end();
295   }
296   $table->end();
297  }
298
299 //////////////////////////////////////////////////////////// interfaces
300 if ( $local_peer ) {
301
302   plc_section ("Interfaces");
303   // display interfaces
304   if( ! $interfaces ) {
305     echo '<p>';
306     plc_warning_text("This node has no interface");
307     echo "Please add an interface to make this a usable PLC node.</p>\n";
308   } else {
309     $headers=array();
310     if ( $privileges ) {
311       // a single symbol, marking 'p' for primary and a delete button for non-primary
312       $headers[' ']='string';
313     }
314          
315     $headers["IP"]="IPAddress";
316     $headers["Method"]="string";
317     $headers["Type"]="string";
318     $headers["MAC"]="string";
319     $headers["bw limit"]="FileSize";
320
321     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
322     $table=new PlcTable("node_interfaces",$headers,2,$table_options);
323     $table->start();
324         
325     foreach ( $interfaces as $interface ) {
326       $interface_id= $interface['interface_id'];
327       $interface_ip= $interface['ip'];
328       $interface_broad= $interface['broadcast'];
329       $interface_primary= $interface['is_primary'];
330       $interface_network= $interface['network'];
331       $interface_dns1= $interface['dns1'];
332       $interface_dns2= $interface['dns2'];
333       $interface_hostname= $interface['hostname'];
334       $interface_netmaks= $interface['netmask'];
335       $interface_gatewary= $interface['gateway'];
336       $interface_mac= $interface['mac'];
337       $interface_bwlimit= $interface['bwlimit'];
338       $interface_type= $interface['type'];
339       $interface_method= $interface['method'];
340
341       $table->row_start();
342       if ( $privileges ) {
343         if (!$interface_primary) {
344           // xxx 
345           $table->cell (plc_delete_link_button ('interfaces.php?id=' . $interface_id . '&delete=1&submitted=1', 
346                                                   '\\nInterface ' . $interface_ip));
347         } else {
348           $table->cell('p');
349         }
350       }
351       $table->cell(l_interface_t($interface_id,$interface_ip));
352       $table->cell($interface_method);
353       $table->cell($interface_type);
354       $table->cell($interface_mac);
355       $table->cell($interface_bwlimit);
356       $table->row_end();
357     }
358     if ($privileges) {
359       $table->tfoot_start();
360       $table->row_start();
361       $table->cell(plc_form_simple_button(l_interface_add($node_id),"Add interface","GET"),
362                    $table->columns(),"right");
363       $table->row_end();
364     }
365     $table->end();
366   }
367  }
368
369 plc_form_end();
370
371 ////////////////////////////////////////////////////////////
372 $peers->block_end($peer_id);
373
374
375 // Print footer
376 include 'plc_footer.php';
377
378 ?>