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