]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - 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
CommitLineData
3ea75603 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b81c77b9 5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
70df8302
MT
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###############################################################################
3ea75603
MT
21
22use strict;
4d70f591 23use List::Util qw(any);
3ea75603
MT
24
25# enable only the following on debugging purpose
cb5e9c6c
CS
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
3ea75603 28
986e08d9 29require '/var/ipfire/general-functions.pl';
3ea75603
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
1bd42c89 32require "/opt/pakfire/lib/functions.pl";
3ea75603 33
131f163c 34my %cgiparams=();
3ea75603 35my $errormessage = '';
cb5e9c6c 36my %color = ();
131f163c 37my %pakfiresettings = ();
cb5e9c6c 38my %mainsettings = ();
3ea75603 39
db9ee62e
LAH
40# Load general settings
41&General::readhash("${General::swroot}/main/settings", \%mainsettings);
cd521e78 42&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
db9ee62e 43&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
3ea75603 44
db9ee62e 45# Get CGI request data
131f163c 46$cgiparams{'ACTION'} = '';
cd521e78 47$cgiparams{'FORCE'} = '';
3ea75603 48
131f163c
MT
49$cgiparams{'INSPAKS'} = '';
50$cgiparams{'DELPAKS'} = '';
3ea75603 51
131f163c 52&Header::getcgihash(\%cgiparams);
d40aff35 53
db9ee62e
LAH
54### Process AJAX/JSON request ###
55if($cgiparams{'ACTION'} eq 'json-getstatus') {
56 # Send HTTP headers
57 _start_json_output();
58
4d70f591
LAH
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
db9ee62e
LAH
67 my %status = (
68 'running' => &_is_pakfire_busy() || "0",
69 'running_since' => &General::age("$Pakfire::lockfile") || "0s",
4d70f591
LAH
70 'reboot' => (-e "/var/run/need_reboot") || "0",
71 'failure' => $failure || "0"
db9ee62e 72 );
db9ee62e
LAH
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
cd521e78 98### Process Pakfire install/update commands ###
3cdb8393
LAH
99if($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')) {
cd521e78
LAH
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
db9ee62e
LAH
129### Start pakfire page ###
130&Header::showhttpheaders();
131
132###--- HTML HEAD ---###
133my $extraHead = <<END
134<style>
219dacef
LAH
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
db9ee62e
LAH
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;
3ea75603 184
db9ee62e
LAH
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'}',
524bbe32 196 'finished': '$Lang::tr{'pakfire finished'}',
4d70f591
LAH
197 'finished error': '$Lang::tr{'pakfire finished error'}',
198 'since': '$Lang::tr{'since'}',
db9ee62e 199
524bbe32 200 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
db9ee62e
LAH
201 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
202 });
4d70f591
LAH
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);
db9ee62e
LAH
209</script>
210END
211;
212###--- END HTML HEAD ---###
213
214&Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
1bd42c89 215&Header::openbigbox('100%', 'left', '', $errormessage);
3ea75603 216
4b5d1f30
LAH
217# Show error message
218if ($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
225if(&_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
246END
247;
248
249 &Header::closebox();
250 &Header::closebigbox();
251 &Header::closepage();
252 exit;
253}
254
255# Show Pakfire install/remove dependencies and confirm form
d255e2d1 256if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
0f506a13
LAH
257 &Header::openbox("100%", "center", $Lang::tr{'request'});
258
75ee0279 259 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
0f506a13
LAH
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'}
fee5c6b7 263 <pre>
5b2a12ff 264END
0f506a13
LAH
265 foreach (@output) {
266 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
267 print "$_\n";
268 }
269 print <<END;
219dacef
LAH
270 </pre></td></tr>
271 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
272 <tr><td colspan='2'>&nbsp;</td></tr>
5b2a12ff 273 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
0f506a13
LAH
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>
5b2a12ff 288END
0f506a13
LAH
289 &Header::closebox();
290 &Header::closebigbox();
291 &Header::closepage();
292 exit;
cd521e78 293
d255e2d1 294} elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
0f506a13
LAH
295 &Header::openbox("100%", "center", $Lang::tr{'request'});
296
75ee0279 297 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
0f506a13
LAH
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'}
fee5c6b7 301 <pre>
74693811 302END
0f506a13
LAH
303 foreach (@output) {
304 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
305 print "$_\n";
306 }
307 print <<END;
219dacef
LAH
308 </pre></td></tr>
309 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
310 <tr><td colspan='2'>&nbsp;</td></tr>
74693811 311 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
0f506a13
LAH
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>
74693811 326END
0f506a13
LAH
327 &Header::closebox();
328 &Header::closebigbox();
329 &Header::closepage();
330 exit;
3ea75603
MT
331}
332
4b5d1f30 333# Show Pakfire main page
3ea75603
MT
334my %selected=();
335my %checked=();
336
f61be862
MT
337$selected{"TREE"} = ();
338$selected{"TREE"}{"stable"} = "";
339$selected{"TREE"}{"testing"} = "";
340$selected{"TREE"}{"unstable"} = "";
341$selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
342
91a08eac 343my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
377560fb
MT
344chomp($core_release);
345my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
346my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
347my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
348my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
349
1bd42c89
MT
350&Header::openbox("100%", "center", "Pakfire");
351
352print <<END;
219dacef 353 <table id="pfmain">
337305ef 354END
324bb888 355if ( -e "/var/run/need_reboot") {
219dacef 356 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
337305ef
JPT
357}
358print <<END;
219dacef
LAH
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>
1e908471 368 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
219dacef
LAH
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>
6666b93d 376 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
219dacef 377 <select name="UPDPAKS" class="pflist" size="5" disabled>
377560fb 378END
219dacef
LAH
379
380 &Pakfire::dblist("upgrade", "forweb");
377560fb
MT
381 print <<END;
382 </select>
377560fb 383 <input type='hidden' name='ACTION' value='upgrade' />
f8aa0679 384 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
377560fb 385 </form>
219dacef
LAH
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>
fee5c6b7 390
b81c77b9 391 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
219dacef
LAH
392 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
393 <select name="INSPAKS" class="pflist" size="10" multiple>
1bd42c89 394END
fee5c6b7 395
219dacef
LAH
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>
b81c77b9 403 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
219dacef
LAH
404 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
405 <select name="DELPAKS" class="pflist" size="10" multiple>
957363eb 406END
5b2a12ff 407
219dacef
LAH
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>
377560fb 416 </table>
4b122800
MT
417END
418
f61be862
MT
419&Header::closebox();
420&Header::openbox("100%", "center", "$Lang::tr{'settings'}");
421
422print <<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>
445END
446
3ea75603 447&Header::closebox();
3ea75603 448&Header::closebigbox();
3ea75603 449&Header::closepage();
d255e2d1
LAH
450
451###--- Internal functions ---###
452
453# Check if pakfire is already running (extend test here if necessary)
454sub _is_pakfire_busy {
4d70f591
LAH
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
d255e2d1
LAH
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
4d70f591
LAH
465 if($pakfire_pid) {
466 return 1;
467 }
468
469 # Pakfire isn't running
470 return 0;
d255e2d1 471}
db9ee62e
LAH
472
473# Send HTTP headers
474sub _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}