3 require_once 'plc_login.php'; // Require login
4 require_once 'plc_session.php'; // Get session and API handles
5 global $plc, $api, $adm;
8 require_once 'plc_drupal.php';
10 include 'plc_header.php';
13 require_once 'plc_functions.php';
14 include 'plc_objects.php';
17 class Download extends Controller {
20 function stage5_bootimage($pcu_id=0, $site_id=0, $node_id=0)
23 $this->load->helper(array('form', 'url'));
24 $this->load->library('validation');
26 $this->validation->set_error_delimiters('<span class="error">', '</span>');
28 # TODO: if result is false, redirect to beginning.
29 /*$result = $this->validation->run();*/
30 /*print "RESULT: ".$result . "<br>";*/
31 /*print $this->validation->error_string . "<br>";*/
34 $data['pcu_id'] = intval($pcu_id);
35 $data['node_id'] = intval($node_id);
36 $data['site_id'] = intval($site_id);
38 $this->load->view('header', $data);
39 $this->load->view('debug', $data);
40 $this->load->view('stage5_bootimage', $data);
41 $this->load->view('footer', $data);
44 function stage6_download($pcu_id=0, $site_id=0, $node_id=0)
47 $this->load->helper(array('form', 'url'));
48 $this->load->helper('download');
49 $this->load->library('validation');
51 $this->validation->set_error_delimiters('<span class="error">', '</span>');
52 $person = new Person($plc->person);
54 $fields['action'] = "Download Action";
55 $this->validation->set_fields($fields);
57 $rules['action'] = "required";
58 $this->validation->set_rules($rules);
60 # TODO: if result is false, redirect to beginning.
61 $result = $this->validation->run();
64 $data['pcu_id'] = intval($pcu_id);
65 $data['node_id'] = intval($node_id);
66 $data['site_id'] = intval($site_id);
67 $data['action'] = $this->validation->action;
69 $data['generic_iso_name'] = $this->get_bootcd_version();
70 $data = $this->get_stage6_data($person, $data);
71 print $this->validation->error_string . "<br>";
72 $this->load->view('header', $data);
73 $this->load->view('debug', $data);
74 $this->load->view('stage6_download', $data);
75 $this->load->view('footer', $data);
78 function getnode($node_id)
81 $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
82 return $plc_node_list[0];
85 function get_stage6_data($person, $data=NULL)
92 $data['has_primary'] = 1;
93 $node_detail = $this->getnode($data['node_id']);
94 $data['node_detail'] = $node_detail;
95 $node_id = $data['node_id'];
96 $action = $_REQUEST['action'];
99 case "download-generic-iso":
100 case "download-generic-usb":
102 if ($action=="download-generic-iso") {
103 $boot_action="generic-iso";
104 $basename=PLC_NAME."-BootCD.iso";
106 $boot_action="generic-usb";
107 $basename=PLC_NAME."-BootCD.usb";
110 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
111 "version" => $this->get_bootcd_version() ) );
112 /* exits on success */
113 $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
115 $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
116 print ("<div class='plc-error'> $error </div>\n");
117 print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
122 case "download-node-floppy-with-iso":
123 case "download-node-floppy-with-usb":
124 case "download-node-floppy":
125 case "download-node-iso":
126 case "download-node-usb":
127 case "download-node-usb-partition":
129 $return= $api->GetNodes( array( $node_id ) );
130 $node_detail= $return[0];
132 // non-admin people need to be affiliated with the right site
133 if( !in_array( 10, $person->data['role_ids'] ) ) {
134 $node_site_id = $node_detail['site_id'];
135 $in_site = in_array ($node_site_id,$person->data['site_ids']);
137 $error= "Insufficient permission. You cannot create configuration files for this node.";
141 $hostname= $node_detail['hostname'];
142 $return= $api->GetInterfaces( array( "node_id" => $node_id ), NULL );
145 $data['has_primary']= 0;
147 if( count($return) > 0 ) {
148 foreach( $return as $node_network_detail ) {
149 if( $node_network_detail['is_primary'] == true ) {
150 $data['has_primary']= 1;
154 $data['node_network_detail'] = $node_network_detail;
157 if( !$data['has_primary'] ) {
160 if( $node_detail['hostname'] == "" ) {
162 $node_detail['hostname']= "<i>Missing</i>";
165 $fields= array("method","ip");
166 foreach( $fields as $field ) {
167 if( $node_network_detail[$field] == "" ) {
169 $node_network_detail[$field]= "<i>Missing</i>";
173 if( $node_network_detail['method'] == "static" ) {
174 $fields= array("gateway","netmask","network","broadcast","dns1");
175 foreach( $fields as $field ) {
176 if( $node_network_detail[$field] == "" ) {
178 $node_network_detail[$field]= "<i>Missing</i>";
183 if( $node_network_detail['method'] != "static" && $node_network_detail['method'] != "dhcp" ) {
185 $node_network_detail['method']= "<i>Unknown method</i>";
189 if( $can_gen_config && isset($_REQUEST['download']) ) {
190 $download = $_REQUEST['download'];
191 if (method_exists($api, "GetBootMedium"))
192 $file_contents= $api->GetBootMedium($node_id, "node-floppy", "");
194 $file_contents= $api->GenerateNodeConfFile($node_id, true);
197 case 'download-node-floppy-with-iso':
198 case 'download-node-floppy-with-usb':
199 case 'download-node-floppy':
200 $boot_action='node-floppy';
201 $basename = "plnode.txt";
203 case 'download-node-iso':
204 $boot_action='node-iso';
205 $basename = "$hostname.iso";
207 case 'download-node-usb':
208 $boot_action='node-usb';
209 $basename = "$hostname.usb";
211 case "download-node-usb-partition":
212 $boot_action='node-usb-partition';
213 $basename = "$hostname-partition.usb";
216 if ($action != 'download-node-floppy')
218 $api->UpdateNode( $hostname, array( "boot_state" => 'disabled',
219 "version" => $this->get_bootcd_version() ) );
221 /* exits on success */
222 $success = $this->deliver_bootmedium($node_id, $boot_action, $basename);
224 $error="Unexpected error from deliver_bootmedium - probably wrong directory modes";
225 print ("<div class='plc-error'> $error </div>\n");
226 print ("<p><a href='/db/nodes/index.php?id=$node_id'>Back to node </a>\n");
230 $action_labels = array (
231 'download-node-floppy' => 'textual node config (for floppy)' ,
232 'download-node-floppy-with-iso' => 'textual node config (for floppy)' ,
233 'download-node-floppy-with-usb' => 'textual node config (for floppy)' ,
234 'download-node-iso' => 'ISO image',
235 'download-node-usb-partition' => 'USB image',
236 'download-node-usb' => 'USB image' );
238 $format = $action_labels [ $action ] ;
239 /*drupal_set_title("Download boot material for $hostname");*/
243 echo "Unknown action $action.";
247 $data['format'] = $format;
248 $data['can_gen_config'] = $can_gen_config;
251 function get_bootcd_version()
253 $BOOTCD="/usr/share/bootcd";
254 $BOOTCDVERSION="$BOOTCD/build/version.txt";
255 $version = trim(file_get_contents($BOOTCDVERSION));
256 $bootcd_version = PLC_NAME . "-BootCD-".$version;
257 return $bootcd_version;
260 function deliver_bootmedium($node_id, $action, $filename)
263 ini_set("memory_limit","300M");
266 case "node-usb-partition":
267 $action = "node-usb";
268 $options[] = "partition";
271 $b64_data = $api->GetBootMedium($node_id,$action,"", $options);
272 $error= $api->error();
275 if ( $action == "node-floppy" )
277 # data comes back in plain text for node-floppy.
280 $data = base64_decode($b64_data);
282 $size = strlen($data);
283 /* exits on success */
284 force_download($filename, $data);