add node-usb-partition as an option for download.
[plewww.git] / planetlab / nodes / node_downloads.php
1 <?php
2
3   // $Id$
4
5   // cleaned up, keep only the actions related to downloading stuff
6   // REQUIRED : node_id=node_id
7   // (*) action='download-node-floppy' : 
8   // (*) action='download-node-iso' : 
9   // (*) action='download-node-usb' : 
10   //                            : same as former downloadconf.php with download unset
11   //     if in addition POST contains a non-empty field 'download' :
12   //                            : performs actual node-dep download
13   // (*) action='download-generic-iso':
14   // (*) action='download-generic-usb':
15   //                            : performs actual generic download
16
17 // delivering node-dependant images requires larger memory limit
18 // trial and error, based on the current sizes
19 // generic-ISO 43980800 
20 // generic-usb 44720128 
21 // 256M OK
22 // 128M OK
23 // 96M OK
24 // 88M KO
25 // 80M KO
26 // 64M KO
27 // Bottom line is, looks like we need in the order of twice the file size
28 // so let's play it safe
29 // Thierry - for 4.2, we need a larger area, was 100 for 4.1
30 ini_set("memory_limit","150M");
31
32 // Require login
33 require_once 'plc_login.php';
34
35 // Get session and API handles
36 require_once 'plc_session.php';
37 global $plc, $api;
38
39 // Common functions
40 require_once 'plc_functions.php';
41 require_once 'details.php';
42
43 // NOTE: this function exits() after it completes its job, 
44 // simply returning leads to html decorations being added around the contents
45 function deliver_and_unlink ($filename) {
46   
47   // for security reasons we want to be able to unlink the resulting file once downloaded
48   // so simply redirecting through a header("Location:") is not good enough
49
50   $size= filesize($filename);
51
52   // headers
53   header ("Content-Type: application/octet-stream");
54   header ("Content-Transfer-Encoding: binary");
55   header ("Content-Disposition: attachment; filename=" . basename($filename) );
56   header ("Content-Length: " . $size );
57   // for getting IE to work properly
58   // from princeton cvs new_plc_www/planetlab/nodes/downloadconf.php 1.2->1.3
59   header ("Pragma: hack");
60   header ("Cache-Control: public, must-revalidate");
61
62   // outputs the whole file contents
63   print (file_get_contents($filename));
64   
65   // unlink the file
66   if (! unlink ($filename) ) {
67     // cannot unlink, but how can we notify this ?
68     // certainly not by printing
69   }
70   exit();
71 }
72
73 function show_download_confirm_button ($api, $node_id, $action, $can_gen_config, $show_details) {
74
75   if( $can_gen_config ) {
76     if ($show_details) {
77       $preview=$api->GetBootMedium($node_id,"node-preview","");
78       print ("<hr />");
79       print ("<h3 class='node_download'>Configuration file contents</h3>");
80       print ("<pre>\n$preview</pre>\n");
81       print ("<hr />");
82     }
83     $action_labels = array ('download-node-floppy' => 'textual node config (for floppy)' ,
84                             'download-node-iso' => 'ISO image',
85                             'download-node-usb' => 'USB image' );
86     
87     $format = $action_labels [ $action ] ;
88     print( "<div id='download_button'> <form method='post' action='node_downloads.php'>");
89     print ("<input type='hidden' name='node_id' value='$node_id'>");
90     print ("<input type='hidden' name='action' value='$action'>");
91     print ("<input type='hidden' name='download' value='1'>");
92     print( "<input type='submit' value='Download $format'>" );
93     print( "</form></div>\n" );
94   } else {
95     echo "<p><font color=red>Configuration file cannot be created until missing values above are updated.</font>";
96   }
97 }
98
99 // check arguments
100
101 if (empty($_POST['node_id'])) {
102   plc_redirect (l_nodes());
103  } else {
104   $node_id = intval($_POST['node_id']);
105 }
106
107 $action=$_POST['action'];
108
109 switch ($action) {
110
111  case "download-generic-iso":
112  case "download-generic-usb":
113    
114    if ($action=="download-generic-iso") {
115      $boot_action="generic-iso";
116    } else {
117      $boot_action="generic-usb";
118    }
119
120    // place the result in a random-named sub dir for clear filenames
121    $filename = $api->GetBootMedium ($node_id, $boot_action, "%d/%n-%p-%a-%v%s");
122    $error=$api->error();
123    // NOTE. for some reason, GetBootMedium sometimes does not report an error but the
124    // file is not created - this happens e.g. when directory owmer/modes are wrong 
125    // in this case we get an empty filename
126    // see /etc/httpd/logs/error_log in this case
127    if (empty($error) && empty($filename)) {
128      $error="Unexpected error from GetBootMedium - probably wrong directory modes";
129    }    
130    if (! empty($error)) {
131      print ("<div class='plc-error'> $error </div>\n");
132      print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
133      return ;
134    } else {
135      deliver_and_unlink ($filename);
136      exit();
137    }
138    break;
139
140    // ACTION: download-node
141    // from former downloadconf.php
142    
143  case "download-node-floppy":
144  case "download-node-iso":
145  case "download-node-usb":
146  case "download-node-usb-partition":
147
148    
149    $nodes = $api->GetNodes( array( $node_id ) );
150    $node = $nodes[0];
151
152    // non-admin people need to be affiliated with the right site
153    if( ! plc_is_admin() ) {
154      $node_site_id = $node['site_id'];
155      $in_site = plc_in_site($node_site_id);
156      if( ! $in_site) {
157        $error= "Insufficient permission. You cannot create configuration files for this node.";
158      }
159    }
160
161    $hostname= $node['hostname'];
162    // search for the primary interface
163    $interfaces = $api->GetInterfaces( array( "node_id" => $node_id, "is_primary"=>true ), NULL );
164    
165    $can_gen_config= 1;
166
167    if ( ! $interfaces ) {
168      $can_gen_config= 0;
169    } else {
170      $interface = $interfaces[0];
171      if ( $node['hostname'] == "" ) {
172        $can_gen_config= 0;
173        $node['hostname']= plc_warning("Missing");
174      }
175      
176      # fields to check
177      $fields= array("method","ip");
178      if ( $interface['method'] == "static" ) 
179        $fields = array_merge ( $fields, array("gateway","netmask","network","broadcast","dns1"));
180      foreach( $fields as $field ) {
181        if( $interface[$field] == "" ) {
182          $can_gen_config= 0;
183          $interface[$field]= plc_warning("Missing");
184        }
185      }
186
187      if(    $interface['method'] != "static" 
188          && $interface['method'] != "dhcp" ) {
189        $can_gen_config= 0;
190        $interface['method']= "<i>Unknown method</i>";
191      }
192    }
193
194    $download= $_POST['download'];
195    
196    if( $can_gen_config && !empty($download) ) {
197      switch ($action) {
198      case 'download-node-floppy':
199        $boot_action='node-floppy'; 
200        $location = "%d/%n-%v-rename-into-plnode%s";
201        $options = array();
202        break;
203      case 'download-node-iso':
204        $boot_action='node-iso';
205        $location = "%d/%n-%a-%v%s";
206        $options = array();
207        break;
208      case 'download-node-usb':
209        $boot_action='node-usb';
210        $location = "%d/%n-%a-%v%s";
211        $options = array();
212        break;
213      case "download-node-usb-partition":
214        $boot_action='node-usb';
215        $location = "%d/%n-%a-%v-partition%s";
216        $options = array('partition');
217        break;
218      }   
219
220      $filename=$api->GetBootMedium($node_id,$boot_action,$location,$options);
221      $error=$api->error();
222      if (empty($error) && empty($filename)) {
223        $error="Unexpected error from GetBootMedium - probably wrong directory modes";
224      }    
225      if (! empty($error)) {
226        print ("<div class='plc-error'> $error </div>\n");
227        print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
228        return ;
229      } else {
230        deliver_and_unlink ($filename);
231        exit();
232      }
233    }
234
235    drupal_set_title("Download boot medium for $hostname");
236
237    $header= <<<EOF
238 <p class='node_download'>
239 <span class='bold'>WARNING:</span> Downloading a new boot medium
240 for this node will generate <span class='bold'>a new node key</span>, and any 
241 formerly downloaded boot medium for that node will become <span class='bold'>outdated</span>.
242 <br/>
243 <br/>
244 In order to create a configuration file for this node using this page,
245 the interface settings must be up to date. Below is summary of these
246 values. 
247 </p>
248
249 EOF;
250
251    echo $header;
252
253    show_download_confirm_button($api, $node_id, $action, $can_gen_config, false);
254    print ("<hr />");
255    print ("<h3 class='node_download'>Current node configuration</h3>");
256
257    $details = new PlekitDetails (false);
258    $details->start();
259
260    if( ! $interface ) {
261      print (plc_warning("This node has no configured primary interface."));
262    } else {
263      $details->tr(l_node_t($node_id,"Node details"),"center");
264      $details->th_td("node_id",$node_id);
265      $details->th_td("Hostname",$node['hostname']);
266
267      $interface_id = $interface['interface_id'];
268      $details->tr(l_interface_t($interface_id,"Interface Details"),"center");
269      $details->th_td("Method",$interface['method']);
270      $details->th_td("IP",$interface['ip']);
271
272      if( $interface['method'] == "static" ) {
273        $details->th_td("Gateway",$interface['gateway']);
274        $details->th_td("Network mask",$interface['netmask']);
275        $details->th_td("Network address",$interface['network']);
276        $details->th_td("Broadcast address",$interface['broadcast']);
277        $details->th_td("DNS 1",$interface['dns1']);
278        if ($interface['dns2'])
279          $details->th_td("DNS 2",$interface['dns2']);
280       }
281
282      $details->tr(href(l_interface_tags($interface_id),"Interface extra settings (tags)"),"center");
283      $interface_id = $interface['interface_id'];
284      $settings=$api->GetInterfaceTags(array("interface_id" => array($interface_id)));
285      if ( ! $settings) {
286        $details->tr("no tag set","center");
287      } else foreach ($settings as $setting) {
288        $category=$setting['category'];
289        $name=$setting['tagname'];
290        $value=$setting['value'];
291        $details->th_td($category . " " . $name,$value);
292      }
293    }
294    $details->end();
295
296  show_download_confirm_button($api, $node_id, $action, $can_gen_config, true);
297  break;
298  
299  default:
300    drupal-set_error("Unkown action $action in node_downloads.php");
301    plc_redirect (l_node($node_id));
302    break;
303  }
304
305 ?>