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