Skip to content

Fix logs name colon symbol, fix binary hash check, fix musllinux detection. #49

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

Merged
merged 3 commits into from
Jan 16, 2023
Merged
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
4 changes: 2 additions & 2 deletions lib/Service/PythonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ public function run(
}
if ($nonBlocking) {
if ($binary) {
$logFile = $cwd . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log';
$logFile = $cwd . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log';
} else {
$appDataDir = $this->ncDataFolder . '/appdata_' . $this->ncInstanceId . '/' . $appId . '/';
$pyBitecodeEnvVar = 'PYTHONBYTECODEBASE="' . $appDataDir . '" ';
$envVariables = $pyBitecodeEnvVar . $envVariables;
$logFile = $appDataDir . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log';
$logFile = $appDataDir . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log';
}
$cmd = $envVariables . 'nohup ' . $cmd . ' > ' . $logFile . ' 2>' . $logFile . ' &';
exec($cmd);
Expand Down
91 changes: 24 additions & 67 deletions lib/Service/UtilsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ public function isVideosSupported(): bool {
}

public function isMusliLinux(): bool {
exec('ldd --version', $output, $result_code);
if ($result_code == 0 && count($output) > 0 && str_contains($output[0], 'musl')) {
exec('ldd --version 2>&1', $output, $result_code);
if (count($output) > 0 && str_contains($output[0], 'musl')) {
return true;
}
return false;
Expand Down Expand Up @@ -274,9 +274,7 @@ public function downloadPythonBinary(
}
$file_name = $filename . '.gz';
$save_file_loc = $dir . $file_name;
$shouldDownloadBinary = $this->compareBinaryHash(
$url, $dir . $filename, $binariesFolder, $filename
);
$shouldDownloadBinary = $this->compareBinaryHash($url, $dir . $filename);
if (!file_exists($dir . $filename) || ($update && $shouldDownloadBinary)) {
$cURL = curl_init($url);
$fp = fopen($save_file_loc, 'wb');
Expand Down Expand Up @@ -314,87 +312,46 @@ public function downloadPythonBinary(
/**
* @codeCoverageIgnore
*
* Compare binary hash from release. If hash not exists return `true` (download anyway)
*
* @param string $url
* @param string $binaryPath
* @param array $binariesFolder,
* @param string $filanem
*
* @return bool
*/
public function compareBinaryHash(
string $url,
string $binaryPath,
array $binariesFolder,
string $filename
) {
public function compareBinaryHash(string $url, string $binaryPath) {
if (file_exists($binaryPath)) {
// get current binary hash (from .sha256 file or directly from existing binary)
if (file_exists($binaryPath . '.sha256')) {
$currentBinaryHash = file_get_contents(
$binaryPath . '.sha256', false, null, 0, 64
);
} else {
$binaryData = file_get_contents($binaryPath);
$currentBinaryHash = hash('sha256', $binaryData);
}
// download new binary sha256 hash from attached file to release
copy($binaryPath . '.sha256', $binaryPath . '.sha256.old');
$newBinaryHash = $this->downloadBinaryHash(
str_replace('.gz', '.sha256', $url), $binariesFolder, $filename
);
// should update binary if hashes not equial
if ($newBinaryHash['success']) {
$binaryData = file_get_contents($binaryPath);
$currentBinaryHash = hash('sha256', $binaryData);
$newBinaryHash = $this->downloadBinaryHash(str_replace('.gz', '.sha256', $url));
if ($newBinaryHash['success'] && strlen($newBinaryHash['binaryHash']) == 64) {
return $currentBinaryHash != $newBinaryHash['binaryHash'];
} else {
// revert back old hash file
copy($binaryPath . '.sha256.old', $binaryPath . '.sha256');
unlink($binaryPath . '.sha256.old');
}
}
return false;
return true;
}

/**
* Perform cURL download binary's sha256 sum file
* Perform cURL to get binary's sha256 sum
*
* @codeCoverageIgnore
*
* @param string $url url to the binary hashsum file
* @param array $binariesFolder appdata binaries folder
* @param string $filename downloaded checksum filename
*
* @return array
*/
public function downloadBinaryHash(
string $url,
array $binariesFolder,
string $filename
): array {
if (isset($binariesFolder['success']) && $binariesFolder['success']) {
$dir = $binariesFolder['path'] . '/';
} else {
return $binariesFolder; // Return getAppDataFolder result
}
$file_name = $filename . '.sha256';
$save_file_loc = $dir . $file_name;
public function downloadBinaryHash(string $url): array {
$cURL = curl_init($url);
$fp = fopen($save_file_loc, 'w');
if ($fp) {
curl_setopt_array($cURL, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RANGE => 64,
]);
$binaryHash = curl_exec($cURL);
curl_close($cURL);
fclose($fp);
return [
'success' => true,
'binaryHash' => $binaryHash,
'binaryHashFilePath' => $save_file_loc,
];
}
return ['success' => false];
curl_setopt_array($cURL, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_RANGE => 64,
]);
$binaryHash = curl_exec($cURL);
curl_close($cURL);
return [
'success' => $binaryHash != false,
'binaryHash' => $binaryHash,
];
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Service/UtilsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public function testIsMusliLinux() {
$exec->expects($this->any())
->willReturnCallback(
function ($command, &$output, &$result_code) {
$this->assertEquals('ldd --version', $command);
$this->assertEquals('ldd --version 2>&1', $command);
$output = ['manylinux'];
$result_code = 0;
$result_code = 1;
}
);
$result = $this->utils->isMusliLinux();
Expand All @@ -207,9 +207,9 @@ public function testIsMusliLinuxWithOverride() {
$exec->expects($this->any())
->willReturnCallback(
function ($command, &$output, &$result_code) {
$this->assertEquals('ldd --version', $command);
$this->assertEquals('ldd --version 2>&1', $command);
$output = ['musl linux'];
$result_code = 0;
$result_code = 1;
}
);
$result = $this->utils->isMusliLinux();
Expand Down Expand Up @@ -356,7 +356,7 @@ public function testGetBinaryNameMuslLinux() {
$exec->expects($this->any())
->willReturnCallback(
function ($command, &$output, &$result_code) {
$this->assertEquals('ldd --version', $command);
$this->assertEquals('ldd --version 2>&1', $command);
$output = ['musl linux'];
$result_code = 0;
}
Expand Down