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