use plc_config.php values to pick default bootcd, otherwise choose last
[www-register-wizard.git] / application / controllers / download.php
1 <?php
2
3 require_once 'plc_login.php'; // Require login
4 require_once 'plc_session.php'; // Get session and API handles
5 global $plc, $api, $adm;
6
7 // Print header
8 require_once 'plc_drupal.php';
9
10 include 'plc_header.php';
11
12 // Common functions
13 require_once 'plc_functions.php';
14 require_once 'plc_config.php';
15 include 'plc_objects.php';
16
17
18 class Download extends Controller {
19         
20         var $pcu_id = 0;
21         function stage5_bootimage($pcu_id=0, $site_id=0, $node_id=0)
22         {
23                 global $api, $plc;
24                 $this->load->helper(array('form', 'url'));
25                 $this->load->library('validation');
26                 
27                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
28
29                 # TODO: if result is false, redirect to beginning.
30                 /*$result = $this->validation->run();*/
31                 /*print "RESULT: ".$result . "<br>";*/
32                 /*print $this->validation->error_string . "<br>";*/
33
34                 $data = array();
35                 $data['pcu_id'] = intval($pcu_id);
36                 $data['node_id'] = intval($node_id);
37                 $data['site_id'] = intval($site_id);
38                 $data['stage'] = 5;
39                 $this->load->view('header', $data);
40                 $this->load->view('debug', $data);
41                 $this->load->view('stage5_bootimage', $data);
42                 $this->load->view('footer', $data);
43         }
44
45         function stage6_download($pcu_id=0, $site_id=0, $node_id=0)
46         {
47                 global $api, $plc;
48                 $this->load->helper(array('form', 'url'));
49                 $this->load->helper('download');
50                 $this->load->library('validation');
51                 
52                 $this->validation->set_error_delimiters('<span class="error">', '</span>');
53                 $person = new Person($plc->person);
54
55                 $fields['action']  = "Download Action";
56                 $this->validation->set_fields($fields);
57
58                 $rules['action']        = "required";
59                 $this->validation->set_rules($rules);
60
61                 # TODO: if result is false, redirect to beginning.
62                 $result = $this->validation->run();
63
64                 $data = array();
65                 $data['pcu_id'] = intval($pcu_id);
66                 $data['node_id'] = intval($node_id);
67                 $data['site_id'] = intval($site_id);
68                 $data['action'] = $this->validation->action;
69                 $data['stage'] = 6;
70                 $data['generic_iso_name'] = $this->get_bootcd_version();
71                 $data = $this->get_stage6_data($person, $data);
72                 print $this->validation->error_string . "<br>";
73                 $this->load->view('header', $data);
74                 $this->load->view('debug', $data);
75                 $this->load->view('stage6_download', $data);
76                 $this->load->view('footer', $data);
77         }
78
79         function getnode($node_id)
80         {
81                 global $api, $plc;
82                 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
83                 return $plc_node_list[0];
84         }
85
86         function get_stage6_data($person, $data=NULL)
87         {
88                 global $api, $plc;
89                 if ( $data == NULL ){
90                     $data = array();
91                 }
92                 $data['api'] = $api;
93                 $data['has_primary'] = 1;
94                 $node_detail = $this->getnode($data['node_id']);
95                 $data['node_detail'] = $node_detail;
96                 $node_id = $data['node_id'];
97                 $action = $_REQUEST['action'];
98                 switch ($action) {
99
100                         case "download-generic-iso":
101                         case "download-generic-usb":
102                    
103                                 if ($action=="download-generic-iso") {
104                                         $boot_action="generic-iso";
105                                         $basename=PLC_NAME."-BootCD.iso";
106                                 } else {
107                                         $boot_action="generic-usb";
108                                         $basename=PLC_NAME."-BootCD.usb";
109                                 }
110
111                                 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
112                                                                                                         "version" => $this->get_bootcd_version() ) );
113                                 /* exits on success */
114                                 $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
115                                 if (! $success ) {
116                                         $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
117                                         print ("<div class='plc-error'> $error </div>\n");
118                                         print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
119                                         return ;
120                                 }
121                                 break;
122
123                         case "download-node-floppy-with-iso":
124                         case "download-node-floppy-with-usb":
125                         case "download-node-floppy":
126                         case "download-node-iso":
127                         case "download-node-usb":
128                         case "download-node-usb-partition":
129                    
130                                 $return= $api->GetNodes( array( $node_id ) );
131                                 $node_detail= $return[0];
132
133                                 // non-admin people need to be affiliated with the right site
134                                 if( !in_array( 10, $person->data['role_ids'] ) ) {
135                                         $node_site_id = $node_detail['site_id'];
136                                         $in_site = in_array ($node_site_id,$person->data['site_ids']);
137                                         if( ! $in_site) {
138                                                 $error= "Insufficient permission. You cannot create configuration files for this node.";
139                                         }
140                                 }
141
142                                 $hostname= $node_detail['hostname'];
143                                 $return= $api->GetInterfaces( array( "node_id" => $node_id ), NULL );
144                    
145                                 $can_gen_config= 1;
146                                 $data['has_primary']= 0;
147                    
148                                 if( count($return) > 0 ) {
149                                         foreach( $return as $node_network_detail ) {
150                                                 if( $node_network_detail['is_primary'] == true ) {
151                                                         $data['has_primary']= 1;
152                                                         break;
153                                                 }
154                                         }
155                                         $data['node_network_detail'] = $node_network_detail;
156                                 }
157
158                                 if( !$data['has_primary'] ) {
159                                         $can_gen_config= 0;
160                                 } else {
161                                         if( $node_detail['hostname'] == "" ) {
162                                                 $can_gen_config= 0;
163                                                 $node_detail['hostname']= "<i>Missing</i>";
164                                         }
165                          
166                                         $fields= array("method","ip");
167                                         foreach( $fields as $field ) {
168                                                 if( $node_network_detail[$field] == "" ) {
169                                                         $can_gen_config= 0;
170                                                         $node_network_detail[$field]= "<i>Missing</i>";
171                                                 }
172                                         }
173
174                                         if( $node_network_detail['method'] == "static" ) {
175                                                 $fields= array("gateway","netmask","network","broadcast","dns1");
176                                                 foreach( $fields as $field ) {
177                                                         if( $node_network_detail[$field] == "" ) {
178                                                                 $can_gen_config= 0;
179                                                                 $node_network_detail[$field]= "<i>Missing</i>";
180                                                         }
181                                                 }
182                                         }
183
184                                         if( $node_network_detail['method'] != "static" && $node_network_detail['method'] != "dhcp" ) {
185                                                 $can_gen_config= 0;
186                                                 $node_network_detail['method']= "<i>Unknown method</i>";
187                                         }
188                                 }
189
190                                 if( $can_gen_config && isset($_REQUEST['download']) ) {
191                                         $download = $_REQUEST['download'];
192                                         if (method_exists($api, "GetBootMedium"))
193                                                 $file_contents= $api->GetBootMedium($node_id, "node-floppy", "");
194                                         else
195                                                 $file_contents= $api->GenerateNodeConfFile($node_id, true);
196                 
197                                         switch ($action) {
198                                                 case 'download-node-floppy-with-iso':
199                                                 case 'download-node-floppy-with-usb':
200                                                 case 'download-node-floppy':
201                                                         $boot_action='node-floppy';
202                                                         $basename = "plnode.txt";
203                                                         break;
204                                                 case 'download-node-iso':
205                                                         $boot_action='node-iso';
206                                                         $basename = "$hostname.iso";
207                                                         break;
208                                                 case 'download-node-usb':
209                                                         $boot_action='node-usb';
210                                                         $basename = "$hostname.usb";
211                                                         break;
212                                                 case "download-node-usb-partition":
213                                                         $boot_action='node-usb-partition';
214                                                         $basename = "$hostname-partition.usb";
215                                                         break;
216                                         }        
217                                         if ($action != 'download-node-floppy')
218                                         {
219                                                 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
220                                                                                   "version" => $this->get_bootcd_version() ) );
221                                         }
222                                         /* exits on success */
223                                         $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
224                                         if (! $success ) {
225                                                 $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
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                                         }
230                                 }
231                                 $action_labels = array (
232                                     'download-node-floppy' => 'textual node config (for floppy)' ,
233                                     'download-node-floppy-with-iso' => 'textual node config (for floppy)' ,
234                                     'download-node-floppy-with-usb' => 'textual node config (for floppy)' ,
235                                         'download-node-iso' => 'ISO image',
236                                         'download-node-usb-partition' => 'USB image',
237                                         'download-node-usb' => 'USB image' );
238
239                                 $format = $action_labels [ $action ] ;
240                                 /*drupal_set_title("Download boot material for $hostname");*/
241                         break;
242                  
243                         default:
244                                 echo "Unknown action $action.";
245                                 exit();
246                         break;
247                 }
248                 $data['format'] = $format;
249                 $data['can_gen_config'] = $can_gen_config;
250                 return $data;
251         }
252         function get_bootcd_version()
253         {
254                 // pick default bootcd flavors. need more flexibility
255                 // TODO: find a way to handle 32/64 bit.
256                 if ( defined("PLC_FLAVOUR_NODE_PLDISTRO") )
257                 {
258                         // This approach only works for 5.0
259                         $BOOTCD = "/usr/share/bootcd-" . PLC_FLAVOUR_NODE_PLDISTRO . "-" . PLC_FLAVOUR_NODE_FCDISTRO . "-" . PLC_FLAVOUR_NODE_ARCH;
260                 } else {
261                         $BOOTCD = exec ("ls -d /usr/share/bootcd*");
262                 }
263
264                 $BOOTCDVERSION="$BOOTCD/build/version.txt";
265                 $version = trim(file_get_contents($BOOTCDVERSION));
266                 $bootcd_version = PLC_NAME . "-BootCD-".$version;
267                 return $bootcd_version;
268         }
269
270         function deliver_bootmedium($node_id, $action, $filename)
271         {
272                 global $api;
273                 ini_set("memory_limit","450M");
274                 $options = array();
275                 switch ($action) {
276                         case "node-usb-partition":
277                                 $action = "node-usb";
278                                 $options[] = "partition";
279                                 break;
280                 }        
281                 $b64_data = $api->GetBootMedium($node_id,$action,"", $options);
282                 $error= $api->error();
283                 if ( empty($error) )
284                 {
285                         if ( $action == "node-floppy" )
286                         {
287                                 # data comes back in plain text for node-floppy.
288                                 $data = $b64_data;
289                         } else {
290                                 $data = base64_decode($b64_data);
291                         }
292                         $size = strlen($data);
293                         /* exits on success */
294                         force_download($filename, $data);
295                 }
296                 return False;
297         }
298
299 }
300 ?>