add node page reviewed
[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 'linetabs.php';
20 require_once 'table.php';
21 require_once 'details.php';
22 require_once 'form.php';
23 require_once 'toggle.php';
24 require_once 'plc_objects.php';
25
26 // -------------------- 
27 // recognized URL arguments
28 $node_id=intval($_GET['id']);
29 if ( ! $node_id ) { plc_error('Malformed URL - id not set'); return; }
30
31 ////////////////////
32 // Get all columns as we focus on only one entry
33 $nodes= $api->GetNodes( array($node_id));
34
35 if (empty($nodes)) {
36   drupal_set_message ("Node " . $node_id . " not found");
37   return;
38  }
39
40 $node=$nodes[0];
41 // node info
42 $hostname= $node['hostname'];
43 $boot_state= $node['boot_state'];
44 $site_id= $node['site_id'];
45 $model= $node['model'];
46 $version= $node['version'];
47 $node_type = $node['node_type'];
48
49 // arrays of ids of node info
50 $slice_ids= $node['slice_ids'];
51 $conf_file_ids= $node['conf_file_ids'];
52 $interface_ids= $node['interface_ids'];
53 $nodegroup_ids= $node['nodegroup_ids'];
54 $pcu_ids= $node['pcu_ids'];
55
56 // get peers
57 $peer_id = $node['peer_id'];
58 $peers=new Peers ($api);
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 // turning this off: GetPCUs is not allowed to users, and we don't show PCUs yet anyway
98 //if( !empty( $pcu_ids ) )
99 //  $PCUs= $api->GetPCUs( $pcu_ids );
100
101 //////////////////// display node info
102
103 drupal_set_title("Details for node " . $hostname);
104 $local_peer= ! $peer_id;
105
106   
107 // extra privileges to admins, and (pi||tech) on this site
108 $privileges = (plc_is_admin () && $local_peer) || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
109   
110 $tabs=array();
111 // available actions
112 $tabs [] = tab_nodes_site($site_id);
113 $tabs [] = tab_site($site_id);
114 //$tabs [] = tab_nodes();
115
116 if ( $local_peer  && $privileges ) {
117     
118   $tabs["Add Interface"]=array('url'=>l_interface_add($node_id),
119                                'bubble'=>"Define new network interface on $hostname");
120   $tabs['Delete'] = array ('url'=>l_actions(),
121                            'method'=>'POST',
122                            'values'=>array('action'=>'delete-node','node_id'=>$node_id),
123                            'bubble'=>"Delete node $hostname",
124                            'confirm'=>'Are you sure to delete ' . $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 plekit_linetabs($tabs);
134
135 // show gray background on foreign objects : start a <div> with proper class
136 $peers->block_start ($peer_id);
137   
138 $toggle = new PlekitToggle ('node',"Details",
139                             array('trigger-bubble'=>'Display and modify details for that node'));
140 $toggle->start();
141
142 $details=new PlekitDetails($privileges);
143 $details->start();
144 if ( ! $local_peer) {
145   $details->th_td("Peer",$peers->peer_link($peer_id));
146   $details->space();
147  }
148
149 $details->form_start(l_actions(),array("action"=>"update-node", "node_id"=>$node_id));
150 // xxx can hostname really be changed like this without breaking the rest, bootcd .. ?
151 //$details->th_td("Hostname",$hostname,"hostname"); 
152 $details->th_td("Hostname",$hostname); 
153 $details->th_td("Model",$model,"model");
154 $details->tr_submit("submit","Update Node");
155 $details->form_end();
156 if ($privileges) $details->space();
157
158 $details->th_td("Type",$node_type);
159 $details->th_td("Version",$version);
160 // let's use plc_objects
161 $Node = new Node($node);
162 $details->th_td("Date created",$Node->dateCreated());
163 $details->th_td("Last contact",$Node->lastContact());
164 $details->th_td("Last update",$Node->lastUpdated());
165
166 // boot area
167 $details->space ();
168 if ( ! ($local_peer && $privileges)) {
169   // just display it
170   $boot_value=$boot_state;
171  } else {
172   $boot_value="";
173   $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
174                                                "action"=>"node-boot-state"));
175   $boot_value .= $boot_form->start_html();
176   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 'failboot'=>'FailBoot', 
177                    'disabled' => 'Disabled', 'install'=>'Install', 'reinstall'=>'Reinstall');
178   $selectors=array();
179   foreach ($states as $dbname=>$displayname) { 
180     $selector=array("display"=>$displayname, "value"=>$dbname);
181     if ($dbname == $boot_state) $selector['selected']=true;
182     $selectors []= $selector;
183   }
184   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
185   $boot_value .= $boot_form->end_html();
186  }
187 $details->th_td ("Boot state",$boot_value);
188
189 // same here for the download area
190 if ( $local_peer  && $privileges) {
191
192   $download_value="";
193   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
194   $download_value .= $download_form->start_html();
195   $selectors = array( 
196                      array("display"=>"-- All in one images --","disabled"=>true),
197                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
198                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
199                      array("display"=>"-- Floppy + generic image --","disabled"=>true),
200                      array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
201                      array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
202                      array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
203   $download_value .= $download_form->select_html("action",$selectors,
204                                                  array('label'=>"Download mode",'autosubmit'=>true));
205   $download_value .= $download_form->end_html();
206   $details->th_td ("Download",$download_value);
207  }
208
209 // site info and all site nodes
210 $details->space ();
211 $details->th_td("Site",l_site_t($site_id,$site_name));
212                    
213 // build list of node links
214 $nodes_area=array();
215 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
216   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
217 }
218 $details->th_tds ("All site nodes",$nodes_area);
219
220 $details->end ();
221 $toggle->end();
222
223 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
224 $form->start();
225
226 //////////////////////////////////////////////////////////// Tags
227 // tags section
228 $show_tags = (plc_is_admin());
229 if ( $local_peer ) {
230   
231   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
232   function get_tagname ($tag) { return $tag['tagname'];}
233   // xxx looks like tech-only see an error here, 
234   // might be that GetNodeTags is not accessible or something
235   $tagnames = array_map ("get_tagname",$tags);
236   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
237   
238   $toggle = new PlekitToggle ('tags',"Tags",
239                               array('trigger-bubble'=>'Inspect and set tags on that node',
240                                     'start-visible'=>$show_tags));
241   $toggle->start();
242
243   $headers=array("Name"=>"string",
244                  "Value"=>"string",
245                  "Nodegroup"=>"string",
246                  );
247   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
248   
249   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
250   $table=new PlekitTable("node_tags",$headers,0,$table_options);
251   $table->start();
252   if ($tags) foreach ($tags as $tag) {
253       // does this match a nodegroup ?
254       $nodegroup_name="n/a";
255       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
256       $nodegroup=$nodegroups_hash[$nodegroup_key];
257       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
258       $table->row_start();
259       $table->cell(l_tag_obj($tag));
260       $table->cell($tag['value']);
261       $table->cell($nodegroup_name);
262       // the remove checkbox
263       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
264       $table->row_end();
265     }
266   
267   if ($privileges) {
268     $table->tfoot_start();
269
270     // remove tag 
271     $table->row_start();
272     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
273                  // use the whole columns and right adjust
274                  $table->columns(), "right");
275     $table->row_end();
276
277     // set tag area
278     $table->row_start();
279     // get list of tag names in the node/* category    
280     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
281     // xxx cannot use onchange=submit() - would need to somehow pass action name 
282     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
283     $selector=array_map("tag_selector",$all_tags);
284     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
285     $table->cell($form->text_html("value","",array('width'=>8)));
286     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),2,"left");
287     $table->row_end();
288   }
289   
290   $table->end();
291   $toggle->end();
292 }
293
294 //////////////////////////////////////////////////////////// interfaces
295 if ( $local_peer ) {
296   $toggle=new PlekitToggle ('interfaces',"Interfaces",
297                             array('trigger-bubble'=>'Inspect and tune interfaces on that node',
298                                   'start-hidden'=>true));
299   $toggle->start();
300   // display interfaces
301   if( ! $interfaces ) {
302     echo '<p>';
303     plc_warning_html("This node has no interface");
304     echo "Please add an interface to make this a usable PLC node.</p>\n";
305   } else {
306     $headers=array();
307
308     $headers["IP"]="IPAddress";
309     $headers["Method"]="string";
310     $headers["Type"]="string";
311     $headers["MAC"]="string";
312     $headers["bw limit"]="FileSize";
313     // a single symbol, marking 'p' for primary and a delete button for non-primary
314     if ( $privileges ) $headers[plc_delete_icon()]='string';
315
316     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
317     $table=new PlekitTable("node_interfaces",$headers,2,$table_options);
318     $table->start();
319         
320     foreach ( $interfaces as $interface ) {
321       $interface_id= $interface['interface_id'];
322       $interface_ip= $interface['ip'];
323       $interface_broad= $interface['broadcast'];
324       $interface_primary= $interface['is_primary'];
325       $interface_network= $interface['network'];
326       $interface_dns1= $interface['dns1'];
327       $interface_dns2= $interface['dns2'];
328       $interface_hostname= $interface['hostname'];
329       $interface_netmaks= $interface['netmask'];
330       $interface_gatewary= $interface['gateway'];
331       $interface_mac= $interface['mac'];
332       $interface_bwlimit= $interface['bwlimit'];
333       $interface_type= $interface['type'];
334       $interface_method= $interface['method'];
335
336       $table->row_start();
337       $table->cell(l_interface_t($interface_id,$interface_ip));
338       $table->cell($interface_method);
339       $table->cell($interface_type);
340       $table->cell($interface_mac);
341       $table->cell($interface_bwlimit);
342       if ( $privileges ) {
343         if ($interface_primary) {
344           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
345         } else {
346           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
347         }
348       }
349       $table->row_end();
350     }
351     if ($privileges) {
352       $table->tfoot_start();
353       $table->row_start();
354       $add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
355       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
356       $table->cell($add_button->html(), 3,"left");
357       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), $table->columns()-3,"right");
358       $table->row_end();
359     }
360     $table->end();
361   }
362   $toggle->end();
363  }
364
365 //////////////////////////////////////////////////////////// slices
366 // display slices
367
368 {
369   $toggle=new PlekitToggle ('slices',"Slices",
370                             array('trigger-bubble'=>'Review slices running on that node',
371                                   'start-hidden'=>true));
372   $toggle->start();
373   if ( ! $slices  ) {
374     plc_warning ("This node is not associated to any slice");
375   } else {
376     $headers=array();
377     $headers['Peer']="string";
378     $headers['Name']="string";
379     $headers['Slivers']="string";
380     $reasonable_page=10;
381     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
382     if (count ($slices) <= $reasonable_page) {
383       $table_options['search_area']=false;
384       $table_options['pagesize_area']=false;
385     }
386     $table=new PlekitTable("node_slices",$headers,1,$table_options);
387     $table->start();
388
389     foreach ($slices as $slice) {
390       $table->row_start();
391       $table->cell ($peers->shortname($peer_id));
392       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
393       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'view'));
394       $table->row_end();
395     }
396     $table->end();
397   }
398   $toggle->end();
399 }
400
401 $form->end();
402
403 ////////////////////////////////////////////////////////////
404 $peers->block_end($peer_id);
405
406 //plekit_linetabs ($tabs,"bottom");
407
408 // Print footer
409 include 'plc_footer.php';
410
411 ?>