coolify/app/Http/Livewire/Project/New/DockerCompose.php

82 lines
2.7 KiB
PHP
Raw Normal View History

2023-09-19 15:51:13 +02:00
<?php
namespace App\Http\Livewire\Project\New;
2023-09-26 14:45:52 +02:00
use App\Models\EnvironmentVariable;
2023-09-19 15:51:13 +02:00
use App\Models\Project;
2023-09-20 15:42:41 +02:00
use App\Models\Service;
2023-09-19 15:51:13 +02:00
use Livewire\Component;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
2023-09-19 15:51:13 +02:00
class DockerCompose extends Component
{
2023-09-25 15:48:43 +02:00
public string $dockerComposeRaw = '';
2023-09-26 14:45:52 +02:00
public string $envFile = '';
2023-09-19 15:51:13 +02:00
public array $parameters;
public array $query;
public function mount()
{
2023-09-25 15:48:43 +02:00
2023-09-19 15:51:13 +02:00
$this->parameters = get_route_parameters();
$this->query = request()->query();
if (isDev()) {
2023-09-25 15:48:43 +02:00
$this->dockerComposeRaw = 'services:
2023-12-03 12:16:33 +01:00
appsmith:
build:
context: .
dockerfile_inline: |
FROM nginx
ARG GIT_COMMIT
ARG GIT_BRANCH
RUN echo "Hello World ${GIT_COMMIT} ${GIT_BRANCH}"
args:
- GIT_COMMIT=cdc3b19
- GIT_BRANCH=${GIT_BRANCH}
2023-09-27 12:45:53 +02:00
environment:
2023-12-03 12:16:33 +01:00
- APPSMITH_MAIL_ENABLED=${APPSMITH_MAIL_ENABLED}
2023-09-27 12:45:53 +02:00
';
2023-09-19 15:51:13 +02:00
}
}
public function submit()
{
try {
$this->validate([
2023-09-25 15:48:43 +02:00
'dockerComposeRaw' => 'required'
]);
2023-09-25 15:48:43 +02:00
$this->dockerComposeRaw = Yaml::dump(Yaml::parse($this->dockerComposeRaw), 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$server_id = $this->query['server_id'];
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$service = Service::create([
'name' => 'service' . Str::random(10),
2023-09-25 15:48:43 +02:00
'docker_compose_raw' => $this->dockerComposeRaw,
'environment_id' => $environment->id,
'server_id' => (int) $server_id,
]);
2023-09-26 14:45:52 +02:00
$variables = parseEnvFormatToArray($this->envFile);
foreach ($variables as $key => $variable) {
EnvironmentVariable::create([
'key' => $key,
'value' => $variable,
'is_build_time' => false,
'is_preview' => false,
'service_id' => $service->id,
]);
}
$service->name = "service-$service->uuid";
2023-09-21 17:48:31 +02:00
$service->parse(isNew: true);
2023-09-21 17:48:31 +02:00
return redirect()->route('project.service.configuration', [
'service_uuid' => $service->uuid,
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-09-19 15:51:13 +02:00
}
}