Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transparent server restart after upgrading #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build-nextcloud-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ test -d "${app_name}/collabora" || mkdir -p "${app_name}/collabora"
test -f "${app_name}/collabora/Collabora_Online.AppImage" || curl "$APPIMAGE_URL" -o "${app_name}/collabora/Collabora_Online.AppImage"
chmod a+x "${app_name}/collabora/Collabora_Online.AppImage"

# Get the version hash from loolwsd and set it in proxy.php
HASH=`./${app_name}/collabora/Collabora_Online.AppImage --version-hash`
echo "HASH: $HASH"
sed -i "s/%LOOLWSD_VERSION_HASH%/$HASH/g" ${app_name}/proxy.php

echo "Signing…"
$occ integrity:sign-app --privateKey=${cert_dir}/${app_name}.key --certificate=${cert_dir}/${app_name}.crt --path=$(pwd)/${app_name}
tar czf ${app_name}.tar.gz ${app_name}
Expand Down
107 changes: 80 additions & 27 deletions proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ function errorExit($msg)
// Where the appimage is installed
$launchCmd = __DIR__ . '/collabora/Collabora_Online.AppImage';

function getLoolwsdPid()
{
exec("pidof loolwsd", $output, $return);
if ($return == 0 && count($output) > 0)
{
debug_log("Loolwsd server running with pid: " . implode(', ', $output));
return $output;
}

debug_log("Loolwsd server is not running.");
return 0;
}

function isLoolwsdRunning()
{
return !empty(getLoolwsdPid());
}

function startLoolwsd()
{
global $launchCmd;
debug_log("Launch the loolwsd server: $launchCmd");
exec("$launchCmd >/dev/null & disown", $output, $return);
if ($return)
{
debug_log("Failed to launch server at $launchCmd.");
// errorExit("Server unavialble."); // disown: not found
}
}

function stopLoolwsd()
{
$pid = getLoolwsdPid();
if (!empty($pid))
{
debug_log("Stopping the loolwsd server with pid: " . implode(', ', $pid));
exec("kill -s TERM " . implode(' ', $pid));
}
}

// parse and emit headers using 'header' ...
function parseLastHeader(&$chunk)
{
Expand All @@ -52,14 +92,14 @@ function parseLastHeader(&$chunk)
$endOfHeaders = false;
foreach ($headers as $h)
{
debug_log("send: $h");
$chop += strlen($h) + 2;
if ($h === '')
{
$endOfHeaders = true;
break;
}
header($h);
debug_log("send: $h");
$chop += strlen($h) + 2;
if ($h === '')
{
$endOfHeaders = true;
break;
}
header($h);
}
// keep looking for the next header.
$chunk = substr($chunk, $chop);
Expand Down Expand Up @@ -103,11 +143,27 @@ function startsWith($string, $with) {
print '{"status":"starting"}';
} else {
$response = file_get_contents("http://localhost:9980/hosting/capabilities", 0, stream_context_create(["http"=>["timeout"=>1]]));
if ($response)
if ($response) {

print '{"status":"OK"}';

// Version check.
$obj = json_decode($response);
$actVer = $obj->{'productVersionHash'};
$expVer = '%LOOLWSD_VERSION_HASH%';
if ($actVer != $expVer) {
// Old/unexpected server version; restart.
error_log("Old server found, restarting. Expected hash $expVer but found $actVer.");
stopLoolwsd();
while (isLoolwsdRunning())
sleep(1);

startLoolwsd();
}
}
else
print '{"status":"starting"}';
fclose($local);
fclose($local);
}

http_response_code(200);
Expand All @@ -123,11 +179,8 @@ function startsWith($string, $with) {
// Start the appimage if necessary
if (!$local)
{
// FIXME: avoid multiple launch attempts...
debug_log("Launch the loolwsd server.");

// TODO use also pidof or something to check that it is already running?
exec("$launchCmd >/dev/null & disown");
if (!isLoolwsdRunning())
startLoolwsd();
}


Expand Down Expand Up @@ -197,7 +250,7 @@ function startsWith($string, $with) {
if ($multiBody != '' && $header == 'Content-Length')
{
debug_log("Substitute Content-Length of " . $value . " with " . strlen($body));
$value = strlen($body);
$value = strlen($body);
}

fwrite($local, "$header: $value\r\n");
Expand All @@ -214,27 +267,27 @@ function startsWith($string, $with) {
$chunk = fread($local, 65536);
if($chunk === false) {
$error = socket_last_error($local);
echo "ERROR ! $error\n";
echo "ERROR ! $error\n";
debug_log("error on chunk: $error");
break;
break;
} elseif($chunk == '') {
debug_log("empty chunk last data");
if ($parsingHeaders)
errorExit("No content in reply from loolwsd. Is SSL enabled in error ?");
if ($parsingHeaders)
errorExit("No content in reply from loolwsd. Is SSL enabled in error ?");
break;
} elseif ($parsingHeaders) {
$rest .= $chunk;
debug_log("build headers to: $rest\n");
if (parseLastHeader($rest)) {
$parsingHeaders = false;
$parsingHeaders = false;

$extOut = fopen("php://output", "w") or errorExit("fundamental error opening PHP output");
fwrite($extOut, $rest);
$rest = '';
debug_log("passed last headers");
}
$extOut = fopen("php://output", "w") or errorExit("fundamental error opening PHP output");
fwrite($extOut, $rest);
$rest = '';
debug_log("passed last headers");
}
} else {
fwrite($extOut, $chunk);
fwrite($extOut, $chunk);
debug_log("proxy : " . strlen($chunk) . " bytes \n");
}
} while(true);
Expand Down