cleaned up tabs
[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 require_once 'plc_objects.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $node_id=intval($_GET['id']);
28 if ( ! $node_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $nodes= $api->GetNodes( array($node_id));
33
34 if (empty($nodes)) {
35   drupal_set_message ("Node " . $node_id . " not found");
36   return;
37  }
38
39 $node=$nodes[0];
40 // node info
41 $hostname= $node['hostname'];
42 $boot_state= $node['boot_state'];
43 $site_id= $node['site_id'];
44 $model= $node['model'];
45 $version= $node['version'];
46 $node_type = $node['node_type'];
47
48 // arrays of ids of node info
49 $slice_ids= $node['slice_ids'];
50 $conf_file_ids= $node['conf_file_ids'];
51 $interface_ids= $node['interface_ids'];
52 $nodegroup_ids= $node['nodegroup_ids'];
53 $pcu_ids= $node['pcu_ids'];
54
55 // get peers
56 $peer_id = $node['peer_id'];
57 $peers=new Peers ($api);
58
59 // gets site info
60 $sites= $api->GetSites( array( $site_id ) );
61 $site=$sites[0];
62 $site_name= $site['name'];
63 $site_node_ids= $site['node_ids'];
64
65 // hash node_id=>hostname for this site's nodes
66 $site_node_hash=array();
67 if( !empty( $site_node_ids ) ) {
68   // get site node info basics
69   $site_nodes= $api->GetNodes( $site_node_ids );
70     
71   foreach( $site_nodes as $site_node ) {
72     $site_node_hash[$site_node['node_id']]= $site_node['hostname'];
73   }
74  }
75   
76 // gets slice info for each slice
77 if( !empty( $slice_ids ) )
78   $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" , "peer_id" ) );
79
80 // get interface info
81 if( !empty( $interface_ids ) )
82   $interfaces= $api->GetInterfaces( $interface_ids );
83
84 // gets nodegroup info
85 if( !empty( $nodegroup_ids ) )
86   $nodegroups= $api->GetNodeGroups( $nodegroup_ids, array("groupname","tag_type_id","value"));
87
88 // xxx Thierry : remaining stuff
89 // (*) events: should display the latest events relating to that node.
90 // disabling call to GetEvents, that gets the session deleted in the DB
91 // (*) conf_files: is fetched but not displayed
92 if( !empty( $conf_file_ids ) )
93   $conf_files= $api->GetConfFiles( $conf_file_ids );
94 // (*) idem for PCUs
95 // gets pcu and port info key to both is $pcu_id
96 // turning this off: GetPCUs is not allowed to users, and we don't show PCUs yet anyway
97 //if( !empty( $pcu_ids ) )
98 //  $PCUs= $api->GetPCUs( $pcu_ids );
99
100 //////////////////// display node info
101
102 drupal_set_title("Details for node " . $hostname);
103 $local_peer= ! $peer_id;
104
105   
106 // extra privileges to admins, and (pi||tech) on this site
107 $privileges = (plc_is_admin () && $local_peer) || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
108   
109 $tabs=array();
110 // available actions
111 $tabs [] = tab_nodes_site($site_id);
112 $tabs [] = tab_site($site_id);
113 $tabs [] = tab_nodes();
114
115 if ( $local_peer  && $privileges ) {
116     
117   $tabs['Delete'] = array ('url'=>l_actions(),
118                            'method'=>'POST',
119                            'values'=>array('action'=>'delete-node','node_id'=>$node_id),
120                            'bubble'=>"Delete node $hostname",
121                            'confirm'=>'Are you sure to delete ' . $hostname. ' ?');
122   // xxx subject to roles
123   $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
124                                'bubble'=>"Define new network interface on $hostname");
125   $tabs["Events"]=array_merge(tablook_event(),
126                               array('url'=>l_event("Node","node",$node_id),
127                                     'bubble'=>"Events for node $hostname"));
128   $tabs["Comon"]=array_merge(tablook_comon(),
129                              array('url'=>l_comon("node_id",$node_id),
130                                    'bubble'=>"Comon page about node $hostname"));
131  }
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 $details=new PlcDetails($privileges);
139 $details->start();
140 if ( ! $local_peer) {
141   $details->th_td("Peer",$peers->peer_link($peer_id));
142   $details->space();
143  }
144
145 $details->form_start(l_actions(),array("action"=>"update-node", "node_id"=>$node_id));
146 $details->th_td("Hostname",$hostname,"hostname"); 
147 $details->th_td("Model",$model,"model");
148 $details->tr_submit("submit","Update Node");
149 $details->form_end();
150 if ($privileges) $details->space();
151
152 $details->th_td("Type",$node_type);
153 $details->th_td("Version",$version);
154 // let's use plc_objects
155 $Node = new Node($node);
156 $details->th_td("Date created",$Node->dateCreated());
157 $details->th_td("Last contact",$Node->lastContact());
158 $details->th_td("Last update",$Node->lastUpdated());
159
160 // boot area
161 $details->space ();
162 if ( ! ($local_peer && $privileges)) {
163   // just display it
164   $boot_value=$boot_state;
165  } else {
166   $boot_value="";
167   $boot_form = new PlcForm (l_actions(), array("node_id"=>$node_id,
168                                                "action"=>"node-boot-state"));
169   $boot_value .= $boot_form->start_html();
170   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 'failboot'=>'FailBoot', 
171                    'disabled' => 'Disabled', 'install'=>'Install', 'reinstall'=>'Reinstall');
172   $selectors=array();
173   foreach ($states as $dbname=>$displayname) { 
174     $selector=array("display"=>$displayname, "value"=>$dbname);
175     if ($dbname == $boot_state) $selector['selected']=true;
176     $selectors []= $selector;
177   }
178   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
179   $boot_value .= $boot_form->end_html();
180  }
181 $details->th_td ("Boot state",$boot_value);
182
183 // same here for the download area
184 if ( $local_peer  && $privileges) {
185
186   $download_value="";
187   $download_form = new PlcForm (l_actions_download(),array("node_id"=>$node_id));
188   $download_value .= $download_form->start_html();
189   $selectors = array( 
190                      array("display"=>"-- All in one images --","disabled"=>true),
191                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
192                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname<"),
193                      array("display"=>"-- Floppy + generic image --","disabled"=>true),
194                      array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
195                      array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
196                      array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
197   $download_value .= $download_form->select_html("action",$selectors,
198                                                  array('label'=>"Download mode",'autosubmit'=>true));
199   $download_value .= $download_form->end_html();
200   $details->th_td ("Download",$download_value);
201  }
202
203 // site info and all site nodes
204 $details->space ();
205 $details->th_td("Site",l_site_t($site_id,$site_name));
206                    
207 // build list of node links
208 $nodes_area=array();
209 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
210   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
211 }
212 $details->th_tds ("All site nodes",$nodes_area);
213
214 $details->end ();
215
216 $form=new PlcForm (l_actions(), array('node_id'=>$node_id));
217 $form->start();
218
219 //////////////////////////////////////////////////////////// Tags
220 // get tags
221 if ( $local_peer ) {
222   
223   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
224   function get_tagname ($tag) { return $tag['tagname'];}
225   $tagnames = array_map ("get_tagname",$tags);
226   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
227   
228   plc_section("Tags");
229   $headers=array("Name"=>"string",
230                  "Value"=>"string",
231                  "Nodegroup"=>"string",
232                  );
233   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
234   
235   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
236   $table=new PlcTable("node_tags",$headers,0,$table_options);
237   $table->start();
238   if ($tags) foreach ($tags as $tag) {
239       // does this match a nodegroup ?
240       $nodegroup_name="n/a";
241       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
242       $nodegroup=$nodegroups_hash[$nodegroup_key];
243       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
244       $table->row_start();
245       $table->cell(l_tag_obj($tag));
246       $table->cell($tag['value']);
247       $table->cell($nodegroup_name);
248       // the remove checkbox
249       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
250       $table->row_end();
251     }
252   
253   if ($privileges) {
254     $table->tfoot_start();
255
256     // remove tag 
257     $table->row_start();
258     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
259                  // use the whole columns and right adjust
260                  $table->columns(), "right");
261     $table->row_end();
262
263     // set tag area
264     $table->row_start();
265     // get list of tag names in the node/* category    
266     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
267     // xxx cannot use onchange=submit() - would need to somehow pass action name 
268     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
269     $selector=array_map("tag_selector",$all_tags);
270     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
271     $table->cell($form->text_html("value","",array('width'=>8)));
272     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),2,"left");
273     $table->row_end();
274   }
275   
276   $table->end();
277  }
278
279 //////////////////////////////////////////////////////////// interfaces
280 if ( $local_peer ) {
281
282   plc_section ("Interfaces");
283   // display interfaces
284   if( ! $interfaces ) {
285     echo '<p>';
286     plc_warning_html("This node has no interface");
287     echo "Please add an interface to make this a usable PLC node.</p>\n";
288   } else {
289     $headers=array();
290
291     $headers["IP"]="IPAddress";
292     $headers["Method"]="string";
293     $headers["Type"]="string";
294     $headers["MAC"]="string";
295     $headers["bw limit"]="FileSize";
296     // a single symbol, marking 'p' for primary and a delete button for non-primary
297     if ( $privileges ) $headers[plc_delete_icon()]='string';
298
299     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
300     $table=new PlcTable("node_interfaces",$headers,2,$table_options);
301     $table->start();
302         
303     foreach ( $interfaces as $interface ) {
304       $interface_id= $interface['interface_id'];
305       $interface_ip= $interface['ip'];
306       $interface_broad= $interface['broadcast'];
307       $interface_primary= $interface['is_primary'];
308       $interface_network= $interface['network'];
309       $interface_dns1= $interface['dns1'];
310       $interface_dns2= $interface['dns2'];
311       $interface_hostname= $interface['hostname'];
312       $interface_netmaks= $interface['netmask'];
313       $interface_gatewary= $interface['gateway'];
314       $interface_mac= $interface['mac'];
315       $interface_bwlimit= $interface['bwlimit'];
316       $interface_type= $interface['type'];
317       $interface_method= $interface['method'];
318
319       $table->row_start();
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       if ( $privileges ) {
326         if ($interface_primary) {
327           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
328         } else {
329           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
330         }
331       }
332       $table->row_end();
333     }
334     if ($privileges) {
335       $table->tfoot_start();
336       $table->row_start();
337       $add_button=new PlcFormButton (l_interface_add($node_id),"add_interface","Add interface","GET");
338       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
339       $table->cell($add_button->html(), 3,"left");
340       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), $table->columns()-3,"right");
341       $table->row_end();
342     }
343     $table->end();
344   }
345  }
346
347 //////////////////////////////////////////////////////////// slices
348 // display slices
349
350 plc_section ("Slices");
351 if ( ! $slices  ) {
352   plc_warning ("This node is not associated to any slice");
353  } else {
354   $headers=array();
355   $headers['Peer']="string";
356   $headers['Name']="string";
357   $headers['Slivers']="string";
358   $reasonable_page=10;
359   $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
360   if (count ($slices) <= $reasonable_page) {
361     $table_options['search_area']=false;
362     $table_options['pagesize_area']=false;
363   }
364   $table=new PlcTable("node_slices",$headers,1,$table_options);
365   $table->start();
366
367   foreach ($slices as $slice) {
368     $table->row_start();
369     $table->cell ($peers->shortname($peer_id));
370     $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
371     $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'view'));
372     $table->row_end();
373   }
374   $table->end();
375  }
376
377 $form->end();
378
379 ////////////////////////////////////////////////////////////
380 $peers->block_end($peer_id);
381
382
383 // Print footer
384 include 'plc_footer.php';
385
386 ?>