mirror of
https://github.com/ershisan99/coolify.git
synced 2025-12-16 20:49:28 +00:00
added public functions
This commit is contained in:
@@ -8,6 +8,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Support\Facades\Process;
|
||||||
|
use Illuminate\Process\InvokedProcess;
|
||||||
use OpenApi\Attributes as OA;
|
use OpenApi\Attributes as OA;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Spatie\Activitylog\Models\Activity;
|
use Spatie\Activitylog\Models\Activity;
|
||||||
@@ -146,6 +148,59 @@ class Application extends BaseModel
|
|||||||
return Application::whereRelation('environment.project.team', 'id', $teamId)->orderBy('name');
|
return Application::whereRelation('environment.project.team', 'id', $teamId)->orderBy('name');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getContainersToStop(bool $previewDeployments = false): array
|
||||||
|
{
|
||||||
|
$containers = $previewDeployments
|
||||||
|
? getCurrentApplicationContainerStatus($this->destination->server, $this->id, includePullrequests: true)
|
||||||
|
: getCurrentApplicationContainerStatus($this->destination->server, $this->id, 0);
|
||||||
|
|
||||||
|
return $containers->pluck('Names')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stopContainers(array $containerNames, $server, int $timeout = 600)
|
||||||
|
{
|
||||||
|
$processes = [];
|
||||||
|
foreach ($containerNames as $containerName) {
|
||||||
|
$processes[$containerName] = $this->stopContainer($containerName, $server, $timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
$startTime = time();
|
||||||
|
while (count($processes) > 0) {
|
||||||
|
$finishedProcesses = array_filter($processes, function ($process) {
|
||||||
|
return !$process->running();
|
||||||
|
});
|
||||||
|
foreach ($finishedProcesses as $containerName => $process) {
|
||||||
|
unset($processes[$containerName]);
|
||||||
|
$this->removeContainer($containerName, $server);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (time() - $startTime >= $timeout) {
|
||||||
|
$this->forceStopRemainingContainers(array_keys($processes), $server);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
usleep(100000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stopContainer(string $containerName, $server, int $timeout): InvokedProcess
|
||||||
|
{
|
||||||
|
return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeContainer(string $containerName, $server)
|
||||||
|
{
|
||||||
|
instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forceStopRemainingContainers(array $containerNames, $server)
|
||||||
|
{
|
||||||
|
foreach ($containerNames as $containerName) {
|
||||||
|
instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);
|
||||||
|
$this->removeContainer($containerName, $server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function delete_configurations()
|
public function delete_configurations()
|
||||||
{
|
{
|
||||||
$server = data_get($this, 'destination.server');
|
$server = data_get($this, 'destination.server');
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Process;
|
||||||
|
use Illuminate\Process\InvokedProcess;
|
||||||
use OpenApi\Attributes as OA;
|
use OpenApi\Attributes as OA;
|
||||||
use Spatie\Url\Url;
|
use Spatie\Url\Url;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
@@ -119,6 +121,59 @@ class Service extends BaseModel
|
|||||||
return $this->morphToMany(Tag::class, 'taggable');
|
return $this->morphToMany(Tag::class, 'taggable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getContainersToStop(bool $previewDeployments = false): array
|
||||||
|
{
|
||||||
|
$containers = $previewDeployments
|
||||||
|
? getCurrentApplicationContainerStatus($this->destination->server, $this->id, includePullrequests: true)
|
||||||
|
: getCurrentApplicationContainerStatus($this->destination->server, $this->id, 0);
|
||||||
|
|
||||||
|
return $containers->pluck('Names')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stopContainers(array $containerNames, $server, int $timeout = 600)
|
||||||
|
{
|
||||||
|
$processes = [];
|
||||||
|
foreach ($containerNames as $containerName) {
|
||||||
|
$processes[$containerName] = $this->stopContainer($containerName, $server, $timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
$startTime = time();
|
||||||
|
while (count($processes) > 0) {
|
||||||
|
$finishedProcesses = array_filter($processes, function ($process) {
|
||||||
|
return !$process->running();
|
||||||
|
});
|
||||||
|
foreach ($finishedProcesses as $containerName => $process) {
|
||||||
|
unset($processes[$containerName]);
|
||||||
|
$this->removeContainer($containerName, $server);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (time() - $startTime >= $timeout) {
|
||||||
|
$this->forceStopRemainingContainers(array_keys($processes), $server);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
usleep(100000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stopContainer(string $containerName, $server, int $timeout): InvokedProcess
|
||||||
|
{
|
||||||
|
return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeContainer(string $containerName, $server)
|
||||||
|
{
|
||||||
|
instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forceStopRemainingContainers(array $containerNames, $server)
|
||||||
|
{
|
||||||
|
foreach ($containerNames as $containerName) {
|
||||||
|
instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);
|
||||||
|
$this->removeContainer($containerName, $server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function delete_configurations()
|
public function delete_configurations()
|
||||||
{
|
{
|
||||||
$server = data_get($this, 'destination.server');
|
$server = data_get($this, 'destination.server');
|
||||||
|
|||||||
Reference in New Issue
Block a user