add node-usb-partition as an option for download.
[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('bubble'=>'Display and modify details for that node',
140                                   'visible'=>get_arg('show_details',true)));
141 $toggle->start();
142
143 $details=new PlekitDetails($privileges);
144 $details->start();
145 if ( ! $local_peer) {
146   $details->th_td("Peer",$peers->peer_link($peer_id));
147   $details->space();
148  }
149
150 $details->form_start(l_actions(),array("action"=>"update-node", "node_id"=>$node_id));
151 // xxx can hostname really be changed like this without breaking the rest, bootcd .. ?
152 //$details->th_td("Hostname",$hostname,"hostname"); 
153 $details->th_td("Hostname",$hostname); 
154 $details->th_td("Model",$model,"model");
155 $details->tr_submit("submit","Update Node");
156 $details->form_end();
157 if ($privileges) $details->space();
158
159 $details->th_td("Type",$node_type);
160 $details->th_td("Version",$version);
161 // let's use plc_objects
162 $Node = new Node($node);
163 $details->th_td("Date created",$Node->dateCreated());
164 $details->th_td("Last contact",$Node->lastContact());
165 $details->th_td("Last update",$Node->lastUpdated());
166
167 // boot area
168 $details->space ();
169 if ( ! ($local_peer && $privileges)) {
170   // just display it
171   $boot_value=$boot_state;
172  } else {
173   $boot_value="";
174   $boot_form = new PlekitForm (l_actions(), array("node_id"=>$node_id,
175                                                "action"=>"node-boot-state"));
176   $boot_value .= $boot_form->start_html();
177   $states = array( 'boot'=>'Boot', 'safeboot'=>'SafeBoot', 'failboot'=>'FailBoot', 
178                    'disabled' => 'Disabled', 'install'=>'Install', 'reinstall'=>'Reinstall');
179   $selectors=array();
180   foreach ($states as $dbname=>$displayname) { 
181     $selector=array("display"=>$displayname, "value"=>$dbname);
182     if ($dbname == $boot_state) $selector['selected']=true;
183     $selectors []= $selector;
184   }
185   $boot_value .= $boot_form->select_html("boot_state",$selectors,array('autosubmit'=>true));
186   $boot_value .= $boot_form->end_html();
187  }
188 $details->th_td ("Boot state",$boot_value);
189
190 // same here for the download area
191 if ( $local_peer  && $privileges) {
192
193   $download_value="";
194   $download_form = new PlekitForm (l_actions_download(),array("node_id"=>$node_id));
195   $download_value .= $download_form->start_html();
196   $selectors = array( 
197                      array("display"=>"-- All in one images --","disabled"=>true),
198                      array("value"=>"download-node-iso","display"=>"Download ISO image for $hostname"),
199                      array("value"=>"download-node-usb","display"=>"Download USB image for $hostname"),
200                      array("value"=>"download-node-usb-partition", "display"=>"Download partitioned, USB image for $hostname"),
201                      array("display"=>"-- Floppy + generic image --","disabled"=>true),
202                      array("value"=>"download-node-floppy","display"=>"Download Floppy file for $hostname"),
203                      array("value"=>"download-generic-iso","display"=>"Download generic ISO image (requires floppy)"),
204                      array("value"=>"download-generic-usb","display"=>"Download generic USB image (requires floppy)"));
205   $download_value .= $download_form->select_html("action",$selectors,
206                                                  array('label'=>"Download mode",'autosubmit'=>true));
207   $download_value .= $download_form->end_html();
208   $details->th_td ("Download",$download_value);
209  }
210
211 // site info and all site nodes
212 $details->space ();
213 $details->th_td("Site",l_site_t($site_id,$site_name));
214                    
215 // build list of node links
216 $nodes_area=array();
217 foreach ($site_node_hash as $hash_node_id => $hash_hostname) {
218   $nodes_area []= l_node_t($hash_node_id,$hash_hostname);
219 }
220 $details->th_tds ("All site nodes",$nodes_area);
221
222 $details->end ();
223 $toggle->end();
224
225 $form=new PlekitForm (l_actions(), array('node_id'=>$node_id));
226 $form->start();
227
228 //////////////////////////////////////////////////////////// slivers
229 {
230   $toggle=new PlekitToggle ('slices',count_english_warning($slices,'sliver'),
231                             array('bubble'=>'Review slices running on that node',
232                                   'visible'=>get_arg('show_slices',false)));
233   $toggle->start();
234   if ( ! $slices  ) {
235     plc_warning ("This node is not associated to any slice");
236   } else {
237     $headers=array();
238     $headers['Peer']="string";
239     $headers['Slice Name']="string";
240     $headers['Sliver']="string";
241     $reasonable_page=10;
242     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
243     if (count ($slices) <= $reasonable_page) {
244       $table_options['search_area']=false;
245       $table_options['pagesize_area']=false;
246     }
247     $table=new PlekitTable("node_slices",$headers,1,$table_options);
248     $table->start();
249
250     foreach ($slices as $slice) {
251       $table->row_start();
252       $peers->cell ($table,$slice['peer_id']);
253       $table->cell (l_slice_t ($slice['slice_id'],$slice['name']));
254       # xxx l_sliver not implemented yet - what should we show exactly ?
255       $table->cell (l_sliver_t ($node_id,$slice['slice_id'],'sliver tags'));
256       $table->row_end();
257     }
258     $table->end();
259   }
260   $toggle->end();
261 }
262
263 //////////////////////////////////////////////////////////// Tags
264 // tags section
265 if ( $local_peer ) {
266   
267   $tags=$api->GetNodeTags (array('node_id'=>$node_id));
268   function get_tagname ($tag) { return $tag['tagname'];}
269   // xxx looks like tech-only see an error here, 
270   // might be that GetNodeTags is not accessible or something
271   $tagnames = array_map ("get_tagname",$tags);
272   $nodegroups_hash=plc_nodegroup_global_hash($api,$tagnames);
273   
274   $toggle = new PlekitToggle ('tags',count_english_warning($tags,'tag'),
275                               array('bubble'=>'Inspect and set tags on that node',
276                                     'visible'=>get_arg('show_tags',false)));
277   $toggle->start();
278
279   $headers=array("Name"=>"string",
280                  "Value"=>"string",
281                  "Nodegroup"=>"string",
282                  );
283   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
284   
285   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
286   $table=new PlekitTable("node_tags",$headers,0,$table_options);
287   $table->start();
288   if ($tags) foreach ($tags as $tag) {
289       // does this match a nodegroup ?
290       $nodegroup_name="n/a";
291       $nodegroup_key=$tag['tagname'] . "=" . $tag['value'];
292       $nodegroup=$nodegroups_hash[$nodegroup_key];
293       if ($nodegroup) $nodegroup_name=l_nodegroup_t($nodegroup['nodegroup_id'],$nodegroup['groupname']);
294       $table->row_start();
295       $table->cell(l_tag_obj($tag));
296       $table->cell($tag['value']);
297       $table->cell($nodegroup_name);
298       // the remove checkbox
299       if (plc_is_admin()) $table->cell ($form->checkbox_html('node_tag_ids[]',$tag['node_tag_id']));
300       $table->row_end();
301     }
302   
303   if ($privileges) {
304     $table->tfoot_start();
305
306     // remove tag 
307     $table->row_start();
308     $table->cell($form->submit_html("delete-node-tags","Remove Tags"),
309                  // use the whole columns and right adjust
310                  array('hfill'=>true,'align'=>'right'));
311     $table->row_end();
312
313     // set tag area
314     $table->row_start();
315     // get list of tag names in the node/* category    
316     $all_tags= $api->GetTagTypes( array ("category"=>"node*"), array("tagname","tag_type_id"));
317     // xxx cannot use onchange=submit() - would need to somehow pass action name 
318     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
319     $selector=array_map("tag_selector",$all_tags);
320     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
321     $table->cell($form->text_html("value","",array('width'=>8)));
322     $table->cell($form->submit_html("set-tag-on-node","Set Tag"),array('columns'=>2,'align'=>'left'));
323     $table->row_end();
324   }
325   
326   $table->end();
327   $toggle->end();
328 }
329
330 //////////////////////////////////////////////////////////// interfaces
331 if ( $local_peer ) {
332   $toggle=new PlekitToggle ('interfaces',count_english_warning($interfaces,'interface'),
333                             array('bubble'=>'Inspect and tune interfaces on that node',
334                                   'visible'=>get_arg('show_interfaces',false)));
335   $toggle->start();
336   // display interfaces
337   if( ! $interfaces ) {
338     echo '<p>';
339     plc_warning_html("This node has no interface");
340     echo "Please add an interface to make this a usable PLC node.</p>\n";
341   } else {
342     $headers=array();
343
344     $headers["IP"]="IPAddress";
345     $headers["Method"]="string";
346     $headers["Type"]="string";
347     $headers["MAC"]="string";
348     $headers["bw limit"]="FileSize";
349     // a single symbol, marking 'p' for primary and a delete button for non-primary
350     if ( $privileges ) $headers[plc_delete_icon()]='string';
351
352     $table_options=array('search_area'=>false,"pagesize_area"=>false,'notes_area'=>false);
353     $table=new PlekitTable("node_interfaces",$headers,2,$table_options);
354     $table->start();
355         
356     foreach ( $interfaces as $interface ) {
357       $interface_id= $interface['interface_id'];
358       $interface_ip= $interface['ip'];
359       $interface_broad= $interface['broadcast'];
360       $interface_primary= $interface['is_primary'];
361       $interface_network= $interface['network'];
362       $interface_dns1= $interface['dns1'];
363       $interface_dns2= $interface['dns2'];
364       $interface_hostname= $interface['hostname'];
365       $interface_netmaks= $interface['netmask'];
366       $interface_gatewary= $interface['gateway'];
367       $interface_mac= $interface['mac'];
368       $interface_bwlimit= $interface['bwlimit'];
369       $interface_type= $interface['type'];
370       $interface_method= $interface['method'];
371
372       $table->row_start();
373       $table->cell(l_interface_t($interface_id,$interface_ip));
374       $table->cell($interface_method);
375       $table->cell($interface_type);
376       $table->cell($interface_mac);
377       $table->cell($interface_bwlimit);
378       if ( $privileges ) {
379         if ($interface_primary) {
380           $table->cell(plc_bubble("p","Cannot delete a primary interface"));
381         } else {
382           $table->cell ($form->checkbox_html('interface_ids[]',$interface_id));
383         }
384       }
385       $table->row_end();
386     }
387     if ($privileges) {
388       $table->tfoot_start();
389       $table->row_start();
390       $add_button=new PlekitFormButton (l_interface_add($node_id),"add","Add Interface","GET");
391       // we should have 6 cols, use 3 for the left (new) and the rest for the right (remove)
392       $table->cell($add_button->html(),array('columns'=> 3,'align'=>'left'));
393       $table->cell($form->submit_html("delete-interfaces","Remove Interfaces"), 
394                    array('columns'=>$table->columns()-3,'align'=>'right'));
395       $table->row_end();
396     }
397     $table->end();
398   }
399   $toggle->end();
400  }
401
402 $form->end();
403
404 ////////////////////////////////////////////////////////////
405 $peers->block_end($peer_id);
406
407 //plekit_linetabs ($tabs,"bottom");
408
409 // Print footer
410 include 'plc_footer.php';
411
412 ?>