Skip to content

Commit

Permalink
Fix logs name colon symbol, fix binary hash check, fix musllinux dete…
Browse files Browse the repository at this point in the history
…ction. (#49)

* Fix log file name escape colon symbol. Fix musllinux detect.

Minor changes in pre-compiled binary hash check

* Fix test

* Fix params to binary check function
  • Loading branch information
andrey18106 authored Jan 16, 2023
1 parent f4f2f1c commit 7838146
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 74 deletions.
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

0 comments on commit 7838146

Please sign in to comment.