]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
pakfire.cgi: Notify user if Pakfire is already performing a task
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / pakfire.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 use List::Util qw(any);
24
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32 require "/opt/pakfire/lib/functions.pl";
33
34 my %cgiparams=();
35 my $errormessage = '';
36 my %color = ();
37 my %pakfiresettings = ();
38 my %mainsettings = ();
39
40 # Load general settings
41 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
42 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
43 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
44
45 # Get CGI request data
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'FORCE'} = '';
48
49 $cgiparams{'INSPAKS'} = '';
50 $cgiparams{'DELPAKS'} = '';
51
52 &Header::getcgihash(\%cgiparams);
53
54 ### Process AJAX/JSON request ###
55 if($cgiparams{'ACTION'} eq 'json-getstatus') {
56 # Send HTTP headers
57 _start_json_output();
58
59 # Read /var/log/messages backwards until a "Pakfire started" header is found,
60 # to capture all messages of the last (i.e. current) Pakfire run
61 my @messages = `tac /var/log/messages | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
62
63 # Test if the log contains an error message (fastest implementation, stops at first match)
64 my $failure = any{ index($_, 'ERROR') != -1 } @messages;
65
66 # Collect Pakfire status
67 my %status = (
68 'running' => &_is_pakfire_busy() || "0",
69 'running_since' => &General::age("$Pakfire::lockfile") || "0s",
70 'reboot' => (-e "/var/run/need_reboot") || "0",
71 'failure' => $failure || "0"
72 );
73
74 # Start JSON file
75 print "{\n";
76
77 foreach my $key (keys %status) {
78 my $value = $status{$key};
79 print qq{\t"$key": "$value",\n};
80 }
81
82 # Print sanitized messages in reverse order to undo previous "tac"
83 print qq{\t"messages": [\n};
84 for my $index (reverse (0 .. $#messages)) {
85 my $line = $messages[$index];
86 $line =~ s/[[:cntrl:]<>&\\]+//g;
87
88 print qq{\t\t"$line"};
89 print ",\n" unless $index < 1;
90 }
91 print "\n\t]\n";
92
93 # Finalize JSON file & stop
94 print "}";
95 exit;
96 }
97
98 ### Process Pakfire install/update commands ###
99 if($cgiparams{'ACTION'} ne '') {
100 if(&_is_pakfire_busy()) {
101 $errormessage = $Lang::tr{'pakfire already busy'};
102 } elsif(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
103 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
104 &General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
105 } elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
106 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
107 &General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
108 } elsif($cgiparams{'ACTION'} eq 'update') {
109 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
110 } elsif($cgiparams{'ACTION'} eq 'upgrade') {
111 &General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
112 } elsif($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
113 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
114
115 # Check for valid input
116 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
117 $errormessage .= $Lang::tr{'pakfire invalid tree'};
118 }
119
120 unless ($errormessage) {
121 &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
122
123 # Update lists
124 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
125 }
126 }
127 }
128
129 ### Start pakfire page ###
130 &Header::showhttpheaders();
131
132 ###--- HTML HEAD ---###
133 my $extraHead = <<END
134 <style>
135 /* Main screen */
136 table#pfmain {
137 width: 100%;
138 border-style: hidden;
139 table-layout: fixed;
140 }
141
142 #pfmain td {
143 padding: 5px 20px 0;
144 text-align: center;
145 }
146 #pfmain tr:not(:last-child) > td {
147 padding-bottom: 1.5em;
148 }
149 #pfmain tr > td.heading {
150 padding: 0;
151 font-weight: bold;
152 background-color: $color{'color20'};
153 }
154
155 .pflist {
156 width: 100%;
157 text-align: left;
158 margin-bottom: 0.8em;
159 }
160
161 /* Pakfire log viewer */
162 section#pflog-header {
163 width: 100%;
164 display: flex;
165 text-align: left;
166 align-items: center;
167 column-gap: 20px;
168 }
169 #pflog-header > div:last-child {
170 margin-left: auto;
171 margin-right: 20px;
172 }
173 #pflog-header span {
174 line-height: 1.3em;
175 }
176 #pflog-header span:empty::before {
177 content: "\\200b"; /* zero width space */
178 }
179
180 pre#pflog-messages {
181 margin-top: 0.7em;
182 padding-top: 0.7em;
183 border-top: 0.5px solid $Header::bordercolour;
184
185 text-align: left;
186 min-height: 15em;
187 overflow-x: auto;
188 }
189 </style>
190
191 <script src="/include/pakfire.js"></script>
192 <script>
193 // Translations
194 pakfire.i18n.load({
195 'working': '$Lang::tr{'pakfire working'}',
196 'finished': '$Lang::tr{'pakfire finished'}',
197 'finished error': '$Lang::tr{'pakfire finished error'}',
198 'since': '$Lang::tr{'since'}',
199
200 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
201 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
202 });
203
204 // AJAX auto refresh interval (in ms, default: 1000)
205 //pakfire.refreshInterval = 1000;
206
207 // Enable returning to main screen (delay in ms)
208 pakfire.setupPageReload(true, 3000);
209 </script>
210 END
211 ;
212 ###--- END HTML HEAD ---###
213
214 &Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
215 &Header::openbigbox('100%', 'left', '', $errormessage);
216
217 # Show error message
218 if ($errormessage) {
219 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
220 print "<font class='base'>$errormessage&nbsp;</font>\n";
221 &Header::closebox();
222 }
223
224 # Show log output while Pakfire is running
225 if(&_is_pakfire_busy()) {
226 &Header::openbox("100%", "center", "Pakfire");
227
228 print <<END
229 <section id="pflog-header">
230 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
231 <div>
232 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
233 <span id="pflog-time"></span><br>
234 <span id="pflog-action"></span>
235 </div>
236 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
237 </section>
238
239 <!-- Pakfire log messages -->
240 <pre id="pflog-messages"></pre>
241 <script>
242 // Start automatic log refresh
243 pakfire.running = true;
244 </script>
245
246 END
247 ;
248
249 &Header::closebox();
250 &Header::closebigbox();
251 &Header::closepage();
252 exit;
253 }
254
255 # Show Pakfire install/remove dependencies and confirm form
256 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
257 &Header::openbox("100%", "center", $Lang::tr{'request'});
258
259 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
260 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
261 print <<END;
262 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
263 <pre>
264 END
265 foreach (@output) {
266 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
267 print "$_\n";
268 }
269 print <<END;
270 </pre></td></tr>
271 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
272 <tr><td colspan='2'>&nbsp;</td></tr>
273 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
274 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
275 <input type='hidden' name='FORCE' value='on' />
276 <input type='hidden' name='ACTION' value='install' />
277 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
278 </form>
279 </td>
280 <td align='left'>
281 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
282 <input type='hidden' name='ACTION' value='' />
283 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
284 </form>
285 </td>
286 </tr>
287 </table>
288 END
289 &Header::closebox();
290 &Header::closebigbox();
291 &Header::closepage();
292 exit;
293
294 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
295 &Header::openbox("100%", "center", $Lang::tr{'request'});
296
297 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
298 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
299 print <<END;
300 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
301 <pre>
302 END
303 foreach (@output) {
304 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
305 print "$_\n";
306 }
307 print <<END;
308 </pre></td></tr>
309 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
310 <tr><td colspan='2'>&nbsp;</td></tr>
311 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
312 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
313 <input type='hidden' name='FORCE' value='on' />
314 <input type='hidden' name='ACTION' value='remove' />
315 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
316 </form>
317 </td>
318 <td align='left'>
319 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
320 <input type='hidden' name='ACTION' value='' />
321 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
322 </form>
323 </td>
324 </tr>
325 </table>
326 END
327 &Header::closebox();
328 &Header::closebigbox();
329 &Header::closepage();
330 exit;
331 }
332
333 # Show Pakfire main page
334 my %selected=();
335 my %checked=();
336
337 $selected{"TREE"} = ();
338 $selected{"TREE"}{"stable"} = "";
339 $selected{"TREE"}{"testing"} = "";
340 $selected{"TREE"}{"unstable"} = "";
341 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
342
343 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
344 chomp($core_release);
345 my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
346 my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
347 my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
348 my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
349
350 &Header::openbox("100%", "center", "Pakfire");
351
352 print <<END;
353 <table id="pfmain">
354 END
355 if ( -e "/var/run/need_reboot") {
356 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
357 }
358 print <<END;
359 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
360 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
361
362 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
363 <hr>
364 <div class="pflist">
365 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
366 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
367 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
368 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
369 </div>
370 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
371 <input type='hidden' name='ACTION' value='update' />
372 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
373 </form>
374 </td>
375 <td>
376 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
377 <select name="UPDPAKS" class="pflist" size="5" disabled>
378 END
379
380 &Pakfire::dblist("upgrade", "forweb");
381 print <<END;
382 </select>
383 <input type='hidden' name='ACTION' value='upgrade' />
384 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
385 </form>
386 </td>
387 </tr>
388 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
389 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
390
391 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
392 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
393 <select name="INSPAKS" class="pflist" size="10" multiple>
394 END
395
396 &Pakfire::dblist("notinstalled", "forweb");
397 print <<END;
398 </select>
399 <input type='hidden' name='ACTION' value='install' />
400 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
401 </form>
402 </td>
403 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
404 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
405 <select name="DELPAKS" class="pflist" size="10" multiple>
406 END
407
408 &Pakfire::dblist("installed", "forweb");
409 print <<END;
410 </select>
411 <input type='hidden' name='ACTION' value='remove' />
412 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
413 </form>
414 </td>
415 </tr>
416 </table>
417 END
418
419 &Header::closebox();
420 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
421
422 print <<END;
423 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
424 <table width='95%'>
425 <tr>
426 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
427 <td width="55%" align="left">
428 <select name="TREE">
429 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
430 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
431 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
432 </select>
433 </td>
434 </tr>
435 <tr>
436 <td colspan="2">&nbsp;</td>
437 </tr>
438 <tr>
439 <td colspan="2" align="center">
440 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
441 </td>
442 </tr>
443 </table>
444 </form>
445 END
446
447 &Header::closebox();
448 &Header::closebigbox();
449 &Header::closepage();
450
451 ###--- Internal functions ---###
452
453 # Check if pakfire is already running (extend test here if necessary)
454 sub _is_pakfire_busy {
455 # Return immediately if lockfile is present
456 if(-e "$Pakfire::lockfile") {
457 return 1;
458 }
459
460 # Check if a PID of a running pakfire instance is found
461 # (The system backpipe command is safe, because no user input is computed.)
462 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
463 chomp($pakfire_pid);
464
465 if($pakfire_pid) {
466 return 1;
467 }
468
469 # Pakfire isn't running
470 return 0;
471 }
472
473 # Send HTTP headers
474 sub _start_json_output {
475 print "Cache-Control: no-cache, no-store\n";
476 print "Content-Type: application/json\n";
477 print "\n"; # End of HTTP headers
478 }