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