From c7da43f50d5d673c84684534dd783465db2cb38e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sun, 25 Feb 2024 23:13:27 +0100 Subject: [PATCH] feat: add static ipv4 ipv6 support --- app/Console/Kernel.php | 28 +++++++++++++-------------- app/Jobs/ApplicationDeploymentJob.php | 26 +++++++++++++++++++++++++ app/Jobs/ContainerStatusJob.php | 8 ++++---- bootstrap/helpers/docker.php | 3 ++- tests/Feature/DockerRunTest.php | 9 +++++++++ 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 636df5d36..c67f98cf7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -78,25 +78,25 @@ private function check_resources($schedule) dispatch($job); })->name('container-status-' . $server->id)->everyMinute()->onOneServer(); if ($server->isLogDrainEnabled()) { - $schedule - ->call(function () use ($server) { - $randomSeconds = rand(1, 40); - $job = new CheckLogDrainContainerJob($server); - $job->delay($randomSeconds); - dispatch($job); - })->name('log-drain-container-check-' . $server->id)->everyMinute()->onOneServer(); // $schedule->job(new CheckLogDrainContainerJob($server))->everyMinute()->onOneServer(); + $schedule + ->call(function () use ($server) { + $randomSeconds = rand(1, 40); + $job = new CheckLogDrainContainerJob($server); + $job->delay($randomSeconds); + dispatch($job); + })->name('log-drain-container-check-' . $server->id)->everyMinute()->onOneServer(); } } foreach ($servers as $server) { - $schedule - ->call(function () use ($server) { - $randomSeconds = rand(1, 40); - $job = new ServerStatusJob($server); - $job->delay($randomSeconds); - dispatch($job); - })->name('server-status-job-' . $server->id)->everyMinute()->onOneServer(); // $schedule->job(new ServerStatusJob($server))->everyMinute()->onOneServer(); + $schedule + ->call(function () use ($server) { + $randomSeconds = rand(1, 40); + $job = new ServerStatusJob($server); + $job->delay($randomSeconds); + dispatch($job); + })->name('server-status-job-' . $server->id)->everyMinute()->onOneServer(); } } private function instance_auto_update($schedule) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index f3e8c4129..61cda4fc6 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1223,6 +1223,19 @@ private function generate_compose_file() if ((bool)$this->application->settings->is_consistent_container_name_enabled) { $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); if (count($custom_compose) > 0) { + $ipv4 = data_get($custom_compose, 'ip.0'); + $ipv6 = data_get($custom_compose, 'ip6.0'); + data_forget($custom_compose, 'ip'); + data_forget($custom_compose, 'ip6'); + if ($ipv4 || $ipv6) { + data_forget($docker_compose['services'][$this->application->uuid], 'networks'); + } + if ($ipv4) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; + } + if ($ipv6) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; + } $docker_compose['services'][$this->container_name] = array_merge_recursive($docker_compose['services'][$this->container_name], $custom_compose); } } else { @@ -1230,6 +1243,19 @@ private function generate_compose_file() data_forget($docker_compose, 'services.' . $this->container_name); $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); if (count($custom_compose) > 0) { + $ipv4 = data_get($custom_compose, 'ip.0'); + $ipv6 = data_get($custom_compose, 'ip6.0'); + data_forget($custom_compose, 'ip'); + data_forget($custom_compose, 'ip6'); + if ($ipv4 || $ipv6) { + data_forget($docker_compose['services'][$this->application->uuid], 'networks'); + } + if ($ipv4) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; + } + if ($ipv6) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; + } $docker_compose['services'][$this->application->uuid] = array_merge_recursive($docker_compose['services'][$this->application->uuid], $custom_compose); } } diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index fbdd555a1..68035a258 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -43,6 +43,10 @@ public function uniqueId(): int public function handle() { + if (!$this->server->isFunctional()) { + return 'Server is not ready.'; + }; + $applications = $this->server->applications(); $skip_these_applications = collect([]); foreach ($applications as $application) { @@ -57,10 +61,6 @@ public function handle() $applications = $applications->filter(function ($value, $key) use ($skip_these_applications) { return !$skip_these_applications->pluck('id')->contains($value->id); }); - - if (!$this->server->isFunctional()) { - return 'Server is not ready.'; - }; try { if ($this->server->isSwarm()) { $containers = instant_remote_process(["docker service inspect $(docker service ls -q) --format '{{json .}}'"], $this->server, false); diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 370c05930..6a3e3f839 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -423,7 +423,7 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null '--security-opt', '--sysctl', '--ulimit', - '--device' + '--device', ]); $mapping = collect([ '--cap-add' => 'cap_add', @@ -435,6 +435,7 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null '--init' => 'init', '--ulimit' => 'ulimits', '--privileged' => 'privileged', + '--ip' => 'ip', ]); foreach ($matches as $match) { $option = $match[1]; diff --git a/tests/Feature/DockerRunTest.php b/tests/Feature/DockerRunTest.php index 27816bf5d..2fee5d8e5 100644 --- a/tests/Feature/DockerRunTest.php +++ b/tests/Feature/DockerRunTest.php @@ -8,6 +8,15 @@ ])->ray(); }); +test('ConvertIp', function () { + $input = '--cap-add=NET_ADMIN --cap-add=NET_RAW --cap-add SYS_ADMIN --ip 127.0.0.1 --ip 127.0.0.2'; + $output = convert_docker_run_to_compose($input); + expect($output)->toBe([ + 'cap_add' => ['NET_ADMIN', 'NET_RAW', 'SYS_ADMIN'], + 'ip' => ['127.0.0.1', '127.0.0.2'] + ])->ray(); +}); + test('ConvertPrivilegedAndInit', function () { $input = '---privileged --init'; $output = convert_docker_run_to_compose($input);