narcissus: point to new source location
[angstrom:dr777s-narcissus.git] / backend.php
1 <?
2 /* Narcissus - Online image builder for the angstrom distribution
3  * Koen Kooi (c) 2008-2010 - all rights reserved 
4  *
5  */
6
7
8 $base_pkg_set = " task-base angstrom-version ";
9
10 if (isset($_POST["action"]) && $_POST["action"] != "") {
11         $action = $_POST["action"];
12 } else {
13         print "Invalid action: $action";
14         exit;
15 }
16
17 if (isset($_POST["machine"])) {
18         $machine = escapeshellcmd(basename($_POST["machine"]));
19 } else {
20         print "Invalid machine";
21         exit;
22 }
23
24 if (isset($_POST["name"]) && $_POST["name"] != "") {
25         $name = escapeshellcmd(basename($_POST["name"]));
26 } else {
27         $name = "unnamed";
28 }
29
30 if (isset($_POST["pkgs"]) && $_POST["pkgs"] != "") {
31         $pkg = $_POST["pkgs"];
32 } else {
33         $pkg = "task-boot";
34 }
35
36 if (isset($_POST["release"]) && $_POST["release"] != "") {
37         $release = $_POST["release"];
38 } else {
39         $release = "stable";
40 }
41
42 if (isset($_POST["imagetype"]) && $_POST["imagetype"] != "") {
43         $imagetype = $_POST["imagetype"];
44 } else {
45         $imagetype = "tgz";
46         $imagesuffix = "tar.gz";
47 }
48
49 if (isset($_POST["manifest"]) && $_POST["manifest"] != "") {
50         $manifest = $_POST["manifest"];
51 } else {
52         $manifest = "no";
53 }
54
55 if (isset($_POST["sdk"]) && $_POST["sdk"] != "") {
56         $sdk = $_POST["sdk"];
57 } else {
58         $sdk = "no";
59 }
60
61 if (isset($_POST["sdkarch"]) && $_POST["sdkarch"] != "") {
62         $sdkarch = $_POST["sdkarch"];
63 } else {
64         $sdkarch = "none";
65 }
66
67 switch($imagetype) {
68         case "tgz":
69                 $imagesuffix = "tar.gz";
70                 break;
71         case "sdimg":
72                 $imagesuffix = "tar.gz";
73                 break;
74         case "ubifs":
75                 $imagesuffix = "ubi";
76                 break;
77         default:
78                 $imagesuffix = $imagetype;
79 }
80
81 switch($action) {
82         case "assemble_image":
83                 print "assembling\n";
84                 assemble_image($machine, $name, $imagetype, $manifest, $sdk, $sdkarch);
85                 break;
86         case "configure_image":
87                 print "configuring\n";
88                 configure_image($machine, $name, $release);
89                 break;
90         case "show_image_link":
91                 show_image_link($machine, $name, $imagesuffix, $manifest, $sdk, $sdkarch);
92                 break;
93         case "install_package":
94                 print "installing $pkg\n";
95                 install_package($machine, $name, $pkg);
96                 break;
97 }
98
99 function show_image_link($machine, $name, $imagesuffix, $manifest, $sdk, $sdkarch) {
100         $foundimage = 0;
101         $foundsdimage = 0;
102         $foundsdk = 0;
103         $foundsources = 0;
104         $printedcacheinfo = 0;
105         $printstring = "";
106
107         switch($sdkarch) {
108                         case "intel32":
109                                 $sdkarchname = "i686";
110                                 break;
111                         case "intel64":
112                                 $sdkarchname = "x86_64";
113                                 break;
114                         default:
115                                 $sdkarchname = "i686";
116                                 break;
117         }
118         
119         $randomname = substr(md5(time()), 0, 6);
120         $deploydir = "deploy/$machine/$randomname";
121         mkdir($deploydir,0777,TRUE);
122         
123         $imagefiles = scandir("work/$machine");
124         print "<br/>";
125         foreach($imagefiles as $value) {
126                 $location = "work/$machine/$value";
127                 // The !== operator must be used.  Using != would not work as expected
128                 // because the position of 'a' is 0. The statement (0 != false) evaluates 
129                 // to false.
130                 if(strpos($value, "$name-image-$machine-sd") !== false) {
131                         rename($location, "$deploydir/$value");
132                         $imgsize = round(filesize("$deploydir/$value") / (1024 * 1024),2);
133                         $printstring .= "<a href='$deploydir/$value'>$value</a> [$imgsize MiB]<br/> "; 
134                         $foundsdimage = 1;
135                         continue;
136                 }
137                 if(strpos($value, "$name-image-$machine.$imagesuffix") !== false) {
138                         rename($location, "$deploydir/$value");
139                         $imgsize = round(filesize("$deploydir/$value") / (1024 * 1024),2);
140                         $imagestring .= "<br/><a href='$deploydir/$value'>$value</a> [$imgsize MiB]: This is the rootfs '$name' for $machine you just built. This will get automatically deleted after 2 days.<br/>";
141
142                         rename("work/$machine/$name-image.bb", "$deploydir/$name-image.bb");
143                         rename("work/$machine/$name-image.txt", "$deploydir/$name-image.txt");
144
145                         if($manifest == "yes") {
146                                 rename("work/$machine/$name-image-manifest.html", "$deploydir/$name-image-manifest.html");
147                                 $imagestring .= "You can also have a look at the software <a href='$deploydir/$name-image-manifest.html' target='manifest'>manifest</a> for this rootfs<br/>";
148                         }
149                         $foundimage = 1;
150                 }
151                 //Angstrom-2010.05-narcissus-hawkboard-i686-random-d4ddcec6-image-toolchain.tar.gz
152                 if(strpos($value, "narcissus-$machine-$sdkarchname-$name-image-$sdk") !== false) {
153                         rename($location, "$deploydir/$value");
154                         $sdksize = round(filesize("$deploydir/$value") / (1024 * 1024),2);
155                         $imagestring .= "<br/><a href='$deploydir/$value'>$value</a> [$sdksize MiB]: $sdk for the generated rootfs.<br/>";
156                         $foundsdk = 1;
157                 }
158                 //random-ed560fe9-image-sources/
159                 if(strpos($value, "$name-image-sources") !== false) {
160                         rename($location, "$deploydir/sources");
161                         $foundsources = 1;
162                 }
163 }       
164         
165         if ($foundimage == 0) {
166                 print "Image not found, something went wrong :/";
167         } else {
168                 print("$imagestring");
169         }
170         
171         if($foundsdimage == 1) {
172                 print(" <br/><br/> The raw SD card image(s) below have a vfat partition populated with the bootloader and kernel, but an <b>empty</b> ext3 partition. You can extract the tarball to that partition to make it ready to boot.<br>The intended size for the SD card is encoded in the file name, e.g. 1GiB for a one gigabyte card.<br/><br/> $printstring");
173         }
174         
175 }
176
177 function configure_image($machine, $name, $release) {
178         print "Machine: $machine, name: $name\n";
179         passthru ("scripts/configure-image.sh $machine $name-image $release && exit");
180 }
181
182 function install_package($machine, $name, $pkg) {
183         print "Machine: $machine, name: $name, pkg: $pkg\n";
184         passthru ("scripts/install-package.sh $machine $name-image $pkg && exit", $installretval);
185         print "<div id=\"retval\">$installretval</div>";
186 }
187
188 function assemble_image($machine, $name, $imagetype, $manifest, $sdk, $sdkarch) {
189         print "Machine: $machine, name: $name, type: $imagetype\n";
190         passthru ("scripts/assemble-image.sh $machine $name-image $imagetype $manifest $sdk $sdkarch && exit", $installretval);
191         print "<div id=\"retval-image\">$installretval</div>";
192 }
193
194
195
196 ?>