]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/pakfire.cgi
pakfire.cgi: Remove "sleep" after running Pakfire command
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / pakfire.cgi
index 143f123b7f71853905175421fe21c55cb73ea96f..e14658ffb2bb394c183d35abee07c3eb2f5e94de 100644 (file)
@@ -30,43 +30,130 @@ require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 require "/opt/pakfire/lib/functions.pl";
 
-my %pakfiresettings=();
+my %cgiparams=();
 my $errormessage = '';
 my %color = ();
+my %pakfiresettings = ();
 my %mainsettings = ();
 
-&Header::showhttpheaders();
+# Load general settings
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
 
-$pakfiresettings{'ACTION'} = '';
-$pakfiresettings{'VALID'} = '';
+# Get CGI request data
+$cgiparams{'ACTION'} = '';
+$cgiparams{'VALID'} = '';
 
-$pakfiresettings{'INSPAKS'} = '';
-$pakfiresettings{'DELPAKS'} = '';
-$pakfiresettings{'AUTOUPDATE'} = 'off';
-$pakfiresettings{'HEALTHCHECK'} = 'on';
-$pakfiresettings{'UUID'} = 'on';
+$cgiparams{'INSPAKS'} = '';
+$cgiparams{'DELPAKS'} = '';
 
-sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();}
+&Header::getcgihash(\%cgiparams);
 
-&Header::getcgihash(\%pakfiresettings);
+### Process AJAX/JSON request ###
+if($cgiparams{'ACTION'} eq 'json-getstatus') {
+       # Send HTTP headers
+       _start_json_output();
 
-&General::readhash("${General::swroot}/main/settings", \%mainsettings);
-&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+       # Collect Pakfire status and log messages
+       my %status = (
+               'running' => &_is_pakfire_busy() || "0",
+               'running_since' => &General::age("$Pakfire::lockfile") || "0s",
+               'reboot' => (-e "/var/run/need_reboot") || "0"
+       );
+       my @messages = `tac /var/log/messages | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
+
+       # Start JSON file
+       print "{\n";
+
+       foreach my $key (keys %status) {
+               my $value = $status{$key};
+               print qq{\t"$key": "$value",\n};
+       }
+
+       # Print sanitized messages in reverse order to undo previous "tac"
+       print qq{\t"messages": [\n};
+       for my $index (reverse (0 .. $#messages)) {
+               my $line = $messages[$index];
+               $line =~ s/[[:cntrl:]<>&\\]+//g;
+
+               print qq{\t\t"$line"};
+               print ",\n" unless $index < 1;
+       }
+       print "\n\t]\n";
+
+       # Finalize JSON file & stop
+       print "}";
+       exit;
+}
 
-&Header::openpage($Lang::tr{'pakfire configuration'}, 1);
+### Start pakfire page ###
+&Header::showhttpheaders();
+
+###--- HTML HEAD ---###
+my $extraHead = <<END
+<style>
+       /* Pakfire log viewer */
+       section#pflog-header {
+               width: 100%;
+               display: flex;
+               text-align: left;
+               align-items: center;
+               column-gap: 20px;
+       }
+       #pflog-header > div:last-child {
+               margin-left: auto;
+               margin-right: 20px;
+       }
+       #pflog-header span {
+               line-height: 1.3em;
+       }
+       #pflog-header span:empty::before {
+               content: "\\200b"; /* zero width space */
+       }
+
+       pre#pflog-messages {
+               margin-top: 0.7em;
+               padding-top: 0.7em;
+               border-top: 0.5px solid $Header::bordercolour;
+
+               text-align: left;
+               min-height: 15em;
+               overflow-x: auto;
+       }
+</style>
+
+<script src="/include/pakfire.js"></script>
+<script>
+       // Translations
+       pakfire.i18n.load({
+               'working': '$Lang::tr{'pakfire working'}',
+               'finished': '$Lang::tr{'pakfire finished'}',
+               'since': '$Lang::tr{'since'} ', //(space is intentional)
+
+               'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
+               'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
+       });
+       
+       // AJAX auto refresh interval
+       pakfire.refreshInterval = 1000;
+</script>
+END
+;
+###--- END HTML HEAD ---###
+
+&Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
 &Header::openbigbox('100%', 'left', '', $errormessage);
 
-if ($pakfiresettings{'ACTION'} eq 'install'){
-       $pakfiresettings{'INSPAKS'} =~ s/\|/\ /g;
-       if ("$pakfiresettings{'FORCE'}" eq "on") {
-               my $command = "/usr/local/bin/pakfire install --non-interactive --no-colors $pakfiresettings{'INSPAKS'} &>/dev/null &";
-               system("$command");
-               system("/bin/sleep 1");
+# Process Pakfire commands
+if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
+       my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
+       if ("$cgiparams{'FORCE'}" eq "on") {
+               &General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
        } else {
                &Header::openbox("100%", "center", $Lang::tr{'request'});
-       my @output = `/usr/local/bin/pakfire resolvedeps --no-colors $pakfiresettings{'INSPAKS'}`;
+               my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
                print <<END;
-               <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'}.$pakfiresettings{'INSPAKS'}.$Lang::tr{'pakfire possible dependency'}
+               <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
                <pre>
 END
                foreach (@output) {
@@ -78,7 +165,7 @@ END
                <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}
                <tr><td colspan='2'>&nbsp;
                <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                                                       <input type='hidden' name='INSPAKS' value='$pakfiresettings{'INSPAKS'}' />
+                                                       <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
                                                        <input type='hidden' name='FORCE' value='on' />
                                                        <input type='hidden' name='ACTION' value='install' />
                                                        <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
@@ -95,18 +182,15 @@ END
                &Header::closepage();
                exit;
        }
-} elsif ($pakfiresettings{'ACTION'} eq 'remove') {
-
-       $pakfiresettings{'DELPAKS'} =~ s/\|/\ /g;
-       if ("$pakfiresettings{'FORCE'}" eq "on") {
-               my $command = "/usr/local/bin/pakfire remove --non-interactive --no-colors $pakfiresettings{'DELPAKS'} &>/dev/null &";
-               system("$command");
-               system("/bin/sleep 1");
+} elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
+       my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
+       if ("$cgiparams{'FORCE'}" eq "on") {
+               &General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
        } else {
                &Header::openbox("100%", "center", $Lang::tr{'request'});
-       my @output = `/usr/local/bin/pakfire resolvedeps --no-colors $pakfiresettings{'DELPAKS'}`;
+               my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
                print <<END;
-               <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'}.$pakfiresettings{'DELPAKS'}.$Lang::tr{'pakfire possible dependency'}
+               <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
                <pre>
 END
                foreach (@output) {
@@ -115,10 +199,10 @@ END
                }
                print <<END;
                </pre>
-               <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}
+               <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}
                <tr><td colspan='2'>&nbsp;
                <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                                                       <input type='hidden' name='DELPAKS' value='$pakfiresettings{'DELPAKS'}' />
+                                                       <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
                                                        <input type='hidden' name='FORCE' value='on' />
                                                        <input type='hidden' name='ACTION' value='remove' />
                                                        <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
@@ -136,23 +220,24 @@ END
                exit;
        }
 
-} elsif ($pakfiresettings{'ACTION'} eq 'update') {
-
-       system("/usr/local/bin/pakfire update --force --no-colors &>/dev/null &");
-       system("/bin/sleep 1");
-} elsif ($pakfiresettings{'ACTION'} eq 'upgrade') {
-       my $command = "/usr/local/bin/pakfire upgrade -y --no-colors &>/dev/null &";
-       system("$command");
-       system("/bin/sleep 1");
-} elsif ($pakfiresettings{'ACTION'} eq "$Lang::tr{'save'}") {
+} elsif (($cgiparams{'ACTION'} eq 'update') && (! &_is_pakfire_busy())) {
+       &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
+} elsif (($cgiparams{'ACTION'} eq 'upgrade') && (! &_is_pakfire_busy())) {
+       &General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
+} elsif ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
+       $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
 
-       if ($pakfiresettings{'AUTOUPDATE'} eq 'on') {
-               system("/usr/local/bin/pakfire enable updates >/dev/null 2>&1");
-       } else {
-               system("/usr/local/bin/pakfire disable updates  >/dev/null 2>&1");
+       # Check for valid input
+       if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
+               $errormessage .= $Lang::tr{'pakfire invalid tree'};
        }
 
-       &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
+       unless ($errormessage) {
+               &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
+
+               # Update lists
+               &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
+       }
 }
 
 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
@@ -160,15 +245,11 @@ END
 my %selected=();
 my %checked=();
 
-$checked{'AUTOUPDATE'}{'off'} = '';
-$checked{'AUTOUPDATE'}{'on'} = '';
-$checked{'AUTOUPDATE'}{$pakfiresettings{'AUTOUPDATE'}} = "checked='checked'";
-$checked{'HEALTHCHECK'}{'off'} = '';
-$checked{'HEALTHCHECK'}{'on'} = '';
-$checked{'HEALTHCHECK'}{$pakfiresettings{'HEALTHCHECK'}} = "checked='checked'";
-$checked{'UUID'}{'off'} = '';
-$checked{'UUID'}{'on'} = '';
-$checked{'UUID'}{$pakfiresettings{'UUID'}} = "checked='checked'";
+$selected{"TREE"} = ();
+$selected{"TREE"}{"stable"} = "";
+$selected{"TREE"}{"testing"} = "";
+$selected{"TREE"}{"unstable"} = "";
+$selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
 
 # DPC move error message to top so it is seen!
 if ($errormessage) {
@@ -177,35 +258,34 @@ if ($errormessage) {
        &Header::closebox();
 }
 
-my $return = `pidof pakfire`;
-chomp($return);
-if ($return) {
-       &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
-       print <<END;
-       <table>
-               <tr><td>
-                               <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' title='$Lang::tr{'aktiv'}' />&nbsp;
-                       <td>
-                               $Lang::tr{'pakfire working'}
-               <tr><td colspan='2' align='center'>
-                       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                               <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
-                       </form>
-               <tr><td colspan='2' align='left'><code>
-END
-       my @output = `grep pakfire /var/log/messages | tail -20`;
-       foreach (@output) {
-               print "$_<br>";
-       }
-       print <<END;
-                       </code>
-               </table>
+# Show log output while Pakfire is running
+if(&_is_pakfire_busy()) {
+       &Header::openbox("100%", "center", "Pakfire");
+
+       print <<END
+<section id="pflog-header">
+       <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
+       <div>
+               <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
+               <span id="pflog-time"></span><br>
+               <span id="pflog-action"></span>
+       </div>
+       <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
+</section>
+
+<!-- Pakfire log messages -->
+<pre id="pflog-messages"></pre>
+<script>
+       pakfire.running = true;
+</script>
+
 END
+;
+
        &Header::closebox();
        &Header::closebigbox();
        &Header::closepage();
        exit;
-       refreshpage();
 }
 
 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
@@ -218,7 +298,7 @@ my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db"
 &Header::openbox("100%", "center", "Pakfire");
 
 print <<END;
-       <table width='95%' cellpadding='5' >
+       <table width='95%' cellpadding='5'>
 END
 if ( -e "/var/run/need_reboot") {
        print "<tr><td align='center' colspan='2'><font color='red'>$Lang::tr{'needreboot'}!</font></td></tr>";
@@ -226,18 +306,21 @@ if ( -e "/var/run/need_reboot") {
 }
 print <<END;
                <tr><td width="50%" bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire system state'}:</b>
-                               <td width="50%">
+
+                       <td width="50%" bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'available updates'}:</b></tr>
+
                <tr><td align="center">$Lang::tr{'pakfire core update level'}: $core_release<hr />
                                        $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br />
                                        $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br />
                                        $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br />
                                        $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
                                        <form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                                               <input type='hidden' name='ACTION' value='update' />
+                                               <input type='hidden' name='ACTION' value='update' /><br />
                                                <input type='submit' value='$Lang::tr{'calamaris refresh list'}' /><br />
                                        </form>
+<br />
                                <td align="center">
-                                <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+                               <form method='post' action='$ENV{'SCRIPT_NAME'}'>
                                        <select name="UPDPAKS" size="5" disabled>
 END
                                                &Pakfire::dblist("upgrade", "forweb");
@@ -251,7 +334,7 @@ END
                <tr><td colspan="2"><!-- Just an empty line -->&nbsp;
                <tr><td bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire available addons'}</b>
                                <td bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire installed addons'}</b>
-               <tr><td align="center">
+               <tr><td style="padding:5px 10px 20px 20px" align="center">
                        <p>$Lang::tr{'pakfire install description'}</p>
                        <form method='post' action='$ENV{'SCRIPT_NAME'}'>
                                <select name="INSPAKS" size="10" multiple>
@@ -265,7 +348,7 @@ print <<END;
                                <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
                        </form>
 
-               <td align="center">
+               <td style="padding:5px 10px 20px 20px" align="center">
                        <p>$Lang::tr{'pakfire uninstall description'}</p>
                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
                        <select name="DELPAKS" size="10" multiple>
@@ -286,19 +369,26 @@ END
 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
 
 print <<END;
-       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+       <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
                <table width='95%'>
-                       <tr><td colspan='2' bgcolor='$color{'color20'}'><b>$Lang::tr{'basic options'}</b></td></tr>
-                       <tr><td align='left' width='45%'>$Lang::tr{'pakfire update daily'}</td><td width="55%" align="left">
-          on <input type='radio' name='AUTOUPDATE' value='on' $checked{'AUTOUPDATE'}{'on'} /> |
-          <input type='radio' name='AUTOUPDATE' value='off' $checked{'AUTOUPDATE'}{'off'} /> off </td></tr>
-                       <tr><td align='left' width='45%'>$Lang::tr{'pakfire health check'}</td><td align="left">
-          on <input type='radio' name='HEALTHCHECK' value='on' $checked{'HEALTHCHECK'}{'on'} /> |
-          <input type='radio' name='HEALTHCHECK' value='off' $checked{'HEALTHCHECK'}{'off'} /> off </td></tr>          
-                       <tr><td align='left' width='45%'>$Lang::tr{'pakfire register'}</td><td align="left">
-          on <input type='radio' name='UUID' value='on' $checked{'UUID'}{'on'} /> |
-          <input type='radio' name='UUID' value='off' $checked{'UUID'}{'off'} /> off </td></tr>
-                       <tr><td colspan="2" align="center"><input type="submit" name="ACTION" value="$Lang::tr{'save'}" /></td></tr>
+                       <tr>
+                               <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
+                               <td width="55%" align="left">
+                                       <select name="TREE">
+                                               <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
+                                               <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
+                                               <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
+                                       </select>
+                               </td>
+                       </tr>
+                       <tr>
+                               <td colspan="2">&nbsp;</td>
+                       </tr>
+                       <tr>
+                               <td colspan="2" align="center">
+                                       <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
+                               </td>
+                       </tr>
                </table>
        </form>
 END
@@ -306,3 +396,23 @@ END
 &Header::closebox();
 &Header::closebigbox();
 &Header::closepage();
+
+###--- Internal functions ---###
+
+# Check if pakfire is already running (extend test here if necessary)
+sub _is_pakfire_busy {
+       # Get PID of a running pakfire instance
+       # (The system backpipe command is safe, because no user input is computed.)
+       my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
+       chomp($pakfire_pid);
+
+       # Test presence of PID or lockfile
+       return (($pakfire_pid) || (-e "$Pakfire::lockfile"));
+}
+
+# Send HTTP headers
+sub _start_json_output {
+       print "Cache-Control: no-cache, no-store\n";
+       print "Content-Type: application/json\n";
+       print "\n"; # End of HTTP headers
+}