Skip to content
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
41 changes: 33 additions & 8 deletions ProcessMaker/Models/ScriptDockerNayraTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
use ProcessMaker\Exception\ScriptException;
use ProcessMaker\Facades\Docker;
use ProcessMaker\ScriptRunners\Base;
use RuntimeException;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;
use UnexpectedValueException;

/**
* Execute a docker container copying files to interchange information.
*/
trait ScriptDockerNayraTrait
{

private $schema = 'http';

/**
Expand All @@ -45,13 +44,16 @@ public function handleNayraDocker(string $code, array $data, array $config, $tim
'timeout' => $timeout,
];
$body = json_encode($params);
$servers = self::getNayraAddresses();
if (!$servers) {

if (!$this->hasConfiguredNayraRestApiHost() && !self::getNayraAddresses()) {
$this->bringUpNayra();
}

$baseUrl = $this->getNayraInstanceUrl();
$url = $baseUrl . '/run_script';
$this->ensureNayraServerIsRunning($baseUrl);
if (!$this->hasConfiguredNayraRestApiHost()) {
$this->ensureNayraServerIsRunning($baseUrl);
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
Expand All @@ -62,8 +64,8 @@ public function handleNayraDocker(string $code, array $data, array $config, $tim
'Content-Length: ' . strlen($body),
]);
$result = curl_exec($ch);
curl_close($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpStatus !== 200) {
$result .= ' HTTP Status: ' . $httpStatus;
$result .= ' URL: ' . $url;
Expand All @@ -75,15 +77,31 @@ public function handleNayraDocker(string $code, array $data, array $config, $tim
]);
throw new ScriptException($result);
}

return $result;
}

private function getNayraInstanceUrl()
{
if ($this->hasConfiguredNayraRestApiHost()) {
return $this->getConfiguredNayraRestApiHost();
}

$servers = self::getNayraAddresses();

return $this->schema . '://' . $servers[0] . ':' . $this->getNayraPort();
}

private function hasConfiguredNayraRestApiHost(): bool
{
return $this->getConfiguredNayraRestApiHost() !== '';
}

private function getConfiguredNayraRestApiHost(): string
{
return rtrim((string) config('app.nayra_rest_api_host'), '/');
}

private function getDockerLogs($instanceName)
{
$docker = Docker::command();
Expand All @@ -92,6 +110,7 @@ private function getDockerLogs($instanceName)
if ($status) {
return 'Error getting logs from Nayra Docker: ' . implode("\n", $logs);
}

return implode("\n", $logs);
}

Expand All @@ -106,6 +125,10 @@ private function ensureNayraServerIsRunning(string $url)
{
$header = @get_headers($url);
if (!$header) {
if ($this->hasConfiguredNayraRestApiHost()) {
throw new ScriptException('Could not connect to configured Nayra REST API host');
}

$this->bringUpNayra(true);
}
}
Expand Down Expand Up @@ -206,7 +229,7 @@ private static function findNayraAddresses($docker, $instanceName, $times): bool
. ($nayraDockerNetwork
? "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'"
: "'{{ .NetworkSettings.IPAddress }}'"
)
)
. " {$instanceName}_nayra 2>/dev/null",
$output,
$status
Expand All @@ -217,6 +240,7 @@ private static function findNayraAddresses($docker, $instanceName, $times): bool
}
if ($ip) {
self::setNayraAddresses([$ip]);

return true;
}
}
Expand Down Expand Up @@ -280,6 +304,7 @@ public static function clearNayraAddresses()
private static function isCacheArrayStore(): bool
{
$cacheDriver = Cache::getFacadeRoot()->getStore();

return $cacheDriver instanceof ArrayStore;
}

Expand Down
91 changes: 91 additions & 0 deletions tests/unit/ProcessMaker/Models/ScriptDockerNayraTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Tests\Unit\ProcessMaker\Models;

use Illuminate\Cache\FileStore;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
use PHPUnit\Framework\TestCase;
use ProcessMaker\Models\ScriptDockerNayraTrait;
use ReflectionClass;

class ScriptDockerNayraTraitTest extends TestCase
{
private string $cachePath;

protected function setUp(): void
{
parent::setUp();

$app = new Container();
Container::setInstance($app);
Facade::setFacadeApplication($app);

$app->instance('config', new ConfigRepository([
'app' => [
'nayra_rest_api_host' => '',
'nayra_port' => 8080,
],
]));

$this->cachePath = sys_get_temp_dir() . '/processmaker-nayra-test-cache-' . uniqid('', true);
Cache::swap(new CacheRepository(new FileStore(new Filesystem(), $this->cachePath)));
}

protected function tearDown(): void
{
TestNayraScriptRunner::clearNayraAddresses();
(new Filesystem())->deleteDirectory($this->cachePath);
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);
Container::setInstance(null);

parent::tearDown();
}

public function testConfiguredNayraRestApiHostIsUsedInsteadOfCachedDockerAddress(): void
{
config([
'app.nayra_rest_api_host' => 'http://nayra-service:8080/',
'app.nayra_port' => 8080,
]);
TestNayraScriptRunner::setNayraAddresses(['172.18.0.5']);

$this->assertSame(
'http://nayra-service:8080',
$this->getNayraInstanceUrl()
);
}

public function testDockerAddressIsUsedWhenNayraRestApiHostIsNotConfigured(): void
{
config([
'app.nayra_rest_api_host' => '',
'app.nayra_port' => 8081,
]);
TestNayraScriptRunner::setNayraAddresses(['172.18.0.5']);

$this->assertSame(
'http://172.18.0.5:8081',
$this->getNayraInstanceUrl()
);
}

private function getNayraInstanceUrl(): string
{
$runner = new TestNayraScriptRunner();
$reflection = new ReflectionClass($runner);
$method = $reflection->getMethod('getNayraInstanceUrl');

return $method->invoke($runner);
}
}

class TestNayraScriptRunner
{
use ScriptDockerNayraTrait;
}
Loading